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