|
Lines 4-10
Link Here
|
| 4 |
#include "util.h" |
4 |
#include "util.h" |
| 5 |
#include "backupfile.h" |
5 |
#include "backupfile.h" |
| 6 |
|
6 |
|
|
|
7 |
#ifdef __GNUC__ |
| 8 |
void my_exit() __attribute__((noreturn)); |
| 9 |
#else |
| 7 |
void my_exit(); |
10 |
void my_exit(); |
|
|
11 |
#endif |
| 8 |
|
12 |
|
| 9 |
#ifndef HAVE_STRERROR |
13 |
#ifndef HAVE_STRERROR |
| 10 |
static char * |
14 |
static char * |
|
Lines 27-33
Link Here
|
| 27 |
move_file(from,to) |
31 |
move_file(from,to) |
| 28 |
char *from, *to; |
32 |
char *from, *to; |
| 29 |
{ |
33 |
{ |
| 30 |
char bakname[512]; |
34 |
char bakname[MAXPATHLEN]; |
| 31 |
Reg1 char *s; |
35 |
Reg1 char *s; |
| 32 |
Reg2 int i; |
36 |
Reg2 int i; |
| 33 |
Reg3 int fromfd; |
37 |
Reg3 int fromfd; |
|
Lines 39-45
Link Here
|
| 39 |
if (debug & 4) |
43 |
if (debug & 4) |
| 40 |
say2("Moving %s to stdout.\n", from); |
44 |
say2("Moving %s to stdout.\n", from); |
| 41 |
#endif |
45 |
#endif |
| 42 |
fromfd = open(from, 0); |
46 |
fromfd = open(from, O_RDONLY); |
| 43 |
if (fromfd < 0) |
47 |
if (fromfd < 0) |
| 44 |
pfatal2("internal error, can't reopen %s", from); |
48 |
pfatal2("internal error, can't reopen %s", from); |
| 45 |
while ((i=read(fromfd, buf, sizeof buf)) > 0) |
49 |
while ((i=read(fromfd, buf, sizeof buf)) > 0) |
|
Lines 50-63
Link Here
|
| 50 |
} |
54 |
} |
| 51 |
|
55 |
|
| 52 |
if (origprae) { |
56 |
if (origprae) { |
| 53 |
Strcpy(bakname, origprae); |
57 |
if (strlcpy(bakname, origprae, sizeof(bakname)) >= sizeof(bakname) || |
| 54 |
Strcat(bakname, to); |
58 |
strlcat(bakname, to, sizeof(bakname)) >= sizeof(bakname)) |
|
|
59 |
fatal2("filename %s too long for buffer\n", origprae); |
| 55 |
} else { |
60 |
} else { |
| 56 |
#ifndef NODIR |
61 |
#ifndef NODIR |
| 57 |
char *backupname = find_backup_file_name(to); |
62 |
char *backupname = find_backup_file_name(to); |
| 58 |
if (backupname == (char *) 0) |
63 |
if (backupname == (char *) 0) |
| 59 |
fatal1("out of memory\n"); |
64 |
fatal1("out of memory\n"); |
| 60 |
Strcpy(bakname, backupname); |
65 |
if (strlcpy(bakname, backupname, sizeof(bakname)) >= sizeof(bakname)) |
|
|
66 |
fatal2("filename %s too long for buffer\n", backupname); |
| 61 |
free(backupname); |
67 |
free(backupname); |
| 62 |
#else /* NODIR */ |
68 |
#else /* NODIR */ |
| 63 |
Strcpy(bakname, to); |
69 |
Strcpy(bakname, to); |
|
Lines 111-117
Link Here
|
| 111 |
to, from, strerror(errno)); |
117 |
to, from, strerror(errno)); |
| 112 |
return -1; |
118 |
return -1; |
| 113 |
} |
119 |
} |
| 114 |
fromfd = open(from, 0); |
120 |
fromfd = open(from, O_RDONLY); |
| 115 |
if (fromfd < 0) |
121 |
if (fromfd < 0) |
| 116 |
pfatal2("internal error, can't reopen %s", from); |
122 |
pfatal2("internal error, can't reopen %s", from); |
| 117 |
while ((i=read(fromfd, buf, sizeof buf)) > 0) |
123 |
while ((i=read(fromfd, buf, sizeof buf)) > 0) |
|
Lines 137-143
Link Here
|
| 137 |
tofd = creat(to, 0666); |
143 |
tofd = creat(to, 0666); |
| 138 |
if (tofd < 0) |
144 |
if (tofd < 0) |
| 139 |
pfatal2("can't create %s", to); |
145 |
pfatal2("can't create %s", to); |
| 140 |
fromfd = open(from, 0); |
146 |
fromfd = open(from, O_RDONLY); |
| 141 |
if (fromfd < 0) |
147 |
if (fromfd < 0) |
| 142 |
pfatal2("internal error, can't reopen %s", from); |
148 |
pfatal2("internal error, can't reopen %s", from); |
| 143 |
while ((i=read(fromfd, buf, sizeof buf)) > 0) |
149 |
while ((i=read(fromfd, buf, sizeof buf)) > 0) |
|
Lines 160-166
Link Here
|
| 160 |
s = "Oops"; |
166 |
s = "Oops"; |
| 161 |
t = s; |
167 |
t = s; |
| 162 |
while (*t++); |
168 |
while (*t++); |
| 163 |
rv = malloc((MEM) (t - s)); |
169 |
rv = (char *)malloc((MEM) (t - s)); |
| 164 |
if (rv == Nullch) { |
170 |
if (rv == Nullch) { |
| 165 |
if (using_plan_a) |
171 |
if (using_plan_a) |
| 166 |
out_of_mem = TRUE; |
172 |
out_of_mem = TRUE; |
|
Lines 236-242
Link Here
|
| 236 |
int r; |
242 |
int r; |
| 237 |
bool tty2 = isatty(2); |
243 |
bool tty2 = isatty(2); |
| 238 |
|
244 |
|
| 239 |
Sprintf(buf, pat, arg1, arg2, arg3); |
245 |
Snprintf(buf, sizeof buf, pat, arg1, arg2, arg3); |
| 240 |
Fflush(stderr); |
246 |
Fflush(stderr); |
| 241 |
write(2, buf, strlen(buf)); |
247 |
write(2, buf, strlen(buf)); |
| 242 |
if (tty2) { /* might be redirected to a file */ |
248 |
if (tty2) { /* might be redirected to a file */ |
|
Lines 247-253
Link Here
|
| 247 |
write(1, buf, strlen(buf)); |
253 |
write(1, buf, strlen(buf)); |
| 248 |
r = read(1, buf, sizeof buf); |
254 |
r = read(1, buf, sizeof buf); |
| 249 |
} |
255 |
} |
| 250 |
else if ((ttyfd = open("/dev/tty", 2)) >= 0 && isatty(ttyfd)) { |
256 |
else if ((ttyfd = open("/dev/tty", O_RDWR)) >= 0 && isatty(ttyfd)) { |
| 251 |
/* might be deleted or unwriteable */ |
257 |
/* might be deleted or unwriteable */ |
| 252 |
write(ttyfd, buf, strlen(buf)); |
258 |
write(ttyfd, buf, strlen(buf)); |
| 253 |
r = read(ttyfd, buf, sizeof buf); |
259 |
r = read(ttyfd, buf, sizeof buf); |
|
Lines 319-363
Link Here
|
| 319 |
Reg1 char *filename; |
325 |
Reg1 char *filename; |
| 320 |
bool striplast; |
326 |
bool striplast; |
| 321 |
{ |
327 |
{ |
| 322 |
char tmpbuf[256]; |
328 |
|
| 323 |
Reg2 char *s = tmpbuf; |
329 |
char *tmpbuf; |
| 324 |
char *dirv[20]; /* Point to the NULs between elements. */ |
330 |
|
| 325 |
Reg3 int i; |
331 |
if ((tmpbuf = strdup(filename)) == NULL) |
| 326 |
Reg4 int dirvp = 0; /* Number of finished entries in dirv. */ |
332 |
fatal1("out of memory\n"); |
| 327 |
|
333 |
|
| 328 |
/* Copy `filename' into `tmpbuf' with a NUL instead of a slash |
334 |
if (striplast) { |
| 329 |
between the directories. */ |
335 |
char *s = strrchr(tmpbuf, '/'); |
| 330 |
while (*filename) { |
336 |
if (s == NULL) |
| 331 |
if (*filename == '/') { |
337 |
return; /* nothing to be done */ |
| 332 |
filename++; |
338 |
*s = '\0'; |
| 333 |
dirv[dirvp++] = s; |
|
|
| 334 |
*s++ = '\0'; |
| 335 |
} |
| 336 |
else { |
| 337 |
*s++ = *filename++; |
| 338 |
} |
| 339 |
} |
| 340 |
*s = '\0'; |
| 341 |
dirv[dirvp] = s; |
| 342 |
if (striplast) |
| 343 |
dirvp--; |
| 344 |
if (dirvp < 0) |
| 345 |
return; |
| 346 |
|
| 347 |
strcpy(buf, "mkdir"); |
| 348 |
s = buf; |
| 349 |
for (i=0; i<=dirvp; i++) { |
| 350 |
struct stat sbuf; |
| 351 |
|
| 352 |
if (stat(tmpbuf, &sbuf) && errno == ENOENT) { |
| 353 |
while (*s) s++; |
| 354 |
*s++ = ' '; |
| 355 |
strcpy(s, tmpbuf); |
| 356 |
} |
| 357 |
*dirv[i] = '/'; |
| 358 |
} |
339 |
} |
| 359 |
if (s != buf) |
340 |
|
| 360 |
system(buf); |
341 |
strcpy(buf, "/bin/mkdir -p "); |
|
|
342 |
if (strlcat(buf, tmpbuf, sizeof(buf)) >= sizeof(buf)) |
| 343 |
fatal2("buffer too small to hold %.20s...\n", tmpbuf); |
| 344 |
|
| 345 |
if (system(buf)) |
| 346 |
pfatal2("%.40s failed", buf); |
| 361 |
} |
347 |
} |
| 362 |
|
348 |
|
| 363 |
/* Make filenames more reasonable. */ |
349 |
/* Make filenames more reasonable. */ |
|
Lines 374-380
Link Here
|
| 374 |
char tmpbuf[200]; |
360 |
char tmpbuf[200]; |
| 375 |
int sleading = strip_leading; |
361 |
int sleading = strip_leading; |
| 376 |
|
362 |
|
| 377 |
if (!at) |
363 |
if (!at || *at == '\0') |
| 378 |
return Nullch; |
364 |
return Nullch; |
| 379 |
while (isspace((unsigned char)*at)) |
365 |
while (isspace((unsigned char)*at)) |
| 380 |
at++; |
366 |
at++; |
|
Lines 410-426
Link Here
|
| 410 |
|
396 |
|
| 411 |
if (stat(name, &filestat) && !assume_exists) { |
397 |
if (stat(name, &filestat) && !assume_exists) { |
| 412 |
char *filebase = basename(name); |
398 |
char *filebase = basename(name); |
| 413 |
int pathlen = filebase - name; |
399 |
char *filedir = dirname(name); |
| 414 |
|
|
|
| 415 |
/* Put any leading path into `tmpbuf'. */ |
| 416 |
strncpy(tmpbuf, name, pathlen); |
| 417 |
|
400 |
|
| 418 |
#define try(f, a1, a2) (Sprintf(tmpbuf + pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0) |
401 |
#define try(f, a1, a2, a3) (Snprintf(tmpbuf, sizeof(tmpbuf), f, a1, a2, a3), \ |
| 419 |
if ( try("RCS/%s%s", filebase, RCSSUFFIX) |
402 |
stat(tmpbuf, &filestat) == 0) |
| 420 |
|| try("RCS/%s%s", filebase, "") |
403 |
if ( try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) |
| 421 |
|| try( "%s%s", filebase, RCSSUFFIX) |
404 |
|| try("%s/RCS/%s%s", filedir, filebase, "") |
| 422 |
|| try("SCCS/%s%s", SCCSPREFIX, filebase) |
405 |
|| try( "%s/%s%s", filedir, filebase, RCSSUFFIX) |
| 423 |
|| try( "%s%s", SCCSPREFIX, filebase)) |
406 |
|| try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) |
|
|
407 |
|| try( "%s/%s%s", filedir, SCCSPREFIX, filebase)) |
| 424 |
return name; |
408 |
return name; |
| 425 |
free(name); |
409 |
free(name); |
| 426 |
name = Nullch; |
410 |
name = Nullch; |