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