Commit | Line | Data |
---|---|---|
f644bb55 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
f10a0ae9 | 11 | #endif /* not lint */ |
f644bb55 | 12 | |
654c61ee | 13 | #ifndef lint |
8b5119c0 | 14 | static char sccsid[] = "@(#)tar.c 5.12 (Berkeley) %G%"; |
f10a0ae9 | 15 | #endif /* not lint */ |
555a248b | 16 | |
1e5bf3dc KD |
17 | /* |
18 | * Tape Archival Program | |
19 | */ | |
415c8aef | 20 | #include <stdio.h> |
d7ab2ea5 | 21 | #include <sys/param.h> |
415c8aef | 22 | #include <sys/stat.h> |
654c61ee | 23 | #include <sys/dir.h> |
dfdd2236 | 24 | #include <sys/ioctl.h> |
06eff236 | 25 | #include <sys/mtio.h> |
828d5b76 | 26 | #include <sys/time.h> |
415c8aef | 27 | #include <signal.h> |
654c61ee | 28 | #include <errno.h> |
dd465ebe | 29 | #include <fcntl.h> |
415c8aef | 30 | |
415c8aef | 31 | #define TBLOCK 512 |
9547dafa | 32 | #define NBLOCK 20 |
415c8aef | 33 | #define NAMSIZ 100 |
555a248b | 34 | |
1e5bf3dc KD |
35 | #define writetape(b) writetbuf(b, 1) |
36 | #define min(a,b) ((a) < (b) ? (a) : (b)) | |
37 | #define max(a,b) ((a) > (b) ? (a) : (b)) | |
38 | ||
415c8aef BJ |
39 | union hblock { |
40 | char dummy[TBLOCK]; | |
41 | struct header { | |
42 | char name[NAMSIZ]; | |
43 | char mode[8]; | |
44 | char uid[8]; | |
45 | char gid[8]; | |
46 | char size[12]; | |
47 | char mtime[12]; | |
48 | char chksum[8]; | |
49 | char linkflag; | |
50 | char linkname[NAMSIZ]; | |
51 | } dbuf; | |
555a248b | 52 | }; |
415c8aef BJ |
53 | |
54 | struct linkbuf { | |
55 | ino_t inum; | |
56 | dev_t devnum; | |
57 | int count; | |
58 | char pathname[NAMSIZ]; | |
59 | struct linkbuf *nextp; | |
555a248b BJ |
60 | }; |
61 | ||
62 | union hblock dblock; | |
095da6e7 | 63 | union hblock *tbuf; |
555a248b BJ |
64 | struct linkbuf *ihead; |
65 | struct stat stbuf; | |
66 | ||
67 | int rflag; | |
8b5119c0 | 68 | int sflag; |
555a248b BJ |
69 | int xflag; |
70 | int vflag; | |
71 | int tflag; | |
72 | int cflag; | |
73 | int mflag; | |
74 | int fflag; | |
654c61ee | 75 | int iflag; |
555a248b BJ |
76 | int oflag; |
77 | int pflag; | |
78 | int wflag; | |
79 | int hflag; | |
8678ae71 | 80 | int Bflag; |
654c61ee | 81 | int Fflag; |
555a248b BJ |
82 | |
83 | int mt; | |
84 | int term; | |
85 | int chksum; | |
86 | int recno; | |
45a92ce3 JL |
87 | int first; |
88 | int prtlinkerr; | |
415c8aef | 89 | int freemem = 1; |
75115c46 | 90 | int nblock = 0; |
555a248b BJ |
91 | int onintr(); |
92 | int onquit(); | |
93 | int onhup(); | |
45a92ce3 | 94 | #ifdef notdef |
555a248b | 95 | int onterm(); |
45a92ce3 | 96 | #endif |
415c8aef BJ |
97 | |
98 | daddr_t low; | |
99 | daddr_t high; | |
555a248b | 100 | daddr_t bsrch(); |
415c8aef | 101 | |
ed4dfcb6 | 102 | FILE *vfile = stdout; |
415c8aef BJ |
103 | FILE *tfile; |
104 | char tname[] = "/tmp/tarXXXXXX"; | |
415c8aef | 105 | char *usefile; |
555a248b | 106 | char magtape[] = "/dev/rmt8"; |
415c8aef | 107 | char *malloc(); |
9c11b20b JL |
108 | long time(); |
109 | off_t lseek(); | |
110 | char *mktemp(); | |
555a248b | 111 | char *strcat(); |
dd465ebe | 112 | char *strcpy(); |
654c61ee | 113 | char *rindex(); |
5ee3622a | 114 | char *getcwd(); |
13fc8845 | 115 | char *getwd(); |
45a92ce3 | 116 | char *getmem(); |
415c8aef BJ |
117 | |
118 | main(argc, argv) | |
119 | int argc; | |
120 | char *argv[]; | |
121 | { | |
122 | char *cp; | |
415c8aef BJ |
123 | |
124 | if (argc < 2) | |
125 | usage(); | |
126 | ||
127 | tfile = NULL; | |
128 | usefile = magtape; | |
129 | argv[argc] = 0; | |
130 | argv++; | |
131 | for (cp = *argv++; *cp; cp++) | |
132 | switch(*cp) { | |
555a248b | 133 | |
415c8aef | 134 | case 'f': |
654c61ee SL |
135 | if (*argv == 0) { |
136 | fprintf(stderr, | |
137 | "tar: tapefile must be specified with 'f' option\n"); | |
138 | usage(); | |
139 | } | |
415c8aef BJ |
140 | usefile = *argv++; |
141 | fflag++; | |
415c8aef | 142 | break; |
555a248b | 143 | |
415c8aef BJ |
144 | case 'c': |
145 | cflag++; | |
146 | rflag++; | |
147 | break; | |
555a248b | 148 | |
415c8aef BJ |
149 | case 'o': |
150 | oflag++; | |
151 | break; | |
555a248b | 152 | |
415c8aef BJ |
153 | case 'p': |
154 | pflag++; | |
155 | break; | |
555a248b | 156 | |
415c8aef BJ |
157 | case 'u': |
158 | mktemp(tname); | |
159 | if ((tfile = fopen(tname, "w")) == NULL) { | |
555a248b | 160 | fprintf(stderr, |
45a92ce3 | 161 | "tar: cannot create temporary file (%s)\n", |
555a248b | 162 | tname); |
415c8aef BJ |
163 | done(1); |
164 | } | |
165 | fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n"); | |
555a248b BJ |
166 | /*FALL THRU*/ |
167 | ||
415c8aef BJ |
168 | case 'r': |
169 | rflag++; | |
415c8aef | 170 | break; |
555a248b | 171 | |
8b5119c0 KB |
172 | case 's': |
173 | sflag++; | |
174 | break; | |
175 | ||
415c8aef BJ |
176 | case 'v': |
177 | vflag++; | |
178 | break; | |
555a248b | 179 | |
415c8aef BJ |
180 | case 'w': |
181 | wflag++; | |
182 | break; | |
555a248b | 183 | |
415c8aef BJ |
184 | case 'x': |
185 | xflag++; | |
186 | break; | |
555a248b | 187 | |
415c8aef BJ |
188 | case 't': |
189 | tflag++; | |
190 | break; | |
555a248b | 191 | |
415c8aef BJ |
192 | case 'm': |
193 | mflag++; | |
194 | break; | |
555a248b | 195 | |
415c8aef BJ |
196 | case '-': |
197 | break; | |
555a248b | 198 | |
415c8aef BJ |
199 | case '0': |
200 | case '1': | |
201 | case '4': | |
202 | case '5': | |
1e5bf3dc | 203 | case '7': |
415c8aef BJ |
204 | case '8': |
205 | magtape[8] = *cp; | |
206 | usefile = magtape; | |
207 | break; | |
555a248b | 208 | |
415c8aef | 209 | case 'b': |
095da6e7 SL |
210 | if (*argv == 0) { |
211 | fprintf(stderr, | |
212 | "tar: blocksize must be specified with 'b' option\n"); | |
213 | usage(); | |
214 | } | |
215 | nblock = atoi(*argv); | |
216 | if (nblock <= 0) { | |
217 | fprintf(stderr, | |
218 | "tar: invalid blocksize \"%s\"\n", *argv); | |
415c8aef BJ |
219 | done(1); |
220 | } | |
095da6e7 | 221 | argv++; |
415c8aef | 222 | break; |
555a248b | 223 | |
415c8aef | 224 | case 'l': |
45a92ce3 | 225 | prtlinkerr++; |
415c8aef | 226 | break; |
555a248b BJ |
227 | |
228 | case 'h': | |
229 | hflag++; | |
230 | break; | |
231 | ||
654c61ee SL |
232 | case 'i': |
233 | iflag++; | |
234 | break; | |
235 | ||
8678ae71 KM |
236 | case 'B': |
237 | Bflag++; | |
238 | break; | |
239 | ||
654c61ee SL |
240 | case 'F': |
241 | Fflag++; | |
242 | break; | |
243 | ||
415c8aef BJ |
244 | default: |
245 | fprintf(stderr, "tar: %c: unknown option\n", *cp); | |
246 | usage(); | |
247 | } | |
248 | ||
555a248b BJ |
249 | if (!rflag && !xflag && !tflag) |
250 | usage(); | |
415c8aef | 251 | if (rflag) { |
555a248b | 252 | if (cflag && tfile != NULL) |
415c8aef | 253 | usage(); |
415c8aef | 254 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
9c11b20b | 255 | (void) signal(SIGINT, onintr); |
415c8aef | 256 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) |
9c11b20b | 257 | (void) signal(SIGHUP, onhup); |
415c8aef | 258 | if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) |
9c11b20b | 259 | (void) signal(SIGQUIT, onquit); |
555a248b | 260 | #ifdef notdef |
415c8aef | 261 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) |
9c11b20b | 262 | (void) signal(SIGTERM, onterm); |
555a248b | 263 | #endif |
dd465ebe | 264 | mt = openmt(usefile, 1); |
415c8aef | 265 | dorep(argv); |
555a248b | 266 | done(0); |
415c8aef | 267 | } |
dd465ebe | 268 | mt = openmt(usefile, 0); |
555a248b BJ |
269 | if (xflag) |
270 | doxtract(argv); | |
415c8aef | 271 | else |
9c11b20b | 272 | dotable(argv); |
415c8aef BJ |
273 | done(0); |
274 | } | |
275 | ||
276 | usage() | |
277 | { | |
555a248b | 278 | fprintf(stderr, |
1e5bf3dc | 279 | "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); |
415c8aef BJ |
280 | done(1); |
281 | } | |
282 | ||
dd465ebe KM |
283 | int |
284 | openmt(tape, writing) | |
285 | char *tape; | |
286 | int writing; | |
287 | { | |
dd465ebe KM |
288 | |
289 | if (strcmp(tape, "-") == 0) { | |
290 | /* | |
291 | * Read from standard input or write to standard output. | |
292 | */ | |
293 | if (writing) { | |
294 | if (cflag == 0) { | |
295 | fprintf(stderr, | |
296 | "tar: can only create standard output archives\n"); | |
297 | done(1); | |
298 | } | |
299 | vfile = stderr; | |
300 | setlinebuf(vfile); | |
301 | mt = dup(1); | |
302 | } else { | |
303 | mt = dup(0); | |
304 | Bflag++; | |
305 | } | |
306 | } else { | |
307 | /* | |
308 | * Use file or tape on local machine. | |
309 | */ | |
310 | if (writing) { | |
311 | if (cflag) | |
9c11b20b | 312 | mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 0666); |
dd465ebe KM |
313 | else |
314 | mt = open(tape, O_RDWR); | |
315 | } else | |
316 | mt = open(tape, O_RDONLY); | |
317 | if (mt < 0) { | |
318 | fprintf(stderr, "tar: "); | |
319 | perror(tape); | |
320 | done(1); | |
321 | } | |
322 | } | |
323 | return(mt); | |
324 | } | |
325 | ||
415c8aef | 326 | dorep(argv) |
555a248b | 327 | char *argv[]; |
415c8aef BJ |
328 | { |
329 | register char *cp, *cp2; | |
f21b792d | 330 | char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent; |
415c8aef BJ |
331 | |
332 | if (!cflag) { | |
333 | getdir(); | |
334 | do { | |
335 | passtape(); | |
336 | if (term) | |
337 | done(0); | |
338 | getdir(); | |
339 | } while (!endtape()); | |
095da6e7 | 340 | backtape(); |
415c8aef BJ |
341 | if (tfile != NULL) { |
342 | char buf[200]; | |
343 | ||
6521d648 | 344 | (void)sprintf(buf, |
555a248b | 345 | "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", |
415c8aef BJ |
346 | tname, tname, tname, tname, tname, tname); |
347 | fflush(tfile); | |
348 | system(buf); | |
349 | freopen(tname, "r", tfile); | |
350 | fstat(fileno(tfile), &stbuf); | |
351 | high = stbuf.st_size; | |
352 | } | |
353 | } | |
354 | ||
5ee3622a | 355 | (void) getcwd(wdir); |
415c8aef BJ |
356 | while (*argv && ! term) { |
357 | cp2 = *argv; | |
358 | if (!strcmp(cp2, "-C") && argv[1]) { | |
359 | argv++; | |
dd465ebe KM |
360 | if (chdir(*argv) < 0) { |
361 | fprintf(stderr, "tar: can't change directories to "); | |
415c8aef | 362 | perror(*argv); |
dd465ebe | 363 | } else |
5ee3622a | 364 | (void) getcwd(wdir); |
415c8aef BJ |
365 | argv++; |
366 | continue; | |
367 | } | |
f21b792d | 368 | parent = wdir; |
415c8aef BJ |
369 | for (cp = *argv; *cp; cp++) |
370 | if (*cp == '/') | |
371 | cp2 = cp; | |
372 | if (cp2 != *argv) { | |
373 | *cp2 = '\0'; | |
f21b792d | 374 | if (chdir(*argv) < 0) { |
dd465ebe | 375 | fprintf(stderr, "tar: can't change directories to "); |
f21b792d SL |
376 | perror(*argv); |
377 | continue; | |
378 | } | |
5ee3622a | 379 | parent = getcwd(tempdir); |
415c8aef BJ |
380 | *cp2 = '/'; |
381 | cp2++; | |
382 | } | |
f21b792d | 383 | putfile(*argv++, cp2, parent); |
3bda777c | 384 | if (chdir(wdir) < 0) { |
45a92ce3 | 385 | fprintf(stderr, "tar: cannot change back?: "); |
3bda777c KM |
386 | perror(wdir); |
387 | } | |
415c8aef BJ |
388 | } |
389 | putempty(); | |
390 | putempty(); | |
391 | flushtape(); | |
45a92ce3 | 392 | if (prtlinkerr == 0) |
555a248b BJ |
393 | return; |
394 | for (; ihead != NULL; ihead = ihead->nextp) { | |
395 | if (ihead->count == 0) | |
396 | continue; | |
095da6e7 | 397 | fprintf(stderr, "tar: missing links to %s\n", ihead->pathname); |
555a248b | 398 | } |
415c8aef BJ |
399 | } |
400 | ||
401 | endtape() | |
402 | { | |
1e5bf3dc | 403 | return (dblock.dbuf.name[0] == '\0'); |
415c8aef BJ |
404 | } |
405 | ||
406 | getdir() | |
407 | { | |
408 | register struct stat *sp; | |
409 | int i; | |
410 | ||
654c61ee | 411 | top: |
555a248b | 412 | readtape((char *)&dblock); |
415c8aef BJ |
413 | if (dblock.dbuf.name[0] == '\0') |
414 | return; | |
415 | sp = &stbuf; | |
416 | sscanf(dblock.dbuf.mode, "%o", &i); | |
417 | sp->st_mode = i; | |
418 | sscanf(dblock.dbuf.uid, "%o", &i); | |
419 | sp->st_uid = i; | |
420 | sscanf(dblock.dbuf.gid, "%o", &i); | |
421 | sp->st_gid = i; | |
422 | sscanf(dblock.dbuf.size, "%lo", &sp->st_size); | |
423 | sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime); | |
424 | sscanf(dblock.dbuf.chksum, "%o", &chksum); | |
654c61ee | 425 | if (chksum != (i = checksum())) { |
095da6e7 | 426 | fprintf(stderr, "tar: directory checksum error (%d != %d)\n", |
654c61ee SL |
427 | chksum, i); |
428 | if (iflag) | |
429 | goto top; | |
415c8aef BJ |
430 | done(2); |
431 | } | |
8b5119c0 KB |
432 | /* strip off leading "/" if present */ |
433 | if (sflag && dblock.dbuf.name[0] == '/') { | |
434 | register char *cp1, *cp2; | |
435 | for (cp1 = cp2 = dblock.dbuf.name; *cp2 && *cp2 == '/'; ++cp2); | |
436 | if (!*cp2) | |
437 | goto top; | |
438 | while (*cp1++ = *cp2++); | |
439 | } | |
415c8aef BJ |
440 | if (tfile != NULL) |
441 | fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime); | |
442 | } | |
443 | ||
444 | passtape() | |
445 | { | |
446 | long blocks; | |
1e5bf3dc | 447 | char *bufp; |
415c8aef BJ |
448 | |
449 | if (dblock.dbuf.linkflag == '1') | |
450 | return; | |
451 | blocks = stbuf.st_size; | |
452 | blocks += TBLOCK-1; | |
453 | blocks /= TBLOCK; | |
454 | ||
1e5bf3dc | 455 | while (blocks-- > 0) |
9c11b20b | 456 | (void) readtbuf(&bufp, TBLOCK); |
415c8aef BJ |
457 | } |
458 | ||
f21b792d | 459 | putfile(longname, shortname, parent) |
555a248b BJ |
460 | char *longname; |
461 | char *shortname; | |
f21b792d | 462 | char *parent; |
415c8aef | 463 | { |
1e5bf3dc KD |
464 | int infile = 0; |
465 | long blocks; | |
415c8aef | 466 | char buf[TBLOCK]; |
1e5bf3dc | 467 | char *bigbuf; |
45a92ce3 | 468 | register char *cp; |
aeb5c9b4 KM |
469 | struct direct *dp; |
470 | DIR *dirp; | |
9c11b20b JL |
471 | register int i; |
472 | long l; | |
f21b792d | 473 | char newparent[NAMSIZ+64]; |
1e5bf3dc KD |
474 | int maxread; |
475 | int hint; /* amount to write to get "in sync" */ | |
415c8aef | 476 | |
f21b792d | 477 | if (!hflag) |
654c61ee SL |
478 | i = lstat(shortname, &stbuf); |
479 | else | |
480 | i = stat(shortname, &stbuf); | |
481 | if (i < 0) { | |
dd465ebe KM |
482 | fprintf(stderr, "tar: "); |
483 | perror(longname); | |
f21b792d SL |
484 | return; |
485 | } | |
654c61ee | 486 | if (tfile != NULL && checkupdate(longname) == 0) |
415c8aef | 487 | return; |
654c61ee SL |
488 | if (checkw('r', longname) == 0) |
489 | return; | |
490 | if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0) | |
415c8aef | 491 | return; |
415c8aef | 492 | |
654c61ee SL |
493 | switch (stbuf.st_mode & S_IFMT) { |
494 | case S_IFDIR: | |
555a248b BJ |
495 | for (i = 0, cp = buf; *cp++ = longname[i++];) |
496 | ; | |
415c8aef BJ |
497 | *--cp = '/'; |
498 | *++cp = 0 ; | |
415c8aef | 499 | if (!oflag) { |
555a248b | 500 | if ((cp - buf) >= NAMSIZ) { |
095da6e7 SL |
501 | fprintf(stderr, "tar: %s: file name too long\n", |
502 | longname); | |
555a248b BJ |
503 | return; |
504 | } | |
505 | stbuf.st_size = 0; | |
506 | tomodes(&stbuf); | |
507 | strcpy(dblock.dbuf.name,buf); | |
6521d648 | 508 | (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); |
9c11b20b | 509 | (void) writetape((char *)&dblock); |
415c8aef | 510 | } |
6521d648 | 511 | (void)sprintf(newparent, "%s/%s", parent, shortname); |
3bda777c KM |
512 | if (chdir(shortname) < 0) { |
513 | perror(shortname); | |
514 | return; | |
515 | } | |
aeb5c9b4 | 516 | if ((dirp = opendir(".")) == NULL) { |
095da6e7 SL |
517 | fprintf(stderr, "tar: %s: directory read error\n", |
518 | longname); | |
3bda777c | 519 | if (chdir(parent) < 0) { |
45a92ce3 | 520 | fprintf(stderr, "tar: cannot change back?: "); |
3bda777c KM |
521 | perror(parent); |
522 | } | |
aeb5c9b4 KM |
523 | return; |
524 | } | |
525 | while ((dp = readdir(dirp)) != NULL && !term) { | |
555a248b BJ |
526 | if (!strcmp(".", dp->d_name) || |
527 | !strcmp("..", dp->d_name)) | |
415c8aef | 528 | continue; |
aeb5c9b4 | 529 | strcpy(cp, dp->d_name); |
9c11b20b | 530 | l = telldir(dirp); |
aeb5c9b4 | 531 | closedir(dirp); |
f21b792d | 532 | putfile(buf, cp, newparent); |
aeb5c9b4 | 533 | dirp = opendir("."); |
9c11b20b | 534 | seekdir(dirp, l); |
415c8aef | 535 | } |
aeb5c9b4 | 536 | closedir(dirp); |
3bda777c | 537 | if (chdir(parent) < 0) { |
45a92ce3 | 538 | fprintf(stderr, "tar: cannot change back?: "); |
3bda777c KM |
539 | perror(parent); |
540 | } | |
654c61ee SL |
541 | break; |
542 | ||
543 | case S_IFLNK: | |
544 | tomodes(&stbuf); | |
545 | if (strlen(longname) >= NAMSIZ) { | |
095da6e7 SL |
546 | fprintf(stderr, "tar: %s: file name too long\n", |
547 | longname); | |
654c61ee SL |
548 | return; |
549 | } | |
550 | strcpy(dblock.dbuf.name, longname); | |
555a248b | 551 | if (stbuf.st_size + 1 >= NAMSIZ) { |
095da6e7 SL |
552 | fprintf(stderr, "tar: %s: symbolic link too long\n", |
553 | longname); | |
555a248b BJ |
554 | return; |
555 | } | |
f21b792d | 556 | i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); |
555a248b | 557 | if (i < 0) { |
dd465ebe | 558 | fprintf(stderr, "tar: can't read symbolic link "); |
f21b792d | 559 | perror(longname); |
555a248b BJ |
560 | return; |
561 | } | |
562 | dblock.dbuf.linkname[i] = '\0'; | |
563 | dblock.dbuf.linkflag = '2'; | |
45a92ce3 JL |
564 | if (vflag) |
565 | fprintf(vfile, "a %s symbolic link to %s\n", | |
566 | longname, dblock.dbuf.linkname); | |
6521d648 KB |
567 | (void)sprintf(dblock.dbuf.size, "%11lo", 0L); |
568 | (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); | |
9c11b20b | 569 | (void) writetape((char *)&dblock); |
654c61ee SL |
570 | break; |
571 | ||
572 | case S_IFREG: | |
573 | if ((infile = open(shortname, 0)) < 0) { | |
dd465ebe KM |
574 | fprintf(stderr, "tar: "); |
575 | perror(longname); | |
415c8aef BJ |
576 | return; |
577 | } | |
654c61ee SL |
578 | tomodes(&stbuf); |
579 | if (strlen(longname) >= NAMSIZ) { | |
095da6e7 SL |
580 | fprintf(stderr, "tar: %s: file name too long\n", |
581 | longname); | |
1e5bf3dc | 582 | close(infile); |
654c61ee SL |
583 | return; |
584 | } | |
585 | strcpy(dblock.dbuf.name, longname); | |
586 | if (stbuf.st_nlink > 1) { | |
587 | struct linkbuf *lp; | |
588 | int found = 0; | |
589 | ||
590 | for (lp = ihead; lp != NULL; lp = lp->nextp) | |
591 | if (lp->inum == stbuf.st_ino && | |
592 | lp->devnum == stbuf.st_dev) { | |
593 | found++; | |
594 | break; | |
595 | } | |
596 | if (found) { | |
597 | strcpy(dblock.dbuf.linkname, lp->pathname); | |
598 | dblock.dbuf.linkflag = '1'; | |
6521d648 | 599 | (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); |
9c11b20b | 600 | (void) writetape( (char *) &dblock); |
45a92ce3 JL |
601 | if (vflag) |
602 | fprintf(vfile, "a %s link to %s\n", | |
603 | longname, lp->pathname); | |
654c61ee SL |
604 | lp->count--; |
605 | close(infile); | |
606 | return; | |
607 | } | |
45a92ce3 JL |
608 | lp = (struct linkbuf *) getmem(sizeof(*lp)); |
609 | if (lp != NULL) { | |
654c61ee SL |
610 | lp->nextp = ihead; |
611 | ihead = lp; | |
612 | lp->inum = stbuf.st_ino; | |
613 | lp->devnum = stbuf.st_dev; | |
614 | lp->count = stbuf.st_nlink - 1; | |
615 | strcpy(lp->pathname, longname); | |
415c8aef BJ |
616 | } |
617 | } | |
1e5bf3dc | 618 | blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; |
45a92ce3 JL |
619 | if (vflag) |
620 | fprintf(vfile, "a %s %ld blocks\n", longname, blocks); | |
6521d648 | 621 | (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); |
1e5bf3dc KD |
622 | hint = writetape((char *)&dblock); |
623 | maxread = max(stbuf.st_blksize, (nblock * TBLOCK)); | |
9c11b20b | 624 | if ((bigbuf = malloc((unsigned)maxread)) == 0) { |
1e5bf3dc KD |
625 | maxread = TBLOCK; |
626 | bigbuf = buf; | |
627 | } | |
415c8aef | 628 | |
1e5bf3dc KD |
629 | while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 |
630 | && blocks > 0) { | |
631 | register int nblks; | |
632 | ||
633 | nblks = ((i-1)/TBLOCK)+1; | |
634 | if (nblks > blocks) | |
635 | nblks = blocks; | |
636 | hint = writetbuf(bigbuf, nblks); | |
637 | blocks -= nblks; | |
654c61ee | 638 | } |
1e5bf3dc KD |
639 | close(infile); |
640 | if (bigbuf != buf) | |
1e5bf3dc | 641 | free(bigbuf); |
dd465ebe | 642 | if (i < 0) { |
9c11b20b | 643 | fprintf(stderr, "tar: Read error on "); |
dd465ebe KM |
644 | perror(longname); |
645 | } else if (blocks != 0 || i != 0) | |
095da6e7 SL |
646 | fprintf(stderr, "tar: %s: file changed size\n", |
647 | longname); | |
654c61ee SL |
648 | while (--blocks >= 0) |
649 | putempty(); | |
650 | break; | |
651 | ||
652 | default: | |
653 | fprintf(stderr, "tar: %s is not a file. Not dumped\n", | |
095da6e7 | 654 | longname); |
654c61ee | 655 | break; |
415c8aef | 656 | } |
415c8aef BJ |
657 | } |
658 | ||
415c8aef | 659 | doxtract(argv) |
555a248b | 660 | char *argv[]; |
415c8aef | 661 | { |
f10a0ae9 | 662 | extern int errno; |
415c8aef | 663 | long blocks, bytes; |
9c11b20b | 664 | int ofile, i; |
415c8aef BJ |
665 | |
666 | for (;;) { | |
9c11b20b JL |
667 | if ((i = wantit(argv)) == 0) |
668 | continue; | |
669 | if (i == -1) | |
670 | break; /* end of tape */ | |
415c8aef BJ |
671 | if (checkw('x', dblock.dbuf.name) == 0) { |
672 | passtape(); | |
673 | continue; | |
674 | } | |
654c61ee SL |
675 | if (Fflag) { |
676 | char *s; | |
677 | ||
678 | if ((s = rindex(dblock.dbuf.name, '/')) == 0) | |
679 | s = dblock.dbuf.name; | |
680 | else | |
681 | s++; | |
682 | if (checkf(s, stbuf.st_mode, Fflag) == 0) { | |
683 | passtape(); | |
684 | continue; | |
685 | } | |
686 | } | |
45a92ce3 JL |
687 | if (checkdir(dblock.dbuf.name)) { /* have a directory */ |
688 | if (mflag == 0) | |
689 | dodirtimes(&dblock); | |
415c8aef | 690 | continue; |
45a92ce3 JL |
691 | } |
692 | if (dblock.dbuf.linkflag == '2') { /* symlink */ | |
5aee55a7 JB |
693 | /* |
694 | * only unlink non directories or empty | |
695 | * directories | |
696 | */ | |
697 | if (rmdir(dblock.dbuf.name) < 0) { | |
698 | if (errno == ENOTDIR) | |
699 | unlink(dblock.dbuf.name); | |
700 | } | |
555a248b | 701 | if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { |
dd465ebe | 702 | fprintf(stderr, "tar: %s: symbolic link failed: ", |
095da6e7 | 703 | dblock.dbuf.name); |
dd465ebe | 704 | perror(""); |
555a248b BJ |
705 | continue; |
706 | } | |
707 | if (vflag) | |
ed4dfcb6 | 708 | fprintf(vfile, "x %s symbolic link to %s\n", |
095da6e7 | 709 | dblock.dbuf.name, dblock.dbuf.linkname); |
75115c46 KD |
710 | #ifdef notdef |
711 | /* ignore alien orders */ | |
712 | chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); | |
45a92ce3 JL |
713 | if (mflag == 0) |
714 | setimes(dblock.dbuf.name, stbuf.st_mtime); | |
75115c46 KD |
715 | if (pflag) |
716 | chmod(dblock.dbuf.name, stbuf.st_mode & 07777); | |
717 | #endif | |
555a248b BJ |
718 | continue; |
719 | } | |
45a92ce3 | 720 | if (dblock.dbuf.linkflag == '1') { /* regular link */ |
5aee55a7 JB |
721 | /* |
722 | * only unlink non directories or empty | |
723 | * directories | |
724 | */ | |
725 | if (rmdir(dblock.dbuf.name) < 0) { | |
726 | if (errno == ENOTDIR) | |
727 | unlink(dblock.dbuf.name); | |
728 | } | |
415c8aef | 729 | if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { |
dd465ebe KM |
730 | fprintf(stderr, "tar: can't link %s to %s: ", |
731 | dblock.dbuf.name, dblock.dbuf.linkname); | |
732 | perror(""); | |
415c8aef BJ |
733 | continue; |
734 | } | |
735 | if (vflag) | |
45a92ce3 | 736 | fprintf(vfile, "%s linked to %s\n", |
095da6e7 | 737 | dblock.dbuf.name, dblock.dbuf.linkname); |
415c8aef BJ |
738 | continue; |
739 | } | |
555a248b | 740 | if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { |
dd465ebe | 741 | fprintf(stderr, "tar: can't create %s: ", |
095da6e7 | 742 | dblock.dbuf.name); |
dd465ebe | 743 | perror(""); |
415c8aef BJ |
744 | passtape(); |
745 | continue; | |
746 | } | |
1e5bf3dc | 747 | chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); |
415c8aef BJ |
748 | blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; |
749 | if (vflag) | |
45a92ce3 | 750 | fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", |
095da6e7 | 751 | dblock.dbuf.name, bytes, blocks); |
1e5bf3dc KD |
752 | for (; blocks > 0;) { |
753 | register int nread; | |
754 | char *bufp; | |
755 | register int nwant; | |
756 | ||
757 | nwant = NBLOCK*TBLOCK; | |
758 | if (nwant > (blocks*TBLOCK)) | |
759 | nwant = (blocks*TBLOCK); | |
760 | nread = readtbuf(&bufp, nwant); | |
45a92ce3 | 761 | if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { |
555a248b | 762 | fprintf(stderr, |
50dff114 | 763 | "tar: %s: HELP - extract write error: ", |
095da6e7 | 764 | dblock.dbuf.name); |
dd465ebe | 765 | perror(""); |
555a248b BJ |
766 | done(2); |
767 | } | |
1e5bf3dc KD |
768 | bytes -= nread; |
769 | blocks -= (((nread-1)/TBLOCK)+1); | |
415c8aef BJ |
770 | } |
771 | close(ofile); | |
45a92ce3 JL |
772 | if (mflag == 0) |
773 | setimes(dblock.dbuf.name, stbuf.st_mtime); | |
cd2661db | 774 | if (pflag) |
555a248b | 775 | chmod(dblock.dbuf.name, stbuf.st_mode & 07777); |
415c8aef | 776 | } |
45a92ce3 JL |
777 | if (mflag == 0) { |
778 | dblock.dbuf.name[0] = '\0'; /* process the whole stack */ | |
779 | dodirtimes(&dblock); | |
780 | } | |
415c8aef BJ |
781 | } |
782 | ||
9c11b20b JL |
783 | dotable(argv) |
784 | char *argv[]; | |
415c8aef | 785 | { |
9c11b20b JL |
786 | register int i; |
787 | ||
415c8aef | 788 | for (;;) { |
9c11b20b JL |
789 | if ((i = wantit(argv)) == 0) |
790 | continue; | |
791 | if (i == -1) | |
792 | break; /* end of tape */ | |
415c8aef BJ |
793 | if (vflag) |
794 | longt(&stbuf); | |
795 | printf("%s", dblock.dbuf.name); | |
796 | if (dblock.dbuf.linkflag == '1') | |
797 | printf(" linked to %s", dblock.dbuf.linkname); | |
555a248b BJ |
798 | if (dblock.dbuf.linkflag == '2') |
799 | printf(" symbolic link to %s", dblock.dbuf.linkname); | |
415c8aef BJ |
800 | printf("\n"); |
801 | passtape(); | |
802 | } | |
803 | } | |
804 | ||
805 | putempty() | |
806 | { | |
807 | char buf[TBLOCK]; | |
415c8aef | 808 | |
095da6e7 | 809 | bzero(buf, sizeof (buf)); |
9c11b20b | 810 | (void) writetape(buf); |
415c8aef BJ |
811 | } |
812 | ||
813 | longt(st) | |
555a248b | 814 | register struct stat *st; |
415c8aef BJ |
815 | { |
816 | register char *cp; | |
817 | char *ctime(); | |
818 | ||
819 | pmode(st); | |
820 | printf("%3d/%1d", st->st_uid, st->st_gid); | |
9c11b20b | 821 | printf("%7ld", st->st_size); |
415c8aef BJ |
822 | cp = ctime(&st->st_mtime); |
823 | printf(" %-12.12s %-4.4s ", cp+4, cp+20); | |
824 | } | |
825 | ||
826 | #define SUID 04000 | |
827 | #define SGID 02000 | |
828 | #define ROWN 0400 | |
829 | #define WOWN 0200 | |
830 | #define XOWN 0100 | |
831 | #define RGRP 040 | |
832 | #define WGRP 020 | |
833 | #define XGRP 010 | |
834 | #define ROTH 04 | |
835 | #define WOTH 02 | |
836 | #define XOTH 01 | |
837 | #define STXT 01000 | |
838 | int m1[] = { 1, ROWN, 'r', '-' }; | |
839 | int m2[] = { 1, WOWN, 'w', '-' }; | |
840 | int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; | |
841 | int m4[] = { 1, RGRP, 'r', '-' }; | |
842 | int m5[] = { 1, WGRP, 'w', '-' }; | |
843 | int m6[] = { 2, SGID, 's', XGRP, 'x', '-' }; | |
844 | int m7[] = { 1, ROTH, 'r', '-' }; | |
845 | int m8[] = { 1, WOTH, 'w', '-' }; | |
846 | int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; | |
847 | ||
848 | int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; | |
849 | ||
850 | pmode(st) | |
555a248b | 851 | register struct stat *st; |
415c8aef BJ |
852 | { |
853 | register int **mp; | |
854 | ||
855 | for (mp = &m[0]; mp < &m[9];) | |
dd465ebe | 856 | selectbits(*mp++, st); |
415c8aef BJ |
857 | } |
858 | ||
dd465ebe | 859 | selectbits(pairp, st) |
555a248b BJ |
860 | int *pairp; |
861 | struct stat *st; | |
415c8aef BJ |
862 | { |
863 | register int n, *ap; | |
864 | ||
865 | ap = pairp; | |
866 | n = *ap++; | |
867 | while (--n>=0 && (st->st_mode&*ap++)==0) | |
868 | ap++; | |
9c11b20b | 869 | putchar(*ap); |
415c8aef BJ |
870 | } |
871 | ||
45a92ce3 | 872 | /* |
5f11d309 JL |
873 | * Make all directories needed by `name'. If `name' is itself |
874 | * a directory on the tar tape (indicated by a trailing '/'), | |
45a92ce3 JL |
875 | * return 1; else 0. |
876 | */ | |
415c8aef | 877 | checkdir(name) |
555a248b | 878 | register char *name; |
415c8aef BJ |
879 | { |
880 | register char *cp; | |
555a248b | 881 | |
654c61ee | 882 | /* |
45a92ce3 | 883 | * Quick check for existence of directory. |
654c61ee SL |
884 | */ |
885 | if ((cp = rindex(name, '/')) == 0) | |
886 | return (0); | |
887 | *cp = '\0'; | |
45a92ce3 | 888 | if (access(name, 0) == 0) { /* already exists */ |
654c61ee | 889 | *cp = '/'; |
45a92ce3 | 890 | return (cp[1] == '\0'); /* return (lastchar == '/') */ |
654c61ee SL |
891 | } |
892 | *cp = '/'; | |
893 | ||
894 | /* | |
895 | * No luck, try to make all directories in path. | |
896 | */ | |
415c8aef | 897 | for (cp = name; *cp; cp++) { |
555a248b BJ |
898 | if (*cp != '/') |
899 | continue; | |
900 | *cp = '\0'; | |
654c61ee | 901 | if (access(name, 0) < 0) { |
13fc8845 SL |
902 | if (mkdir(name, 0777) < 0) { |
903 | perror(name); | |
654c61ee SL |
904 | *cp = '/'; |
905 | return (0); | |
415c8aef | 906 | } |
1e5bf3dc | 907 | chown(name, stbuf.st_uid, stbuf.st_gid); |
5f11d309 JL |
908 | if (pflag && cp[1] == '\0') /* dir on the tape */ |
909 | chmod(name, stbuf.st_mode & 07777); | |
415c8aef | 910 | } |
555a248b | 911 | *cp = '/'; |
415c8aef | 912 | } |
555a248b | 913 | return (cp[-1]=='/'); |
415c8aef BJ |
914 | } |
915 | ||
916 | onintr() | |
917 | { | |
9c11b20b | 918 | (void) signal(SIGINT, SIG_IGN); |
415c8aef BJ |
919 | term++; |
920 | } | |
921 | ||
922 | onquit() | |
923 | { | |
9c11b20b | 924 | (void) signal(SIGQUIT, SIG_IGN); |
415c8aef BJ |
925 | term++; |
926 | } | |
927 | ||
928 | onhup() | |
929 | { | |
9c11b20b | 930 | (void) signal(SIGHUP, SIG_IGN); |
415c8aef BJ |
931 | term++; |
932 | } | |
933 | ||
45a92ce3 | 934 | #ifdef notdef |
415c8aef BJ |
935 | onterm() |
936 | { | |
9c11b20b | 937 | (void) signal(SIGTERM, SIG_IGN); |
415c8aef BJ |
938 | term++; |
939 | } | |
45a92ce3 | 940 | #endif |
415c8aef BJ |
941 | |
942 | tomodes(sp) | |
943 | register struct stat *sp; | |
944 | { | |
945 | register char *cp; | |
946 | ||
947 | for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) | |
948 | *cp = '\0'; | |
6521d648 KB |
949 | (void)sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777); |
950 | (void)sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid); | |
951 | (void)sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid); | |
952 | (void)sprintf(dblock.dbuf.size, "%11lo ", sp->st_size); | |
953 | (void)sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); | |
415c8aef BJ |
954 | } |
955 | ||
956 | checksum() | |
957 | { | |
958 | register i; | |
959 | register char *cp; | |
960 | ||
555a248b BJ |
961 | for (cp = dblock.dbuf.chksum; |
962 | cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) | |
415c8aef BJ |
963 | *cp = ' '; |
964 | i = 0; | |
965 | for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) | |
966 | i += *cp; | |
555a248b | 967 | return (i); |
415c8aef BJ |
968 | } |
969 | ||
970 | checkw(c, name) | |
555a248b | 971 | char *name; |
415c8aef | 972 | { |
555a248b BJ |
973 | if (!wflag) |
974 | return (1); | |
975 | printf("%c ", c); | |
976 | if (vflag) | |
977 | longt(&stbuf); | |
978 | printf("%s: ", name); | |
979 | return (response() == 'y'); | |
415c8aef BJ |
980 | } |
981 | ||
982 | response() | |
983 | { | |
984 | char c; | |
985 | ||
986 | c = getchar(); | |
987 | if (c != '\n') | |
555a248b BJ |
988 | while (getchar() != '\n') |
989 | ; | |
990 | else | |
991 | c = 'n'; | |
992 | return (c); | |
415c8aef BJ |
993 | } |
994 | ||
654c61ee SL |
995 | checkf(name, mode, howmuch) |
996 | char *name; | |
997 | int mode, howmuch; | |
998 | { | |
999 | int l; | |
1000 | ||
ed4dfcb6 KD |
1001 | if ((mode & S_IFMT) == S_IFDIR){ |
1002 | if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) | |
1003 | return(0); | |
1004 | return(1); | |
1005 | } | |
654c61ee SL |
1006 | if ((l = strlen(name)) < 3) |
1007 | return (1); | |
1008 | if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') | |
1009 | return (0); | |
1010 | if (strcmp(name, "core") == 0 || | |
1011 | strcmp(name, "errs") == 0 || | |
1012 | (howmuch > 1 && strcmp(name, "a.out") == 0)) | |
1013 | return (0); | |
1014 | /* SHOULD CHECK IF IT IS EXECUTABLE */ | |
1015 | return (1); | |
1016 | } | |
1017 | ||
45a92ce3 | 1018 | /* Is the current file a new file, or the newest one of the same name? */ |
415c8aef | 1019 | checkupdate(arg) |
555a248b | 1020 | char *arg; |
415c8aef BJ |
1021 | { |
1022 | char name[100]; | |
555a248b | 1023 | long mtime; |
415c8aef BJ |
1024 | daddr_t seekp; |
1025 | daddr_t lookup(); | |
1026 | ||
1027 | rewind(tfile); | |
1028 | for (;;) { | |
1029 | if ((seekp = lookup(arg)) < 0) | |
555a248b | 1030 | return (1); |
415c8aef BJ |
1031 | fseek(tfile, seekp, 0); |
1032 | fscanf(tfile, "%s %lo", name, &mtime); | |
555a248b | 1033 | return (stbuf.st_mtime > mtime); |
415c8aef BJ |
1034 | } |
1035 | } | |
1036 | ||
1037 | done(n) | |
1038 | { | |
1e5bf3dc | 1039 | unlink(tname); |
415c8aef BJ |
1040 | exit(n); |
1041 | } | |
1042 | ||
9c11b20b JL |
1043 | /* |
1044 | * Do we want the next entry on the tape, i.e. is it selected? If | |
1045 | * not, skip over the entire entry. Return -1 if reached end of tape. | |
1046 | */ | |
1047 | wantit(argv) | |
1048 | char *argv[]; | |
1049 | { | |
1050 | register char **cp; | |
1051 | ||
1052 | getdir(); | |
1053 | if (endtape()) | |
1054 | return (-1); | |
1055 | if (*argv == 0) | |
1056 | return (1); | |
1057 | for (cp = argv; *cp; cp++) | |
1058 | if (prefix(*cp, dblock.dbuf.name)) | |
1059 | return (1); | |
1060 | passtape(); | |
1061 | return (0); | |
1062 | } | |
1063 | ||
45a92ce3 JL |
1064 | /* |
1065 | * Does s2 begin with the string s1, on a directory boundary? | |
1066 | */ | |
415c8aef | 1067 | prefix(s1, s2) |
555a248b | 1068 | register char *s1, *s2; |
415c8aef BJ |
1069 | { |
1070 | while (*s1) | |
1071 | if (*s1++ != *s2++) | |
555a248b | 1072 | return (0); |
415c8aef | 1073 | if (*s2) |
555a248b BJ |
1074 | return (*s2 == '/'); |
1075 | return (1); | |
415c8aef BJ |
1076 | } |
1077 | ||
415c8aef BJ |
1078 | #define N 200 |
1079 | int njab; | |
555a248b | 1080 | |
415c8aef BJ |
1081 | daddr_t |
1082 | lookup(s) | |
555a248b | 1083 | char *s; |
415c8aef BJ |
1084 | { |
1085 | register i; | |
1086 | daddr_t a; | |
1087 | ||
1088 | for(i=0; s[i]; i++) | |
555a248b | 1089 | if (s[i] == ' ') |
415c8aef BJ |
1090 | break; |
1091 | a = bsrch(s, i, low, high); | |
555a248b | 1092 | return (a); |
415c8aef BJ |
1093 | } |
1094 | ||
1095 | daddr_t | |
1096 | bsrch(s, n, l, h) | |
555a248b BJ |
1097 | daddr_t l, h; |
1098 | char *s; | |
415c8aef BJ |
1099 | { |
1100 | register i, j; | |
1101 | char b[N]; | |
1102 | daddr_t m, m1; | |
1103 | ||
1104 | njab = 0; | |
1105 | ||
1106 | loop: | |
555a248b | 1107 | if (l >= h) |
45a92ce3 | 1108 | return ((daddr_t) -1); |
415c8aef | 1109 | m = l + (h-l)/2 - N/2; |
555a248b | 1110 | if (m < l) |
415c8aef BJ |
1111 | m = l; |
1112 | fseek(tfile, m, 0); | |
1113 | fread(b, 1, N, tfile); | |
1114 | njab++; | |
1115 | for(i=0; i<N; i++) { | |
555a248b | 1116 | if (b[i] == '\n') |
415c8aef BJ |
1117 | break; |
1118 | m++; | |
1119 | } | |
555a248b | 1120 | if (m >= h) |
45a92ce3 | 1121 | return ((daddr_t) -1); |
415c8aef BJ |
1122 | m1 = m; |
1123 | j = i; | |
1124 | for(i++; i<N; i++) { | |
1125 | m1++; | |
555a248b | 1126 | if (b[i] == '\n') |
415c8aef BJ |
1127 | break; |
1128 | } | |
1129 | i = cmp(b+j, s, n); | |
555a248b | 1130 | if (i < 0) { |
415c8aef BJ |
1131 | h = m; |
1132 | goto loop; | |
1133 | } | |
555a248b | 1134 | if (i > 0) { |
415c8aef BJ |
1135 | l = m1; |
1136 | goto loop; | |
1137 | } | |
555a248b | 1138 | return (m); |
415c8aef BJ |
1139 | } |
1140 | ||
1141 | cmp(b, s, n) | |
555a248b | 1142 | char *b, *s; |
415c8aef BJ |
1143 | { |
1144 | register i; | |
1145 | ||
555a248b | 1146 | if (b[0] != '\n') |
1e5bf3dc | 1147 | exit(2); |
415c8aef | 1148 | for(i=0; i<n; i++) { |
555a248b BJ |
1149 | if (b[i+1] > s[i]) |
1150 | return (-1); | |
1151 | if (b[i+1] < s[i]) | |
1152 | return (1); | |
415c8aef | 1153 | } |
555a248b | 1154 | return (b[i+1] == ' '? 0 : -1); |
415c8aef BJ |
1155 | } |
1156 | ||
45a92ce3 | 1157 | readtape(buffer) |
555a248b | 1158 | char *buffer; |
1e5bf3dc KD |
1159 | { |
1160 | char *bufp; | |
1e5bf3dc | 1161 | |
45a92ce3 JL |
1162 | if (first == 0) |
1163 | getbuf(); | |
9c11b20b | 1164 | (void) readtbuf(&bufp, TBLOCK); |
1e5bf3dc KD |
1165 | bcopy(bufp, buffer, TBLOCK); |
1166 | return(TBLOCK); | |
1167 | } | |
1168 | ||
1169 | readtbuf(bufpp, size) | |
1170 | char **bufpp; | |
1171 | int size; | |
415c8aef | 1172 | { |
06eff236 | 1173 | register int i; |
415c8aef BJ |
1174 | |
1175 | if (recno >= nblock || first == 0) { | |
9c11b20b | 1176 | if ((i = bread(mt, (char *)tbuf, TBLOCK*nblock)) < 0) |
dd465ebe | 1177 | mterr("read", i, 3); |
415c8aef BJ |
1178 | if (first == 0) { |
1179 | if ((i % TBLOCK) != 0) { | |
095da6e7 | 1180 | fprintf(stderr, "tar: tape blocksize error\n"); |
415c8aef BJ |
1181 | done(3); |
1182 | } | |
1183 | i /= TBLOCK; | |
06eff236 | 1184 | if (i != nblock) { |
095da6e7 | 1185 | fprintf(stderr, "tar: blocksize = %d\n", i); |
415c8aef BJ |
1186 | nblock = i; |
1187 | } | |
75115c46 | 1188 | first = 1; |
415c8aef BJ |
1189 | } |
1190 | recno = 0; | |
1191 | } | |
1e5bf3dc KD |
1192 | if (size > ((nblock-recno)*TBLOCK)) |
1193 | size = (nblock-recno)*TBLOCK; | |
1194 | *bufpp = (char *)&tbuf[recno]; | |
1195 | recno += (size/TBLOCK); | |
1196 | return (size); | |
415c8aef BJ |
1197 | } |
1198 | ||
1e5bf3dc KD |
1199 | writetbuf(buffer, n) |
1200 | register char *buffer; | |
1201 | register int n; | |
415c8aef | 1202 | { |
dd465ebe | 1203 | int i; |
45a92ce3 JL |
1204 | |
1205 | if (first == 0) { | |
75115c46 KD |
1206 | getbuf(); |
1207 | first = 1; | |
1208 | } | |
415c8aef | 1209 | if (recno >= nblock) { |
9c11b20b | 1210 | i = write(mt, (char *)tbuf, TBLOCK*nblock); |
dd465ebe KM |
1211 | if (i != TBLOCK*nblock) |
1212 | mterr("write", i, 2); | |
415c8aef BJ |
1213 | recno = 0; |
1214 | } | |
1e5bf3dc KD |
1215 | |
1216 | /* | |
1217 | * Special case: We have an empty tape buffer, and the | |
1218 | * users data size is >= the tape block size: Avoid | |
1219 | * the bcopy and dma direct to tape. BIG WIN. Add the | |
1220 | * residual to the tape buffer. | |
1221 | */ | |
1222 | while (recno == 0 && n >= nblock) { | |
dd465ebe KM |
1223 | i = write(mt, buffer, TBLOCK*nblock); |
1224 | if (i != TBLOCK*nblock) | |
1225 | mterr("write", i, 2); | |
1e5bf3dc KD |
1226 | n -= nblock; |
1227 | buffer += (nblock * TBLOCK); | |
415c8aef | 1228 | } |
1e5bf3dc KD |
1229 | |
1230 | while (n-- > 0) { | |
1231 | bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); | |
1232 | buffer += TBLOCK; | |
1233 | if (recno >= nblock) { | |
9c11b20b | 1234 | i = write(mt, (char *)tbuf, TBLOCK*nblock); |
dd465ebe KM |
1235 | if (i != TBLOCK*nblock) |
1236 | mterr("write", i, 2); | |
1e5bf3dc KD |
1237 | recno = 0; |
1238 | } | |
1239 | } | |
1240 | ||
1241 | /* Tell the user how much to write to get in sync */ | |
1242 | return (nblock - recno); | |
415c8aef BJ |
1243 | } |
1244 | ||
1245 | backtape() | |
1246 | { | |
dd465ebe | 1247 | static int mtdev = 1; |
06eff236 | 1248 | static struct mtop mtop = {MTBSR, 1}; |
dd465ebe KM |
1249 | struct mtget mtget; |
1250 | ||
1251 | if (mtdev == 1) | |
9c11b20b | 1252 | mtdev = ioctl(mt, MTIOCGET, (char *)&mtget); |
06eff236 | 1253 | if (mtdev == 0) { |
9c11b20b | 1254 | if (ioctl(mt, MTIOCTOP, (char *)&mtop) < 0) { |
dd465ebe KM |
1255 | fprintf(stderr, "tar: tape backspace error: "); |
1256 | perror(""); | |
415c8aef BJ |
1257 | done(4); |
1258 | } | |
06eff236 | 1259 | } else |
45a92ce3 | 1260 | lseek(mt, (daddr_t) -TBLOCK*nblock, 1); |
06eff236 | 1261 | recno--; |
415c8aef BJ |
1262 | } |
1263 | ||
1264 | flushtape() | |
1265 | { | |
dd465ebe KM |
1266 | int i; |
1267 | ||
9c11b20b | 1268 | i = write(mt, (char *)tbuf, TBLOCK*nblock); |
dd465ebe KM |
1269 | if (i != TBLOCK*nblock) |
1270 | mterr("write", i, 2); | |
1271 | } | |
1272 | ||
1273 | mterr(operation, i, exitcode) | |
1274 | char *operation; | |
1275 | int i; | |
1276 | { | |
1277 | fprintf(stderr, "tar: tape %s error: ", operation); | |
1278 | if (i < 0) | |
1279 | perror(""); | |
1280 | else | |
1281 | fprintf(stderr, "unexpected EOF\n"); | |
1282 | done(exitcode); | |
415c8aef BJ |
1283 | } |
1284 | ||
8678ae71 KM |
1285 | bread(fd, buf, size) |
1286 | int fd; | |
1287 | char *buf; | |
1288 | int size; | |
1289 | { | |
1290 | int count; | |
1291 | static int lastread = 0; | |
1292 | ||
45a92ce3 JL |
1293 | if (!Bflag) |
1294 | return (read(fd, buf, size)); | |
75115c46 | 1295 | |
8678ae71 | 1296 | for (count = 0; count < size; count += lastread) { |
8c526af6 KM |
1297 | lastread = read(fd, buf, size - count); |
1298 | if (lastread <= 0) { | |
8678ae71 KM |
1299 | if (count > 0) |
1300 | return (count); | |
1301 | return (lastread); | |
1302 | } | |
8678ae71 KM |
1303 | buf += lastread; |
1304 | } | |
1305 | return (count); | |
1306 | } | |
5ee3622a SL |
1307 | |
1308 | char * | |
1309 | getcwd(buf) | |
1310 | char *buf; | |
1311 | { | |
5ee3622a SL |
1312 | if (getwd(buf) == NULL) { |
1313 | fprintf(stderr, "tar: %s\n", buf); | |
1e5bf3dc | 1314 | exit(1); |
5ee3622a SL |
1315 | } |
1316 | return (buf); | |
1317 | } | |
ed4dfcb6 KD |
1318 | |
1319 | getbuf() | |
1320 | { | |
75115c46 | 1321 | |
dd465ebe | 1322 | if (nblock == 0) { |
ed4dfcb6 KD |
1323 | fstat(mt, &stbuf); |
1324 | if ((stbuf.st_mode & S_IFMT) == S_IFCHR) | |
dd465ebe | 1325 | nblock = NBLOCK; |
ed4dfcb6 | 1326 | else { |
dd465ebe KM |
1327 | nblock = stbuf.st_blksize / TBLOCK; |
1328 | if (nblock == 0) | |
1329 | nblock = NBLOCK; | |
ed4dfcb6 KD |
1330 | } |
1331 | } | |
9c11b20b | 1332 | tbuf = (union hblock *)malloc((unsigned)nblock*TBLOCK); |
ed4dfcb6 KD |
1333 | if (tbuf == NULL) { |
1334 | fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", | |
1335 | nblock); | |
1336 | done(1); | |
1337 | } | |
1338 | } | |
75115c46 | 1339 | |
45a92ce3 | 1340 | /* |
5f11d309 JL |
1341 | * Save this directory and its mtime on the stack, popping and setting |
1342 | * the mtimes of any stacked dirs which aren't parents of this one. | |
1343 | * A null directory causes the entire stack to be unwound and set. | |
45a92ce3 | 1344 | * |
5f11d309 JL |
1345 | * Since all the elements of the directory "stack" share a common |
1346 | * prefix, we can make do with one string. We keep only the current | |
1347 | * directory path, with an associated array of mtime's, one for each | |
1348 | * '/' in the path. A negative mtime means no mtime. The mtime's are | |
1349 | * offset by one (first index 1, not 0) because calling this with a null | |
1350 | * directory causes mtime[0] to be set. | |
1351 | * | |
45a92ce3 JL |
1352 | * This stack algorithm is not guaranteed to work for tapes created |
1353 | * with the 'r' option, but the vast majority of tapes with | |
1354 | * directories are not. This avoids saving every directory record on | |
1355 | * the tape and setting all the times at the end. | |
1356 | */ | |
1357 | char dirstack[NAMSIZ]; | |
1358 | #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ | |
1359 | time_t mtime[NTIM]; | |
75115c46 | 1360 | |
45a92ce3 JL |
1361 | dodirtimes(hp) |
1362 | union hblock *hp; | |
75115c46 | 1363 | { |
45a92ce3 JL |
1364 | register char *p = dirstack; |
1365 | register char *q = hp->dbuf.name; | |
1366 | register int ndir = 0; | |
1367 | char *savp; | |
1368 | int savndir; | |
1369 | ||
1370 | /* Find common prefix */ | |
50dff114 | 1371 | while (*p == *q && *p) { |
45a92ce3 JL |
1372 | if (*p++ == '/') |
1373 | ++ndir; | |
1374 | q++; | |
1375 | } | |
75115c46 | 1376 | |
45a92ce3 JL |
1377 | savp = p; |
1378 | savndir = ndir; | |
1379 | while (*p) { | |
1380 | /* | |
1381 | * Not a child: unwind the stack, setting the times. | |
1382 | * The order we do this doesn't matter, so we go "forward." | |
1383 | */ | |
1384 | if (*p++ == '/') | |
1385 | if (mtime[++ndir] >= 0) { | |
1386 | *--p = '\0'; /* zap the slash */ | |
1387 | setimes(dirstack, mtime[ndir]); | |
1388 | *p++ = '/'; | |
1389 | } | |
1390 | } | |
1391 | p = savp; | |
1392 | ndir = savndir; | |
1393 | ||
1394 | /* Push this one on the "stack" */ | |
1395 | while (*p = *q++) /* append the rest of the new dir */ | |
1396 | if (*p++ == '/') | |
1397 | mtime[++ndir] = -1; | |
1398 | mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ | |
1399 | } | |
75115c46 | 1400 | |
45a92ce3 JL |
1401 | setimes(path, mt) |
1402 | char *path; | |
1403 | time_t mt; | |
1404 | { | |
1405 | struct timeval tv[2]; | |
1406 | ||
1407 | tv[0].tv_sec = time((time_t *) 0); | |
1408 | tv[1].tv_sec = mt; | |
1409 | tv[0].tv_usec = tv[1].tv_usec = 0; | |
dd465ebe KM |
1410 | if (utimes(path, tv) < 0) { |
1411 | fprintf(stderr, "tar: can't set time on %s: ", path); | |
1412 | perror(""); | |
1413 | } | |
45a92ce3 JL |
1414 | } |
1415 | ||
1416 | char * | |
1417 | getmem(size) | |
1418 | { | |
1419 | char *p = malloc((unsigned) size); | |
1420 | ||
1421 | if (p == NULL && freemem) { | |
1422 | fprintf(stderr, | |
1423 | "tar: out of memory, link and directory modtime info lost\n"); | |
1424 | freemem = 0; | |
75115c46 | 1425 | } |
45a92ce3 | 1426 | return (p); |
75115c46 | 1427 | } |