Annotation of src/usr.bin/mg/file.c, Revision 1.97
1.97 ! bcallah 1: /* $OpenBSD: file.c,v 1.96 2015/03/19 21:22:15 bcallah Exp $ */
1.37 kjell 2:
3: /* This file is in the public domain. */
1.5 niklas 4:
1.1 deraadt 5: /*
1.4 millert 6: * File commands.
1.1 deraadt 7: */
8:
1.96 bcallah 9: #include <sys/queue.h>
1.77 lum 10: #include <sys/stat.h>
1.96 bcallah 11: #include <errno.h>
12: #include <libgen.h>
13: #include <signal.h>
14: #include <stdio.h>
15: #include <stdlib.h>
16: #include <string.h>
17: #include <unistd.h>
1.77 lum 18:
1.96 bcallah 19: #include "def.h"
1.41 kjell 20:
1.75 kjell 21: size_t xdirname(char *, const char *, size_t);
1.66 kjell 22:
1.1 deraadt 23: /*
1.4 millert 24: * Insert a file into the current buffer. Real easy - just call the
1.1 deraadt 25: * insertfile routine with the file name.
26: */
1.3 millert 27: /* ARGSUSED */
28: int
1.15 vincent 29: fileinsert(int f, int n)
1.1 deraadt 30: {
1.29 vincent 31: char fname[NFILEN], *bufp, *adjf;
1.1 deraadt 32:
1.54 kjell 33: if (getbufcwd(fname, sizeof(fname)) != TRUE)
34: fname[0] = '\0';
35: bufp = eread("Insert file: ", fname, NFILEN,
36: EFNEW | EFCR | EFFILE | EFDEF);
1.29 vincent 37: if (bufp == NULL)
1.32 db 38: return (ABORT);
1.29 vincent 39: else if (bufp[0] == '\0')
1.32 db 40: return (FALSE);
1.58 jason 41: adjf = adjustname(bufp, TRUE);
1.22 vincent 42: if (adjf == NULL)
43: return (FALSE);
1.32 db 44: return (insertfile(adjf, NULL, FALSE));
1.1 deraadt 45: }
46:
47: /*
1.9 mickey 48: * Select a file for editing. Look around to see if you can find the file
1.4 millert 49: * in another buffer; if you can find it, just switch to the buffer. If
1.9 mickey 50: * you cannot find the file, create a new buffer, read in the text, and
1.4 millert 51: * switch to the new buffer.
1.1 deraadt 52: */
1.3 millert 53: /* ARGSUSED */
54: int
1.15 vincent 55: filevisit(int f, int n)
1.1 deraadt 56: {
1.45 deraadt 57: struct buffer *bp;
1.54 kjell 58: char fname[NFILEN], *bufp, *adjf;
1.39 kjell 59: int status;
1.34 cloder 60:
1.54 kjell 61: if (getbufcwd(fname, sizeof(fname)) != TRUE)
1.34 cloder 62: fname[0] = '\0';
1.38 kjell 63: bufp = eread("Find file: ", fname, NFILEN,
64: EFNEW | EFCR | EFFILE | EFDEF);
1.29 vincent 65: if (bufp == NULL)
1.32 db 66: return (ABORT);
1.29 vincent 67: else if (bufp[0] == '\0')
1.32 db 68: return (FALSE);
1.58 jason 69: adjf = adjustname(fname, TRUE);
1.22 vincent 70: if (adjf == NULL)
71: return (FALSE);
1.3 millert 72: if ((bp = findbuffer(adjf)) == NULL)
1.32 db 73: return (FALSE);
1.1 deraadt 74: curbp = bp;
1.55 kjell 75: if (showbuffer(bp, curwp, WFFULL) != TRUE)
1.32 db 76: return (FALSE);
1.39 kjell 77: if (bp->b_fname[0] == '\0') {
1.30 jfb 78: if ((status = readin(adjf)) != TRUE)
79: killbuffer(bp);
1.32 db 80: return (status);
1.30 jfb 81: }
1.32 db 82: return (TRUE);
1.35 jason 83: }
84:
1.36 kjell 85: /*
86: * Replace the current file with an alternate one. Semantics for finding
87: * the replacement file are the same as 'filevisit', except the current
88: * buffer is killed before the switch. If the kill fails, or is aborted,
89: * revert to the original file.
90: */
1.43 kjell 91: /* ARGSUSED */
1.35 jason 92: int
93: filevisitalt(int f, int n)
94: {
1.45 deraadt 95: struct buffer *bp;
1.54 kjell 96: char fname[NFILEN], *bufp, *adjf;
1.36 kjell 97: int status;
98:
1.54 kjell 99: if (getbufcwd(fname, sizeof(fname)) != TRUE)
1.36 kjell 100: fname[0] = '\0';
101: bufp = eread("Find alternate file: ", fname, NFILEN,
102: EFNEW | EFCR | EFFILE | EFDEF);
103: if (bufp == NULL)
1.35 jason 104: return (ABORT);
1.36 kjell 105: else if (bufp[0] == '\0')
106: return (FALSE);
107:
108: status = killbuffer(curbp);
109: if (status == ABORT || status == FALSE)
110: return (ABORT);
111:
1.58 jason 112: adjf = adjustname(fname, TRUE);
1.36 kjell 113: if (adjf == NULL)
114: return (FALSE);
115: if ((bp = findbuffer(adjf)) == NULL)
116: return (FALSE);
117: curbp = bp;
1.55 kjell 118: if (showbuffer(bp, curwp, WFFULL) != TRUE)
1.36 kjell 119: return (FALSE);
120: if (bp->b_fname[0] == '\0') {
121: if ((status = readin(adjf)) != TRUE)
122: killbuffer(bp);
123: return (status);
124: }
125: return (TRUE);
1.1 deraadt 126: }
127:
1.17 vincent 128: int
129: filevisitro(int f, int n)
130: {
131: int error;
132:
133: error = filevisit(f, n);
134: if (error != TRUE)
135: return (error);
136: curbp->b_flag |= BFREADONLY;
137: return (TRUE);
138: }
1.32 db 139:
1.1 deraadt 140: /*
1.4 millert 141: * Pop to a file in the other window. Same as the last function, but uses
1.1 deraadt 142: * popbuf instead of showbuffer.
143: */
1.3 millert 144: /* ARGSUSED */
145: int
1.15 vincent 146: poptofile(int f, int n)
1.1 deraadt 147: {
1.45 deraadt 148: struct buffer *bp;
149: struct mgwin *wp;
1.29 vincent 150: char fname[NFILEN], *adjf, *bufp;
1.39 kjell 151: int status;
1.1 deraadt 152:
1.54 kjell 153: if (getbufcwd(fname, sizeof(fname)) != TRUE)
154: fname[0] = '\0';
1.29 vincent 155: if ((bufp = eread("Find file in other window: ", fname, NFILEN,
1.54 kjell 156: EFNEW | EFCR | EFFILE | EFDEF)) == NULL)
1.32 db 157: return (ABORT);
1.29 vincent 158: else if (bufp[0] == '\0')
1.32 db 159: return (FALSE);
1.58 jason 160: adjf = adjustname(fname, TRUE);
1.22 vincent 161: if (adjf == NULL)
162: return (FALSE);
1.3 millert 163: if ((bp = findbuffer(adjf)) == NULL)
1.32 db 164: return (FALSE);
1.56 kjell 165: if (bp == curbp)
166: return (splitwind(f, n));
1.71 kjell 167: if ((wp = popbuf(bp, WNONE)) == NULL)
1.32 db 168: return (FALSE);
1.1 deraadt 169: curbp = bp;
170: curwp = wp;
1.39 kjell 171: if (bp->b_fname[0] == '\0') {
1.30 jfb 172: if ((status = readin(adjf)) != TRUE)
173: killbuffer(bp);
1.32 db 174: return (status);
1.30 jfb 175: }
1.32 db 176: return (TRUE);
1.1 deraadt 177: }
178:
179: /*
1.9 mickey 180: * Read the file "fname" into the current buffer. Make all of the text
181: * in the buffer go away, after checking for unsaved changes. This is
182: * called by the "read" command, the "visit" command, and the mainline
1.19 vincent 183: * (for "mg file").
1.1 deraadt 184: */
1.3 millert 185: int
1.19 vincent 186: readin(char *fname)
1.3 millert 187: {
1.45 deraadt 188: struct mgwin *wp;
1.82 lum 189: struct stat statbuf;
1.41 kjell 190: int status, i, ro = FALSE;
1.20 vincent 191: PF *ael;
1.88 florian 192: char dp[NFILEN];
1.1 deraadt 193:
1.4 millert 194: /* might be old */
195: if (bclear(curbp) != TRUE)
1.32 db 196: return (TRUE);
1.57 kjell 197: /* Clear readonly. May be set by autoexec path */
1.68 kjell 198: curbp->b_flag &= ~BFREADONLY;
1.30 jfb 199: if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
1.92 lum 200: dobeep();
1.30 jfb 201: ewprintf("File is not readable: %s", fname);
1.32 db 202: return (FALSE);
1.30 jfb 203: }
1.18 vincent 204:
1.57 kjell 205: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
206: if (wp->w_bufp == curbp) {
1.81 lum 207: wp->w_dotp = wp->w_linep = bfirstlp(curbp);
208: wp->w_doto = 0;
209: wp->w_markp = NULL;
210: wp->w_marko = 0;
1.57 kjell 211: }
212: }
213:
1.18 vincent 214: /*
215: * Call auto-executing function if we need to.
216: */
217: if ((ael = find_autoexec(fname)) != NULL) {
218: for (i = 0; ael[i] != NULL; i++)
219: (*ael[i])(0, 1);
220: free(ael);
221: }
1.4 millert 222:
223: /* no change */
224: curbp->b_flag &= ~BFCHG;
1.16 vincent 225:
1.41 kjell 226: /*
1.82 lum 227: * Set the buffer READONLY flag if any of following are true:
228: * 1. file is a directory.
229: * 2. file is read-only.
230: * 3. file doesn't exist and directory is read-only.
1.41 kjell 231: */
1.82 lum 232: if (fisdir(fname) == TRUE) {
1.41 kjell 233: ro = TRUE;
1.95 florian 234: } else if ((access(fname, W_OK) == -1)) {
235: if (errno != ENOENT) {
1.82 lum 236: ro = TRUE;
1.95 florian 237: } else if (errno == ENOENT) {
238: (void)xdirname(dp, fname, sizeof(dp));
239: (void)strlcat(dp, "/", sizeof(dp));
240:
241: /* Missing directory; keep buffer rw, like emacs */
242: if (stat(dp, &statbuf) == -1 && errno == ENOENT) {
243: if (eyorn("Missing directory, create") == TRUE)
244: (void)do_makedir(dp);
245: } else if (access(dp, W_OK) == -1 && errno == EACCES) {
246: ewprintf("File not found and directory"
247: " write-protected");
248: ro = TRUE;
249: }
1.82 lum 250: }
251: }
1.41 kjell 252: if (ro == TRUE)
1.16 vincent 253: curbp->b_flag |= BFREADONLY;
1.25 deraadt 254:
1.65 pyr 255: if (startrow) {
1.24 deraadt 256: gotoline(FFARG, startrow);
1.65 pyr 257: startrow = 0;
258: }
1.16 vincent 259:
1.60 kjell 260: undo_add_modified();
1.32 db 261: return (status);
1.1 deraadt 262: }
1.4 millert 263:
1.1 deraadt 264: /*
265: * NB, getting file attributes is done here under control of a flag
266: * rather than in readin, which would be cleaner. I was concerned
267: * that some operating system might require the file to be open
268: * in order to get the information. Similarly for writing.
269: */
270:
271: /*
1.39 kjell 272: * Insert a file in the current buffer, after dot. If file is a directory,
273: * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
274: * If file is a regular file, set mark at the end of the text inserted;
1.47 kjell 275: * point at the beginning. Return a standard status. Print a summary
276: * (lines read, error message) out as well. This routine also does the
277: * read end of backup processing. The BFBAK flag, if set in a buffer,
278: * says that a backup should be taken. It is set when a file is read in,
279: * but not on a new file. You don't need to make a backup copy of nothing.
1.1 deraadt 280: */
1.47 kjell 281:
1.9 mickey 282: static char *line = NULL;
283: static int linesize = 0;
1.1 deraadt 284:
1.3 millert 285: int
1.39 kjell 286: insertfile(char *fname, char *newname, int replacebuf)
1.3 millert 287: {
1.45 deraadt 288: struct buffer *bp;
289: struct line *lp1, *lp2;
290: struct line *olp; /* line we started at */
291: struct mgwin *wp;
1.63 kjell 292: int nbytes, s, nline = 0, siz, x, x2;
1.19 vincent 293: int opos; /* offset we started at */
1.59 kjell 294: int oline; /* original line number */
1.80 lum 295: FILE *ffp;
1.4 millert 296:
1.39 kjell 297: if (replacebuf == TRUE)
1.69 kjell 298: x = undo_enable(FFRAND, 0);
1.63 kjell 299: else
300: x = undo_enabled();
1.23 vincent 301:
1.4 millert 302: lp1 = NULL;
1.1 deraadt 303: if (line == NULL) {
304: line = malloc(NLINE);
1.21 vincent 305: if (line == NULL)
306: panic("out of memory");
1.1 deraadt 307: linesize = NLINE;
308: }
1.4 millert 309:
310: /* cheap */
311: bp = curbp;
1.54 kjell 312: if (newname != NULL) {
313: (void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
1.75 kjell 314: (void)xdirname(bp->b_cwd, newname, sizeof(bp->b_cwd));
1.54 kjell 315: (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
316: }
1.4 millert 317:
318: /* hard file open */
1.80 lum 319: if ((s = ffropen(&ffp, fname, (replacebuf == TRUE) ? bp : NULL))
320: == FIOERR)
1.1 deraadt 321: goto out;
1.4 millert 322: if (s == FIOFNF) {
323: /* file not found */
1.1 deraadt 324: if (newname != NULL)
325: ewprintf("(New file)");
1.3 millert 326: else
327: ewprintf("(File not found)");
1.1 deraadt 328: goto out;
1.40 kjell 329: } else if (s == FIODIR) {
330: /* file was a directory */
331: if (replacebuf == FALSE) {
1.92 lum 332: dobeep();
1.40 kjell 333: ewprintf("Cannot insert: file is a directory, %s",
334: fname);
335: goto cleanup;
336: }
337: killbuffer(bp);
1.75 kjell 338: bp = dired_(fname);
339: undo_enable(FFRAND, x);
340: if (bp == NULL)
1.40 kjell 341: return (FALSE);
342: curbp = bp;
1.55 kjell 343: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54 kjell 344: } else {
1.75 kjell 345: (void)xdirname(bp->b_cwd, fname, sizeof(bp->b_cwd));
1.54 kjell 346: (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
1.1 deraadt 347: }
348: opos = curwp->w_doto;
1.64 kjell 349: oline = curwp->w_dotline;
350: /*
351: * Open a new line at dot and start inserting after it.
352: * We will delete this newline after insertion.
353: * Disable undo, as we create the undo record manually.
354: */
1.69 kjell 355: x2 = undo_enable(FFRAND, 0);
1.20 vincent 356: (void)lnewline();
1.1 deraadt 357: olp = lback(curwp->w_dotp);
1.69 kjell 358: undo_enable(FFRAND, x2);
1.4 millert 359:
360: nline = 0;
1.19 vincent 361: siz = 0;
1.80 lum 362: while ((s = ffgetline(ffp, line, linesize, &nbytes)) != FIOERR) {
1.64 kjell 363: retry:
1.19 vincent 364: siz += nbytes + 1;
1.3 millert 365: switch (s) {
366: case FIOSUC:
1.48 kjell 367: /* FALLTHRU */
1.4 millert 368: case FIOEOF:
1.59 kjell 369: ++nline;
1.3 millert 370: if ((lp1 = lalloc(nbytes)) == NULL) {
1.4 millert 371: /* keep message on the display */
372: s = FIOERR;
1.64 kjell 373: undo_add_insert(olp, opos,
374: siz - nbytes - 1 - 1);
1.4 millert 375: goto endoffile;
1.3 millert 376: }
377: bcopy(line, <ext(lp1)[0], nbytes);
378: lp2 = lback(curwp->w_dotp);
379: lp2->l_fp = lp1;
380: lp1->l_fp = curwp->w_dotp;
381: lp1->l_bp = lp2;
382: curwp->w_dotp->l_bp = lp1;
1.64 kjell 383: if (s == FIOEOF) {
384: undo_add_insert(olp, opos, siz - 1);
1.3 millert 385: goto endoffile;
1.64 kjell 386: }
1.3 millert 387: break;
1.19 vincent 388: case FIOLONG: {
1.4 millert 389: /* a line too long to fit in our buffer */
1.9 mickey 390: char *cp;
391: int newsize;
1.3 millert 392:
393: newsize = linesize * 2;
394: if (newsize < 0 ||
1.51 deraadt 395: (cp = malloc(newsize)) == NULL) {
1.92 lum 396: dobeep();
1.3 millert 397: ewprintf("Could not allocate %d bytes",
1.4 millert 398: newsize);
399: s = FIOERR;
400: goto endoffile;
1.3 millert 401: }
402: bcopy(line, cp, linesize);
403: free(line);
404: line = cp;
1.80 lum 405: s = ffgetline(ffp, line + linesize, linesize,
1.4 millert 406: &nbytes);
1.3 millert 407: nbytes += linesize;
408: linesize = newsize;
409: if (s == FIOERR)
410: goto endoffile;
1.64 kjell 411: goto retry;
1.3 millert 412: }
413: default:
1.92 lum 414: dobeep();
1.3 millert 415: ewprintf("Unknown code %d reading file", s);
416: s = FIOERR;
417: break;
1.1 deraadt 418: }
419: }
420: endoffile:
1.4 millert 421: /* ignore errors */
1.80 lum 422: (void)ffclose(ffp, NULL);
1.4 millert 423: /* don't zap an error */
424: if (s == FIOEOF) {
1.3 millert 425: if (nline == 1)
426: ewprintf("(Read 1 line)");
427: else
428: ewprintf("(Read %d lines)", nline);
1.1 deraadt 429: }
1.4 millert 430: /* set mark at the end of the text */
1.1 deraadt 431: curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
432: curwp->w_marko = llength(curwp->w_markp);
1.64 kjell 433: curwp->w_markline = oline + nline + 1;
434: /*
435: * if we are at the end of the file, ldelnewline is a no-op,
436: * but we still need to decrement the line and markline counts
437: * as we've accounted for this fencepost in our arithmetic
438: */
439: if (lforw(curwp->w_dotp) == curwp->w_bufp->b_headp) {
440: curwp->w_bufp->b_lines--;
441: curwp->w_markline--;
442: } else
443: (void)ldelnewline();
1.1 deraadt 444: curwp->w_dotp = olp;
445: curwp->w_doto = opos;
1.59 kjell 446: curwp->w_dotline = oline;
1.61 kjell 447: if (olp == curbp->b_headp)
1.3 millert 448: curwp->w_dotp = lforw(olp);
1.1 deraadt 449: if (newname != NULL)
1.3 millert 450: bp->b_flag |= BFCHG | BFBAK; /* Need a backup. */
451: else
452: bp->b_flag |= BFCHG;
453: /*
1.32 db 454: * If the insert was at the end of buffer, set lp1 to the end of
1.3 millert 455: * buffer line, and lp2 to the beginning of the newly inserted text.
456: * (Otherwise lp2 is set to NULL.) This is used below to set
457: * pointers in other windows correctly if they are also at the end of
458: * buffer.
1.1 deraadt 459: */
1.61 kjell 460: lp1 = bp->b_headp;
1.1 deraadt 461: if (curwp->w_markp == lp1) {
462: lp2 = curwp->w_dotp;
463: } else {
1.4 millert 464: /* delete extraneous newline */
1.7 art 465: (void)ldelnewline();
1.1 deraadt 466: out: lp2 = NULL;
467: }
1.3 millert 468: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 469: if (wp->w_bufp == curbp) {
1.70 kjell 470: wp->w_rflag |= WFMODE | WFEDIT;
1.1 deraadt 471: if (wp != curwp && lp2 != NULL) {
1.3 millert 472: if (wp->w_dotp == lp1)
473: wp->w_dotp = lp2;
474: if (wp->w_markp == lp1)
475: wp->w_markp = lp2;
476: if (wp->w_linep == lp1)
477: wp->w_linep = lp2;
1.1 deraadt 478: }
479: }
480: }
1.59 kjell 481: bp->b_lines += nline;
1.40 kjell 482: cleanup:
1.69 kjell 483: undo_enable(FFRAND, x);
1.23 vincent 484:
1.39 kjell 485: /* return FALSE if error */
1.32 db 486: return (s != FIOERR);
1.1 deraadt 487: }
488:
489: /*
1.9 mickey 490: * Ask for a file name and write the contents of the current buffer to that
491: * file. Update the remembered file name and clear the buffer changed flag.
1.4 millert 492: * This handling of file names is different from the earlier versions and
1.27 jmc 493: * is more compatible with Gosling EMACS than with ITS EMACS.
1.1 deraadt 494: */
1.3 millert 495: /* ARGSUSED */
496: int
1.20 vincent 497: filewrite(int f, int n)
1.1 deraadt 498: {
1.77 lum 499: struct stat statbuf;
1.4 millert 500: int s;
1.77 lum 501: char fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25];
1.53 kjell 502: char *adjfname, *bufp;
1.80 lum 503: FILE *ffp;
1.29 vincent 504:
1.54 kjell 505: if (getbufcwd(fname, sizeof(fname)) != TRUE)
506: fname[0] = '\0';
1.29 vincent 507: if ((bufp = eread("Write file: ", fname, NFILEN,
1.54 kjell 508: EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.32 db 509: return (ABORT);
1.29 vincent 510: else if (bufp[0] == '\0')
1.32 db 511: return (FALSE);
1.1 deraadt 512:
1.58 jason 513: adjfname = adjustname(fname, TRUE);
1.22 vincent 514: if (adjfname == NULL)
515: return (FALSE);
1.77 lum 516:
517: /* Check if file exists; write checks done later */
518: if (stat(adjfname, &statbuf) == 0) {
1.91 lum 519: if (S_ISDIR(statbuf.st_mode)) {
520: dobeep();
521: ewprintf("%s is a directory", adjfname);
522: return (FALSE);
523: }
1.77 lum 524: snprintf(tmp, sizeof(tmp), "File `%s' exists; overwrite",
525: adjfname);
526: if ((s = eyorn(tmp)) != TRUE)
527: return (s);
528: }
529:
1.1 deraadt 530: /* old attributes are no longer current */
531: bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.80 lum 532: if ((s = writeout(&ffp, curbp, adjfname)) == TRUE) {
1.53 kjell 533: (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
1.54 kjell 534: if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
535: (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
1.73 kjell 536: if (augbname(bn, curbp->b_fname, sizeof(bn))
1.53 kjell 537: == FALSE)
538: return (FALSE);
1.67 kjell 539: free(curbp->b_bname);
1.53 kjell 540: if ((curbp->b_bname = strdup(bn)) == NULL)
541: return (FALSE);
1.78 lum 542: (void)fupdstat(curbp);
1.1 deraadt 543: curbp->b_flag &= ~(BFBAK | BFCHG);
544: upmodes(curbp);
1.87 florian 545: undo_add_boundary(FFRAND, 1);
546: undo_add_modified();
1.1 deraadt 547: }
1.32 db 548: return (s);
1.1 deraadt 549: }
550:
551: /*
1.4 millert 552: * Save the contents of the current buffer back into its associated file.
1.1 deraadt 553: */
1.97 ! bcallah 554: static int makebackup = TRUE;
1.1 deraadt 555:
1.3 millert 556: /* ARGSUSED */
557: int
1.20 vincent 558: filesave(int f, int n)
1.1 deraadt 559: {
1.79 lum 560: if (curbp->b_fname[0] == '\0')
561: return (filewrite(f, n));
562: else
563: return (buffsave(curbp));
1.1 deraadt 564: }
565:
566: /*
1.9 mickey 567: * Save the contents of the buffer argument into its associated file. Do
568: * nothing if there have been no changes (is this a bug, or a feature?).
569: * Error if there is no remembered file name. If this is the first write
1.4 millert 570: * since the read or visit, then a backup copy of the file is made.
1.9 mickey 571: * Allow user to select whether or not to make backup files by looking at
1.4 millert 572: * the value of makebackup.
1.1 deraadt 573: */
1.3 millert 574: int
1.45 deraadt 575: buffsave(struct buffer *bp)
1.3 millert 576: {
1.4 millert 577: int s;
1.80 lum 578: FILE *ffp;
1.1 deraadt 579:
1.4 millert 580: /* return, no changes */
581: if ((bp->b_flag & BFCHG) == 0) {
1.1 deraadt 582: ewprintf("(No changes need to be saved)");
1.32 db 583: return (TRUE);
1.1 deraadt 584: }
1.4 millert 585:
586: /* must have a name */
587: if (bp->b_fname[0] == '\0') {
1.92 lum 588: dobeep();
1.1 deraadt 589: ewprintf("No file name");
590: return (FALSE);
591: }
1.4 millert 592:
1.68 kjell 593: /* Ensure file has not been modified elsewhere */
594: /* We don't use the ignore flag here */
595: if (fchecktime(bp) != TRUE) {
596: if ((s = eyesno("File has changed on disk since last save. "
597: "Save anyway")) != TRUE)
598: return (s);
599: }
600:
1.3 millert 601: if (makebackup && (bp->b_flag & BFBAK)) {
1.1 deraadt 602: s = fbackupfile(bp->b_fname);
1.4 millert 603: /* hard error */
604: if (s == ABORT)
1.32 db 605: return (FALSE);
1.4 millert 606: /* softer error */
1.9 mickey 607: if (s == FALSE &&
1.4 millert 608: (s = eyesno("Backup error, save anyway")) != TRUE)
1.32 db 609: return (s);
1.1 deraadt 610: }
1.80 lum 611: if ((s = writeout(&ffp, bp, bp->b_fname)) == TRUE) {
1.78 lum 612: (void)fupdstat(bp);
1.1 deraadt 613: bp->b_flag &= ~(BFCHG | BFBAK);
614: upmodes(bp);
1.87 florian 615: undo_add_boundary(FFRAND, 1);
616: undo_add_modified();
1.1 deraadt 617: }
1.32 db 618: return (s);
1.1 deraadt 619: }
620:
1.3 millert 621: /*
622: * Since we don't have variables (we probably should) this is a command
623: * processor for changing the value of the make backup flag. If no argument
624: * is given, sets makebackup to true, so backups are made. If an argument is
1.59 kjell 625: * given, no backup files are made when saving a new version of a file.
1.1 deraadt 626: */
1.3 millert 627: /* ARGSUSED */
628: int
1.20 vincent 629: makebkfile(int f, int n)
1.1 deraadt 630: {
1.3 millert 631: if (f & FFARG)
632: makebackup = n > 0;
633: else
634: makebackup = !makebackup;
1.1 deraadt 635: ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32 db 636: return (TRUE);
1.1 deraadt 637: }
638:
639: /*
640: * NB: bp is passed to both ffwopen and ffclose because some
641: * attribute information may need to be updated at open time
642: * and others after the close. This is OS-dependent. Note
643: * that the ff routines are assumed to be able to tell whether
644: * the attribute information has been set up in this buffer
645: * or not.
646: */
647:
648: /*
1.9 mickey 649: * This function performs the details of file writing; writing the file
650: * in buffer bp to file fn. Uses the file management routines in the
1.4 millert 651: * "fileio.c" package. Most of the grief is checking of some sort.
1.78 lum 652: * You may want to call fupdstat() after using this function.
1.1 deraadt 653: */
1.3 millert 654: int
1.80 lum 655: writeout(FILE ** ffp, struct buffer *bp, char *fn)
1.3 millert 656: {
1.83 lum 657: struct stat statbuf;
1.4 millert 658: int s;
1.88 florian 659: char dp[NFILEN];
1.83 lum 660:
661: if (stat(fn, &statbuf) == -1 && errno == ENOENT) {
1.84 lum 662: errno = 0;
1.89 florian 663: (void)xdirname(dp, fn, sizeof(dp));
664: (void)strlcat(dp, "/", sizeof(dp));
1.83 lum 665: if (access(dp, W_OK) && errno == EACCES) {
1.92 lum 666: dobeep();
1.88 florian 667: ewprintf("Directory %s write-protected", dp);
1.83 lum 668: return (FIOERR);
669: } else if (errno == ENOENT) {
1.92 lum 670: dobeep();
671: ewprintf("%s: no such directory", dp);
1.83 lum 672: return (FIOERR);
673: }
674: }
1.4 millert 675: /* open writes message */
1.80 lum 676: if ((s = ffwopen(ffp, fn, bp)) != FIOSUC)
1.1 deraadt 677: return (FALSE);
1.80 lum 678: s = ffputbuf(*ffp, bp);
1.4 millert 679: if (s == FIOSUC) {
680: /* no write error */
1.80 lum 681: s = ffclose(*ffp, bp);
1.3 millert 682: if (s == FIOSUC)
1.1 deraadt 683: ewprintf("Wrote %s", fn);
1.76 lum 684: } else {
685: /* print a message indicating write error */
1.80 lum 686: (void)ffclose(*ffp, bp);
1.92 lum 687: dobeep();
1.76 lum 688: ewprintf("Unable to write %s", fn);
689: }
1.32 db 690: return (s == FIOSUC);
1.1 deraadt 691: }
692:
693: /*
1.4 millert 694: * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1 deraadt 695: * mode line updated.
696: */
1.7 art 697: void
1.45 deraadt 698: upmodes(struct buffer *bp)
1.3 millert 699: {
1.45 deraadt 700: struct mgwin *wp;
1.1 deraadt 701:
702: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3 millert 703: if (bp == NULL || curwp->w_bufp == bp)
1.70 kjell 704: wp->w_rflag |= WFMODE;
1.66 kjell 705: }
706:
707: /*
1.75 kjell 708: * dirname using strlcpy semantic.
709: * Like dirname() except an empty string is returned in
1.66 kjell 710: * place of "/". This means we can always add a trailing
711: * slash and be correct.
1.75 kjell 712: * Address portability issues by copying argument
713: * before using. Some implementations modify the input string.
1.66 kjell 714: */
1.75 kjell 715: size_t
716: xdirname(char *dp, const char *path, size_t dplen)
1.66 kjell 717: {
1.75 kjell 718: char ts[NFILEN];
719: size_t len;
720:
721: (void)strlcpy(ts, path, NFILEN);
722: len = strlcpy(dp, dirname(ts), dplen);
723: if (dplen > 0 && dp[0] == '/' && dp[1] == '\0') {
724: dp[0] = '\0';
725: len = 0;
726: }
727: return (len);
728: }
729:
730: /*
731: * basename using strlcpy/strlcat semantic.
732: * Address portability issue by copying argument
733: * before using: some implementations modify the input string.
734: */
735: size_t
736: xbasename(char *bp, const char *path, size_t bplen)
737: {
738: char ts[NFILEN];
739:
740: (void)strlcpy(ts, path, NFILEN);
741: return (strlcpy(bp, basename(ts), bplen));
1.1 deraadt 742: }