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