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