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