[BACK]Return to touch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / error

Annotation of src/usr.bin/error/touch.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: touch.c,v 1.6 1998/07/10 14:09:56 mickey Exp $        */
1.1       deraadt     2: /*     $NetBSD: touch.c,v 1.3 1995/09/02 06:15:54 jtc Exp $    */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)touch.c    8.1 (Berkeley) 6/6/93";
                     40: #endif
1.7     ! deraadt    41: static char rcsid[] = "$OpenBSD: touch.c,v 1.6 1998/07/10 14:09:56 mickey Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
1.7     ! deraadt    44: #include <sys/param.h>
1.1       deraadt    45: #include <sys/stat.h>
                     46: #include <signal.h>
                     47: #include <unistd.h>
                     48: #include <stdio.h>
                     49: #include <ctype.h>
                     50: #include <stdlib.h>
                     51: #include <string.h>
                     52: #include "error.h"
                     53: #include "pathnames.h"
                     54:
                     55: /*
                     56:  *     Iterate through errors
                     57:  */
                     58: #define EITERATE(p, fv, i)     for (p = fv[i]; p < fv[i+1]; p++)
                     59: #define        ECITERATE(ei, p, lb)    for (ei = lb; p = errors[ei],ei < nerrors; ei++)
                     60:
                     61: #define        FILEITERATE(fi, lb)     for (fi = lb; fi <= nfiles; fi++)
                     62: int    touchstatus = Q_YES;
                     63:
                     64: findfiles(nerrors, errors, r_nfiles, r_files)
                     65:                int     nerrors;
                     66:        Eptr    *errors;
                     67:                int     *r_nfiles;
                     68:        Eptr    ***r_files;
                     69: {
                     70:                int     nfiles;
                     71:        Eptr    **files;
                     72:
                     73:                char    *name;
                     74:        reg     int     ei;
                     75:                int     fi;
                     76:        reg     Eptr    errorp;
                     77:
                     78:        nfiles = countfiles(errors);
                     79:
                     80:        files = (Eptr**)Calloc(nfiles + 3, sizeof (Eptr*));
                     81:        touchedfiles = (boolean *)Calloc(nfiles+3, sizeof(boolean));
                     82:        /*
                     83:         *      Now, partition off the error messages
                     84:         *      into those that are synchronization, discarded or
                     85:         *      not specific to any file, and those that were
                     86:         *      nulled or true errors.
                     87:         */
                     88:        files[0] = &errors[0];
                     89:        ECITERATE(ei, errorp, 0){
                     90:                if ( ! (NOTSORTABLE(errorp->error_e_class)))
                     91:                        break;
                     92:        }
                     93:        /*
                     94:         *      Now, and partition off all error messages
                     95:         *      for a given file.
                     96:         */
                     97:        files[1] = &errors[ei];
                     98:        touchedfiles[0] = touchedfiles[1] = FALSE;
                     99:        name = "\1";
                    100:        fi = 1;
                    101:        ECITERATE(ei, errorp, ei){
                    102:                if (   (errorp->error_e_class == C_NULLED)
                    103:                    || (errorp->error_e_class == C_TRUE) ){
                    104:                        if (strcmp(errorp->error_text[0], name) != 0){
                    105:                                name = errorp->error_text[0];
                    106:                                touchedfiles[fi] = FALSE;
                    107:                                files[fi] = &errors[ei];
                    108:                                fi++;
                    109:                        }
                    110:                }
                    111:        }
                    112:        files[fi] = &errors[nerrors];
                    113:        *r_nfiles = nfiles;
                    114:        *r_files = files;
                    115: }
                    116:
                    117: int countfiles(errors)
                    118:        Eptr    *errors;
                    119: {
                    120:        char    *name;
                    121:        int     ei;
                    122:        reg     Eptr    errorp;
                    123:
                    124:        int     nfiles;
                    125:        nfiles = 0;
                    126:        name = "\1";
                    127:        ECITERATE(ei, errorp, 0){
                    128:                if (SORTABLE(errorp->error_e_class)){
                    129:                        if (strcmp(errorp->error_text[0],name) != 0){
                    130:                                nfiles++;
                    131:                                name = errorp->error_text[0];
                    132:                        }
                    133:                }
                    134:        }
                    135:        return(nfiles);
                    136: }
                    137: char   *class_table[] = {
                    138:        /*C_UNKNOWN     0       */      "Unknown",
                    139:        /*C_IGNORE      1       */      "ignore",
                    140:        /*C_SYNC        2       */      "synchronization",
                    141:        /*C_DISCARD     3       */      "discarded",
                    142:        /*C_NONSPEC     4       */      "non specific",
                    143:        /*C_THISFILE    5       */      "specific to this file",
                    144:        /*C_NULLED      6       */      "nulled",
                    145:        /*C_TRUE        7       */      "true",
                    146:        /*C_DUPL        8       */      "duplicated"
                    147: };
                    148:
                    149: int    class_count[C_LAST - C_FIRST] = {0};
                    150:
                    151: filenames(nfiles, files)
                    152:        int     nfiles;
                    153:        Eptr    **files;
                    154: {
                    155:        reg     int     fi;
                    156:                char    *sep = " ";
                    157:        extern  char    *class_table[];
                    158:                int     someerrors;
                    159:
                    160:        /*
                    161:         *      first, simply dump out errors that
                    162:         *      don't pertain to any file
                    163:         */
                    164:        someerrors = nopertain(files);
                    165:
                    166:        if (nfiles){
                    167:                someerrors++;
                    168:                fprintf(stdout, terse
                    169:                        ? "%d file%s"
                    170:                        : "%d file%s contain%s errors",
                    171:                        nfiles, plural(nfiles), verbform(nfiles));
                    172:                if (!terse){
                    173:                        FILEITERATE(fi, 1){
                    174:                                fprintf(stdout, "%s\"%s\" (%d)",
                    175:                                        sep, (*files[fi])->error_text[0],
                    176:                                        files[fi+1] - files[fi]);
                    177:                                sep = ", ";
                    178:                        }
                    179:                }
                    180:                fprintf(stdout, "\n");
                    181:        }
                    182:        if (!someerrors)
                    183:                fprintf(stdout, "No errors.\n");
                    184: }
                    185:
                    186: /*
                    187:  *     Dump out errors that don't pertain to any file
                    188:  */
                    189: int nopertain(files)
                    190:        Eptr    **files;
                    191: {
                    192:        int     type;
                    193:        int     someerrors = 0;
                    194:        reg     Eptr    *erpp;
                    195:        reg     Eptr    errorp;
                    196:
                    197:        if (files[1] - files[0] <= 0)
                    198:                return(0);
                    199:        for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
                    200:                if (class_count[type] <= 0)
                    201:                        continue;
                    202:                if (type > C_SYNC)
                    203:                        someerrors++;
                    204:                if (terse){
                    205:                        fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
                    206:                                class_count[type], class_table[type]);
                    207:                } else {
                    208:                        fprintf(stdout, "\n\t%d %s errors follow\n",
                    209:                                class_count[type], class_table[type]);
                    210:                        EITERATE(erpp, files, 0){
                    211:                                errorp = *erpp;
                    212:                                if (errorp->error_e_class == type){
                    213:                                        errorprint(stdout, errorp, TRUE);
                    214:                                }
                    215:                        }
                    216:                }
                    217:        }
                    218:        return(someerrors);
                    219: }
                    220:
                    221: extern boolean notouch;
                    222:
                    223: boolean touchfiles(nfiles, files, r_edargc, r_edargv)
                    224:        int     nfiles;
                    225:        Eptr    **files;
                    226:        int     *r_edargc;
                    227:        char    ***r_edargv;
                    228: {
                    229:                char    *name;
                    230:        reg     Eptr    errorp;
                    231:        reg     int     fi;
                    232:        reg     Eptr    *erpp;
                    233:                int             ntrueerrors;
                    234:                boolean         scribbled;
                    235:                int             n_pissed_on;    /* # of file touched*/
                    236:                int     spread;
                    237:
                    238:        FILEITERATE(fi, 1){
                    239:                name = (*files[fi])->error_text[0];
                    240:                spread = files[fi+1] - files[fi];
                    241:                fprintf(stdout, terse
                    242:                        ? "\"%s\" has %d error%s, "
                    243:                        : "\nFile \"%s\" has %d error%s.\n"
                    244:                        , name ,spread ,plural(spread));
                    245:                /*
                    246:                 *      First, iterate through all error messages in this file
                    247:                 *      to see how many of the error messages really will
                    248:                 *      get inserted into the file.
                    249:                 */
                    250:                ntrueerrors = 0;
                    251:                EITERATE(erpp, files, fi){
                    252:                        errorp = *erpp;
                    253:                        if (errorp->error_e_class == C_TRUE)
                    254:                                ntrueerrors++;
                    255:                }
                    256:                fprintf(stdout, terse
                    257:                  ? "insert %d\n"
                    258:                  : "\t%d of these errors can be inserted into the file.\n",
                    259:                        ntrueerrors);
                    260:
                    261:                hackfile(name, files, fi, ntrueerrors);
                    262:        }
                    263:        scribbled = FALSE;
                    264:        n_pissed_on = 0;
                    265:        FILEITERATE(fi, 1){
                    266:                scribbled |= touchedfiles[fi];
                    267:                n_pissed_on++;
                    268:        }
                    269:        if (scribbled){
                    270:                /*
                    271:                 *      Construct an execv argument
                    272:                 */
                    273:                execvarg(n_pissed_on, r_edargc, r_edargv);
                    274:                return(TRUE);
                    275:        } else {
                    276:                if (!terse)
                    277:                        fprintf(stdout, "You didn't touch any files.\n");
                    278:                return(FALSE);
                    279:        }
                    280: }
                    281:
                    282: hackfile(name, files, ix, nerrors)
                    283:        char    *name;
                    284:        Eptr    **files;
                    285:        int     ix;
                    286: {
                    287:        boolean previewed;
                    288:        int     errordest;      /* where errors go*/
                    289:
                    290:        if (!oktotouch(name)) {
                    291:                previewed = FALSE;
                    292:                errordest = TOSTDOUT;
                    293:        } else {
                    294:                previewed = preview(name, nerrors, files, ix);
                    295:                errordest = settotouch(name);
                    296:        }
                    297:
                    298:        if (errordest != TOSTDOUT)
                    299:                touchedfiles[ix] = TRUE;
                    300:
                    301:        if (previewed && (errordest == TOSTDOUT))
                    302:                return;
                    303:
                    304:        diverterrors(name, errordest, files, ix, previewed, nerrors);
                    305:
                    306:        if (errordest == TOTHEFILE){
                    307:                /*
                    308:                 *      overwrite the original file
                    309:                 */
                    310:                writetouched(1);
                    311:        }
                    312: }
                    313:
                    314: boolean preview(name, nerrors, files, ix)
                    315:        char    *name;
                    316:        int     nerrors;
                    317:        Eptr    **files;
                    318:        int     ix;
                    319: {
                    320:        int     back;
                    321:        reg     Eptr    *erpp;
                    322:
                    323:        if (nerrors <= 0)
                    324:                return(FALSE);
                    325:        back = FALSE;
                    326:        if(query){
                    327:                switch(inquire(terse
                    328:                    ? "Preview? "
                    329:                    : "Do you want to preview the errors first? ")){
                    330:                case Q_YES:
                    331:                case Q_yes:
                    332:                        back = TRUE;
                    333:                        EITERATE(erpp, files, ix){
                    334:                                errorprint(stdout, *erpp, TRUE);
                    335:                        }
                    336:                        if (!terse)
                    337:                                fprintf(stdout, "\n");
                    338:                default:
                    339:                        break;
                    340:                }
                    341:        }
                    342:        return(back);
                    343: }
                    344:
                    345: int settotouch(name)
                    346:        char    *name;
                    347: {
                    348:        int     dest = TOSTDOUT;
                    349:
                    350:        if (query){
                    351:                switch(touchstatus = inquire(terse
                    352:                        ? "Touch? "
                    353:                        : "Do you want to touch file \"%s\"? ",
                    354:                        name)){
                    355:                case Q_NO:
                    356:                case Q_no:
                    357:                        return(dest);
                    358:                default:
                    359:                        break;
                    360:                }
                    361:        }
                    362:
                    363:        switch(probethisfile(name)){
                    364:        case F_NOTREAD:
                    365:                dest = TOSTDOUT;
                    366:                fprintf(stdout, terse
                    367:                        ? "\"%s\" unreadable\n"
                    368:                        : "File \"%s\" is unreadable\n",
                    369:                        name);
                    370:                break;
                    371:        case F_NOTWRITE:
                    372:                dest = TOSTDOUT;
                    373:                fprintf(stdout, terse
                    374:                        ? "\"%s\" unwritable\n"
                    375:                        : "File \"%s\" is unwritable\n",
                    376:                        name);
                    377:                break;
                    378:        case F_NOTEXIST:
                    379:                dest = TOSTDOUT;
                    380:                fprintf(stdout, terse
                    381:                        ? "\"%s\" not found\n"
                    382:                        : "Can't find file \"%s\" to insert error messages into.\n",
                    383:                        name);
                    384:                break;
                    385:        default:
                    386:                dest = edit(name) ? TOSTDOUT : TOTHEFILE;
                    387:                break;
                    388:        }
                    389:        return(dest);
                    390: }
                    391:
                    392: diverterrors(name, dest, files, ix, previewed, nterrors)
                    393:        char    *name;
                    394:        int     dest;
                    395:        Eptr    **files;
                    396:        int     ix;
                    397:        boolean previewed;
                    398:        int     nterrors;
                    399: {
                    400:        int     nerrors;
                    401:        reg     Eptr    *erpp;
                    402:        reg     Eptr    errorp;
                    403:
                    404:        nerrors = files[ix+1] - files[ix];
                    405:
                    406:        if (   (nerrors != nterrors)
                    407:            && (!previewed) ){
                    408:                fprintf(stdout, terse
                    409:                        ? "Uninserted errors\n"
                    410:                        : ">>Uninserted errors for file \"%s\" follow.\n",
                    411:                        name);
                    412:        }
                    413:
                    414:        EITERATE(erpp, files, ix){
                    415:                errorp = *erpp;
                    416:                if (errorp->error_e_class != C_TRUE){
                    417:                        if (previewed || touchstatus == Q_NO)
                    418:                                continue;
                    419:                        errorprint(stdout, errorp, TRUE);
                    420:                        continue;
                    421:                }
                    422:                switch (dest){
                    423:                case TOSTDOUT:
                    424:                        if (previewed || touchstatus == Q_NO)
                    425:                                continue;
                    426:                        errorprint(stdout,errorp, TRUE);
                    427:                        break;
                    428:                case TOTHEFILE:
                    429:                        insert(errorp->error_line);
                    430:                        text(errorp, FALSE);
                    431:                        break;
                    432:                }
                    433:        }
                    434: }
                    435:
                    436: int oktotouch(filename)
                    437:        char    *filename;
                    438: {
                    439:        extern          char    *suffixlist;
                    440:        reg     char    *src;
                    441:        reg     char    *pat;
                    442:                        char    *osrc;
                    443:
                    444:        pat = suffixlist;
                    445:        if (pat == 0)
                    446:                return(0);
                    447:        if (*pat == '*')
                    448:                return(1);
                    449:        while (*pat++ != '.')
                    450:                continue;
                    451:        --pat;          /* point to the period */
                    452:
                    453:        for (src = &filename[strlen(filename)], --src;
                    454:             (src > filename) && (*src != '.'); --src)
                    455:                continue;
                    456:        if (*src != '.')
                    457:                return(0);
                    458:
                    459:        for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++){
                    460:                for (;   *src                   /* not at end of the source */
                    461:                      && *pat                   /* not off end of pattern */
                    462:                      && *pat != '.'            /* not off end of sub pattern */
                    463:                      && *pat != '*'            /* not wild card */
                    464:                      && *src == *pat;          /* and equal... */
                    465:                      src++, pat++)
                    466:                        continue;
                    467:                if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
                    468:                        return(1);
                    469:                if (*src != 0 && *pat == '*')
                    470:                        return(1);
                    471:                while (*pat && *pat != '.')
                    472:                        pat++;
                    473:                if (! *pat)
                    474:                        return(0);
                    475:        }
                    476:        return(0);
                    477: }
                    478: /*
                    479:  *     Construct an execv argument
                    480:  *     We need 1 argument for the editor's name
                    481:  *     We need 1 argument for the initial search string
                    482:  *     We need n_pissed_on arguments for the file names
                    483:  *     We need 1 argument that is a null for execv.
                    484:  *     The caller fills in the editor's name.
                    485:  *     We fill in the initial search string.
                    486:  *     We fill in the arguments, and the null.
                    487:  */
                    488: execvarg(n_pissed_on, r_argc, r_argv)
                    489:        int     n_pissed_on;
                    490:        int     *r_argc;
                    491:        char    ***r_argv;
                    492: {
                    493:        Eptr    p;
                    494:        char    *sep;
                    495:        int     fi;
                    496:
                    497:        (*r_argv) = (char **)Calloc(n_pissed_on + 3, sizeof(char *));
                    498:        (*r_argc) =  n_pissed_on + 2;
                    499:        (*r_argv)[1] = "+1;/###/";
                    500:        n_pissed_on = 2;
                    501:        if (!terse){
                    502:                fprintf(stdout, "You touched file(s):");
                    503:                sep = " ";
                    504:        }
                    505:        FILEITERATE(fi, 1){
                    506:                if (!touchedfiles[fi])
                    507:                        continue;
                    508:                p = *(files[fi]);
                    509:                if (!terse){
                    510:                        fprintf(stdout,"%s\"%s\"", sep, p->error_text[0]);
                    511:                        sep = ", ";
                    512:                }
                    513:                (*r_argv)[n_pissed_on++] = p->error_text[0];
                    514:        }
                    515:        if (!terse)
                    516:                fprintf(stdout, "\n");
                    517:        (*r_argv)[n_pissed_on] = 0;
                    518: }
                    519:
                    520: FILE   *o_touchedfile; /* the old file */
                    521: FILE   *n_touchedfile; /* the new file */
                    522: char   *o_name;
1.7     ! deraadt   523: char   n_name[MAXPATHLEN];
1.1       deraadt   524: int    o_lineno;
                    525: int    n_lineno;
                    526: boolean        tempfileopen = FALSE;
                    527: /*
                    528:  *     open the file; guaranteed to be both readable and writable
                    529:  *     Well, if it isn't, then return TRUE if something failed
                    530:  */
                    531: boolean edit(name)
                    532:        char    *name;
                    533: {
1.2       deraadt   534:        int fd;
                    535:
1.1       deraadt   536:        o_name = name;
                    537:        if ( (o_touchedfile = fopen(name, "r")) == NULL){
1.6       mickey    538:                warn("Can't open file \"%s\" to touch (read)", name);
1.1       deraadt   539:                return(TRUE);
                    540:        }
1.7     ! deraadt   541:        strlcpy(n_name, _PATH_TMPFILE, sizeof(n_name));
1.2       deraadt   542:        if ((fd = mkstemp(n_name)) == -1 ||
                    543:            (n_touchedfile = fdopen(fd, "w")) == NULL) {
                    544:                if (fd != -1)
                    545:                        close(fd);
1.6       mickey    546:                warn("Can't open file \"%s\" to touch (write)", name);
1.1       deraadt   547:                return(TRUE);
                    548:        }
                    549:        tempfileopen = TRUE;
                    550:        n_lineno = 0;
                    551:        o_lineno = 0;
                    552:        return(FALSE);
                    553: }
                    554: /*
                    555:  *     Position to the line (before, after) the line given by place
                    556:  */
                    557: char   edbuf[BUFSIZ];
                    558: insert(place)
                    559:        int     place;
                    560: {
                    561:        --place;        /* always insert messages before the offending line*/
                    562:        for(; o_lineno < place; o_lineno++, n_lineno++){
                    563:                if(fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
                    564:                        return;
                    565:                fputs(edbuf, n_touchedfile);
                    566:        }
                    567: }
                    568:
                    569: text(p, use_all)
                    570:        reg     Eptr    p;
                    571:                boolean use_all;
                    572: {
                    573:        int     offset = use_all ? 0 : 2;
                    574:
                    575:        fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
                    576:        fprintf(n_touchedfile, "%d [%s] ",
                    577:                p->error_line,
                    578:                lang_table[p->error_language].lang_name);
                    579:        wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
                    580:        fputs(lang_table[p->error_language].lang_outcomment,n_touchedfile);
                    581:        n_lineno++;
                    582: }
                    583:
                    584: /*
                    585:  *     write the touched file to its temporary copy,
                    586:  *     then bring the temporary in over the local file
                    587:  */
                    588: writetouched(overwrite)
                    589:        int     overwrite;
                    590: {
                    591:        reg     int     nread;
                    592:        reg     FILE    *localfile;
                    593:        reg     FILE    *tmpfile;
                    594:                int     botch;
                    595:                int     oktorm;
                    596:
                    597:        botch = 0;
                    598:        oktorm = 1;
                    599:        while((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != NULL){
                    600:                if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
                    601:                        /*
                    602:                         *      Catastrophe in temporary area: file system full?
                    603:                         */
                    604:                        botch = 1;
1.6       mickey    605:                        warnx("write failure: No errors inserted in \"%s\"",
                    606:                              o_name);
1.1       deraadt   607:                }
                    608:        }
                    609:        fclose(n_touchedfile);
                    610:        fclose(o_touchedfile);
                    611:        /*
                    612:         *      Now, copy the temp file back over the original
                    613:         *      file, thus preserving links, etc
                    614:         */
                    615:        if (botch == 0 && overwrite){
                    616:                botch = 0;
                    617:                localfile = NULL;
                    618:                tmpfile = NULL;
                    619:                if ((localfile = fopen(o_name, "w")) == NULL){
1.6       mickey    620:                        warn("Can't open file \"%s\" to overwrite", o_name);
1.1       deraadt   621:                        botch++;
                    622:                }
                    623:                if ((tmpfile = fopen(n_name, "r")) == NULL){
1.6       mickey    624:                        warn("Can't open file \"%s\" to read", n_name);
1.1       deraadt   625:                        botch++;
                    626:                }
                    627:                if (!botch)
                    628:                        oktorm = mustoverwrite(localfile, tmpfile);
                    629:                if (localfile != NULL)
                    630:                        fclose(localfile);
                    631:                if (tmpfile != NULL)
                    632:                        fclose(tmpfile);
                    633:        }
1.6       mickey    634:        if (oktorm == 0)
                    635:                errx(1, "Catastrophe: A copy of \"%s\": was saved in \"%s\"",
                    636:                    o_name, n_name);
1.1       deraadt   637:        /*
                    638:         *      Kiss the temp file good bye
                    639:         */
                    640:        unlink(n_name);
                    641:        tempfileopen = FALSE;
                    642:        return(TRUE);
                    643: }
                    644: /*
                    645:  *     return 1 if the tmpfile can be removed after writing it out
                    646:  */
                    647: int mustoverwrite(preciousfile, tmpfile)
                    648:        FILE    *preciousfile;
                    649:        FILE    *tmpfile;
                    650: {
                    651:        int     nread;
                    652:
                    653:        while((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != NULL){
                    654:                if (mustwrite(edbuf, nread, preciousfile) == 0)
                    655:                        return(0);
                    656:        }
                    657:        return(1);
                    658: }
                    659: /*
                    660:  *     return 0 on catastrophe
                    661:  */
                    662: mustwrite(base, n, preciousfile)
                    663:        char    *base;
                    664:        int     n;
                    665:        FILE    *preciousfile;
                    666: {
                    667:        int     nwrote;
                    668:
                    669:        if (n <= 0)
                    670:                return(1);
                    671:        nwrote = fwrite(base, 1, n, preciousfile);
                    672:        if (nwrote == n)
                    673:                return(1);
1.6       mickey    674:        err(NULL);
1.1       deraadt   675:        switch(inquire(terse
                    676:            ? "Botch overwriting: retry? "
                    677:            : "Botch overwriting the source file: retry? ")){
                    678:        case Q_YES:
                    679:        case Q_yes:
                    680:                mustwrite(base + nwrote, n - nwrote, preciousfile);
                    681:                return(1);
                    682:        case Q_NO:
                    683:        case Q_no:
                    684:                switch(inquire("Are you sure? ")){
                    685:                case Q_YES:
                    686:                case Q_yes:
                    687:                        return(0);
                    688:                case Q_NO:
                    689:                case Q_no:
                    690:                        mustwrite(base + nwrote, n - nwrote, preciousfile);
                    691:                        return(1);
                    692:                }
                    693:        default:
                    694:                return(0);
                    695:        }
                    696: }
                    697:
                    698: void
                    699: onintr()
                    700: {
                    701:        switch(inquire(terse
                    702:            ? "\nContinue? "
                    703:            : "\nInterrupt: Do you want to continue? ")){
                    704:        case Q_YES:
                    705:        case Q_yes:
                    706:                signal(SIGINT, onintr);
                    707:                return;
                    708:        default:
                    709:                if (tempfileopen){
                    710:                        /*
                    711:                         *      Don't overwrite the original file!
                    712:                         */
                    713:                        writetouched(0);
                    714:                }
                    715:                exit(1);
                    716:        }
                    717:        /*NOTREACHED*/
                    718: }
                    719:
                    720: errorprint(place, errorp, print_all)
                    721:        FILE    *place;
                    722:        Eptr    errorp;
                    723:        boolean print_all;
                    724: {
                    725:        int     offset = print_all ? 0 : 2;
                    726:
                    727:        if (errorp->error_e_class == C_IGNORE)
                    728:                return;
                    729:        fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
                    730:        wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
                    731:        putc('\n', place);
                    732: }
                    733:
                    734: int inquire(fmt, a1, a2)
                    735:        char    *fmt;
                    736:        /*VARARGS1*/
                    737: {
                    738:        char    buffer[128];
                    739:
                    740:        if (queryfile == NULL)
                    741:                return(0);
                    742:        for(;;){
                    743:                do{
                    744:                        fflush(stdout);
                    745:                        fprintf(stderr, fmt, a1, a2);
                    746:                        fflush(stderr);
                    747:                } while (fgets(buffer, 127, queryfile) == NULL);
                    748:                switch(buffer[0]){
                    749:                case 'Y':       return(Q_YES);
                    750:                case 'y':       return(Q_yes);
                    751:                case 'N':       return(Q_NO);
                    752:                case 'n':       return(Q_no);
                    753:                default:        fprintf(stderr, "Yes or No only!\n");
                    754:                }
                    755:        }
                    756: }
                    757:
                    758: int probethisfile(name)
                    759:        char    *name;
                    760: {
                    761:        struct stat statbuf;
                    762:        if (stat(name, &statbuf) < 0)
                    763:                return(F_NOTEXIST);
                    764:        if((statbuf.st_mode & S_IREAD) == 0)
                    765:                return(F_NOTREAD);
                    766:        if((statbuf.st_mode & S_IWRITE) == 0)
                    767:                return(F_NOTWRITE);
                    768:        return(F_TOUCHIT);
                    769: }