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

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