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