touch.c revision 1.26 1 1.26 joerg /* $NetBSD: touch.c,v 1.26 2011/05/24 12:24:22 joerg 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.26 joerg __RCSID("$NetBSD: touch.c,v 1.26 2011/05/24 12:24:22 joerg 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.19 dholland static boolean preview(const char *, 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.22 dholland static int mustwrite(const char *, unsigned, 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.20 dholland spread = 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.20 dholland previewed = preview(name, 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.20 dholland preview(const char *name, 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.18 dholland switch (inquire(terse
367 1.1 cgd ? "Preview? "
368 1.18 dholland : "Do you want to preview the errors first? ")) {
369 1.1 cgd case Q_YES:
370 1.1 cgd case Q_yes:
371 1.21 dholland back = true;
372 1.20 dholland EITERATE(erpp, my_files, ix) {
373 1.21 dholland errorprint(stdout, *erpp, true);
374 1.1 cgd }
375 1.1 cgd if (!terse)
376 1.1 cgd fprintf(stdout, "\n");
377 1.15 lukem case Q_error:
378 1.1 cgd default:
379 1.1 cgd break;
380 1.1 cgd }
381 1.1 cgd }
382 1.18 dholland return (back);
383 1.1 cgd }
384 1.1 cgd
385 1.17 dholland static int
386 1.19 dholland settotouch(const char *name)
387 1.1 cgd {
388 1.18 dholland int dest = TOSTDOUT;
389 1.1 cgd
390 1.18 dholland if (query) {
391 1.18 dholland switch (inquire(terse
392 1.1 cgd ? "Touch? "
393 1.1 cgd : "Do you want to touch file \"%s\"? ",
394 1.18 dholland name)) {
395 1.1 cgd case Q_NO:
396 1.1 cgd case Q_no:
397 1.15 lukem case Q_error:
398 1.15 lukem touchstatus = Q_NO;
399 1.18 dholland return (dest);
400 1.1 cgd default:
401 1.15 lukem touchstatus = Q_YES;
402 1.1 cgd break;
403 1.1 cgd }
404 1.1 cgd }
405 1.1 cgd
406 1.18 dholland switch (probethisfile(name)) {
407 1.1 cgd case F_NOTREAD:
408 1.1 cgd dest = TOSTDOUT;
409 1.1 cgd fprintf(stdout, terse
410 1.1 cgd ? "\"%s\" unreadable\n"
411 1.1 cgd : "File \"%s\" is unreadable\n",
412 1.1 cgd name);
413 1.1 cgd break;
414 1.1 cgd case F_NOTWRITE:
415 1.1 cgd dest = TOSTDOUT;
416 1.1 cgd fprintf(stdout, terse
417 1.1 cgd ? "\"%s\" unwritable\n"
418 1.1 cgd : "File \"%s\" is unwritable\n",
419 1.1 cgd name);
420 1.1 cgd break;
421 1.1 cgd case F_NOTEXIST:
422 1.1 cgd dest = TOSTDOUT;
423 1.1 cgd fprintf(stdout, terse
424 1.1 cgd ? "\"%s\" not found\n"
425 1.1 cgd : "Can't find file \"%s\" to insert error messages into.\n",
426 1.1 cgd name);
427 1.1 cgd break;
428 1.1 cgd default:
429 1.1 cgd dest = edit(name) ? TOSTDOUT : TOTHEFILE;
430 1.1 cgd break;
431 1.1 cgd }
432 1.18 dholland return (dest);
433 1.1 cgd }
434 1.1 cgd
435 1.17 dholland static void
436 1.20 dholland diverterrors(const char *name, int dest, Eptr **my_files, int ix,
437 1.19 dholland boolean previewed, int nterrors)
438 1.1 cgd {
439 1.20 dholland int my_nerrors;
440 1.18 dholland Eptr *erpp;
441 1.18 dholland Eptr errorp;
442 1.1 cgd
443 1.20 dholland my_nerrors = my_files[ix+1] - my_files[ix];
444 1.1 cgd
445 1.21 dholland if (my_nerrors != nterrors && !previewed) {
446 1.26 joerg if (terse)
447 1.26 joerg printf("Uninserted errors\n");
448 1.26 joerg else
449 1.26 joerg printf(">>Uninserted errors for file \"%s\" follow.\n",
450 1.26 joerg name);
451 1.1 cgd }
452 1.1 cgd
453 1.20 dholland EITERATE(erpp, my_files, ix) {
454 1.1 cgd errorp = *erpp;
455 1.18 dholland if (errorp->error_e_class != C_TRUE) {
456 1.1 cgd if (previewed || touchstatus == Q_NO)
457 1.1 cgd continue;
458 1.21 dholland errorprint(stdout, errorp, true);
459 1.1 cgd continue;
460 1.1 cgd }
461 1.18 dholland switch (dest) {
462 1.1 cgd case TOSTDOUT:
463 1.1 cgd if (previewed || touchstatus == Q_NO)
464 1.1 cgd continue;
465 1.21 dholland errorprint(stdout,errorp, true);
466 1.1 cgd break;
467 1.1 cgd case TOTHEFILE:
468 1.1 cgd insert(errorp->error_line);
469 1.21 dholland text(errorp, false);
470 1.1 cgd break;
471 1.1 cgd }
472 1.1 cgd }
473 1.1 cgd }
474 1.1 cgd
475 1.17 dholland static int
476 1.19 dholland oktotouch(const char *filename)
477 1.1 cgd {
478 1.19 dholland const char *src;
479 1.22 dholland const char *pat;
480 1.19 dholland const char *osrc;
481 1.1 cgd
482 1.1 cgd pat = suffixlist;
483 1.1 cgd if (pat == 0)
484 1.18 dholland return (0);
485 1.1 cgd if (*pat == '*')
486 1.18 dholland return (1);
487 1.1 cgd while (*pat++ != '.')
488 1.1 cgd continue;
489 1.1 cgd --pat; /* point to the period */
490 1.1 cgd
491 1.1 cgd for (src = &filename[strlen(filename)], --src;
492 1.21 dholland src > filename && *src != '.'; --src)
493 1.1 cgd continue;
494 1.1 cgd if (*src != '.')
495 1.18 dholland return (0);
496 1.1 cgd
497 1.18 dholland for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++) {
498 1.1 cgd for (; *src /* not at end of the source */
499 1.1 cgd && *pat /* not off end of pattern */
500 1.1 cgd && *pat != '.' /* not off end of sub pattern */
501 1.1 cgd && *pat != '*' /* not wild card */
502 1.1 cgd && *src == *pat; /* and equal... */
503 1.1 cgd src++, pat++)
504 1.1 cgd continue;
505 1.1 cgd if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
506 1.18 dholland return (1);
507 1.1 cgd if (*src != 0 && *pat == '*')
508 1.18 dholland return (1);
509 1.1 cgd while (*pat && *pat != '.')
510 1.1 cgd pat++;
511 1.18 dholland if (!*pat)
512 1.18 dholland return (0);
513 1.1 cgd }
514 1.18 dholland return (0);
515 1.1 cgd }
516 1.6 lukem
517 1.1 cgd /*
518 1.18 dholland * Construct an execv argument
519 1.18 dholland * We need 1 argument for the editor's name
520 1.18 dholland * We need 1 argument for the initial search string
521 1.18 dholland * We need n_pissed_on arguments for the file names
522 1.18 dholland * We need 1 argument that is a null for execv.
523 1.18 dholland * The caller fills in the editor's name.
524 1.18 dholland * We fill in the initial search string.
525 1.18 dholland * We fill in the arguments, and the null.
526 1.1 cgd */
527 1.17 dholland static void
528 1.13 wiz execvarg(int n_pissed_on, int *r_argc, char ***r_argv)
529 1.1 cgd {
530 1.18 dholland Eptr p;
531 1.23 christos const char *sep, *name;
532 1.18 dholland int fi;
533 1.1 cgd
534 1.6 lukem sep = NULL;
535 1.21 dholland (*r_argv) = Calloc(n_pissed_on + 3, sizeof(char *));
536 1.1 cgd (*r_argc) = n_pissed_on + 2;
537 1.22 dholland (*r_argv)[1] = Strdup("+1;/###/"); /* XXX leaked */
538 1.1 cgd n_pissed_on = 2;
539 1.18 dholland if (!terse) {
540 1.1 cgd fprintf(stdout, "You touched file(s):");
541 1.1 cgd sep = " ";
542 1.1 cgd }
543 1.20 dholland FILEITERATE(fi, 1, nfiles) {
544 1.1 cgd if (!touchedfiles[fi])
545 1.1 cgd continue;
546 1.1 cgd p = *(files[fi]);
547 1.23 christos name = makename(p->error_text[0], filelevel);
548 1.18 dholland if (!terse) {
549 1.23 christos fprintf(stdout,"%s\"%s\"", sep, name);
550 1.1 cgd sep = ", ";
551 1.1 cgd }
552 1.23 christos (*r_argv)[n_pissed_on++] = __UNCONST(name);
553 1.1 cgd }
554 1.1 cgd if (!terse)
555 1.1 cgd fprintf(stdout, "\n");
556 1.1 cgd (*r_argv)[n_pissed_on] = 0;
557 1.1 cgd }
558 1.1 cgd
559 1.17 dholland static FILE *o_touchedfile; /* the old file */
560 1.17 dholland static FILE *n_touchedfile; /* the new file */
561 1.19 dholland static const char *o_name;
562 1.17 dholland static char n_name[MAXPATHLEN];
563 1.17 dholland static int o_lineno;
564 1.17 dholland static int n_lineno;
565 1.21 dholland static boolean tempfileopen = false;
566 1.18 dholland
567 1.1 cgd /*
568 1.18 dholland * open the file; guaranteed to be both readable and writable
569 1.18 dholland * Well, if it isn't, then return TRUE if something failed
570 1.1 cgd */
571 1.17 dholland static boolean
572 1.19 dholland edit(const char *name)
573 1.1 cgd {
574 1.4 lukem int fd;
575 1.9 kleink const char *tmpdir;
576 1.4 lukem
577 1.1 cgd o_name = name;
578 1.18 dholland if ((o_touchedfile = fopen(name, "r")) == NULL) {
579 1.25 christos warn("Can't open file `%s' to touch (read)", name);
580 1.21 dholland return true;
581 1.1 cgd }
582 1.9 kleink if ((tmpdir = getenv("TMPDIR")) == NULL)
583 1.9 kleink tmpdir = _PATH_TMP;
584 1.9 kleink (void)snprintf(n_name, sizeof (n_name), "%s/%s", tmpdir, TMPFILE);
585 1.4 lukem fd = -1;
586 1.4 lukem if ((fd = mkstemp(n_name)) == -1 ||
587 1.4 lukem (n_touchedfile = fdopen(fd, "w")) == NULL) {
588 1.25 christos warn("Can't open file `%s' to touch (write)", name);
589 1.4 lukem if (fd != -1)
590 1.4 lukem close(fd);
591 1.21 dholland return true;
592 1.1 cgd }
593 1.21 dholland tempfileopen = true;
594 1.1 cgd n_lineno = 0;
595 1.1 cgd o_lineno = 0;
596 1.21 dholland return false;
597 1.1 cgd }
598 1.6 lukem
599 1.1 cgd /*
600 1.18 dholland * Position to the line (before, after) the line given by place
601 1.1 cgd */
602 1.17 dholland static char edbuf[BUFSIZ];
603 1.6 lukem
604 1.17 dholland static void
605 1.13 wiz insert(int place)
606 1.1 cgd {
607 1.18 dholland --place; /* always insert messages before the offending line */
608 1.18 dholland for (; o_lineno < place; o_lineno++, n_lineno++) {
609 1.18 dholland if (fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
610 1.1 cgd return;
611 1.1 cgd fputs(edbuf, n_touchedfile);
612 1.1 cgd }
613 1.1 cgd }
614 1.1 cgd
615 1.17 dholland static void
616 1.13 wiz text(Eptr p, boolean use_all)
617 1.1 cgd {
618 1.18 dholland int offset = use_all ? 0 : 2;
619 1.1 cgd
620 1.1 cgd fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
621 1.1 cgd fprintf(n_touchedfile, "%d [%s] ",
622 1.1 cgd p->error_line,
623 1.1 cgd lang_table[p->error_language].lang_name);
624 1.1 cgd wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
625 1.18 dholland fputs(lang_table[p->error_language].lang_outcomment, n_touchedfile);
626 1.1 cgd n_lineno++;
627 1.1 cgd }
628 1.1 cgd
629 1.1 cgd /*
630 1.18 dholland * write the touched file to its temporary copy,
631 1.18 dholland * then bring the temporary in over the local file
632 1.1 cgd */
633 1.17 dholland static boolean
634 1.13 wiz writetouched(int overwrite)
635 1.1 cgd {
636 1.22 dholland unsigned nread;
637 1.18 dholland FILE *localfile;
638 1.20 dholland FILE *temp;
639 1.18 dholland int botch;
640 1.18 dholland int oktorm;
641 1.1 cgd
642 1.1 cgd botch = 0;
643 1.1 cgd oktorm = 1;
644 1.5 pk while ((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != 0) {
645 1.18 dholland if (nread != fwrite(edbuf, 1, nread, n_touchedfile)) {
646 1.1 cgd /*
647 1.18 dholland * Catastrophe in temporary area: file system full?
648 1.1 cgd */
649 1.1 cgd botch = 1;
650 1.25 christos warn("write failure: No errors inserted in `%s'",
651 1.25 christos o_name);
652 1.1 cgd }
653 1.1 cgd }
654 1.1 cgd fclose(n_touchedfile);
655 1.1 cgd fclose(o_touchedfile);
656 1.18 dholland
657 1.1 cgd /*
658 1.18 dholland * Now, copy the temp file back over the original
659 1.18 dholland * file, thus preserving links, etc
660 1.1 cgd */
661 1.18 dholland if (botch == 0 && overwrite) {
662 1.1 cgd botch = 0;
663 1.1 cgd localfile = NULL;
664 1.20 dholland temp = NULL;
665 1.18 dholland if ((localfile = fopen(o_name, "w")) == NULL) {
666 1.25 christos warn("Can't open file `%s' to overwrite", o_name);
667 1.1 cgd botch++;
668 1.1 cgd }
669 1.20 dholland if ((temp = fopen(n_name, "r")) == NULL) {
670 1.25 christos warn("Can't open file `%s' to read", n_name);
671 1.1 cgd botch++;
672 1.1 cgd }
673 1.1 cgd if (!botch)
674 1.20 dholland oktorm = mustoverwrite(localfile, temp);
675 1.1 cgd if (localfile != NULL)
676 1.1 cgd fclose(localfile);
677 1.20 dholland if (temp != NULL)
678 1.20 dholland fclose(temp);
679 1.1 cgd }
680 1.25 christos if (oktorm == 0)
681 1.25 christos errx(1, "Catastrophe: A copy of `%s': was saved in `%s'",
682 1.25 christos o_name, n_name);
683 1.1 cgd /*
684 1.18 dholland * Kiss the temp file good bye
685 1.1 cgd */
686 1.1 cgd unlink(n_name);
687 1.21 dholland tempfileopen = false;
688 1.21 dholland return true;
689 1.1 cgd }
690 1.6 lukem
691 1.1 cgd /*
692 1.18 dholland * return 1 if the tmpfile can be removed after writing it out
693 1.1 cgd */
694 1.17 dholland static int
695 1.20 dholland mustoverwrite(FILE *preciousfile, FILE *temp)
696 1.1 cgd {
697 1.22 dholland unsigned nread;
698 1.1 cgd
699 1.20 dholland while ((nread = fread(edbuf, 1, sizeof(edbuf), temp)) != 0) {
700 1.1 cgd if (mustwrite(edbuf, nread, preciousfile) == 0)
701 1.18 dholland return (0);
702 1.1 cgd }
703 1.18 dholland return (1);
704 1.1 cgd }
705 1.18 dholland
706 1.1 cgd /*
707 1.18 dholland * return 0 on catastrophe
708 1.1 cgd */
709 1.17 dholland static int
710 1.22 dholland mustwrite(const char *base, unsigned n, FILE *preciousfile)
711 1.1 cgd {
712 1.22 dholland unsigned nwrote;
713 1.1 cgd
714 1.1 cgd if (n <= 0)
715 1.18 dholland return (1);
716 1.1 cgd nwrote = fwrite(base, 1, n, preciousfile);
717 1.1 cgd if (nwrote == n)
718 1.18 dholland return (1);
719 1.25 christos warn("write failed");
720 1.18 dholland switch (inquire(terse
721 1.1 cgd ? "Botch overwriting: retry? "
722 1.18 dholland : "Botch overwriting the source file: retry? ")) {
723 1.1 cgd case Q_YES:
724 1.1 cgd case Q_yes:
725 1.1 cgd mustwrite(base + nwrote, n - nwrote, preciousfile);
726 1.18 dholland return (1);
727 1.1 cgd case Q_NO:
728 1.1 cgd case Q_no:
729 1.18 dholland switch (inquire("Are you sure? ")) {
730 1.15 lukem case Q_error:
731 1.1 cgd case Q_YES:
732 1.1 cgd case Q_yes:
733 1.18 dholland return (0);
734 1.1 cgd case Q_NO:
735 1.1 cgd case Q_no:
736 1.1 cgd mustwrite(base + nwrote, n - nwrote, preciousfile);
737 1.18 dholland return (1);
738 1.1 cgd }
739 1.15 lukem case Q_error:
740 1.1 cgd default:
741 1.18 dholland return (0);
742 1.1 cgd }
743 1.1 cgd }
744 1.1 cgd
745 1.1 cgd void
746 1.16 lukem onintr(int sig)
747 1.1 cgd {
748 1.18 dholland switch (inquire(terse
749 1.1 cgd ? "\nContinue? "
750 1.18 dholland : "\nInterrupt: Do you want to continue? ")) {
751 1.1 cgd case Q_YES:
752 1.1 cgd case Q_yes:
753 1.16 lukem signal(sig, onintr);
754 1.1 cgd return;
755 1.15 lukem case Q_error:
756 1.1 cgd default:
757 1.18 dholland if (tempfileopen) {
758 1.1 cgd /*
759 1.18 dholland * Don't overwrite the original file!
760 1.1 cgd */
761 1.1 cgd writetouched(0);
762 1.1 cgd }
763 1.16 lukem (void)raise_default_signal(sig);
764 1.16 lukem _exit(127);
765 1.1 cgd }
766 1.1 cgd /*NOTREACHED*/
767 1.1 cgd }
768 1.1 cgd
769 1.17 dholland static void
770 1.13 wiz errorprint(FILE *place, Eptr errorp, boolean print_all)
771 1.1 cgd {
772 1.18 dholland int offset = print_all ? 0 : 2;
773 1.1 cgd
774 1.1 cgd if (errorp->error_e_class == C_IGNORE)
775 1.1 cgd return;
776 1.1 cgd fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
777 1.1 cgd wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
778 1.1 cgd putc('\n', place);
779 1.1 cgd }
780 1.1 cgd
781 1.6 lukem int
782 1.19 dholland inquire(const char *fmt, ...)
783 1.1 cgd {
784 1.6 lukem va_list ap;
785 1.18 dholland char buffer[128];
786 1.1 cgd
787 1.1 cgd if (queryfile == NULL)
788 1.18 dholland return (Q_error);
789 1.18 dholland for (;;) {
790 1.15 lukem fflush(stdout);
791 1.15 lukem va_start(ap, fmt);
792 1.15 lukem vfprintf(stderr, fmt, ap);
793 1.15 lukem va_end(ap);
794 1.15 lukem fflush(stderr);
795 1.15 lukem if (fgets(buffer, 127, queryfile) == NULL)
796 1.18 dholland return (Q_error);
797 1.18 dholland switch (buffer[0]) {
798 1.18 dholland case 'Y': return (Q_YES);
799 1.18 dholland case 'y': return (Q_yes);
800 1.18 dholland case 'N': return (Q_NO);
801 1.18 dholland case 'n': return (Q_no);
802 1.18 dholland default: fprintf(stderr, "Yes or No only!\n");
803 1.1 cgd }
804 1.1 cgd }
805 1.1 cgd }
806 1.1 cgd
807 1.17 dholland static int
808 1.19 dholland probethisfile(const char *name)
809 1.1 cgd {
810 1.1 cgd struct stat statbuf;
811 1.18 dholland
812 1.1 cgd if (stat(name, &statbuf) < 0)
813 1.18 dholland return (F_NOTEXIST);
814 1.18 dholland if ((statbuf.st_mode & S_IREAD) == 0)
815 1.18 dholland return (F_NOTREAD);
816 1.18 dholland if ((statbuf.st_mode & S_IWRITE) == 0)
817 1.18 dholland return (F_NOTWRITE);
818 1.18 dholland return (F_TOUCHIT);
819 1.1 cgd }
820