util.c revision 1.11 1 1.11 kristerw /* $NetBSD: util.c,v 1.11 2002/03/11 23:29:02 kristerw Exp $ */
2 1.5 christos #include <sys/cdefs.h>
3 1.2 mycroft #ifndef lint
4 1.11 kristerw __RCSID("$NetBSD: util.c,v 1.11 2002/03/11 23:29:02 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.10 kristerw
13 1.5 christos #include <stdarg.h>
14 1.5 christos #include <stdlib.h>
15 1.5 christos #include <unistd.h>
16 1.5 christos #include <fcntl.h>
17 1.1 cgd
18 1.11 kristerw /*
19 1.11 kristerw * Rename a file, copying it if necessary.
20 1.11 kristerw */
21 1.1 cgd int
22 1.9 kristerw move_file(char *from, char *to)
23 1.1 cgd {
24 1.11 kristerw char bakname[512];
25 1.11 kristerw char *s;
26 1.11 kristerw int i;
27 1.11 kristerw int fromfd;
28 1.1 cgd
29 1.11 kristerw /* to stdout? */
30 1.1 cgd
31 1.11 kristerw if (strEQ(to, "-")) {
32 1.1 cgd #ifdef DEBUGGING
33 1.11 kristerw if (debug & 4)
34 1.11 kristerw say("Moving %s to stdout.\n", from);
35 1.1 cgd #endif
36 1.11 kristerw fromfd = open(from, 0);
37 1.11 kristerw if (fromfd < 0)
38 1.11 kristerw pfatal("internal error, can't reopen %s", from);
39 1.11 kristerw while ((i = read(fromfd, buf, sizeof buf)) > 0)
40 1.11 kristerw if (write(1, buf, i) != 1)
41 1.11 kristerw pfatal("write failed");
42 1.11 kristerw Close(fromfd);
43 1.11 kristerw return 0;
44 1.11 kristerw }
45 1.1 cgd
46 1.11 kristerw if (origprae) {
47 1.11 kristerw Strcpy(bakname, origprae);
48 1.11 kristerw Strcat(bakname, to);
49 1.11 kristerw } else {
50 1.1 cgd #ifndef NODIR
51 1.11 kristerw char *backupname = find_backup_file_name(to);
52 1.11 kristerw if (backupname == NULL)
53 1.11 kristerw fatal("out of memory\n");
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.11 kristerw * Allocate a unique area for a string.
173 1.11 kristerw */
174 1.1 cgd char *
175 1.9 kristerw savestr(char *s)
176 1.1 cgd {
177 1.11 kristerw char *rv;
178 1.11 kristerw char *t;
179 1.1 cgd
180 1.11 kristerw if (!s)
181 1.11 kristerw s = "Oops";
182 1.11 kristerw t = s;
183 1.11 kristerw while (*t++)
184 1.11 kristerw ;
185 1.11 kristerw rv = malloc(t - s);
186 1.11 kristerw if (rv == NULL) {
187 1.11 kristerw if (using_plan_a)
188 1.11 kristerw out_of_mem = TRUE;
189 1.11 kristerw else
190 1.11 kristerw fatal("out of memory\n");
191 1.11 kristerw } else {
192 1.11 kristerw t = rv;
193 1.11 kristerw while ((*t++ = *s++) != '\0');
194 1.11 kristerw }
195 1.11 kristerw return rv;
196 1.1 cgd }
197 1.1 cgd
198 1.11 kristerw /*
199 1.11 kristerw * Vanilla terminal output (buffered).
200 1.11 kristerw */
201 1.1 cgd void
202 1.5 christos say(const char *pat, ...)
203 1.1 cgd {
204 1.11 kristerw va_list ap;
205 1.11 kristerw va_start(ap, pat);
206 1.5 christos
207 1.11 kristerw vfprintf(stderr, pat, ap);
208 1.11 kristerw va_end(ap);
209 1.11 kristerw Fflush(stderr);
210 1.1 cgd }
211 1.1 cgd
212 1.11 kristerw /*
213 1.11 kristerw * Terminal output, pun intended.
214 1.11 kristerw */
215 1.1 cgd void /* very void */
216 1.5 christos fatal(const char *pat, ...)
217 1.1 cgd {
218 1.11 kristerw va_list ap;
219 1.11 kristerw va_start(ap, pat);
220 1.5 christos
221 1.11 kristerw fprintf(stderr, "patch: **** ");
222 1.11 kristerw vfprintf(stderr, pat, ap);
223 1.11 kristerw va_end(ap);
224 1.11 kristerw my_exit(1);
225 1.1 cgd }
226 1.1 cgd
227 1.11 kristerw /*
228 1.11 kristerw * Say something from patch, something from the system, then silence...
229 1.11 kristerw */
230 1.1 cgd void /* very void */
231 1.5 christos pfatal(const char *pat, ...)
232 1.1 cgd {
233 1.11 kristerw va_list ap;
234 1.11 kristerw int errnum = errno;
235 1.11 kristerw va_start(ap, pat);
236 1.5 christos
237 1.11 kristerw fprintf(stderr, "patch: **** ");
238 1.11 kristerw vfprintf(stderr, pat, ap);
239 1.11 kristerw fprintf(stderr, ": %s\n", strerror(errnum));
240 1.11 kristerw va_end(ap);
241 1.11 kristerw my_exit(1);
242 1.1 cgd }
243 1.1 cgd
244 1.11 kristerw /*
245 1.11 kristerw * Get a response from the user, somehow or other.
246 1.11 kristerw */
247 1.1 cgd void
248 1.5 christos ask(const char *pat, ...)
249 1.1 cgd {
250 1.11 kristerw int ttyfd;
251 1.11 kristerw int r;
252 1.11 kristerw bool tty2 = isatty(2);
253 1.11 kristerw va_list ap;
254 1.11 kristerw va_start(ap, pat);
255 1.11 kristerw
256 1.11 kristerw (void)vsprintf(buf, pat, ap);
257 1.11 kristerw va_end(ap);
258 1.11 kristerw Fflush(stderr);
259 1.11 kristerw write(2, buf, strlen(buf));
260 1.11 kristerw if (tty2) { /* might be redirected to a file */
261 1.11 kristerw r = read(2, buf, sizeof buf);
262 1.11 kristerw } else if (isatty(1)) { /* this may be new file output */
263 1.11 kristerw Fflush(stdout);
264 1.11 kristerw write(1, buf, strlen(buf));
265 1.11 kristerw r = read(1, buf, sizeof buf);
266 1.11 kristerw } else if ((ttyfd = open("/dev/tty", 2)) >= 0 && isatty(ttyfd)) {
267 1.1 cgd /* might be deleted or unwriteable */
268 1.11 kristerw write(ttyfd, buf, strlen(buf));
269 1.11 kristerw r = read(ttyfd, buf, sizeof buf);
270 1.11 kristerw Close(ttyfd);
271 1.11 kristerw } else if (isatty(0)) { /* this is probably patch input */
272 1.11 kristerw Fflush(stdin);
273 1.11 kristerw write(0, buf, strlen(buf));
274 1.11 kristerw r = read(0, buf, sizeof buf);
275 1.11 kristerw } else { /* no terminal at all--default it */
276 1.11 kristerw buf[0] = '\n';
277 1.11 kristerw r = 1;
278 1.11 kristerw }
279 1.11 kristerw if (r <= 0)
280 1.11 kristerw buf[0] = 0;
281 1.11 kristerw else
282 1.11 kristerw buf[r] = '\0';
283 1.11 kristerw if (!tty2)
284 1.11 kristerw say("%s", buf);
285 1.1 cgd }
286 1.1 cgd
287 1.11 kristerw /*
288 1.11 kristerw * How to handle certain events when not in a critical region.
289 1.11 kristerw */
290 1.1 cgd void
291 1.9 kristerw set_signals(int reset)
292 1.1 cgd {
293 1.11 kristerw static void (*hupval)(int);
294 1.11 kristerw static void (*intval)(int);
295 1.1 cgd
296 1.11 kristerw if (!reset) {
297 1.11 kristerw hupval = signal(SIGHUP, SIG_IGN);
298 1.11 kristerw if (hupval != SIG_IGN)
299 1.11 kristerw hupval = my_exit;
300 1.11 kristerw intval = signal(SIGINT, SIG_IGN);
301 1.11 kristerw if (intval != SIG_IGN)
302 1.11 kristerw intval = my_exit;
303 1.11 kristerw }
304 1.11 kristerw Signal(SIGHUP, hupval);
305 1.11 kristerw Signal(SIGINT, intval);
306 1.1 cgd }
307 1.1 cgd
308 1.11 kristerw /*
309 1.11 kristerw * How to handle certain events when in a critical region.
310 1.11 kristerw */
311 1.1 cgd void
312 1.1 cgd ignore_signals()
313 1.1 cgd {
314 1.11 kristerw Signal(SIGHUP, SIG_IGN);
315 1.11 kristerw Signal(SIGINT, SIG_IGN);
316 1.1 cgd }
317 1.1 cgd
318 1.11 kristerw /*
319 1.11 kristerw * Make sure we'll have the directories to create a file.
320 1.11 kristerw * If `striplast' is TRUE, ignore the last element of `filename'.
321 1.11 kristerw */
322 1.1 cgd void
323 1.9 kristerw makedirs(char *filename, bool striplast)
324 1.1 cgd {
325 1.11 kristerw char tmpbuf[256];
326 1.11 kristerw char *s = tmpbuf;
327 1.11 kristerw char *dirv[20]; /* Point to the NULs between elements. */
328 1.11 kristerw int i;
329 1.11 kristerw int dirvp = 0; /* Number of finished entries in dirv. */
330 1.11 kristerw
331 1.11 kristerw /*
332 1.11 kristerw * Copy `filename' into `tmpbuf' with a NUL instead of a slash
333 1.11 kristerw * between the directories.
334 1.11 kristerw */
335 1.11 kristerw while (*filename) {
336 1.11 kristerw if (*filename == '/') {
337 1.11 kristerw filename++;
338 1.11 kristerw dirv[dirvp++] = s;
339 1.11 kristerw *s++ = '\0';
340 1.11 kristerw } else {
341 1.11 kristerw *s++ = *filename++;
342 1.11 kristerw }
343 1.11 kristerw }
344 1.11 kristerw *s = '\0';
345 1.11 kristerw dirv[dirvp] = s;
346 1.11 kristerw if (striplast)
347 1.11 kristerw dirvp--;
348 1.11 kristerw if (dirvp < 0)
349 1.11 kristerw return;
350 1.11 kristerw
351 1.11 kristerw strcpy(buf, "mkdir");
352 1.11 kristerw s = buf;
353 1.11 kristerw for (i = 0; i <= dirvp; i++) {
354 1.11 kristerw struct stat sbuf;
355 1.11 kristerw
356 1.11 kristerw if (stat(tmpbuf, &sbuf) && errno == ENOENT) {
357 1.11 kristerw while (*s)
358 1.11 kristerw s++;
359 1.11 kristerw *s++ = ' ';
360 1.11 kristerw strcpy(s, tmpbuf);
361 1.11 kristerw }
362 1.11 kristerw *dirv[i] = '/';
363 1.11 kristerw }
364 1.11 kristerw if (s != buf)
365 1.11 kristerw system(buf);
366 1.1 cgd }
367 1.1 cgd
368 1.11 kristerw /*
369 1.11 kristerw * Make filenames more reasonable.
370 1.11 kristerw */
371 1.1 cgd char *
372 1.9 kristerw fetchname(char *at, int strip_leading, int assume_exists)
373 1.1 cgd {
374 1.11 kristerw char *fullname;
375 1.11 kristerw char *name;
376 1.11 kristerw char *t;
377 1.11 kristerw char tmpbuf[200];
378 1.11 kristerw int sleading = strip_leading;
379 1.11 kristerw
380 1.11 kristerw if (!at)
381 1.11 kristerw return NULL;
382 1.11 kristerw while (isspace((unsigned char)*at))
383 1.11 kristerw at++;
384 1.1 cgd #ifdef DEBUGGING
385 1.11 kristerw if (debug & 128)
386 1.11 kristerw say("fetchname %s %d %d\n", at, strip_leading, assume_exists);
387 1.1 cgd #endif
388 1.11 kristerw filename_is_dev_null = FALSE;
389 1.11 kristerw if (strnEQ(at, "/dev/null", 9)) {
390 1.11 kristerw /* So files can be created by diffing against /dev/null. */
391 1.11 kristerw filename_is_dev_null = TRUE;
392 1.11 kristerw return NULL;
393 1.11 kristerw }
394 1.11 kristerw name = fullname = t = savestr(at);
395 1.11 kristerw
396 1.11 kristerw /* Strip off up to `sleading' leading slashes and null terminate. */
397 1.11 kristerw for (; *t && !isspace((unsigned char)*t); t++)
398 1.11 kristerw if (*t == '/')
399 1.11 kristerw if (--sleading >= 0)
400 1.11 kristerw name = t + 1;
401 1.11 kristerw *t = '\0';
402 1.11 kristerw
403 1.11 kristerw /*
404 1.11 kristerw * If no -p option was given (957 is the default value!),
405 1.11 kristerw * we were given a relative pathname,
406 1.11 kristerw * and the leading directories that we just stripped off all exist,
407 1.11 kristerw * put them back on.
408 1.11 kristerw */
409 1.11 kristerw if (strip_leading == 957 && name != fullname && *fullname != '/') {
410 1.11 kristerw name[-1] = '\0';
411 1.11 kristerw if (stat(fullname, &filestat) == 0 &&
412 1.11 kristerw S_ISDIR(filestat.st_mode)) {
413 1.11 kristerw name[-1] = '/';
414 1.11 kristerw name = fullname;
415 1.11 kristerw }
416 1.11 kristerw }
417 1.11 kristerw
418 1.11 kristerw name = savestr(name);
419 1.11 kristerw free(fullname);
420 1.11 kristerw
421 1.11 kristerw if (stat(name, &filestat) && !assume_exists) {
422 1.11 kristerw char *filebase = basename(name);
423 1.11 kristerw int pathlen = filebase - name;
424 1.11 kristerw
425 1.11 kristerw /* Put any leading path into `tmpbuf'. */
426 1.11 kristerw strncpy(tmpbuf, name, pathlen);
427 1.11 kristerw
428 1.11 kristerw #define try(f, a1, a2) \
429 1.11 kristerw (Sprintf(tmpbuf + pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0)
430 1.11 kristerw #define try1(f, a1) \
431 1.11 kristerw (Sprintf(tmpbuf + pathlen, f, a1), stat(tmpbuf, &filestat) == 0)
432 1.11 kristerw if (try("RCS/%s%s", filebase, RCSSUFFIX) ||
433 1.11 kristerw try1("RCS/%s" , filebase) ||
434 1.11 kristerw try( "%s%s", filebase, RCSSUFFIX) ||
435 1.11 kristerw try("SCCS/%s%s", SCCSPREFIX, filebase) ||
436 1.11 kristerw try( "%s%s", SCCSPREFIX, filebase))
437 1.11 kristerw return name;
438 1.11 kristerw free(name);
439 1.11 kristerw name = NULL;
440 1.11 kristerw }
441 1.1 cgd
442 1.11 kristerw return name;
443 1.1 cgd }
444