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