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