touch.c revision 1.13 1 /* $NetBSD: touch.c,v 1.13 2002/05/26 22:41:21 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: touch.c,v 1.13 2002/05/26 22:41:21 wiz Exp $");
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <ctype.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <stdarg.h>
53 #include "error.h"
54 #include "pathnames.h"
55
56 /*
57 * Iterate through errors
58 */
59 #define EITERATE(p, fv, i) for (p = fv[i]; p < fv[i+1]; p++)
60 #define ECITERATE(ei, p, lb) for (ei = lb; p = errors[ei],ei < nerrors; ei++)
61
62 #define FILEITERATE(fi, lb) for (fi = lb; fi <= nfiles; fi++)
63 int touchstatus = Q_YES;
64
65 extern char *suffixlist;
66
67 void
68 findfiles(int nerrors, Eptr *errors, int *r_nfiles, Eptr ***r_files)
69 {
70 int nfiles;
71 Eptr **files;
72
73 char *name;
74 int ei;
75 int fi;
76 Eptr errorp;
77
78 nfiles = countfiles(errors);
79
80 files = (Eptr**)Calloc(nfiles + 3, sizeof (Eptr*));
81 touchedfiles = (boolean *)Calloc(nfiles+3, sizeof(boolean));
82 /*
83 * Now, partition off the error messages
84 * into those that are synchronization, discarded or
85 * not specific to any file, and those that were
86 * nulled or true errors.
87 */
88 files[0] = &errors[0];
89 ECITERATE(ei, errorp, 0){
90 if ( ! (NOTSORTABLE(errorp->error_e_class)))
91 break;
92 }
93 /*
94 * Now, and partition off all error messages
95 * for a given file.
96 */
97 files[1] = &errors[ei];
98 touchedfiles[0] = touchedfiles[1] = FALSE;
99 name = "\1";
100 fi = 1;
101 ECITERATE(ei, errorp, ei){
102 if ( (errorp->error_e_class == C_NULLED)
103 || (errorp->error_e_class == C_TRUE) ){
104 if (strcmp(errorp->error_text[0], name) != 0){
105 name = errorp->error_text[0];
106 touchedfiles[fi] = FALSE;
107 files[fi] = &errors[ei];
108 fi++;
109 }
110 }
111 }
112 files[fi] = &errors[nerrors];
113 *r_nfiles = nfiles;
114 *r_files = files;
115 }
116
117 int
118 countfiles(Eptr *errors)
119 {
120 char *name;
121 int ei;
122 Eptr errorp;
123
124 int nfiles;
125 nfiles = 0;
126 name = "\1";
127 ECITERATE(ei, errorp, 0){
128 if (SORTABLE(errorp->error_e_class)){
129 if (strcmp(errorp->error_text[0],name) != 0){
130 nfiles++;
131 name = errorp->error_text[0];
132 }
133 }
134 }
135 return(nfiles);
136 }
137
138 char *class_table[] = {
139 /*C_UNKNOWN 0 */ "Unknown",
140 /*C_IGNORE 1 */ "ignore",
141 /*C_SYNC 2 */ "synchronization",
142 /*C_DISCARD 3 */ "discarded",
143 /*C_NONSPEC 4 */ "non specific",
144 /*C_THISFILE 5 */ "specific to this file",
145 /*C_NULLED 6 */ "nulled",
146 /*C_TRUE 7 */ "true",
147 /*C_DUPL 8 */ "duplicated"
148 };
149
150 int class_count[C_LAST - C_FIRST] = {0};
151
152 void
153 filenames(int nfiles, Eptr **files)
154 {
155 int fi;
156 char *sep = " ";
157 int someerrors;
158
159 /*
160 * first, simply dump out errors that
161 * don't pertain to any file
162 */
163 someerrors = nopertain(files);
164
165 if (nfiles){
166 someerrors++;
167 if (terse)
168 fprintf(stdout, "%d file%s", nfiles, plural(nfiles));
169 else
170 fprintf(stdout, "%d file%s contain%s errors",
171 nfiles, plural(nfiles), verbform(nfiles));
172 if (!terse){
173 FILEITERATE(fi, 1){
174 fprintf(stdout, "%s\"%s\" (%d)",
175 sep, (*files[fi])->error_text[0],
176 (int)(files[fi+1] - files[fi]));
177 sep = ", ";
178 }
179 }
180 fprintf(stdout, "\n");
181 }
182 if (!someerrors)
183 fprintf(stdout, "No errors.\n");
184 }
185
186 /*
187 * Dump out errors that don't pertain to any file
188 */
189 int
190 nopertain(Eptr **files)
191 {
192 int type;
193 int someerrors = 0;
194 Eptr *erpp;
195 Eptr errorp;
196
197 if (files[1] - files[0] <= 0)
198 return(0);
199 for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
200 if (class_count[type] <= 0)
201 continue;
202 if (type > C_SYNC)
203 someerrors++;
204 if (terse){
205 fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
206 class_count[type], class_table[type]);
207 } else {
208 fprintf(stdout, "\n\t%d %s errors follow\n",
209 class_count[type], class_table[type]);
210 EITERATE(erpp, files, 0){
211 errorp = *erpp;
212 if (errorp->error_e_class == type){
213 errorprint(stdout, errorp, TRUE);
214 }
215 }
216 }
217 }
218 return(someerrors);
219 }
220
221 extern boolean notouch;
222
223 boolean
224 touchfiles(int nfiles, Eptr **files, int *r_edargc, char ***r_edargv)
225 {
226 char *name;
227 Eptr errorp;
228 int fi;
229 Eptr *erpp;
230 int ntrueerrors;
231 boolean scribbled;
232 int n_pissed_on; /* # of file touched*/
233 int spread;
234
235 FILEITERATE(fi, 1){
236 name = (*files[fi])->error_text[0];
237 spread = files[fi+1] - files[fi];
238 fprintf(stdout, terse
239 ? "\"%s\" has %d error%s, "
240 : "\nFile \"%s\" has %d error%s.\n"
241 , name ,spread ,plural(spread));
242 /*
243 * First, iterate through all error messages in this file
244 * to see how many of the error messages really will
245 * get inserted into the file.
246 */
247 ntrueerrors = 0;
248 EITERATE(erpp, files, fi){
249 errorp = *erpp;
250 if (errorp->error_e_class == C_TRUE)
251 ntrueerrors++;
252 }
253 fprintf(stdout, terse
254 ? "insert %d\n"
255 : "\t%d of these errors can be inserted into the file.\n",
256 ntrueerrors);
257
258 hackfile(name, files, fi, ntrueerrors);
259 }
260 scribbled = FALSE;
261 n_pissed_on = 0;
262 FILEITERATE(fi, 1){
263 scribbled |= touchedfiles[fi];
264 n_pissed_on++;
265 }
266 if (scribbled){
267 /*
268 * Construct an execv argument
269 */
270 execvarg(n_pissed_on, r_edargc, r_edargv);
271 return(TRUE);
272 } else {
273 if (!terse)
274 fprintf(stdout, "You didn't touch any files.\n");
275 return(FALSE);
276 }
277 }
278
279 void
280 hackfile(char *name, Eptr **files, int ix, int nerrors)
281 {
282 boolean previewed;
283 int errordest; /* where errors go*/
284
285 if (!oktotouch(name)) {
286 previewed = FALSE;
287 errordest = TOSTDOUT;
288 } else {
289 previewed = preview(name, nerrors, files, ix);
290 errordest = settotouch(name);
291 }
292
293 if (errordest != TOSTDOUT)
294 touchedfiles[ix] = TRUE;
295
296 if (previewed && (errordest == TOSTDOUT))
297 return;
298
299 diverterrors(name, errordest, files, ix, previewed, nerrors);
300
301 if (errordest == TOTHEFILE){
302 /*
303 * overwrite the original file
304 */
305 writetouched(1);
306 }
307 }
308
309 boolean
310 preview(char *name, int nerrors, Eptr **files, int ix)
311 {
312 int back;
313 Eptr *erpp;
314
315 if (nerrors <= 0)
316 return(FALSE);
317 back = FALSE;
318 if(query){
319 switch(inquire(terse
320 ? "Preview? "
321 : "Do you want to preview the errors first? ")){
322 case Q_YES:
323 case Q_yes:
324 back = TRUE;
325 EITERATE(erpp, files, ix){
326 errorprint(stdout, *erpp, TRUE);
327 }
328 if (!terse)
329 fprintf(stdout, "\n");
330 default:
331 break;
332 }
333 }
334 return(back);
335 }
336
337 int
338 settotouch(char *name)
339 {
340 int dest = TOSTDOUT;
341
342 if (query){
343 switch(touchstatus = inquire(terse
344 ? "Touch? "
345 : "Do you want to touch file \"%s\"? ",
346 name)){
347 case Q_NO:
348 case Q_no:
349 return(dest);
350 default:
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_YES:
684 case Q_yes:
685 return(0);
686 case Q_NO:
687 case Q_no:
688 mustwrite(base + nwrote, n - nwrote, preciousfile);
689 return(1);
690 }
691 default:
692 return(0);
693 }
694 }
695
696 void
697 onintr(int dummy)
698 {
699 switch(inquire(terse
700 ? "\nContinue? "
701 : "\nInterrupt: Do you want to continue? ")){
702 case Q_YES:
703 case Q_yes:
704 signal(SIGINT, onintr);
705 return;
706 default:
707 if (tempfileopen){
708 /*
709 * Don't overwrite the original file!
710 */
711 writetouched(0);
712 }
713 exit(1);
714 }
715 /*NOTREACHED*/
716 }
717
718 void
719 errorprint(FILE *place, Eptr errorp, boolean print_all)
720 {
721 int offset = print_all ? 0 : 2;
722
723 if (errorp->error_e_class == C_IGNORE)
724 return;
725 fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
726 wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
727 putc('\n', place);
728 }
729
730 int
731 inquire(char *fmt, ...)
732 {
733 va_list ap;
734 char buffer[128];
735
736 if (queryfile == NULL)
737 return(0);
738 for(;;){
739 do{
740 fflush(stdout);
741 va_start(ap, fmt);
742 vfprintf(stderr, fmt, ap);
743 va_end(ap);
744 fflush(stderr);
745 } while (fgets(buffer, 127, queryfile) == NULL);
746 switch(buffer[0]){
747 case 'Y': return(Q_YES);
748 case 'y': return(Q_yes);
749 case 'N': return(Q_NO);
750 case 'n': return(Q_no);
751 default: fprintf(stderr, "Yes or No only!\n");
752 }
753 }
754 }
755
756 int
757 probethisfile(char *name)
758 {
759 struct stat statbuf;
760 if (stat(name, &statbuf) < 0)
761 return(F_NOTEXIST);
762 if((statbuf.st_mode & S_IREAD) == 0)
763 return(F_NOTREAD);
764 if((statbuf.st_mode & S_IWRITE) == 0)
765 return(F_NOTWRITE);
766 return(F_TOUCHIT);
767 }
768