1 /* $FreeBSD: src/gnu/usr.bin/patch/util.c,v 1.7.6.2 2002/04/30 20:40:02 gad Exp $ */
2 /* $DragonFly: src/gnu/usr.bin/patch/Attic/util.c,v 1.2 2003/06/17 04:25:47 dillon Exp $ */
10 #include "backupfile.h"
12 void my_exit(int _status); /* in patch.c */
16 private_strerror (errnum)
19 extern char *sys_errlist[];
22 if (errnum > 0 && errnum <= sys_nerr)
23 return sys_errlist[errnum];
24 return "Unknown system error";
26 #define strerror private_strerror
27 #endif /* !HAVE_STRERROR */
29 /* Rename a file, copying it if necessary. */
32 move_file(char *from, char *to)
44 say2("Moving %s to stdout.\n", from);
46 fromfd = open(from, 0);
48 pfatal2("internal error, can't reopen %s", from);
49 while ((i=read(fromfd, buf, sizeof buf)) > 0)
50 if (write(1, buf, i) != 1)
51 pfatal1("write failed");
57 Strcpy(bakname, origprae);
61 char *backupname = find_backup_file_name(to);
62 if (backupname == (char *) 0)
63 fatal1("out of memory\n");
64 Strcpy(bakname, backupname);
68 Strcat(bakname, simple_backup_suffix);
72 if (stat(to, &filestat) == 0) { /* output file exists */
73 dev_t to_device = filestat.st_dev;
74 ino_t to_inode = filestat.st_ino;
75 char *simplename = bakname;
77 for (s=bakname; *s; s++) {
81 /* Find a backup name that is not the same file.
82 Change the first lowercase char into uppercase;
83 if that isn't sufficient, chop off the first char and try again. */
84 while (stat(bakname, &filestat) == 0 &&
85 to_device == filestat.st_dev && to_inode == filestat.st_ino) {
86 /* Skip initial non-lowercase chars. */
87 for (s=simplename; *s && !islower((unsigned char)*s); s++) ;
89 *s = toupper((unsigned char)*s);
91 Strcpy(simplename, simplename+1);
93 while (unlink(bakname) >= 0) ; /* while() is for benefit of Eunice */
96 say3("Moving %s to %s.\n", to, bakname);
98 if (rename(to, bakname) < 0) {
99 say4("Can't backup %s, output is in %s: %s\n", to, from,
103 while (unlink(to) >= 0) ;
107 say3("Moving %s to %s.\n", from, to);
109 if (rename(from, to) < 0) { /* different file system? */
112 tofd = creat(to, 0666);
114 say4("Can't create %s, output is in %s: %s\n",
115 to, from, strerror(errno));
118 fromfd = open(from, 0);
120 pfatal2("internal error, can't reopen %s", from);
121 while ((i=read(fromfd, buf, sizeof buf)) > 0)
122 if (write(tofd, buf, i) != i)
123 pfatal1("write failed");
134 copy_file(char *from, char *to)
140 tofd = creat(to, 0666);
142 pfatal2("can't create %s", to);
143 fromfd = open(from, 0);
145 pfatal2("internal error, can't reopen %s", from);
146 while ((i=read(fromfd, buf, sizeof buf)) > 0)
147 if (write(tofd, buf, i) != i)
148 pfatal2("write to %s failed", to);
153 /* Allocate a unique area for a string. */
165 rv = malloc((MEM) (t - s));
170 fatal1("out of memory\n");
174 while ((*t++ = *s++));
179 #if defined(lint) && defined(CANVARARG)
182 say(pat) char *pat; { ; }
184 fatal(pat) char *pat; { ; }
186 pfatal(pat) char *pat; { ; }
188 ask(pat) char *pat; { ; }
192 /* Vanilla terminal output (buffered). */
195 say(pat,arg1,arg2,arg3)
199 fprintf(stderr, pat, arg1, arg2, arg3);
203 /* Terminal output, pun intended. */
206 fatal(pat,arg1,arg2,arg3)
210 fprintf(stderr, "patch: **** ");
211 fprintf(stderr, pat, arg1, arg2, arg3);
215 /* Say something from patch, something from the system, then silence . . . */
218 pfatal(pat,arg1,arg2,arg3)
224 fprintf(stderr, "patch: **** ");
225 fprintf(stderr, pat, arg1, arg2, arg3);
226 fprintf(stderr, ": %s\n", strerror(errnum));
230 /* Get a response from the user, somehow or other. */
233 ask(pat,arg1,arg2,arg3)
239 bool tty2 = isatty(2);
241 Sprintf(buf, pat, arg1, arg2, arg3);
243 write(2, buf, strlen(buf));
244 if (tty2) { /* might be redirected to a file */
245 r = read(2, buf, sizeof buf);
247 else if (isatty(1)) { /* this may be new file output */
249 write(1, buf, strlen(buf));
250 r = read(1, buf, sizeof buf);
252 else if ((ttyfd = open(_PATH_TTY, 2)) >= 0 && isatty(ttyfd)) {
253 /* might be deleted or unwriteable */
254 write(ttyfd, buf, strlen(buf));
255 r = read(ttyfd, buf, sizeof buf);
258 else if (isatty(0)) { /* this is probably patch input */
260 write(0, buf, strlen(buf));
261 r = read(0, buf, sizeof buf);
263 else { /* no terminal at all--default it */
267 return 0; /* signal possible error */
277 return 0; /* if there was an error, return it */
283 /* How to handle certain events when not in a critical region. */
286 set_signals(int reset)
289 static RETSIGTYPE (*hupval)(),(*intval)();
292 hupval = signal(SIGHUP, SIG_IGN);
293 if (hupval != SIG_IGN)
294 hupval = (RETSIGTYPE(*)())my_exit;
295 intval = signal(SIGINT, SIG_IGN);
296 if (intval != SIG_IGN)
297 intval = (RETSIGTYPE(*)())my_exit;
299 Signal(SIGHUP, hupval);
300 Signal(SIGINT, intval);
304 /* How to handle certain events when in a critical region. */
310 Signal(SIGHUP, SIG_IGN);
311 Signal(SIGINT, SIG_IGN);
315 /* Make sure we'll have the directories to create a file.
316 If `striplast' is TRUE, ignore the last element of `filename'. */
319 makedirs(filename,striplast)
324 Reg2 char *s = tmpbuf;
325 char *dirv[20]; /* Point to the NULs between elements. */
327 Reg4 int dirvp = 0; /* Number of finished entries in dirv. */
329 /* Copy `filename' into `tmpbuf' with a NUL instead of a slash
330 between the directories. */
332 if (*filename == '/') {
348 strcpy(buf, "mkdir");
350 for (i=0; i<=dirvp; i++) {
353 if (stat(tmpbuf, &sbuf) && errno == ENOENT) {
364 /* Make filenames more reasonable. */
367 fetchname(char *at, int strip_leading, int assume_exists)
373 int sleading = strip_leading;
377 while (isspace((unsigned char)*at))
381 say4("fetchname %s %d %d\n",at,strip_leading,assume_exists);
383 if (strnEQ(at, _PATH_DEVNULL, sizeof _PATH_DEVNULL - 1)) /* so files can be created by diffing */
384 return Nullch; /* against /dev/null. */
385 name = fullname = t = savestr(at);
387 /* Strip off up to `sleading' leading slashes and null terminate. */
388 for (; *t && !isspace((unsigned char)*t); t++)
394 /* If no -p option was given (957 is the default value!),
395 we were given a relative pathname,
396 and the leading directories that we just stripped off all exist,
398 if (strip_leading == 957 && name != fullname && *fullname != '/') {
400 if (stat(fullname, &filestat) == 0 && S_ISDIR (filestat.st_mode)) {
406 name = savestr(name);
409 if (stat(name, &filestat) && !assume_exists) {
410 char *filebase = basename(name);
411 int pathlen = filebase - name;
413 /* Put any leading path into `tmpbuf'. */
414 strncpy(tmpbuf, name, pathlen);
416 #define try(f, a1, a2) (Sprintf(tmpbuf + pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0)
417 if ( try("RCS/%s%s", filebase, RCSSUFFIX)
418 || try("RCS/%s%s", filebase, "")
419 || try( "%s%s", filebase, RCSSUFFIX)
420 || try("SCCS/%s%s", SCCSPREFIX, filebase)
421 || try( "%s%s", SCCSPREFIX, filebase))
431 xmalloc(unsigned int size)
433 register char *p = (char *) malloc (size);
435 fatal("out of memory");