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