[BACK]Return to file.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/file.c, Revision 1.90

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