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