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 */
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, buf_size)) > 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++) {
82 * Find a backup name that is not the same file.
83 * Change the first lowercase char into uppercase;
84 * if that isn't sufficient, chop off the first char
87 while (stat(bakname, &filestat) == 0 &&
88 to_device == filestat.st_dev &&
89 to_inode == filestat.st_ino) {
90 /* Skip initial non-lowercase chars. */
91 for (s=simplename; *s && !islower((unsigned char)*s);
95 *s = toupper((unsigned char)*s);
97 Strcpy(simplename, simplename + 1);
99 while (unlink(bakname) >= 0)
100 ; /* while() is for benefit of Eunice */
103 say3("Moving %s to %s.\n", to, bakname);
105 if (rename(to, bakname) < 0) {
106 say4("Can't backup %s, output is in %s: %s\n", to, from,
110 while (unlink(to) >= 0)
115 say3("Moving %s to %s.\n", from, to);
117 if (rename(from, to) < 0) { /* different file system? */
120 tofd = creat(to, 0666);
122 say4("Can't create %s, output is in %s: %s\n",
123 to, from, strerror(errno));
126 fromfd = open(from, 0);
128 pfatal2("internal error, can't reopen %s", from);
129 while ((i = read(fromfd, buf, buf_size)) > 0)
130 if (write(tofd, buf, i) != i)
131 pfatal1("write failed");
143 copy_file(char *from, char *to)
149 tofd = creat(to, 0666);
151 pfatal2("can't create %s", to);
152 fromfd = open(from, 0);
154 pfatal2("internal error, can't reopen %s", from);
155 while ((i = read(fromfd, buf, buf_size)) > 0)
156 if (write(tofd, buf, i) != i)
157 pfatal2("write to %s failed", to);
163 * Allocate a unique area for a string.
176 rv = malloc((MEM) (t - s));
181 fatal1("out of memory\n");
184 while ((*t++ = *s++));
189 #if defined(lint) && defined(CANVARARG)
192 say(pat) char *pat; { ; }
194 fatal(pat) char *pat; { ; }
196 pfatal(pat) char *pat; { ; }
198 ask(pat) char *pat; { ; }
203 * Vanilla terminal output (buffered).
206 say(pat,arg1,arg2,arg3)
210 fprintf(stderr, pat, arg1, arg2, arg3);
215 * Terminal output, pun intended.
218 fatal(pat,arg1,arg2,arg3)
222 fprintf(stderr, "patch: **** ");
223 fprintf(stderr, pat, arg1, arg2, arg3);
228 * Say something from patch, something from the system, then silence...
231 pfatal(pat,arg1,arg2,arg3)
237 fprintf(stderr, "patch: **** ");
238 fprintf(stderr, pat, arg1, arg2, arg3);
239 fprintf(stderr, ": %s\n", strerror(errnum));
244 * Get a response from the user, somehow or other.
247 ask(pat,arg1,arg2,arg3)
253 bool tty2 = isatty(2);
255 Sprintf(buf, pat, arg1, arg2, arg3);
257 write(2, buf, strlen(buf));
258 if (tty2) { /* might be redirected to a file */
259 r = read(2, buf, buf_size);
260 } else if (isatty(1)) { /* this may be new file output */
262 write(1, buf, strlen(buf));
263 r = read(1, buf, buf_size);
264 } else if ((ttyfd = open(_PATH_TTY, 2)) >= 0 && isatty(ttyfd)) {
265 /* might be deleted or unwriteable */
266 write(ttyfd, buf, strlen(buf));
267 r = read(ttyfd, buf, buf_size);
269 } else if (isatty(0)) { /* this is probably patch input */
271 write(0, buf, strlen(buf));
272 r = read(0, buf, buf_size);
273 } else { /* no terminal at all--default it */
277 return 0; /* signal possible error */
287 return 0; /* if there was an error, return it */
294 * How to handle certain events when not in a critical region.
297 set_signals(int reset)
300 static RETSIGTYPE (*hupval)(),(*intval)();
303 hupval = signal(SIGHUP, SIG_IGN);
304 if (hupval != SIG_IGN)
305 hupval = (RETSIGTYPE(*)())my_exit;
306 intval = signal(SIGINT, SIG_IGN);
307 if (intval != SIG_IGN)
308 intval = (RETSIGTYPE(*)())my_exit;
310 Signal(SIGHUP, hupval);
311 Signal(SIGINT, intval);
316 * How to handle certain events when in a critical region.
322 Signal(SIGHUP, SIG_IGN);
323 Signal(SIGINT, SIG_IGN);
328 * Make sure we'll have the directories to create a file.
329 * If `striplast' is TRUE, ignore the last element of `filename'.
332 makedirs(filename,striplast)
337 Reg2 char *s = tmpbuf;
338 char *dirv[20]; /* Point to the NULs between elements. */
340 Reg4 int dirvp = 0; /* Number of finished entries in dirv. */
343 * Copy `filename' into `tmpbuf' with a NUL instead of a slash
344 * between the directories.
347 if (*filename == '/') {
362 strcpy(buf, "mkdir");
364 for (i = 0; i <= dirvp; i++) {
367 if (stat(tmpbuf, &sbuf) && errno == ENOENT) {
380 * Make filenames more reasonable.
383 fetchname(char *at, int strip_leading, int assume_exists)
389 int sleading = strip_leading;
393 while (isspace((unsigned char)*at))
397 say4("fetchname %s %d %d\n",at,strip_leading,assume_exists);
399 if (strnEQ(at, _PATH_DEVNULL, sizeof _PATH_DEVNULL - 1))
400 /* So files can be created by diffing against /dev/null. */
402 name = fullname = t = savestr(at);
404 /* Strip off up to `sleading' leading slashes and null terminate. */
405 for (; *t && !isspace((unsigned char)*t); t++)
412 * If no -p option was given (957 is the default value!),
413 * we were given a relative pathname,
414 * and the leading directories that we just stripped off all exist,
417 if (strip_leading == 957 && name != fullname && *fullname != '/') {
419 if (stat(fullname, &filestat) == 0 &&
420 S_ISDIR(filestat.st_mode)) {
426 name = savestr(name);
429 if (stat(name, &filestat) && !assume_exists) {
430 char *filebase = basename(name);
431 int pathlen = filebase - name;
433 /* Put any leading path into `tmpbuf'. */
434 strncpy(tmpbuf, name, pathlen);
436 #define try(f, a1, a2) \
437 (Sprintf(tmpbuf + pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0)
438 if (try("RCS/%s%s", filebase, RCSSUFFIX) ||
439 try("RCS/%s%s", filebase, "") ||
440 try( "%s%s", filebase, RCSSUFFIX) ||
441 try("SCCS/%s%s", SCCSPREFIX, filebase) ||
442 try( "%s%s", SCCSPREFIX, filebase))
452 xmalloc(unsigned int size)
454 register char *p = (char *) malloc (size);
456 fatal("out of memory");