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

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