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

1.67    ! kjell       1: /*     $OpenBSD: file.c,v 1.66 2008/06/13 18:45:41 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.54      kjell       9: #include <libgen.h>
                     10:
1.41      kjell      11: #include "def.h"
                     12:
1.66      kjell      13: static char *xdirname(const char *);
                     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.3       millert   159:        if ((wp = popbuf(bp)) == 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 */
                    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.62      kjell     223:                        wp->w_dotp = wp->w_linep = bfirstlp(curbp);
1.57      kjell     224:                        wp->w_doto = 0;
                    225:                        wp->w_markp = NULL;
                    226:                        wp->w_marko = 0;
                    227:                }
                    228:        }
                    229:
1.18      vincent   230:        /*
                    231:         * Call auto-executing function if we need to.
                    232:         */
                    233:        if ((ael = find_autoexec(fname)) != NULL) {
                    234:                for (i = 0; ael[i] != NULL; i++)
                    235:                        (*ael[i])(0, 1);
                    236:                free(ael);
                    237:        }
1.4       millert   238:
                    239:        /* no change */
                    240:        curbp->b_flag &= ~BFCHG;
1.16      vincent   241:
1.41      kjell     242:        /*
                    243:         * We need to set the READONLY flag after we insert the file,
                    244:         * unless the file is a directory.
                    245:         */
1.16      vincent   246:        if (access(fname, W_OK) && errno != ENOENT)
1.41      kjell     247:                ro = TRUE;
                    248:        if (fisdir(fname) == TRUE)
                    249:                ro = TRUE;
                    250:        if (ro == TRUE)
1.16      vincent   251:                curbp->b_flag |= BFREADONLY;
1.25      deraadt   252:
1.65      pyr       253:        if (startrow) {
1.24      deraadt   254:                gotoline(FFARG, startrow);
1.65      pyr       255:                startrow = 0;
                    256:        }
1.16      vincent   257:
1.60      kjell     258:        undo_add_modified();
1.32      db        259:        return (status);
1.1       deraadt   260: }
1.4       millert   261:
1.1       deraadt   262: /*
                    263:  * NB, getting file attributes is done here under control of a flag
                    264:  * rather than in readin, which would be cleaner.  I was concerned
                    265:  * that some operating system might require the file to be open
                    266:  * in order to get the information.  Similarly for writing.
                    267:  */
                    268:
                    269: /*
1.39      kjell     270:  * Insert a file in the current buffer, after dot. If file is a directory,
                    271:  * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
                    272:  * If file is a regular file, set mark at the end of the text inserted;
1.47      kjell     273:  * point at the beginning.  Return a standard status. Print a summary
                    274:  * (lines read, error message) out as well. This routine also does the
                    275:  * read end of backup processing.  The BFBAK flag, if set in a buffer,
                    276:  * says that a backup should be taken.  It is set when a file is read in,
                    277:  * but not on a new file. You don't need to make a backup copy of nothing.
1.1       deraadt   278:  */
1.47      kjell     279:
1.9       mickey    280: static char    *line = NULL;
                    281: static int     linesize = 0;
1.1       deraadt   282:
1.3       millert   283: int
1.39      kjell     284: insertfile(char *fname, char *newname, int replacebuf)
1.3       millert   285: {
1.45      deraadt   286:        struct buffer   *bp;
                    287:        struct line     *lp1, *lp2;
                    288:        struct line     *olp;                   /* line we started at */
                    289:        struct mgwin    *wp;
1.63      kjell     290:        int      nbytes, s, nline = 0, siz, x, x2;
1.19      vincent   291:        int      opos;                  /* offset we started at */
1.59      kjell     292:        int      oline;                 /* original line number */
1.66      kjell     293:        char *dp;
1.4       millert   294:
1.39      kjell     295:        if (replacebuf == TRUE)
1.23      vincent   296:                x = undo_enable(FALSE);
1.63      kjell     297:        else
                    298:                x = undo_enabled();
1.23      vincent   299:
1.4       millert   300:        lp1 = NULL;
1.1       deraadt   301:        if (line == NULL) {
                    302:                line = malloc(NLINE);
1.21      vincent   303:                if (line == NULL)
                    304:                        panic("out of memory");
1.1       deraadt   305:                linesize = NLINE;
                    306:        }
1.4       millert   307:
                    308:        /* cheap */
                    309:        bp = curbp;
1.54      kjell     310:        if (newname != NULL) {
                    311:                (void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
1.66      kjell     312:                dp = xdirname(newname);
                    313:                (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
1.54      kjell     314:                (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
1.66      kjell     315:                free(dp);
1.54      kjell     316:        }
1.4       millert   317:
                    318:        /* hard file open */
1.39      kjell     319:        if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR)
1.1       deraadt   320:                goto out;
1.4       millert   321:        if (s == FIOFNF) {
                    322:                /* file not found */
1.1       deraadt   323:                if (newname != NULL)
                    324:                        ewprintf("(New file)");
1.3       millert   325:                else
                    326:                        ewprintf("(File not found)");
1.1       deraadt   327:                goto out;
1.40      kjell     328:        } else if (s == FIODIR) {
                    329:                /* file was a directory */
                    330:                if (replacebuf == FALSE) {
                    331:                        ewprintf("Cannot insert: file is a directory, %s",
                    332:                            fname);
                    333:                        goto cleanup;
                    334:                }
                    335:                killbuffer(bp);
                    336:                if ((bp = dired_(fname)) == NULL)
                    337:                        return (FALSE);
                    338:                undo_enable(x);
                    339:                curbp = bp;
1.55      kjell     340:                return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54      kjell     341:        } else {
1.66      kjell     342:                dp = xdirname(fname);
                    343:                (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
1.54      kjell     344:                (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
1.66      kjell     345:                free(dp);
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.23      vincent   354:        x2 = undo_enable(FALSE);
1.20      vincent   355:        (void)lnewline();
1.1       deraadt   356:        olp = lback(curwp->w_dotp);
1.23      vincent   357:        undo_enable(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.8       art       419:        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.3       millert   467:                        wp->w_flag |= 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.39      kjell     480:        undo_enable(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.4       millert   496:        int      s;
1.53      kjell     497:        char     fname[NFILEN], bn[NBUFN];
                    498:        char    *adjfname, *bufp;
1.29      vincent   499:
1.54      kjell     500:        if (getbufcwd(fname, sizeof(fname)) != TRUE)
                    501:                fname[0] = '\0';
1.29      vincent   502:        if ((bufp = eread("Write file: ", fname, NFILEN,
1.54      kjell     503:            EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.32      db        504:                return (ABORT);
1.29      vincent   505:        else if (bufp[0] == '\0')
1.32      db        506:                return (FALSE);
1.1       deraadt   507:
1.58      jason     508:        adjfname = adjustname(fname, TRUE);
1.22      vincent   509:        if (adjfname == NULL)
                    510:                return (FALSE);
1.1       deraadt   511:        /* old attributes are no longer current */
                    512:        bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3       millert   513:        if ((s = writeout(curbp, adjfname)) == TRUE) {
1.53      kjell     514:                (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
1.54      kjell     515:                if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
                    516:                        (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
                    517:                if (augbname(bn, basename(curbp->b_fname), sizeof(bn))
1.53      kjell     518:                    == FALSE)
                    519:                        return (FALSE);
1.67    ! kjell     520:                free(curbp->b_bname);
1.53      kjell     521:                if ((curbp->b_bname = strdup(bn)) == NULL)
                    522:                        return (FALSE);
1.1       deraadt   523:                curbp->b_flag &= ~(BFBAK | BFCHG);
                    524:                upmodes(curbp);
                    525:        }
1.32      db        526:        return (s);
1.1       deraadt   527: }
                    528:
                    529: /*
1.4       millert   530:  * Save the contents of the current buffer back into its associated file.
1.1       deraadt   531:  */
                    532: #ifndef        MAKEBACKUP
                    533: #define        MAKEBACKUP TRUE
1.4       millert   534: #endif /* !MAKEBACKUP */
1.9       mickey    535: static int     makebackup = MAKEBACKUP;
1.1       deraadt   536:
1.3       millert   537: /* ARGSUSED */
                    538: int
1.20      vincent   539: filesave(int f, int n)
1.1       deraadt   540: {
1.32      db        541:        return (buffsave(curbp));
1.1       deraadt   542: }
                    543:
                    544: /*
1.9       mickey    545:  * Save the contents of the buffer argument into its associated file.  Do
                    546:  * nothing if there have been no changes (is this a bug, or a feature?).
                    547:  * Error if there is no remembered file name. If this is the first write
1.4       millert   548:  * since the read or visit, then a backup copy of the file is made.
1.9       mickey    549:  * Allow user to select whether or not to make backup files by looking at
1.4       millert   550:  * the value of makebackup.
1.1       deraadt   551:  */
1.3       millert   552: int
1.45      deraadt   553: buffsave(struct buffer *bp)
1.3       millert   554: {
1.4       millert   555:        int      s;
1.1       deraadt   556:
1.4       millert   557:        /* return, no changes */
                    558:        if ((bp->b_flag & BFCHG) == 0) {
1.1       deraadt   559:                ewprintf("(No changes need to be saved)");
1.32      db        560:                return (TRUE);
1.1       deraadt   561:        }
1.4       millert   562:
                    563:        /* must have a name */
                    564:        if (bp->b_fname[0] == '\0') {
1.1       deraadt   565:                ewprintf("No file name");
                    566:                return (FALSE);
                    567:        }
1.4       millert   568:
1.3       millert   569:        if (makebackup && (bp->b_flag & BFBAK)) {
1.1       deraadt   570:                s = fbackupfile(bp->b_fname);
1.4       millert   571:                /* hard error */
                    572:                if (s == ABORT)
1.32      db        573:                        return (FALSE);
1.4       millert   574:                /* softer error */
1.9       mickey    575:                if (s == FALSE &&
1.4       millert   576:                    (s = eyesno("Backup error, save anyway")) != TRUE)
1.32      db        577:                        return (s);
1.1       deraadt   578:        }
1.3       millert   579:        if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1       deraadt   580:                bp->b_flag &= ~(BFCHG | BFBAK);
                    581:                upmodes(bp);
                    582:        }
1.32      db        583:        return (s);
1.1       deraadt   584: }
                    585:
1.3       millert   586: /*
                    587:  * Since we don't have variables (we probably should) this is a command
                    588:  * processor for changing the value of the make backup flag.  If no argument
                    589:  * is given, sets makebackup to true, so backups are made.  If an argument is
1.59      kjell     590:  * given, no backup files are made when saving a new version of a file.
1.1       deraadt   591:  */
1.3       millert   592: /* ARGSUSED */
                    593: int
1.20      vincent   594: makebkfile(int f, int n)
1.1       deraadt   595: {
1.3       millert   596:        if (f & FFARG)
                    597:                makebackup = n > 0;
                    598:        else
                    599:                makebackup = !makebackup;
1.1       deraadt   600:        ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32      db        601:        return (TRUE);
1.1       deraadt   602: }
                    603:
                    604: /*
                    605:  * NB: bp is passed to both ffwopen and ffclose because some
                    606:  * attribute information may need to be updated at open time
                    607:  * and others after the close.  This is OS-dependent.  Note
                    608:  * that the ff routines are assumed to be able to tell whether
                    609:  * the attribute information has been set up in this buffer
                    610:  * or not.
                    611:  */
                    612:
                    613: /*
1.9       mickey    614:  * This function performs the details of file writing; writing the file
                    615:  * in buffer bp to file fn. Uses the file management routines in the
1.4       millert   616:  * "fileio.c" package. Most of the grief is checking of some sort.
1.1       deraadt   617:  */
1.3       millert   618: int
1.45      deraadt   619: writeout(struct buffer *bp, char *fn)
1.3       millert   620: {
1.4       millert   621:        int      s;
1.1       deraadt   622:
1.4       millert   623:        /* open writes message */
                    624:        if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1       deraadt   625:                return (FALSE);
                    626:        s = ffputbuf(bp);
1.4       millert   627:        if (s == FIOSUC) {
                    628:                /* no write error */
1.1       deraadt   629:                s = ffclose(bp);
1.3       millert   630:                if (s == FIOSUC)
1.1       deraadt   631:                        ewprintf("Wrote %s", fn);
1.4       millert   632:        } else
                    633:                /* ignore close error if it is a write error */
1.7       art       634:                (void)ffclose(bp);
1.32      db        635:        return (s == FIOSUC);
1.1       deraadt   636: }
                    637:
                    638: /*
1.4       millert   639:  * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1       deraadt   640:  * mode line updated.
                    641:  */
1.7       art       642: void
1.45      deraadt   643: upmodes(struct buffer *bp)
1.3       millert   644: {
1.45      deraadt   645:        struct mgwin    *wp;
1.1       deraadt   646:
                    647:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3       millert   648:                if (bp == NULL || curwp->w_bufp == bp)
                    649:                        wp->w_flag |= WFMODE;
1.66      kjell     650: }
                    651:
                    652: /*
                    653:  * Same as dirname, except an empty string is returned in
                    654:  * place of "/". This means we can always add a trailing
                    655:  * slash and be correct.
                    656:  * Unlike dirname, we allocate. Caller must free.
                    657:  */
                    658: static char *
                    659: xdirname(const char *path)
                    660: {
                    661:        char *dp;
                    662:
                    663:        dp = dirname(path);
                    664:        if (*dp && dp[0] == '/' && dp[1] == '\0')
                    665:                return (strdup(""));
                    666:
                    667:        return (strdup(dp));
1.1       deraadt   668: }