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