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