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

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