[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.77

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