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