1 /* $FreeBSD: src/gnu/usr.bin/patch/util.c,v 1.7.6.2 2002/04/30 20:40:02 gad Exp $ */
9 #include "backupfile.h"
11 void my_exit(int _status); /* in patch.c */
15 private_strerror (errnum)
18 extern char *sys_errlist[];
21 if (errnum > 0 && errnum <= sys_nerr)
22 return sys_errlist[errnum];
23 return "Unknown system error";
25 #define strerror private_strerror
26 #endif /* !HAVE_STRERROR */
28 /* Rename a file, copying it if necessary. */
31 move_file(char *from, char *to)
43 say2("Moving %s to stdout.\n", from);
45 fromfd = open(from, 0);
47 pfatal2("internal error, can't reopen %s", from);
48 while ((i=read(fromfd, buf, sizeof buf)) > 0)
49 if (write(1, buf, i) != 1)
50 pfatal1("write failed");
56 Strcpy(bakname, origprae);
60 char *backupname = find_backup_file_name(to);
61 if (backupname == (char *) 0)
62 fatal1("out of memory\n");
63 Strcpy(bakname, backupname);
67 Strcat(bakname, simple_backup_suffix);
71 if (stat(to, &filestat) == 0) { /* output file exists */
72 dev_t to_device = filestat.st_dev;
73 ino_t to_inode = filestat.st_ino;
74 char *simplename = bakname;
76 for (s=bakname; *s; s++) {
80 /* Find a backup name that is not the same file.
81 Change the first lowercase char into uppercase;
82 if that isn't sufficient, chop off the first char and try again. */
83 while (stat(bakname, &filestat) == 0 &&
84 to_device == filestat.st_dev && to_inode == filestat.st_ino) {
85 /* Skip initial non-lowercase chars. */
86 for (s=simplename; *s && !islower((unsigned char)*s); s++) ;
88 *s = toupper((unsigned char)*s);
90 Strcpy(simplename, simplename+1);
92 while (unlink(bakname) >= 0) ; /* while() is for benefit of Eunice */
95 say3("Moving %s to %s.\n", to, bakname);
97 if (rename(to, bakname) < 0) {
98 say4("Can't backup %s, output is in %s: %s\n", to, from,
102 while (unlink(to) >= 0) ;
106 say3("Moving %s to %s.\n", from, to);
108 if (rename(from, to) < 0) { /* different file system? */
111 tofd = creat(to, 0666);
113 say4("Can't create %s, output is in %s: %s\n",
114 to, from, strerror(errno));
117 fromfd = open(from, 0);
119 pfatal2("internal error, can't reopen %s", from);
120 while ((i=read(fromfd, buf, sizeof buf)) > 0)
121 if (write(tofd, buf, i) != i)
122 pfatal1("write failed");
133 copy_file(char *from, char *to)
139 tofd = creat(to, 0666);
141 pfatal2("can't create %s", to);
142 fromfd = open(from, 0);
144 pfatal2("internal error, can't reopen %s", from);
145 while ((i=read(fromfd, buf, sizeof buf)) > 0)
146 if (write(tofd, buf, i) != i)
147 pfatal2("write to %s failed", to);
152 /* Allocate a unique area for a string. */
164 rv = malloc((MEM) (t - s));
169 fatal1("out of memory\n");
173 while ((*t++ = *s++));
178 #if defined(lint) && defined(CANVARARG)
181 say(pat) char *pat; { ; }
183 fatal(pat) char *pat; { ; }
185 pfatal(pat) char *pat; { ; }
187 ask(pat) char *pat; { ; }
191 /* Vanilla terminal output (buffered). */
194 say(pat,arg1,arg2,arg3)
198 fprintf(stderr, pat, arg1, arg2, arg3);
202 /* Terminal output, pun intended. */
205 fatal(pat,arg1,arg2,arg3)
209 fprintf(stderr, "patch: **** ");
210 fprintf(stderr, pat, arg1, arg2, arg3);
214 /* Say something from patch, something from the system, then silence . . . */
217 pfatal(pat,arg1,arg2,arg3)
223 fprintf(stderr, "patch: **** ");
224 fprintf(stderr, pat, arg1, arg2, arg3);
225 fprintf(stderr, ": %s\n", strerror(errnum));
229 /* Get a response from the user, somehow or other. */
232 ask(pat,arg1,arg2,arg3)
238 bool tty2 = isatty(2);
240 Sprintf(buf, pat, arg1, arg2, arg3);
242 write(2, buf, strlen(buf));
243 if (tty2) { /* might be redirected to a file */
244 r = read(2, buf, sizeof buf);
246 else if (isatty(1)) { /* this may be new file output */
248 write(1, buf, strlen(buf));
249 r = read(1, buf, sizeof buf);
251 else if ((ttyfd = open(_PATH_TTY, 2)) >= 0 && isatty(ttyfd)) {
252 /* might be deleted or unwriteable */
253 write(ttyfd, buf, strlen(buf));
254 r = read(ttyfd, buf, sizeof buf);
257 else if (isatty(0)) { /* this is probably patch input */
259 write(0, buf, strlen(buf));
260 r = read(0, buf, sizeof buf);
262 else { /* no terminal at all--default it */
266 return 0; /* signal possible error */
276 return 0; /* if there was an error, return it */
282 /* How to handle certain events when not in a critical region. */
285 set_signals(int reset)
288 static RETSIGTYPE (*hupval)(),(*intval)();
291 hupval = signal(SIGHUP, SIG_IGN);
292 if (hupval != SIG_IGN)
293 hupval = (RETSIGTYPE(*)())my_exit;
294 intval = signal(SIGINT, SIG_IGN);
295 if (intval != SIG_IGN)
296 intval = (RETSIGTYPE(*)())my_exit;
298 Signal(SIGHUP, hupval);
299 Signal(SIGINT, intval);
303 /* How to handle certain events when in a critical region. */
309 Signal(SIGHUP, SIG_IGN);
310 Signal(SIGINT, SIG_IGN);
314 /* Make sure we'll have the directories to create a file.
315 If `striplast' is TRUE, ignore the last element of `filename'. */
318 makedirs(filename,striplast)
323 Reg2 char *s = tmpbuf;
324 char *dirv[20]; /* Point to the NULs between elements. */
326 Reg4 int dirvp = 0; /* Number of finished entries in dirv. */
328 /* Copy `filename' into `tmpbuf' with a NUL instead of a slash
329 between the directories. */
331 if (*filename == '/') {
347 strcpy(buf, "mkdir");
349 for (i=0; i<=dirvp; i++) {
352 if (stat(tmpbuf, &sbuf) && errno == ENOENT) {
363 /* Make filenames more reasonable. */
366 fetchname(char *at, int strip_leading, int assume_exists)
372 int sleading = strip_leading;
376 while (isspace((unsigned char)*at))
380 say4("fetchname %s %d %d\n",at,strip_leading,assume_exists);
382 if (strnEQ(at, _PATH_DEVNULL, sizeof _PATH_DEVNULL - 1)) /* so files can be created by diffing */
383 return Nullch; /* against /dev/null. */
384 name = fullname = t = savestr(at);
386 /* Strip off up to `sleading' leading slashes and null terminate. */
387 for (; *t && !isspace((unsigned char)*t); t++)
393 /* If no -p option was given (957 is the default value!),
394 we were given a relative pathname,
395 and the leading directories that we just stripped off all exist,
397 if (strip_leading == 957 && name != fullname && *fullname != '/') {
399 if (stat(fullname, &filestat) == 0 && S_ISDIR (filestat.st_mode)) {
405 name = savestr(name);
408 if (stat(name, &filestat) && !assume_exists) {
409 char *filebase = basename(name);
410 int pathlen = filebase - name;
412 /* Put any leading path into `tmpbuf'. */
413 strncpy(tmpbuf, name, pathlen);
415 #define try(f, a1, a2) (Sprintf(tmpbuf + pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0)
416 if ( try("RCS/%s%s", filebase, RCSSUFFIX)
417 || try("RCS/%s%s", filebase, "")
418 || try( "%s%s", filebase, RCSSUFFIX)
419 || try("SCCS/%s%s", SCCSPREFIX, filebase)
420 || try( "%s%s", SCCSPREFIX, filebase))
430 xmalloc(unsigned int size)
432 register char *p = (char *) malloc (size);
434 fatal("out of memory");