121639Sdist /*
2*61989Sbostic * Copyright (c) 1980, 1993
3*61989Sbostic * The Regents of the University of California. All rights reserved.
434664Sbostic *
542683Sbostic * %sccs.include.redist.c%
621639Sdist */
721639Sdist
821639Sdist #ifndef lint
9*61989Sbostic static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 06/06/93";
1034664Sbostic #endif /* not lint */
1121639Sdist
1237791Sbostic #include <sys/types.h>
1337791Sbostic #include <sys/stat.h>
1446694Sbostic #include <signal.h>
1546694Sbostic #include <unistd.h>
161460Sroot #include <stdio.h>
171460Sroot #include <ctype.h>
1846694Sbostic #include <stdlib.h>
1946694Sbostic #include <string.h>
201460Sroot #include "error.h"
2137791Sbostic #include "pathnames.h"
221460Sroot
235595Srrh /*
245595Srrh * Iterate through errors
255595Srrh */
265595Srrh #define EITERATE(p, fv, i) for (p = fv[i]; p < fv[i+1]; p++)
275595Srrh #define ECITERATE(ei, p, lb) for (ei = lb; p = errors[ei],ei < nerrors; ei++)
285595Srrh
295595Srrh #define FILEITERATE(fi, lb) for (fi = lb; fi <= nfiles; fi++)
305595Srrh int touchstatus = Q_YES;
315595Srrh
findfiles(nerrors,errors,r_nfiles,r_files)321460Sroot findfiles(nerrors, errors, r_nfiles, r_files)
335595Srrh int nerrors;
345595Srrh Eptr *errors;
355595Srrh int *r_nfiles;
365595Srrh Eptr ***r_files;
371460Sroot {
385595Srrh int nfiles;
395595Srrh Eptr **files;
401460Sroot
415595Srrh char *name;
425595Srrh reg int ei;
435595Srrh int fi;
445595Srrh reg Eptr errorp;
455595Srrh
465595Srrh nfiles = countfiles(errors);
475595Srrh
485595Srrh files = (Eptr**)Calloc(nfiles + 3, sizeof (Eptr*));
491460Sroot touchedfiles = (boolean *)Calloc(nfiles+3, sizeof(boolean));
501460Sroot /*
515595Srrh * Now, partition off the error messages
521460Sroot * into those that are synchronization, discarded or
531460Sroot * not specific to any file, and those that were
541460Sroot * nulled or true errors.
551460Sroot */
561460Sroot files[0] = &errors[0];
575595Srrh ECITERATE(ei, errorp, 0){
585595Srrh if ( ! (NOTSORTABLE(errorp->error_e_class)))
595595Srrh break;
601460Sroot }
611460Sroot /*
625595Srrh * Now, and partition off all error messages
631460Sroot * for a given file.
641460Sroot */
655595Srrh files[1] = &errors[ei];
661460Sroot touchedfiles[0] = touchedfiles[1] = FALSE;
675595Srrh name = "\1";
685595Srrh fi = 1;
695595Srrh ECITERATE(ei, errorp, ei){
705595Srrh if ( (errorp->error_e_class == C_NULLED)
715595Srrh || (errorp->error_e_class == C_TRUE) ){
725595Srrh if (strcmp(errorp->error_text[0], name) != 0){
735595Srrh name = errorp->error_text[0];
745595Srrh touchedfiles[fi] = FALSE;
755595Srrh files[fi] = &errors[ei];
765595Srrh fi++;
771460Sroot }
781460Sroot }
791460Sroot }
805595Srrh files[fi] = &errors[nerrors];
811460Sroot *r_nfiles = nfiles;
821460Sroot *r_files = files;
831460Sroot }
841460Sroot
countfiles(errors)855595Srrh int countfiles(errors)
865595Srrh Eptr *errors;
875595Srrh {
885595Srrh char *name;
895595Srrh int ei;
905595Srrh reg Eptr errorp;
915595Srrh
925595Srrh int nfiles;
935595Srrh nfiles = 0;
945595Srrh name = "\1";
955595Srrh ECITERATE(ei, errorp, 0){
965595Srrh if (SORTABLE(errorp->error_e_class)){
975595Srrh if (strcmp(errorp->error_text[0],name) != 0){
985595Srrh nfiles++;
995595Srrh name = errorp->error_text[0];
1005595Srrh }
1015595Srrh }
1025595Srrh }
1035595Srrh return(nfiles);
1045595Srrh }
1051460Sroot char *class_table[] = {
1061460Sroot /*C_UNKNOWN 0 */ "Unknown",
1071460Sroot /*C_IGNORE 1 */ "ignore",
1081460Sroot /*C_SYNC 2 */ "synchronization",
1091460Sroot /*C_DISCARD 3 */ "discarded",
1101460Sroot /*C_NONSPEC 4 */ "non specific",
1111460Sroot /*C_THISFILE 5 */ "specific to this file",
1121460Sroot /*C_NULLED 6 */ "nulled",
1131460Sroot /*C_TRUE 7 */ "true",
1141460Sroot /*C_DUPL 8 */ "duplicated"
1151460Sroot };
1161460Sroot
1171460Sroot int class_count[C_LAST - C_FIRST] = {0};
1181460Sroot
filenames(nfiles,files)1191460Sroot filenames(nfiles, files)
1201460Sroot int nfiles;
1215595Srrh Eptr **files;
1221460Sroot {
1235595Srrh reg int fi;
1245595Srrh char *sep = " ";
1255595Srrh extern char *class_table[];
1265595Srrh int someerrors;
1271460Sroot
1281460Sroot /*
1295595Srrh * first, simply dump out errors that
1301460Sroot * don't pertain to any file
1311460Sroot */
1325595Srrh someerrors = nopertain(files);
1335595Srrh
1341460Sroot if (nfiles){
1351460Sroot someerrors++;
1365595Srrh fprintf(stdout, terse
1375595Srrh ? "%d file%s"
1385595Srrh : "%d file%s contain%s errors",
1395595Srrh nfiles, plural(nfiles), verbform(nfiles));
1405595Srrh if (!terse){
1415595Srrh FILEITERATE(fi, 1){
1425595Srrh fprintf(stdout, "%s\"%s\" (%d)",
1435595Srrh sep, (*files[fi])->error_text[0],
1445595Srrh files[fi+1] - files[fi]);
1455595Srrh sep = ", ";
1465595Srrh }
1471460Sroot }
1481460Sroot fprintf(stdout, "\n");
1491460Sroot }
1501460Sroot if (!someerrors)
1511460Sroot fprintf(stdout, "No errors.\n");
1521460Sroot }
1531460Sroot
1545595Srrh /*
1555595Srrh * Dump out errors that don't pertain to any file
1565595Srrh */
nopertain(files)1575595Srrh int nopertain(files)
1585595Srrh Eptr **files;
1595595Srrh {
1605595Srrh int type;
1615595Srrh int someerrors = 0;
1625595Srrh reg Eptr *erpp;
1635595Srrh reg Eptr errorp;
1645595Srrh
1655595Srrh if (files[1] - files[0] <= 0)
1665595Srrh return(0);
1675595Srrh for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
1685595Srrh if (class_count[type] <= 0)
1695595Srrh continue;
1705595Srrh if (type > C_SYNC)
1715595Srrh someerrors++;
1725595Srrh if (terse){
1735595Srrh fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
1745595Srrh class_count[type], class_table[type]);
1755595Srrh } else {
1765595Srrh fprintf(stdout, "\n\t%d %s errors follow\n",
1775595Srrh class_count[type], class_table[type]);
1785595Srrh EITERATE(erpp, files, 0){
1795595Srrh errorp = *erpp;
1805595Srrh if (errorp->error_e_class == type){
1815595Srrh errorprint(stdout, errorp, TRUE);
1825595Srrh }
1835595Srrh }
1845595Srrh }
1855595Srrh }
1865595Srrh return(someerrors);
1875595Srrh }
1885595Srrh
1891460Sroot extern boolean notouch;
1901460Sroot
touchfiles(nfiles,files,r_edargc,r_edargv)1911460Sroot boolean touchfiles(nfiles, files, r_edargc, r_edargv)
1921460Sroot int nfiles;
1935595Srrh Eptr **files;
1941460Sroot int *r_edargc;
1951460Sroot char ***r_edargv;
1961460Sroot {
1975595Srrh char *name;
1985595Srrh reg Eptr errorp;
1995595Srrh reg int fi;
2005595Srrh reg Eptr *erpp;
2015595Srrh int ntrueerrors;
2025595Srrh boolean scribbled;
2035595Srrh int n_pissed_on; /* # of file touched*/
2045595Srrh int spread;
2051462Sroot
2065595Srrh FILEITERATE(fi, 1){
2075595Srrh name = (*files[fi])->error_text[0];
2085595Srrh spread = files[fi+1] - files[fi];
2095595Srrh fprintf(stdout, terse
2105595Srrh ? "\"%s\" has %d error%s, "
2115595Srrh : "\nFile \"%s\" has %d error%s.\n"
2125595Srrh , name ,spread ,plural(spread));
2131460Sroot /*
2141460Sroot * First, iterate through all error messages in this file
2151460Sroot * to see how many of the error messages really will
2161460Sroot * get inserted into the file.
2171460Sroot */
2185595Srrh ntrueerrors = 0;
2195595Srrh EITERATE(erpp, files, fi){
2201460Sroot errorp = *erpp;
2211460Sroot if (errorp->error_e_class == C_TRUE)
2221460Sroot ntrueerrors++;
2231460Sroot }
2245595Srrh fprintf(stdout, terse
2255595Srrh ? "insert %d\n"
2265595Srrh : "\t%d of these errors can be inserted into the file.\n",
2271460Sroot ntrueerrors);
2281460Sroot
2295595Srrh hackfile(name, files, fi, ntrueerrors);
2305595Srrh }
2311460Sroot scribbled = FALSE;
2325595Srrh n_pissed_on = 0;
2335595Srrh FILEITERATE(fi, 1){
2345595Srrh scribbled |= touchedfiles[fi];
2351460Sroot n_pissed_on++;
2361460Sroot }
2371460Sroot if (scribbled){
2381460Sroot /*
2391460Sroot * Construct an execv argument
2401460Sroot */
2415595Srrh execvarg(n_pissed_on, r_edargc, r_edargv);
2421460Sroot return(TRUE);
2431460Sroot } else {
2445595Srrh if (!terse)
2455595Srrh fprintf(stdout, "You didn't touch any files.\n");
2461460Sroot return(FALSE);
2471460Sroot }
2485595Srrh }
2491460Sroot
hackfile(name,files,ix,nerrors)2505595Srrh hackfile(name, files, ix, nerrors)
2515595Srrh char *name;
2525595Srrh Eptr **files;
2535595Srrh int ix;
2545595Srrh {
2555595Srrh boolean previewed;
2565595Srrh int errordest; /* where errors go*/
2575595Srrh
25817212Sralph if (!oktotouch(name)) {
25917212Sralph previewed = FALSE;
26017212Sralph errordest = TOSTDOUT;
26117212Sralph } else {
26217212Sralph previewed = preview(name, nerrors, files, ix);
26317212Sralph errordest = settotouch(name);
26417212Sralph }
2655595Srrh
2665595Srrh if (errordest != TOSTDOUT)
2675595Srrh touchedfiles[ix] = TRUE;
2685595Srrh
2695595Srrh if (previewed && (errordest == TOSTDOUT))
2705595Srrh return;
2715595Srrh
2725595Srrh diverterrors(name, errordest, files, ix, previewed, nerrors);
2735595Srrh
2746612Srrh if (errordest == TOTHEFILE){
2756612Srrh /*
2766612Srrh * overwrite the original file
2776612Srrh */
2786612Srrh writetouched(1);
2796612Srrh }
2805595Srrh }
2815595Srrh
preview(name,nerrors,files,ix)2825595Srrh boolean preview(name, nerrors, files, ix)
2835595Srrh char *name;
2845595Srrh int nerrors;
2855595Srrh Eptr **files;
2865595Srrh int ix;
2875595Srrh {
2885595Srrh int back;
2895595Srrh reg Eptr *erpp;
2905595Srrh
2915595Srrh if (nerrors <= 0)
29217212Sralph return(FALSE);
29317212Sralph back = FALSE;
2945595Srrh if(query){
2955595Srrh switch(inquire(terse
2965595Srrh ? "Preview? "
2975595Srrh : "Do you want to preview the errors first? ")){
2985595Srrh case Q_YES:
2995595Srrh case Q_yes:
30017212Sralph back = TRUE;
3015595Srrh EITERATE(erpp, files, ix){
3025595Srrh errorprint(stdout, *erpp, TRUE);
3035595Srrh }
3045595Srrh if (!terse)
3055595Srrh fprintf(stdout, "\n");
3065595Srrh default:
3075595Srrh break;
3085595Srrh }
3095595Srrh }
3105595Srrh return(back);
3115595Srrh }
3125595Srrh
settotouch(name)3135595Srrh int settotouch(name)
3145595Srrh char *name;
3155595Srrh {
3165595Srrh int dest = TOSTDOUT;
3175595Srrh
3185595Srrh if (query){
3195595Srrh switch(touchstatus = inquire(terse
3205595Srrh ? "Touch? "
3215595Srrh : "Do you want to touch file \"%s\"? ",
3225595Srrh name)){
3235595Srrh case Q_NO:
3245595Srrh case Q_no:
3255595Srrh return(dest);
3265595Srrh default:
3275595Srrh break;
3285595Srrh }
3295595Srrh }
3305595Srrh
3315595Srrh switch(probethisfile(name)){
3325595Srrh case F_NOTREAD:
3335595Srrh dest = TOSTDOUT;
3345595Srrh fprintf(stdout, terse
3355595Srrh ? "\"%s\" unreadable\n"
3365595Srrh : "File \"%s\" is unreadable\n",
3375595Srrh name);
3385595Srrh break;
3395595Srrh case F_NOTWRITE:
3405595Srrh dest = TOSTDOUT;
3415595Srrh fprintf(stdout, terse
3425595Srrh ? "\"%s\" unwritable\n"
3435595Srrh : "File \"%s\" is unwritable\n",
3445595Srrh name);
3455595Srrh break;
3465595Srrh case F_NOTEXIST:
3475595Srrh dest = TOSTDOUT;
3485595Srrh fprintf(stdout, terse
3495595Srrh ? "\"%s\" not found\n"
3505595Srrh : "Can't find file \"%s\" to insert error messages into.\n",
3515595Srrh name);
3525595Srrh break;
3535595Srrh default:
3545595Srrh dest = edit(name) ? TOSTDOUT : TOTHEFILE;
3555595Srrh break;
3565595Srrh }
3575595Srrh return(dest);
3585595Srrh }
3595595Srrh
diverterrors(name,dest,files,ix,previewed,nterrors)3605595Srrh diverterrors(name, dest, files, ix, previewed, nterrors)
3615595Srrh char *name;
3625595Srrh int dest;
3635595Srrh Eptr **files;
3645595Srrh int ix;
3655595Srrh boolean previewed;
3665595Srrh int nterrors;
3675595Srrh {
3685595Srrh int nerrors;
3695595Srrh reg Eptr *erpp;
3705595Srrh reg Eptr errorp;
3715595Srrh
3725595Srrh nerrors = files[ix+1] - files[ix];
3735595Srrh
3745595Srrh if ( (nerrors != nterrors)
3755595Srrh && (!previewed) ){
3765595Srrh fprintf(stdout, terse
3775595Srrh ? "Uninserted errors\n"
3785595Srrh : ">>Uninserted errors for file \"%s\" follow.\n",
3795595Srrh name);
3805595Srrh }
3815595Srrh
3825595Srrh EITERATE(erpp, files, ix){
3835595Srrh errorp = *erpp;
3845595Srrh if (errorp->error_e_class != C_TRUE){
3855595Srrh if (previewed || touchstatus == Q_NO)
3865595Srrh continue;
3875595Srrh errorprint(stdout, errorp, TRUE);
3885595Srrh continue;
3895595Srrh }
3905595Srrh switch (dest){
3915595Srrh case TOSTDOUT:
3925595Srrh if (previewed || touchstatus == Q_NO)
3935595Srrh continue;
3945595Srrh errorprint(stdout,errorp, TRUE);
3955595Srrh break;
3965595Srrh case TOTHEFILE:
3975595Srrh insert(errorp->error_line);
3985595Srrh text(errorp, FALSE);
3995595Srrh break;
4005595Srrh }
4015595Srrh }
4025595Srrh }
4035595Srrh
oktotouch(filename)4045595Srrh int oktotouch(filename)
4051460Sroot char *filename;
4061460Sroot {
4071460Sroot extern char *suffixlist;
4085595Srrh reg char *src;
4095595Srrh reg char *pat;
4101460Sroot char *osrc;
4111460Sroot
4121460Sroot pat = suffixlist;
4131460Sroot if (pat == 0)
4141460Sroot return(0);
4151460Sroot if (*pat == '*')
4161460Sroot return(1);
4171460Sroot while (*pat++ != '.')
4181460Sroot continue;
4191460Sroot --pat; /* point to the period */
4201460Sroot
4211460Sroot for (src = &filename[strlen(filename)], --src;
4221460Sroot (src > filename) && (*src != '.'); --src)
4231460Sroot continue;
4241460Sroot if (*src != '.')
4251460Sroot return(0);
4261460Sroot
4271460Sroot for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++){
4281460Sroot for (; *src /* not at end of the source */
4291460Sroot && *pat /* not off end of pattern */
4301460Sroot && *pat != '.' /* not off end of sub pattern */
4311460Sroot && *pat != '*' /* not wild card */
4321460Sroot && *src == *pat; /* and equal... */
4331460Sroot src++, pat++)
4341460Sroot continue;
4351460Sroot if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
4361460Sroot return(1);
4371460Sroot if (*src != 0 && *pat == '*')
4381460Sroot return(1);
4391460Sroot while (*pat && *pat != '.')
4401460Sroot pat++;
4411460Sroot if (! *pat)
4421460Sroot return(0);
4431460Sroot }
4441460Sroot return(0);
4451460Sroot }
4465595Srrh /*
4475595Srrh * Construct an execv argument
4485595Srrh * We need 1 argument for the editor's name
4495595Srrh * We need 1 argument for the initial search string
4505595Srrh * We need n_pissed_on arguments for the file names
4515595Srrh * We need 1 argument that is a null for execv.
4525595Srrh * The caller fills in the editor's name.
4535595Srrh * We fill in the initial search string.
4545595Srrh * We fill in the arguments, and the null.
4555595Srrh */
execvarg(n_pissed_on,r_argc,r_argv)4565595Srrh execvarg(n_pissed_on, r_argc, r_argv)
4575595Srrh int n_pissed_on;
4585595Srrh int *r_argc;
4595595Srrh char ***r_argv;
4605595Srrh {
4615595Srrh Eptr p;
4625595Srrh char *sep;
4635595Srrh int fi;
4641460Sroot
4655595Srrh (*r_argv) = (char **)Calloc(n_pissed_on + 3, sizeof(char *));
4665595Srrh (*r_argc) = n_pissed_on + 2;
4675595Srrh (*r_argv)[1] = "+1;/###/";
4685595Srrh n_pissed_on = 2;
4695595Srrh if (!terse){
4705595Srrh fprintf(stdout, "You touched file(s):");
4715595Srrh sep = " ";
4725595Srrh }
4735595Srrh FILEITERATE(fi, 1){
4745595Srrh if (!touchedfiles[fi])
4755595Srrh continue;
4765595Srrh p = *(files[fi]);
4775595Srrh if (!terse){
4785595Srrh fprintf(stdout,"%s\"%s\"", sep, p->error_text[0]);
4795595Srrh sep = ", ";
4805595Srrh }
4815595Srrh (*r_argv)[n_pissed_on++] = p->error_text[0];
4825595Srrh }
4835595Srrh if (!terse)
4845595Srrh fprintf(stdout, "\n");
4855595Srrh (*r_argv)[n_pissed_on] = 0;
4865595Srrh }
4875595Srrh
4881460Sroot FILE *o_touchedfile; /* the old file */
4891460Sroot FILE *n_touchedfile; /* the new file */
4901460Sroot char *o_name;
4916612Srrh char n_name[64];
49237791Sbostic char *canon_name = _PATH_TMP;
4931460Sroot int o_lineno;
4941460Sroot int n_lineno;
4951460Sroot boolean tempfileopen = FALSE;
4961460Sroot /*
4971460Sroot * open the file; guaranteed to be both readable and writable
4981460Sroot * Well, if it isn't, then return TRUE if something failed
4991460Sroot */
edit(name)5001460Sroot boolean edit(name)
5011460Sroot char *name;
5021460Sroot {
5031460Sroot o_name = name;
5041460Sroot if ( (o_touchedfile = fopen(name, "r")) == NULL){
5051460Sroot fprintf(stderr, "%s: Can't open file \"%s\" to touch (read).\n",
5061460Sroot processname, name);
5071460Sroot return(TRUE);
5081460Sroot }
5095595Srrh (void)strcpy(n_name, canon_name);
5105595Srrh (void)mktemp(n_name);
5111460Sroot if ( (n_touchedfile = fopen(n_name, "w")) == NULL){
5121460Sroot fprintf(stderr,"%s: Can't open file \"%s\" to touch (write).\n",
5131460Sroot processname, name);
5141460Sroot return(TRUE);
5151460Sroot }
5161460Sroot tempfileopen = TRUE;
5171460Sroot n_lineno = 0;
5181460Sroot o_lineno = 0;
5191460Sroot return(FALSE);
5201460Sroot }
5211460Sroot /*
5221460Sroot * Position to the line (before, after) the line given by place
5231460Sroot */
5245595Srrh char edbuf[BUFSIZ];
insert(place)5251460Sroot insert(place)
5261460Sroot int place;
5271460Sroot {
5281460Sroot --place; /* always insert messages before the offending line*/
5291460Sroot for(; o_lineno < place; o_lineno++, n_lineno++){
5305595Srrh if(fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
5311460Sroot return;
5325595Srrh fputs(edbuf, n_touchedfile);
5331460Sroot }
5341460Sroot }
5351460Sroot
text(p,use_all)5365595Srrh text(p, use_all)
5375595Srrh reg Eptr p;
5385595Srrh boolean use_all;
5391460Sroot {
5401460Sroot int offset = use_all ? 0 : 2;
5415595Srrh
5425595Srrh fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
5431460Sroot fprintf(n_touchedfile, "%d [%s] ",
5445595Srrh p->error_line,
5455595Srrh lang_table[p->error_language].lang_name);
5465595Srrh wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
5475595Srrh fputs(lang_table[p->error_language].lang_outcomment,n_touchedfile);
5481460Sroot n_lineno++;
5491460Sroot }
5501460Sroot
5516612Srrh /*
5526612Srrh * write the touched file to its temporary copy,
5536612Srrh * then bring the temporary in over the local file
5546612Srrh */
writetouched(overwrite)5556612Srrh writetouched(overwrite)
5566612Srrh int overwrite;
5571460Sroot {
5586612Srrh reg int nread;
5596612Srrh reg FILE *localfile;
5606612Srrh reg FILE *tmpfile;
5616612Srrh int botch;
5629309Srrh int oktorm;
5635595Srrh
5649309Srrh botch = 0;
5659309Srrh oktorm = 1;
5665595Srrh while((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != NULL){
5679309Srrh if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
5689309Srrh /*
5699309Srrh * Catastrophe in temporary area: file system full?
5709309Srrh */
5719309Srrh botch = 1;
5729309Srrh fprintf(stderr,
5739309Srrh "%s: write failure: No errors inserted in \"%s\"\n",
5749309Srrh processname, o_name);
5759309Srrh }
5761460Sroot }
5771460Sroot fclose(n_touchedfile);
5781460Sroot fclose(o_touchedfile);
5796612Srrh /*
5806612Srrh * Now, copy the temp file back over the original
5816612Srrh * file, thus preserving links, etc
5826612Srrh */
5839309Srrh if (botch == 0 && overwrite){
5846612Srrh botch = 0;
5856612Srrh localfile = NULL;
5866612Srrh tmpfile = NULL;
5876612Srrh if ((localfile = fopen(o_name, "w")) == NULL){
5886612Srrh fprintf(stderr,
5896612Srrh "%s: Can't open file \"%s\" to overwrite.\n",
5906612Srrh processname, o_name);
5916612Srrh botch++;
5926612Srrh }
5936612Srrh if ((tmpfile = fopen(n_name, "r")) == NULL){
5946612Srrh fprintf(stderr, "%s: Can't open file \"%s\" to read.\n",
5956612Srrh processname, n_name);
5966612Srrh botch++;
5976612Srrh }
5989309Srrh if (!botch)
5999309Srrh oktorm = mustoverwrite(localfile, tmpfile);
6006612Srrh if (localfile != NULL)
6016612Srrh fclose(localfile);
6026612Srrh if (tmpfile != NULL)
6036612Srrh fclose(tmpfile);
6046612Srrh }
6059309Srrh if (oktorm == 0){
60635266Sbostic fprintf(stderr, "%s: Catastrophe: A copy of \"%s\": was saved in \"%s\"\n",
6079309Srrh processname, o_name, n_name);
6089309Srrh exit(1);
6099309Srrh }
6106612Srrh /*
6116612Srrh * Kiss the temp file good bye
6126612Srrh */
6131460Sroot unlink(n_name);
6141460Sroot tempfileopen = FALSE;
6156612Srrh return(TRUE);
6161460Sroot }
6179309Srrh /*
6189309Srrh * return 1 if the tmpfile can be removed after writing it out
6199309Srrh */
mustoverwrite(preciousfile,tmpfile)6209309Srrh int mustoverwrite(preciousfile, tmpfile)
6219309Srrh FILE *preciousfile;
6229309Srrh FILE *tmpfile;
6239309Srrh {
6249309Srrh int nread;
6255595Srrh
6269309Srrh while((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != NULL){
6279309Srrh if (mustwrite(edbuf, nread, preciousfile) == 0)
6289309Srrh return(0);
6299309Srrh }
6309309Srrh return(1);
6319309Srrh }
6329309Srrh /*
6339309Srrh * return 0 on catastrophe
6349309Srrh */
mustwrite(base,n,preciousfile)6359309Srrh mustwrite(base, n, preciousfile)
6369309Srrh char *base;
6379309Srrh int n;
6389309Srrh FILE *preciousfile;
6399309Srrh {
6409309Srrh int nwrote;
6419309Srrh
6429309Srrh if (n <= 0)
6439309Srrh return(1);
6449309Srrh nwrote = fwrite(base, 1, n, preciousfile);
6459309Srrh if (nwrote == n)
6469309Srrh return(1);
6479309Srrh perror(processname);
6489309Srrh switch(inquire(terse
6499309Srrh ? "Botch overwriting: retry? "
6509309Srrh : "Botch overwriting the source file: retry? ")){
6519309Srrh case Q_YES:
6529309Srrh case Q_yes:
6539309Srrh mustwrite(base + nwrote, n - nwrote, preciousfile);
6549309Srrh return(1);
6559309Srrh case Q_NO:
6569309Srrh case Q_no:
6579309Srrh switch(inquire("Are you sure? ")){
6589309Srrh case Q_YES:
6599309Srrh case Q_yes:
6609309Srrh return(0);
6619309Srrh case Q_NO:
6629309Srrh case Q_no:
6639309Srrh mustwrite(base + nwrote, n - nwrote, preciousfile);
6649309Srrh return(1);
6659309Srrh }
66616564Sralph default:
66716564Sralph return(0);
6689309Srrh }
6699309Srrh }
6709309Srrh
67146694Sbostic void
onintr()6721460Sroot onintr()
6731460Sroot {
6745595Srrh switch(inquire(terse
6755595Srrh ? "\nContinue? "
6765595Srrh : "\nInterrupt: Do you want to continue? ")){
6775595Srrh case Q_YES:
6785595Srrh case Q_yes:
6791460Sroot signal(SIGINT, onintr);
6801460Sroot return;
6815595Srrh default:
6826612Srrh if (tempfileopen){
6836612Srrh /*
6846612Srrh * Don't overwrite the original file!
6856612Srrh */
6866612Srrh writetouched(0);
6876612Srrh }
6885595Srrh exit(1);
6891460Sroot }
6905595Srrh /*NOTREACHED*/
6911460Sroot }
6925595Srrh
errorprint(place,errorp,print_all)6931460Sroot errorprint(place, errorp, print_all)
6941460Sroot FILE *place;
6955595Srrh Eptr errorp;
6961460Sroot boolean print_all;
6971460Sroot {
6981460Sroot int offset = print_all ? 0 : 2;
6991460Sroot
7001460Sroot if (errorp->error_e_class == C_IGNORE)
7011460Sroot return;
7021460Sroot fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
7031460Sroot wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
7041460Sroot putc('\n', place);
7051460Sroot }
7061460Sroot
inquire(fmt,a1,a2)7075595Srrh int inquire(fmt, a1, a2)
7081460Sroot char *fmt;
7091460Sroot /*VARARGS1*/
7101460Sroot {
7111460Sroot char buffer[128];
71216564Sralph
71316564Sralph if (queryfile == NULL)
71416564Sralph return(0);
7151460Sroot for(;;){
7161460Sroot do{
7171460Sroot fflush(stdout);
7181460Sroot fprintf(stderr, fmt, a1, a2);
7191460Sroot fflush(stderr);
7201460Sroot } while (fgets(buffer, 127, queryfile) == NULL);
7215595Srrh switch(buffer[0]){
7225595Srrh case 'Y': return(Q_YES);
7235595Srrh case 'y': return(Q_yes);
7245595Srrh case 'N': return(Q_NO);
7255595Srrh case 'n': return(Q_no);
7265595Srrh default: fprintf(stderr, "Yes or No only!\n");
7275595Srrh }
7281460Sroot }
7291460Sroot }
7301460Sroot
probethisfile(name)7315595Srrh int probethisfile(name)
7325595Srrh char *name;
7331460Sroot {
7341460Sroot struct stat statbuf;
7355595Srrh if (stat(name, &statbuf) < 0)
7365595Srrh return(F_NOTEXIST);
7375595Srrh if((statbuf.st_mode & S_IREAD) == 0)
7385595Srrh return(F_NOTREAD);
7395595Srrh if((statbuf.st_mode & S_IWRITE) == 0)
7405595Srrh return(F_NOTWRITE);
7415595Srrh return(F_TOUCHIT);
7421460Sroot }
743