util.c revision 1.9 1 1.9 kristerw /* $NetBSD: util.c,v 1.9 2002/03/08 21:57:33 kristerw Exp $ */
2 1.5 christos #include <sys/cdefs.h>
3 1.2 mycroft #ifndef lint
4 1.9 kristerw __RCSID("$NetBSD: util.c,v 1.9 2002/03/08 21:57:33 kristerw Exp $");
5 1.2 mycroft #endif /* not lint */
6 1.2 mycroft
7 1.1 cgd #include "EXTERN.h"
8 1.1 cgd #include "common.h"
9 1.1 cgd #include "INTERN.h"
10 1.1 cgd #include "util.h"
11 1.1 cgd #include "backupfile.h"
12 1.5 christos #ifdef __STDC__
13 1.5 christos #include <stdarg.h>
14 1.5 christos #else
15 1.5 christos #include <varargs.h>
16 1.5 christos #endif
17 1.1 cgd
18 1.5 christos #include <stdlib.h>
19 1.5 christos #include <unistd.h>
20 1.5 christos #include <fcntl.h>
21 1.1 cgd
22 1.1 cgd /* Rename a file, copying it if necessary. */
23 1.1 cgd
24 1.1 cgd int
25 1.9 kristerw move_file(char *from, char *to)
26 1.1 cgd {
27 1.1 cgd char bakname[512];
28 1.9 kristerw char *s;
29 1.9 kristerw int i;
30 1.9 kristerw int fromfd;
31 1.1 cgd
32 1.1 cgd /* to stdout? */
33 1.1 cgd
34 1.1 cgd if (strEQ(to, "-")) {
35 1.1 cgd #ifdef DEBUGGING
36 1.1 cgd if (debug & 4)
37 1.1 cgd say2("Moving %s to stdout.\n", from);
38 1.1 cgd #endif
39 1.1 cgd fromfd = open(from, 0);
40 1.1 cgd if (fromfd < 0)
41 1.1 cgd pfatal2("internal error, can't reopen %s", from);
42 1.1 cgd while ((i=read(fromfd, buf, sizeof buf)) > 0)
43 1.1 cgd if (write(1, buf, i) != 1)
44 1.1 cgd pfatal1("write failed");
45 1.1 cgd Close(fromfd);
46 1.1 cgd return 0;
47 1.1 cgd }
48 1.1 cgd
49 1.1 cgd if (origprae) {
50 1.1 cgd Strcpy(bakname, origprae);
51 1.1 cgd Strcat(bakname, to);
52 1.1 cgd } else {
53 1.1 cgd #ifndef NODIR
54 1.1 cgd char *backupname = find_backup_file_name(to);
55 1.1 cgd if (backupname == (char *) 0)
56 1.1 cgd fatal1("out of memory\n");
57 1.1 cgd Strcpy(bakname, backupname);
58 1.1 cgd free(backupname);
59 1.1 cgd #else /* NODIR */
60 1.1 cgd Strcpy(bakname, to);
61 1.1 cgd Strcat(bakname, simple_backup_suffix);
62 1.1 cgd #endif /* NODIR */
63 1.1 cgd }
64 1.1 cgd
65 1.1 cgd if (stat(to, &filestat) == 0) { /* output file exists */
66 1.1 cgd dev_t to_device = filestat.st_dev;
67 1.1 cgd ino_t to_inode = filestat.st_ino;
68 1.1 cgd char *simplename = bakname;
69 1.1 cgd
70 1.1 cgd for (s=bakname; *s; s++) {
71 1.1 cgd if (*s == '/')
72 1.1 cgd simplename = s+1;
73 1.1 cgd }
74 1.1 cgd /* Find a backup name that is not the same file.
75 1.1 cgd Change the first lowercase char into uppercase;
76 1.1 cgd if that isn't sufficient, chop off the first char and try again. */
77 1.1 cgd while (stat(bakname, &filestat) == 0 &&
78 1.1 cgd to_device == filestat.st_dev && to_inode == filestat.st_ino) {
79 1.1 cgd /* Skip initial non-lowercase chars. */
80 1.6 christos for (s=simplename; *s && !islower((unsigned char)*s); s++) ;
81 1.1 cgd if (*s)
82 1.1 cgd *s = toupper(*s);
83 1.1 cgd else
84 1.1 cgd Strcpy(simplename, simplename+1);
85 1.1 cgd }
86 1.1 cgd while (unlink(bakname) >= 0) ; /* while() is for benefit of Eunice */
87 1.1 cgd #ifdef DEBUGGING
88 1.1 cgd if (debug & 4)
89 1.1 cgd say3("Moving %s to %s.\n", to, bakname);
90 1.1 cgd #endif
91 1.1 cgd if (link(to, bakname) < 0) {
92 1.1 cgd /* Maybe `to' is a symlink into a different file system.
93 1.1 cgd Copying replaces the symlink with a file; using rename
94 1.1 cgd would be better. */
95 1.9 kristerw int tofd;
96 1.9 kristerw int bakfd;
97 1.1 cgd
98 1.1 cgd bakfd = creat(bakname, 0666);
99 1.1 cgd if (bakfd < 0) {
100 1.1 cgd say4("Can't backup %s, output is in %s: %s\n", to, from,
101 1.1 cgd strerror(errno));
102 1.1 cgd return -1;
103 1.1 cgd }
104 1.1 cgd tofd = open(to, 0);
105 1.1 cgd if (tofd < 0)
106 1.1 cgd pfatal2("internal error, can't open %s", to);
107 1.1 cgd while ((i=read(tofd, buf, sizeof buf)) > 0)
108 1.1 cgd if (write(bakfd, buf, i) != i)
109 1.1 cgd pfatal1("write failed");
110 1.1 cgd Close(tofd);
111 1.1 cgd Close(bakfd);
112 1.1 cgd }
113 1.1 cgd while (unlink(to) >= 0) ;
114 1.1 cgd }
115 1.1 cgd #ifdef DEBUGGING
116 1.1 cgd if (debug & 4)
117 1.1 cgd say3("Moving %s to %s.\n", from, to);
118 1.1 cgd #endif
119 1.1 cgd if (link(from, to) < 0) { /* different file system? */
120 1.9 kristerw int tofd;
121 1.1 cgd
122 1.1 cgd tofd = creat(to, 0666);
123 1.1 cgd if (tofd < 0) {
124 1.1 cgd say4("Can't create %s, output is in %s: %s\n",
125 1.1 cgd to, from, strerror(errno));
126 1.1 cgd return -1;
127 1.1 cgd }
128 1.1 cgd fromfd = open(from, 0);
129 1.1 cgd if (fromfd < 0)
130 1.1 cgd pfatal2("internal error, can't reopen %s", from);
131 1.1 cgd while ((i=read(fromfd, buf, sizeof buf)) > 0)
132 1.1 cgd if (write(tofd, buf, i) != i)
133 1.1 cgd pfatal1("write failed");
134 1.1 cgd Close(fromfd);
135 1.1 cgd Close(tofd);
136 1.1 cgd }
137 1.1 cgd Unlink(from);
138 1.1 cgd return 0;
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd /* Copy a file. */
142 1.1 cgd
143 1.1 cgd void
144 1.9 kristerw copy_file(char *from, char *to)
145 1.1 cgd {
146 1.9 kristerw int tofd;
147 1.9 kristerw int fromfd;
148 1.9 kristerw int i;
149 1.1 cgd
150 1.1 cgd tofd = creat(to, 0666);
151 1.1 cgd if (tofd < 0)
152 1.1 cgd pfatal2("can't create %s", to);
153 1.1 cgd fromfd = open(from, 0);
154 1.1 cgd if (fromfd < 0)
155 1.1 cgd pfatal2("internal error, can't reopen %s", from);
156 1.1 cgd while ((i=read(fromfd, buf, sizeof buf)) > 0)
157 1.1 cgd if (write(tofd, buf, i) != i)
158 1.1 cgd pfatal2("write to %s failed", to);
159 1.1 cgd Close(fromfd);
160 1.1 cgd Close(tofd);
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd /* Allocate a unique area for a string. */
164 1.1 cgd
165 1.1 cgd char *
166 1.9 kristerw savestr(char *s)
167 1.1 cgd {
168 1.9 kristerw char *rv;
169 1.9 kristerw char *t;
170 1.1 cgd
171 1.1 cgd if (!s)
172 1.1 cgd s = "Oops";
173 1.1 cgd t = s;
174 1.1 cgd while (*t++);
175 1.1 cgd rv = malloc((MEM) (t - s));
176 1.1 cgd if (rv == Nullch) {
177 1.1 cgd if (using_plan_a)
178 1.1 cgd out_of_mem = TRUE;
179 1.1 cgd else
180 1.1 cgd fatal1("out of memory\n");
181 1.1 cgd }
182 1.1 cgd else {
183 1.1 cgd t = rv;
184 1.5 christos while ((*t++ = *s++) != '\0');
185 1.1 cgd }
186 1.1 cgd return rv;
187 1.1 cgd }
188 1.1 cgd
189 1.1 cgd #if defined(lint) && defined(CANVARARG)
190 1.1 cgd
191 1.1 cgd /*VARARGS ARGSUSED*/
192 1.1 cgd say(pat) char *pat; { ; }
193 1.1 cgd /*VARARGS ARGSUSED*/
194 1.1 cgd fatal(pat) char *pat; { ; }
195 1.1 cgd /*VARARGS ARGSUSED*/
196 1.1 cgd pfatal(pat) char *pat; { ; }
197 1.1 cgd /*VARARGS ARGSUSED*/
198 1.1 cgd ask(pat) char *pat; { ; }
199 1.1 cgd
200 1.1 cgd #else
201 1.1 cgd
202 1.1 cgd /* Vanilla terminal output (buffered). */
203 1.1 cgd
204 1.1 cgd void
205 1.5 christos #ifdef __STDC__
206 1.5 christos say(const char *pat, ...)
207 1.5 christos #else
208 1.5 christos say(va_alist)
209 1.5 christos va_dcl
210 1.5 christos #endif
211 1.1 cgd {
212 1.5 christos va_list ap;
213 1.5 christos #ifdef __STDC__
214 1.5 christos va_start(ap, pat);
215 1.5 christos #else
216 1.5 christos const char *pat;
217 1.5 christos
218 1.5 christos va_start(ap);
219 1.5 christos pat = va_arg(ap, const char *);
220 1.5 christos #endif
221 1.5 christos
222 1.5 christos vfprintf(stderr, pat, ap);
223 1.5 christos va_end(ap);
224 1.1 cgd Fflush(stderr);
225 1.1 cgd }
226 1.1 cgd
227 1.1 cgd /* Terminal output, pun intended. */
228 1.1 cgd
229 1.1 cgd void /* very void */
230 1.5 christos #ifdef __STDC__
231 1.5 christos fatal(const char *pat, ...)
232 1.5 christos #else
233 1.5 christos fatal(va_alist)
234 1.5 christos va_dcl
235 1.5 christos #endif
236 1.1 cgd {
237 1.5 christos va_list ap;
238 1.5 christos #ifdef __STDC__
239 1.5 christos va_start(ap, pat);
240 1.5 christos #else
241 1.5 christos const char *pat;
242 1.5 christos
243 1.5 christos va_start(ap);
244 1.5 christos pat = va_arg(ap, const char *);
245 1.5 christos #endif
246 1.5 christos
247 1.1 cgd fprintf(stderr, "patch: **** ");
248 1.5 christos vfprintf(stderr, pat, ap);
249 1.5 christos va_end(ap);
250 1.1 cgd my_exit(1);
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd /* Say something from patch, something from the system, then silence . . . */
254 1.1 cgd
255 1.1 cgd void /* very void */
256 1.5 christos #ifdef __STDC__
257 1.5 christos pfatal(const char *pat, ...)
258 1.5 christos #else
259 1.5 christos pfatal(va_alist)
260 1.5 christos va_dcl
261 1.5 christos #endif
262 1.1 cgd {
263 1.5 christos va_list ap;
264 1.1 cgd int errnum = errno;
265 1.5 christos #ifdef __STDC__
266 1.5 christos va_start(ap, pat);
267 1.5 christos #else
268 1.5 christos const char *pat;
269 1.1 cgd
270 1.5 christos va_start(ap);
271 1.5 christos pat = va_arg(ap, const char *);
272 1.5 christos #endif
273 1.5 christos
274 1.1 cgd fprintf(stderr, "patch: **** ");
275 1.5 christos vfprintf(stderr, pat, ap);
276 1.1 cgd fprintf(stderr, ": %s\n", strerror(errnum));
277 1.5 christos va_end(ap);
278 1.1 cgd my_exit(1);
279 1.1 cgd }
280 1.1 cgd /* Get a response from the user, somehow or other. */
281 1.1 cgd
282 1.1 cgd void
283 1.5 christos #ifdef __STDC__
284 1.5 christos ask(const char *pat, ...)
285 1.5 christos #else
286 1.5 christos ask(va_alist)
287 1.5 christos va_dcl
288 1.5 christos #endif
289 1.1 cgd {
290 1.1 cgd int ttyfd;
291 1.1 cgd int r;
292 1.1 cgd bool tty2 = isatty(2);
293 1.5 christos va_list ap;
294 1.5 christos #ifdef __STDC__
295 1.5 christos va_start(ap, pat);
296 1.5 christos #else
297 1.5 christos const char *pat;
298 1.5 christos
299 1.5 christos va_start(ap);
300 1.5 christos pat = va_arg(ap, const char *);
301 1.5 christos #endif
302 1.1 cgd
303 1.5 christos (void) vsprintf(buf, pat, ap);
304 1.5 christos va_end(ap);
305 1.1 cgd Fflush(stderr);
306 1.1 cgd write(2, buf, strlen(buf));
307 1.1 cgd if (tty2) { /* might be redirected to a file */
308 1.1 cgd r = read(2, buf, sizeof buf);
309 1.1 cgd }
310 1.1 cgd else if (isatty(1)) { /* this may be new file output */
311 1.1 cgd Fflush(stdout);
312 1.1 cgd write(1, buf, strlen(buf));
313 1.1 cgd r = read(1, buf, sizeof buf);
314 1.1 cgd }
315 1.1 cgd else if ((ttyfd = open("/dev/tty", 2)) >= 0 && isatty(ttyfd)) {
316 1.1 cgd /* might be deleted or unwriteable */
317 1.1 cgd write(ttyfd, buf, strlen(buf));
318 1.1 cgd r = read(ttyfd, buf, sizeof buf);
319 1.1 cgd Close(ttyfd);
320 1.1 cgd }
321 1.1 cgd else if (isatty(0)) { /* this is probably patch input */
322 1.1 cgd Fflush(stdin);
323 1.1 cgd write(0, buf, strlen(buf));
324 1.1 cgd r = read(0, buf, sizeof buf);
325 1.1 cgd }
326 1.1 cgd else { /* no terminal at all--default it */
327 1.1 cgd buf[0] = '\n';
328 1.1 cgd r = 1;
329 1.1 cgd }
330 1.1 cgd if (r <= 0)
331 1.1 cgd buf[0] = 0;
332 1.1 cgd else
333 1.1 cgd buf[r] = '\0';
334 1.1 cgd if (!tty2)
335 1.8 is say2("%s",buf);
336 1.1 cgd }
337 1.1 cgd #endif /* lint */
338 1.1 cgd
339 1.1 cgd /* How to handle certain events when not in a critical region. */
340 1.1 cgd
341 1.1 cgd void
342 1.9 kristerw set_signals(int reset)
343 1.1 cgd {
344 1.1 cgd #ifndef lint
345 1.1 cgd #ifdef VOIDSIG
346 1.5 christos static void (*hupval) __P((int)),(*intval) __P((int));
347 1.1 cgd #else
348 1.5 christos static int (*hupval) __P((int)),(*intval)__P((int));
349 1.1 cgd #endif
350 1.1 cgd
351 1.1 cgd if (!reset) {
352 1.1 cgd hupval = signal(SIGHUP, SIG_IGN);
353 1.1 cgd if (hupval != SIG_IGN)
354 1.1 cgd #ifdef VOIDSIG
355 1.1 cgd hupval = my_exit;
356 1.1 cgd #else
357 1.5 christos hupval = (int(*) __P((int)))my_exit;
358 1.1 cgd #endif
359 1.1 cgd intval = signal(SIGINT, SIG_IGN);
360 1.1 cgd if (intval != SIG_IGN)
361 1.1 cgd #ifdef VOIDSIG
362 1.1 cgd intval = my_exit;
363 1.1 cgd #else
364 1.5 christos intval = (int(*) __P((int)))my_exit;
365 1.1 cgd #endif
366 1.1 cgd }
367 1.1 cgd Signal(SIGHUP, hupval);
368 1.1 cgd Signal(SIGINT, intval);
369 1.1 cgd #endif
370 1.1 cgd }
371 1.1 cgd
372 1.1 cgd /* How to handle certain events when in a critical region. */
373 1.1 cgd
374 1.1 cgd void
375 1.1 cgd ignore_signals()
376 1.1 cgd {
377 1.1 cgd Signal(SIGHUP, SIG_IGN);
378 1.1 cgd Signal(SIGINT, SIG_IGN);
379 1.1 cgd }
380 1.1 cgd
381 1.1 cgd /* Make sure we'll have the directories to create a file.
382 1.1 cgd If `striplast' is TRUE, ignore the last element of `filename'. */
383 1.1 cgd
384 1.1 cgd void
385 1.9 kristerw makedirs(char *filename, bool striplast)
386 1.1 cgd {
387 1.1 cgd char tmpbuf[256];
388 1.9 kristerw char *s = tmpbuf;
389 1.1 cgd char *dirv[20]; /* Point to the NULs between elements. */
390 1.9 kristerw int i;
391 1.9 kristerw int dirvp = 0; /* Number of finished entries in dirv. */
392 1.1 cgd
393 1.1 cgd /* Copy `filename' into `tmpbuf' with a NUL instead of a slash
394 1.1 cgd between the directories. */
395 1.1 cgd while (*filename) {
396 1.1 cgd if (*filename == '/') {
397 1.1 cgd filename++;
398 1.1 cgd dirv[dirvp++] = s;
399 1.1 cgd *s++ = '\0';
400 1.1 cgd }
401 1.1 cgd else {
402 1.1 cgd *s++ = *filename++;
403 1.1 cgd }
404 1.1 cgd }
405 1.1 cgd *s = '\0';
406 1.1 cgd dirv[dirvp] = s;
407 1.1 cgd if (striplast)
408 1.1 cgd dirvp--;
409 1.1 cgd if (dirvp < 0)
410 1.1 cgd return;
411 1.1 cgd
412 1.1 cgd strcpy(buf, "mkdir");
413 1.1 cgd s = buf;
414 1.1 cgd for (i=0; i<=dirvp; i++) {
415 1.1 cgd struct stat sbuf;
416 1.1 cgd
417 1.1 cgd if (stat(tmpbuf, &sbuf) && errno == ENOENT) {
418 1.1 cgd while (*s) s++;
419 1.1 cgd *s++ = ' ';
420 1.1 cgd strcpy(s, tmpbuf);
421 1.1 cgd }
422 1.1 cgd *dirv[i] = '/';
423 1.1 cgd }
424 1.1 cgd if (s != buf)
425 1.1 cgd system(buf);
426 1.1 cgd }
427 1.1 cgd
428 1.1 cgd /* Make filenames more reasonable. */
429 1.1 cgd
430 1.1 cgd char *
431 1.9 kristerw fetchname(char *at, int strip_leading, int assume_exists)
432 1.1 cgd {
433 1.1 cgd char *fullname;
434 1.1 cgd char *name;
435 1.9 kristerw char *t;
436 1.1 cgd char tmpbuf[200];
437 1.1 cgd int sleading = strip_leading;
438 1.1 cgd
439 1.1 cgd if (!at)
440 1.1 cgd return Nullch;
441 1.6 christos while (isspace((unsigned char)*at))
442 1.1 cgd at++;
443 1.1 cgd #ifdef DEBUGGING
444 1.1 cgd if (debug & 128)
445 1.1 cgd say4("fetchname %s %d %d\n",at,strip_leading,assume_exists);
446 1.1 cgd #endif
447 1.7 sommerfe filename_is_dev_null = FALSE;
448 1.7 sommerfe if (strnEQ(at, "/dev/null", 9)) { /* so files can be created by diffing */
449 1.7 sommerfe filename_is_dev_null = TRUE;
450 1.1 cgd return Nullch; /* against /dev/null. */
451 1.7 sommerfe }
452 1.1 cgd name = fullname = t = savestr(at);
453 1.1 cgd
454 1.1 cgd /* Strip off up to `sleading' leading slashes and null terminate. */
455 1.6 christos for (; *t && !isspace((unsigned char)*t); t++)
456 1.1 cgd if (*t == '/')
457 1.1 cgd if (--sleading >= 0)
458 1.1 cgd name = t+1;
459 1.1 cgd *t = '\0';
460 1.1 cgd
461 1.1 cgd /* If no -p option was given (957 is the default value!),
462 1.1 cgd we were given a relative pathname,
463 1.1 cgd and the leading directories that we just stripped off all exist,
464 1.1 cgd put them back on. */
465 1.1 cgd if (strip_leading == 957 && name != fullname && *fullname != '/') {
466 1.1 cgd name[-1] = '\0';
467 1.1 cgd if (stat(fullname, &filestat) == 0 && S_ISDIR (filestat.st_mode)) {
468 1.1 cgd name[-1] = '/';
469 1.1 cgd name=fullname;
470 1.1 cgd }
471 1.1 cgd }
472 1.1 cgd
473 1.1 cgd name = savestr(name);
474 1.1 cgd free(fullname);
475 1.1 cgd
476 1.1 cgd if (stat(name, &filestat) && !assume_exists) {
477 1.1 cgd char *filebase = basename(name);
478 1.1 cgd int pathlen = filebase - name;
479 1.1 cgd
480 1.1 cgd /* Put any leading path into `tmpbuf'. */
481 1.1 cgd strncpy(tmpbuf, name, pathlen);
482 1.1 cgd
483 1.1 cgd #define try(f, a1, a2) (Sprintf(tmpbuf + pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0)
484 1.5 christos #define try1(f, a1) (Sprintf(tmpbuf + pathlen, f, a1), stat(tmpbuf, &filestat) == 0)
485 1.1 cgd if ( try("RCS/%s%s", filebase, RCSSUFFIX)
486 1.5 christos || try1("RCS/%s" , filebase)
487 1.1 cgd || try( "%s%s", filebase, RCSSUFFIX)
488 1.1 cgd || try("SCCS/%s%s", SCCSPREFIX, filebase)
489 1.1 cgd || try( "%s%s", SCCSPREFIX, filebase))
490 1.1 cgd return name;
491 1.1 cgd free(name);
492 1.1 cgd name = Nullch;
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd return name;
496 1.1 cgd }
497