Annotation of src/usr.bin/mg/file.c, Revision 1.74
1.74 ! kjell 1: /* $OpenBSD: file.c,v 1.73 2011/01/18 16:29:37 kjell 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.54 kjell 11: #include <libgen.h>
1.41 kjell 12:
1.66 kjell 13: static char *xdirname(const char *);
14:
1.1 deraadt 15: /*
1.4 millert 16: * Insert a file into the current buffer. Real easy - just call the
1.1 deraadt 17: * insertfile routine with the file name.
18: */
1.3 millert 19: /* ARGSUSED */
20: int
1.15 vincent 21: fileinsert(int f, int n)
1.1 deraadt 22: {
1.29 vincent 23: char fname[NFILEN], *bufp, *adjf;
1.1 deraadt 24:
1.54 kjell 25: if (getbufcwd(fname, sizeof(fname)) != TRUE)
26: fname[0] = '\0';
27: bufp = eread("Insert file: ", fname, NFILEN,
28: EFNEW | EFCR | EFFILE | EFDEF);
1.29 vincent 29: if (bufp == NULL)
1.32 db 30: return (ABORT);
1.29 vincent 31: else if (bufp[0] == '\0')
1.32 db 32: return (FALSE);
1.58 jason 33: adjf = adjustname(bufp, TRUE);
1.22 vincent 34: if (adjf == NULL)
35: return (FALSE);
1.32 db 36: return (insertfile(adjf, NULL, FALSE));
1.1 deraadt 37: }
38:
39: /*
1.9 mickey 40: * Select a file for editing. Look around to see if you can find the file
1.4 millert 41: * in another buffer; if you can find it, just switch to the buffer. If
1.9 mickey 42: * you cannot find the file, create a new buffer, read in the text, and
1.4 millert 43: * switch to the new buffer.
1.1 deraadt 44: */
1.3 millert 45: /* ARGSUSED */
46: int
1.15 vincent 47: filevisit(int f, int n)
1.1 deraadt 48: {
1.45 deraadt 49: struct buffer *bp;
1.54 kjell 50: char fname[NFILEN], *bufp, *adjf;
1.39 kjell 51: int status;
1.34 cloder 52:
1.54 kjell 53: if (getbufcwd(fname, sizeof(fname)) != TRUE)
1.34 cloder 54: fname[0] = '\0';
1.38 kjell 55: bufp = eread("Find file: ", fname, NFILEN,
56: EFNEW | EFCR | EFFILE | EFDEF);
1.29 vincent 57: if (bufp == NULL)
1.32 db 58: return (ABORT);
1.29 vincent 59: else if (bufp[0] == '\0')
1.32 db 60: return (FALSE);
1.58 jason 61: adjf = adjustname(fname, TRUE);
1.22 vincent 62: if (adjf == NULL)
63: return (FALSE);
1.3 millert 64: if ((bp = findbuffer(adjf)) == NULL)
1.32 db 65: return (FALSE);
1.1 deraadt 66: curbp = bp;
1.55 kjell 67: if (showbuffer(bp, curwp, WFFULL) != TRUE)
1.32 db 68: return (FALSE);
1.39 kjell 69: if (bp->b_fname[0] == '\0') {
1.30 jfb 70: if ((status = readin(adjf)) != TRUE)
71: killbuffer(bp);
1.32 db 72: return (status);
1.30 jfb 73: }
1.32 db 74: return (TRUE);
1.35 jason 75: }
76:
1.36 kjell 77: /*
78: * Replace the current file with an alternate one. Semantics for finding
79: * the replacement file are the same as 'filevisit', except the current
80: * buffer is killed before the switch. If the kill fails, or is aborted,
81: * revert to the original file.
82: */
1.43 kjell 83: /* ARGSUSED */
1.35 jason 84: int
85: filevisitalt(int f, int n)
86: {
1.45 deraadt 87: struct buffer *bp;
1.54 kjell 88: char fname[NFILEN], *bufp, *adjf;
1.36 kjell 89: int status;
90:
1.54 kjell 91: if (getbufcwd(fname, sizeof(fname)) != TRUE)
1.36 kjell 92: fname[0] = '\0';
93: bufp = eread("Find alternate file: ", fname, NFILEN,
94: EFNEW | EFCR | EFFILE | EFDEF);
95: if (bufp == NULL)
1.35 jason 96: return (ABORT);
1.36 kjell 97: else if (bufp[0] == '\0')
98: return (FALSE);
99:
100: status = killbuffer(curbp);
101: if (status == ABORT || status == FALSE)
102: return (ABORT);
103:
1.58 jason 104: adjf = adjustname(fname, TRUE);
1.36 kjell 105: if (adjf == NULL)
106: return (FALSE);
107: if ((bp = findbuffer(adjf)) == NULL)
108: return (FALSE);
109: curbp = bp;
1.55 kjell 110: if (showbuffer(bp, curwp, WFFULL) != TRUE)
1.36 kjell 111: return (FALSE);
112: if (bp->b_fname[0] == '\0') {
113: if ((status = readin(adjf)) != TRUE)
114: killbuffer(bp);
115: return (status);
116: }
117: return (TRUE);
1.1 deraadt 118: }
119:
1.17 vincent 120: int
121: filevisitro(int f, int n)
122: {
123: int error;
124:
125: error = filevisit(f, n);
126: if (error != TRUE)
127: return (error);
128: curbp->b_flag |= BFREADONLY;
129: return (TRUE);
130: }
1.32 db 131:
1.1 deraadt 132: /*
1.4 millert 133: * Pop to a file in the other window. Same as the last function, but uses
1.1 deraadt 134: * popbuf instead of showbuffer.
135: */
1.3 millert 136: /* ARGSUSED */
137: int
1.15 vincent 138: poptofile(int f, int n)
1.1 deraadt 139: {
1.45 deraadt 140: struct buffer *bp;
141: struct mgwin *wp;
1.29 vincent 142: char fname[NFILEN], *adjf, *bufp;
1.39 kjell 143: int status;
1.1 deraadt 144:
1.54 kjell 145: if (getbufcwd(fname, sizeof(fname)) != TRUE)
146: fname[0] = '\0';
1.29 vincent 147: if ((bufp = eread("Find file in other window: ", fname, NFILEN,
1.54 kjell 148: EFNEW | EFCR | EFFILE | EFDEF)) == NULL)
1.32 db 149: return (ABORT);
1.29 vincent 150: else if (bufp[0] == '\0')
1.32 db 151: return (FALSE);
1.58 jason 152: adjf = adjustname(fname, TRUE);
1.22 vincent 153: if (adjf == NULL)
154: return (FALSE);
1.3 millert 155: if ((bp = findbuffer(adjf)) == NULL)
1.32 db 156: return (FALSE);
1.56 kjell 157: if (bp == curbp)
158: return (splitwind(f, n));
1.71 kjell 159: if ((wp = popbuf(bp, WNONE)) == NULL)
1.32 db 160: return (FALSE);
1.1 deraadt 161: curbp = bp;
162: curwp = wp;
1.39 kjell 163: if (bp->b_fname[0] == '\0') {
1.30 jfb 164: if ((status = readin(adjf)) != TRUE)
165: killbuffer(bp);
1.32 db 166: return (status);
1.30 jfb 167: }
1.32 db 168: return (TRUE);
1.1 deraadt 169: }
170:
171: /*
1.32 db 172: * Given a file name, either find the buffer it uses, or create a new
1.1 deraadt 173: * empty buffer to put it in.
174: */
1.45 deraadt 175: struct buffer *
1.44 kjell 176: findbuffer(char *fn)
1.1 deraadt 177: {
1.52 deraadt 178: struct buffer *bp;
179: char bname[NBUFN], fname[NBUFN];
1.1 deraadt 180:
1.44 kjell 181: if (strlcpy(fname, fn, sizeof(fname)) >= sizeof(fname)) {
182: ewprintf("filename too long");
183: return (NULL);
184: }
185:
1.3 millert 186: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.6 art 187: if (strcmp(bp->b_fname, fname) == 0)
1.32 db 188: return (bp);
1.1 deraadt 189: }
1.53 kjell 190: /* Not found. Create a new one, adjusting name first */
1.54 kjell 191: if (augbname(bname, fname, sizeof(bname)) == FALSE)
1.44 kjell 192: return (NULL);
1.10 vincent 193:
1.53 kjell 194: bp = bfind(bname, TRUE);
195: return (bp);
1.1 deraadt 196: }
197:
198: /*
1.9 mickey 199: * Read the file "fname" into the current buffer. Make all of the text
200: * in the buffer go away, after checking for unsaved changes. This is
201: * called by the "read" command, the "visit" command, and the mainline
1.19 vincent 202: * (for "mg file").
1.1 deraadt 203: */
1.3 millert 204: int
1.19 vincent 205: readin(char *fname)
1.3 millert 206: {
1.45 deraadt 207: struct mgwin *wp;
1.41 kjell 208: int status, i, ro = FALSE;
1.20 vincent 209: PF *ael;
1.1 deraadt 210:
1.4 millert 211: /* might be old */
212: if (bclear(curbp) != TRUE)
1.32 db 213: return (TRUE);
1.57 kjell 214: /* Clear readonly. May be set by autoexec path */
1.68 kjell 215: curbp->b_flag &= ~BFREADONLY;
1.30 jfb 216: if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
217: ewprintf("File is not readable: %s", fname);
1.32 db 218: return (FALSE);
1.30 jfb 219: }
1.18 vincent 220:
1.57 kjell 221: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
222: if (wp->w_bufp == curbp) {
1.72 kjell 223: if ((fisdir(fname)) != TRUE) {
224: wp->w_dotp = wp->w_linep = bfirstlp(curbp);
225: wp->w_doto = 0;
226: wp->w_markp = NULL;
227: wp->w_marko = 0;
228: }
1.57 kjell 229: }
230: }
231:
1.18 vincent 232: /*
233: * Call auto-executing function if we need to.
234: */
235: if ((ael = find_autoexec(fname)) != NULL) {
236: for (i = 0; ael[i] != NULL; i++)
237: (*ael[i])(0, 1);
238: free(ael);
239: }
1.4 millert 240:
241: /* no change */
242: curbp->b_flag &= ~BFCHG;
1.16 vincent 243:
1.41 kjell 244: /*
245: * We need to set the READONLY flag after we insert the file,
246: * unless the file is a directory.
247: */
1.16 vincent 248: if (access(fname, W_OK) && errno != ENOENT)
1.41 kjell 249: ro = TRUE;
250: if (fisdir(fname) == TRUE)
251: ro = TRUE;
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.66 kjell 295: char *dp;
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.66 kjell 314: dp = xdirname(newname);
315: (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
1.54 kjell 316: (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
1.66 kjell 317: free(dp);
1.54 kjell 318: }
1.4 millert 319:
320: /* hard file open */
1.39 kjell 321: if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR)
1.1 deraadt 322: goto out;
1.4 millert 323: if (s == FIOFNF) {
324: /* file not found */
1.1 deraadt 325: if (newname != NULL)
326: ewprintf("(New file)");
1.3 millert 327: else
328: ewprintf("(File not found)");
1.1 deraadt 329: goto out;
1.40 kjell 330: } else if (s == FIODIR) {
331: /* file was a directory */
332: if (replacebuf == FALSE) {
333: ewprintf("Cannot insert: file is a directory, %s",
334: fname);
335: goto cleanup;
336: }
337: killbuffer(bp);
338: if ((bp = dired_(fname)) == NULL)
339: return (FALSE);
1.69 kjell 340: undo_enable(FFRAND, x);
1.40 kjell 341: curbp = bp;
1.55 kjell 342: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54 kjell 343: } else {
1.66 kjell 344: dp = xdirname(fname);
345: (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
1.54 kjell 346: (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
1.66 kjell 347: free(dp);
1.1 deraadt 348: }
349: opos = curwp->w_doto;
1.64 kjell 350: oline = curwp->w_dotline;
351: /*
352: * Open a new line at dot and start inserting after it.
353: * We will delete this newline after insertion.
354: * Disable undo, as we create the undo record manually.
355: */
1.69 kjell 356: x2 = undo_enable(FFRAND, 0);
1.20 vincent 357: (void)lnewline();
1.1 deraadt 358: olp = lback(curwp->w_dotp);
1.69 kjell 359: undo_enable(FFRAND, x2);
1.4 millert 360:
361: nline = 0;
1.19 vincent 362: siz = 0;
1.3 millert 363: while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
1.64 kjell 364: retry:
1.19 vincent 365: siz += nbytes + 1;
1.3 millert 366: switch (s) {
367: case FIOSUC:
1.48 kjell 368: /* FALLTHRU */
1.4 millert 369: case FIOEOF:
1.59 kjell 370: ++nline;
1.3 millert 371: if ((lp1 = lalloc(nbytes)) == NULL) {
1.4 millert 372: /* keep message on the display */
373: s = FIOERR;
1.64 kjell 374: undo_add_insert(olp, opos,
375: siz - nbytes - 1 - 1);
1.4 millert 376: goto endoffile;
1.3 millert 377: }
378: bcopy(line, <ext(lp1)[0], nbytes);
379: lp2 = lback(curwp->w_dotp);
380: lp2->l_fp = lp1;
381: lp1->l_fp = curwp->w_dotp;
382: lp1->l_bp = lp2;
383: curwp->w_dotp->l_bp = lp1;
1.64 kjell 384: if (s == FIOEOF) {
385: undo_add_insert(olp, opos, siz - 1);
1.3 millert 386: goto endoffile;
1.64 kjell 387: }
1.3 millert 388: break;
1.19 vincent 389: case FIOLONG: {
1.4 millert 390: /* a line too long to fit in our buffer */
1.9 mickey 391: char *cp;
392: int newsize;
1.3 millert 393:
394: newsize = linesize * 2;
395: if (newsize < 0 ||
1.51 deraadt 396: (cp = malloc(newsize)) == NULL) {
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.9 mickey 405: s = ffgetline(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:
414: ewprintf("Unknown code %d reading file", s);
415: s = FIOERR;
416: break;
1.1 deraadt 417: }
418: }
419: endoffile:
1.4 millert 420: /* ignore errors */
1.8 art 421: ffclose(NULL);
1.4 millert 422: /* don't zap an error */
423: if (s == FIOEOF) {
1.3 millert 424: if (nline == 1)
425: ewprintf("(Read 1 line)");
426: else
427: ewprintf("(Read %d lines)", nline);
1.1 deraadt 428: }
1.4 millert 429: /* set mark at the end of the text */
1.1 deraadt 430: curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
431: curwp->w_marko = llength(curwp->w_markp);
1.64 kjell 432: curwp->w_markline = oline + nline + 1;
433: /*
434: * if we are at the end of the file, ldelnewline is a no-op,
435: * but we still need to decrement the line and markline counts
436: * as we've accounted for this fencepost in our arithmetic
437: */
438: if (lforw(curwp->w_dotp) == curwp->w_bufp->b_headp) {
439: curwp->w_bufp->b_lines--;
440: curwp->w_markline--;
441: } else
442: (void)ldelnewline();
1.1 deraadt 443: curwp->w_dotp = olp;
444: curwp->w_doto = opos;
1.59 kjell 445: curwp->w_dotline = oline;
1.61 kjell 446: if (olp == curbp->b_headp)
1.3 millert 447: curwp->w_dotp = lforw(olp);
1.1 deraadt 448: if (newname != NULL)
1.3 millert 449: bp->b_flag |= BFCHG | BFBAK; /* Need a backup. */
450: else
451: bp->b_flag |= BFCHG;
452: /*
1.32 db 453: * If the insert was at the end of buffer, set lp1 to the end of
1.3 millert 454: * buffer line, and lp2 to the beginning of the newly inserted text.
455: * (Otherwise lp2 is set to NULL.) This is used below to set
456: * pointers in other windows correctly if they are also at the end of
457: * buffer.
1.1 deraadt 458: */
1.61 kjell 459: lp1 = bp->b_headp;
1.1 deraadt 460: if (curwp->w_markp == lp1) {
461: lp2 = curwp->w_dotp;
462: } else {
1.4 millert 463: /* delete extraneous newline */
1.7 art 464: (void)ldelnewline();
1.1 deraadt 465: out: lp2 = NULL;
466: }
1.3 millert 467: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 468: if (wp->w_bufp == curbp) {
1.70 kjell 469: wp->w_rflag |= WFMODE | WFEDIT;
1.1 deraadt 470: if (wp != curwp && lp2 != NULL) {
1.3 millert 471: if (wp->w_dotp == lp1)
472: wp->w_dotp = lp2;
473: if (wp->w_markp == lp1)
474: wp->w_markp = lp2;
475: if (wp->w_linep == lp1)
476: wp->w_linep = lp2;
1.1 deraadt 477: }
478: }
479: }
1.59 kjell 480: bp->b_lines += nline;
1.40 kjell 481: cleanup:
1.69 kjell 482: undo_enable(FFRAND, x);
1.23 vincent 483:
1.39 kjell 484: /* return FALSE if error */
1.32 db 485: return (s != FIOERR);
1.1 deraadt 486: }
487:
488: /*
1.9 mickey 489: * Ask for a file name and write the contents of the current buffer to that
490: * file. Update the remembered file name and clear the buffer changed flag.
1.4 millert 491: * This handling of file names is different from the earlier versions and
1.27 jmc 492: * is more compatible with Gosling EMACS than with ITS EMACS.
1.1 deraadt 493: */
1.3 millert 494: /* ARGSUSED */
495: int
1.20 vincent 496: filewrite(int f, int n)
1.1 deraadt 497: {
1.4 millert 498: int s;
1.53 kjell 499: char fname[NFILEN], bn[NBUFN];
500: char *adjfname, *bufp;
1.29 vincent 501:
1.54 kjell 502: if (getbufcwd(fname, sizeof(fname)) != TRUE)
503: fname[0] = '\0';
1.29 vincent 504: if ((bufp = eread("Write file: ", fname, NFILEN,
1.54 kjell 505: EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.32 db 506: return (ABORT);
1.29 vincent 507: else if (bufp[0] == '\0')
1.32 db 508: return (FALSE);
1.1 deraadt 509:
1.58 jason 510: adjfname = adjustname(fname, TRUE);
1.22 vincent 511: if (adjfname == NULL)
512: return (FALSE);
1.1 deraadt 513: /* old attributes are no longer current */
514: bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3 millert 515: if ((s = writeout(curbp, adjfname)) == TRUE) {
1.53 kjell 516: (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
1.54 kjell 517: if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
518: (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
1.73 kjell 519: if (augbname(bn, curbp->b_fname, sizeof(bn))
1.53 kjell 520: == FALSE)
521: return (FALSE);
1.67 kjell 522: free(curbp->b_bname);
1.53 kjell 523: if ((curbp->b_bname = strdup(bn)) == NULL)
524: return (FALSE);
1.1 deraadt 525: curbp->b_flag &= ~(BFBAK | BFCHG);
526: upmodes(curbp);
527: }
1.32 db 528: return (s);
1.1 deraadt 529: }
530:
531: /*
1.4 millert 532: * Save the contents of the current buffer back into its associated file.
1.1 deraadt 533: */
534: #ifndef MAKEBACKUP
535: #define MAKEBACKUP TRUE
1.4 millert 536: #endif /* !MAKEBACKUP */
1.9 mickey 537: static int makebackup = MAKEBACKUP;
1.1 deraadt 538:
1.3 millert 539: /* ARGSUSED */
540: int
1.20 vincent 541: filesave(int f, int n)
1.1 deraadt 542: {
1.32 db 543: return (buffsave(curbp));
1.1 deraadt 544: }
545:
546: /*
1.9 mickey 547: * Save the contents of the buffer argument into its associated file. Do
548: * nothing if there have been no changes (is this a bug, or a feature?).
549: * Error if there is no remembered file name. If this is the first write
1.4 millert 550: * since the read or visit, then a backup copy of the file is made.
1.9 mickey 551: * Allow user to select whether or not to make backup files by looking at
1.4 millert 552: * the value of makebackup.
1.1 deraadt 553: */
1.3 millert 554: int
1.45 deraadt 555: buffsave(struct buffer *bp)
1.3 millert 556: {
1.4 millert 557: int s;
1.1 deraadt 558:
1.4 millert 559: /* return, no changes */
560: if ((bp->b_flag & BFCHG) == 0) {
1.1 deraadt 561: ewprintf("(No changes need to be saved)");
1.32 db 562: return (TRUE);
1.1 deraadt 563: }
1.4 millert 564:
565: /* must have a name */
566: if (bp->b_fname[0] == '\0') {
1.1 deraadt 567: ewprintf("No file name");
568: return (FALSE);
569: }
1.4 millert 570:
1.68 kjell 571: /* Ensure file has not been modified elsewhere */
572: /* We don't use the ignore flag here */
573: if (fchecktime(bp) != TRUE) {
574: if ((s = eyesno("File has changed on disk since last save. "
575: "Save anyway")) != TRUE)
576: return (s);
577: }
578:
1.3 millert 579: if (makebackup && (bp->b_flag & BFBAK)) {
1.1 deraadt 580: s = fbackupfile(bp->b_fname);
1.4 millert 581: /* hard error */
582: if (s == ABORT)
1.32 db 583: return (FALSE);
1.4 millert 584: /* softer error */
1.9 mickey 585: if (s == FALSE &&
1.4 millert 586: (s = eyesno("Backup error, save anyway")) != TRUE)
1.32 db 587: return (s);
1.1 deraadt 588: }
1.3 millert 589: if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1 deraadt 590: bp->b_flag &= ~(BFCHG | BFBAK);
591: upmodes(bp);
592: }
1.32 db 593: return (s);
1.1 deraadt 594: }
595:
1.3 millert 596: /*
597: * Since we don't have variables (we probably should) this is a command
598: * processor for changing the value of the make backup flag. If no argument
599: * is given, sets makebackup to true, so backups are made. If an argument is
1.59 kjell 600: * given, no backup files are made when saving a new version of a file.
1.1 deraadt 601: */
1.3 millert 602: /* ARGSUSED */
603: int
1.20 vincent 604: makebkfile(int f, int n)
1.1 deraadt 605: {
1.3 millert 606: if (f & FFARG)
607: makebackup = n > 0;
608: else
609: makebackup = !makebackup;
1.1 deraadt 610: ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32 db 611: return (TRUE);
1.1 deraadt 612: }
613:
614: /*
615: * NB: bp is passed to both ffwopen and ffclose because some
616: * attribute information may need to be updated at open time
617: * and others after the close. This is OS-dependent. Note
618: * that the ff routines are assumed to be able to tell whether
619: * the attribute information has been set up in this buffer
620: * or not.
621: */
622:
623: /*
1.9 mickey 624: * This function performs the details of file writing; writing the file
625: * in buffer bp to file fn. Uses the file management routines in the
1.4 millert 626: * "fileio.c" package. Most of the grief is checking of some sort.
1.1 deraadt 627: */
1.3 millert 628: int
1.45 deraadt 629: writeout(struct buffer *bp, char *fn)
1.3 millert 630: {
1.4 millert 631: int s;
1.1 deraadt 632:
1.4 millert 633: /* open writes message */
634: if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1 deraadt 635: return (FALSE);
636: s = ffputbuf(bp);
1.4 millert 637: if (s == FIOSUC) {
638: /* no write error */
1.1 deraadt 639: s = ffclose(bp);
1.3 millert 640: if (s == FIOSUC)
1.1 deraadt 641: ewprintf("Wrote %s", fn);
1.4 millert 642: } else
643: /* ignore close error if it is a write error */
1.7 art 644: (void)ffclose(bp);
1.68 kjell 645: (void)fupdstat(bp);
1.32 db 646: return (s == FIOSUC);
1.1 deraadt 647: }
648:
649: /*
1.4 millert 650: * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1 deraadt 651: * mode line updated.
652: */
1.7 art 653: void
1.45 deraadt 654: upmodes(struct buffer *bp)
1.3 millert 655: {
1.45 deraadt 656: struct mgwin *wp;
1.1 deraadt 657:
658: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3 millert 659: if (bp == NULL || curwp->w_bufp == bp)
1.70 kjell 660: wp->w_rflag |= WFMODE;
1.66 kjell 661: }
662:
663: /*
664: * Same as dirname, except an empty string is returned in
665: * place of "/". This means we can always add a trailing
666: * slash and be correct.
667: * Unlike dirname, we allocate. Caller must free.
668: */
669: static char *
670: xdirname(const char *path)
671: {
672: char *dp;
673:
674: dp = dirname(path);
675: if (*dp && dp[0] == '/' && dp[1] == '\0')
676: return (strdup(""));
677:
678: return (strdup(dp));
1.1 deraadt 679: }