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