fix for close bug in tablet driver (from dagobah!bill)
[unix-history] / usr / src / old / tar / tar.c
CommitLineData
654c61ee 1#ifndef lint
07269bf2 2static char *sccsid = "@(#)tar.c 4.18 (Berkeley) %G%";
654c61ee 3#endif
555a248b
BJ
4
5/*
6 * Tape Archival Program
7 */
415c8aef 8#include <stdio.h>
d7ab2ea5 9#include <sys/param.h>
415c8aef 10#include <sys/stat.h>
654c61ee 11#include <sys/dir.h>
dfdd2236 12#include <sys/ioctl.h>
06eff236 13#include <sys/mtio.h>
828d5b76 14#include <sys/time.h>
415c8aef 15#include <signal.h>
654c61ee 16#include <errno.h>
415c8aef 17
415c8aef 18#define TBLOCK 512
9547dafa 19#define NBLOCK 20
415c8aef 20#define NAMSIZ 100
555a248b 21
415c8aef
BJ
22union hblock {
23 char dummy[TBLOCK];
24 struct header {
25 char name[NAMSIZ];
26 char mode[8];
27 char uid[8];
28 char gid[8];
29 char size[12];
30 char mtime[12];
31 char chksum[8];
32 char linkflag;
33 char linkname[NAMSIZ];
34 } dbuf;
555a248b 35};
415c8aef
BJ
36
37struct linkbuf {
38 ino_t inum;
39 dev_t devnum;
40 int count;
41 char pathname[NAMSIZ];
42 struct linkbuf *nextp;
555a248b
BJ
43};
44
45union hblock dblock;
095da6e7 46union hblock *tbuf;
555a248b
BJ
47struct linkbuf *ihead;
48struct stat stbuf;
49
50int rflag;
51int xflag;
52int vflag;
53int tflag;
54int cflag;
55int mflag;
56int fflag;
654c61ee 57int iflag;
555a248b
BJ
58int oflag;
59int pflag;
60int wflag;
61int hflag;
8678ae71 62int Bflag;
654c61ee 63int Fflag;
555a248b
BJ
64
65int mt;
66int term;
67int chksum;
68int recno;
69int first;
70int linkerrok;
415c8aef 71int freemem = 1;
06eff236 72int nblock = NBLOCK;
555a248b
BJ
73int onintr();
74int onquit();
75int onhup();
76int onterm();
415c8aef
BJ
77
78daddr_t low;
79daddr_t high;
555a248b 80daddr_t bsrch();
415c8aef
BJ
81
82FILE *tfile;
83char tname[] = "/tmp/tarXXXXXX";
415c8aef 84char *usefile;
555a248b 85char magtape[] = "/dev/rmt8";
415c8aef 86char *malloc();
555a248b
BJ
87char *sprintf();
88char *strcat();
654c61ee 89char *rindex();
5ee3622a 90char *getcwd();
13fc8845 91char *getwd();
415c8aef
BJ
92
93main(argc, argv)
94int argc;
95char *argv[];
96{
97 char *cp;
415c8aef
BJ
98
99 if (argc < 2)
100 usage();
101
102 tfile = NULL;
103 usefile = magtape;
104 argv[argc] = 0;
105 argv++;
106 for (cp = *argv++; *cp; cp++)
107 switch(*cp) {
555a248b 108
415c8aef 109 case 'f':
654c61ee
SL
110 if (*argv == 0) {
111 fprintf(stderr,
112 "tar: tapefile must be specified with 'f' option\n");
113 usage();
114 }
415c8aef
BJ
115 usefile = *argv++;
116 fflag++;
415c8aef 117 break;
555a248b 118
415c8aef
BJ
119 case 'c':
120 cflag++;
121 rflag++;
122 break;
555a248b 123
415c8aef
BJ
124 case 'o':
125 oflag++;
126 break;
555a248b 127
415c8aef
BJ
128 case 'p':
129 pflag++;
130 break;
555a248b 131
415c8aef
BJ
132 case 'u':
133 mktemp(tname);
134 if ((tfile = fopen(tname, "w")) == NULL) {
555a248b
BJ
135 fprintf(stderr,
136 "Tar: cannot create temporary file (%s)\n",
137 tname);
415c8aef
BJ
138 done(1);
139 }
140 fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
555a248b
BJ
141 /*FALL THRU*/
142
415c8aef
BJ
143 case 'r':
144 rflag++;
415c8aef 145 break;
555a248b 146
415c8aef
BJ
147 case 'v':
148 vflag++;
149 break;
555a248b 150
415c8aef
BJ
151 case 'w':
152 wflag++;
153 break;
555a248b 154
415c8aef
BJ
155 case 'x':
156 xflag++;
157 break;
555a248b 158
415c8aef
BJ
159 case 't':
160 tflag++;
161 break;
555a248b 162
415c8aef
BJ
163 case 'm':
164 mflag++;
165 break;
555a248b 166
415c8aef
BJ
167 case '-':
168 break;
555a248b 169
415c8aef
BJ
170 case '0':
171 case '1':
172 case '4':
173 case '5':
174 case '7':
175 case '8':
176 magtape[8] = *cp;
177 usefile = magtape;
178 break;
555a248b 179
415c8aef 180 case 'b':
095da6e7
SL
181 if (*argv == 0) {
182 fprintf(stderr,
183 "tar: blocksize must be specified with 'b' option\n");
184 usage();
185 }
186 nblock = atoi(*argv);
187 if (nblock <= 0) {
188 fprintf(stderr,
189 "tar: invalid blocksize \"%s\"\n", *argv);
415c8aef
BJ
190 done(1);
191 }
095da6e7 192 argv++;
415c8aef 193 break;
555a248b 194
415c8aef
BJ
195 case 'l':
196 linkerrok++;
197 break;
555a248b
BJ
198
199 case 'h':
200 hflag++;
201 break;
202
654c61ee
SL
203 case 'i':
204 iflag++;
205 break;
206
8678ae71
KM
207 case 'B':
208 Bflag++;
209 break;
210
654c61ee
SL
211 case 'F':
212 Fflag++;
213 break;
214
415c8aef
BJ
215 default:
216 fprintf(stderr, "tar: %c: unknown option\n", *cp);
217 usage();
218 }
219
555a248b
BJ
220 if (!rflag && !xflag && !tflag)
221 usage();
095da6e7
SL
222 tbuf = (union hblock *)malloc(nblock*TBLOCK);
223 if (tbuf == NULL) {
224 fprintf(stderr, "tar: blocksize %d too big, can't get memory\n",
225 nblock);
226 done(1);
227 }
415c8aef 228 if (rflag) {
555a248b 229 if (cflag && tfile != NULL)
415c8aef 230 usage();
415c8aef
BJ
231 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
232 signal(SIGINT, onintr);
233 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
234 signal(SIGHUP, onhup);
235 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
236 signal(SIGQUIT, onquit);
555a248b 237#ifdef notdef
415c8aef
BJ
238 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
239 signal(SIGTERM, onterm);
555a248b 240#endif
415c8aef
BJ
241 if (strcmp(usefile, "-") == 0) {
242 if (cflag == 0) {
555a248b 243 fprintf(stderr,
095da6e7 244 "tar: can only create standard output archives\n");
415c8aef
BJ
245 done(1);
246 }
247 mt = dup(1);
248 nblock = 1;
555a248b 249 } else if ((mt = open(usefile, 2)) < 0) {
415c8aef 250 if (cflag == 0 || (mt = creat(usefile, 0666)) < 0) {
555a248b
BJ
251 fprintf(stderr,
252 "tar: cannot open %s\n", usefile);
415c8aef
BJ
253 done(1);
254 }
255 }
415c8aef 256 dorep(argv);
555a248b 257 done(0);
415c8aef 258 }
555a248b
BJ
259 if (strcmp(usefile, "-") == 0) {
260 mt = dup(0);
261 nblock = 1;
262 } else if ((mt = open(usefile, 0)) < 0) {
263 fprintf(stderr, "tar: cannot open %s\n", usefile);
264 done(1);
415c8aef 265 }
555a248b
BJ
266 if (xflag)
267 doxtract(argv);
415c8aef 268 else
555a248b 269 dotable();
415c8aef
BJ
270 done(0);
271}
272
273usage()
274{
555a248b 275 fprintf(stderr,
095da6e7 276"tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n");
415c8aef
BJ
277 done(1);
278}
279
280dorep(argv)
555a248b 281 char *argv[];
415c8aef
BJ
282{
283 register char *cp, *cp2;
f21b792d 284 char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
415c8aef
BJ
285
286 if (!cflag) {
287 getdir();
288 do {
289 passtape();
290 if (term)
291 done(0);
292 getdir();
293 } while (!endtape());
095da6e7 294 backtape();
415c8aef
BJ
295 if (tfile != NULL) {
296 char buf[200];
297
555a248b
BJ
298 sprintf(buf,
299"sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
415c8aef
BJ
300 tname, tname, tname, tname, tname, tname);
301 fflush(tfile);
302 system(buf);
303 freopen(tname, "r", tfile);
304 fstat(fileno(tfile), &stbuf);
305 high = stbuf.st_size;
306 }
307 }
308
5ee3622a 309 (void) getcwd(wdir);
415c8aef
BJ
310 while (*argv && ! term) {
311 cp2 = *argv;
312 if (!strcmp(cp2, "-C") && argv[1]) {
313 argv++;
314 if (chdir(*argv) < 0)
315 perror(*argv);
316 else
5ee3622a 317 (void) getcwd(wdir);
415c8aef
BJ
318 argv++;
319 continue;
320 }
f21b792d 321 parent = wdir;
415c8aef
BJ
322 for (cp = *argv; *cp; cp++)
323 if (*cp == '/')
324 cp2 = cp;
325 if (cp2 != *argv) {
326 *cp2 = '\0';
f21b792d
SL
327 if (chdir(*argv) < 0) {
328 perror(*argv);
329 continue;
330 }
5ee3622a 331 parent = getcwd(tempdir);
415c8aef
BJ
332 *cp2 = '/';
333 cp2++;
334 }
f21b792d 335 putfile(*argv++, cp2, parent);
415c8aef
BJ
336 chdir(wdir);
337 }
338 putempty();
339 putempty();
340 flushtape();
555a248b
BJ
341 if (linkerrok == 0)
342 return;
343 for (; ihead != NULL; ihead = ihead->nextp) {
344 if (ihead->count == 0)
345 continue;
095da6e7 346 fprintf(stderr, "tar: missing links to %s\n", ihead->pathname);
555a248b 347 }
415c8aef
BJ
348}
349
350endtape()
351{
095da6e7 352 return (dblock.dbuf.name[0] == '\0');
415c8aef
BJ
353}
354
355getdir()
356{
357 register struct stat *sp;
358 int i;
359
654c61ee 360top:
555a248b 361 readtape((char *)&dblock);
415c8aef
BJ
362 if (dblock.dbuf.name[0] == '\0')
363 return;
364 sp = &stbuf;
365 sscanf(dblock.dbuf.mode, "%o", &i);
366 sp->st_mode = i;
367 sscanf(dblock.dbuf.uid, "%o", &i);
368 sp->st_uid = i;
369 sscanf(dblock.dbuf.gid, "%o", &i);
370 sp->st_gid = i;
371 sscanf(dblock.dbuf.size, "%lo", &sp->st_size);
372 sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
373 sscanf(dblock.dbuf.chksum, "%o", &chksum);
654c61ee 374 if (chksum != (i = checksum())) {
095da6e7 375 fprintf(stderr, "tar: directory checksum error (%d != %d)\n",
654c61ee
SL
376 chksum, i);
377 if (iflag)
378 goto top;
415c8aef
BJ
379 done(2);
380 }
381 if (tfile != NULL)
382 fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime);
383}
384
385passtape()
386{
387 long blocks;
388 char buf[TBLOCK];
389
390 if (dblock.dbuf.linkflag == '1')
391 return;
392 blocks = stbuf.st_size;
393 blocks += TBLOCK-1;
394 blocks /= TBLOCK;
395
396 while (blocks-- > 0)
397 readtape(buf);
398}
399
f21b792d 400putfile(longname, shortname, parent)
555a248b
BJ
401 char *longname;
402 char *shortname;
f21b792d 403 char *parent;
415c8aef 404{
654c61ee 405 int infile = 0;
415c8aef
BJ
406 long blocks;
407 char buf[TBLOCK];
408 register char *cp, *cp2;
aeb5c9b4
KM
409 struct direct *dp;
410 DIR *dirp;
415c8aef 411 int i, j;
f21b792d 412 char newparent[NAMSIZ+64];
654c61ee 413 extern int errno;
415c8aef 414
f21b792d 415 if (!hflag)
654c61ee
SL
416 i = lstat(shortname, &stbuf);
417 else
418 i = stat(shortname, &stbuf);
419 if (i < 0) {
420 switch (errno) {
421 case EACCES:
422 fprintf(stderr, "tar: %s: cannot open file\n", longname);
423 break;
424 case ENOENT:
425 fprintf(stderr, "tar: %s: no such file or directory\n",
426 longname);
427 break;
428 default:
429 fprintf(stderr, "tar: %s: cannot stat file\n", longname);
430 break;
431 }
f21b792d
SL
432 return;
433 }
654c61ee 434 if (tfile != NULL && checkupdate(longname) == 0)
415c8aef 435 return;
654c61ee
SL
436 if (checkw('r', longname) == 0)
437 return;
438 if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
415c8aef 439 return;
415c8aef 440
654c61ee
SL
441 switch (stbuf.st_mode & S_IFMT) {
442 case S_IFDIR:
555a248b
BJ
443 for (i = 0, cp = buf; *cp++ = longname[i++];)
444 ;
415c8aef
BJ
445 *--cp = '/';
446 *++cp = 0 ;
415c8aef 447 if (!oflag) {
555a248b 448 if ((cp - buf) >= NAMSIZ) {
095da6e7
SL
449 fprintf(stderr, "tar: %s: file name too long\n",
450 longname);
555a248b
BJ
451 return;
452 }
453 stbuf.st_size = 0;
454 tomodes(&stbuf);
455 strcpy(dblock.dbuf.name,buf);
456 sprintf(dblock.dbuf.chksum, "%6o", checksum());
457 writetape((char *)&dblock);
415c8aef 458 }
f21b792d 459 sprintf(newparent, "%s/%s", parent, shortname);
415c8aef 460 chdir(shortname);
aeb5c9b4 461 if ((dirp = opendir(".")) == NULL) {
095da6e7
SL
462 fprintf(stderr, "tar: %s: directory read error\n",
463 longname);
f21b792d 464 chdir(parent);
aeb5c9b4
KM
465 return;
466 }
467 while ((dp = readdir(dirp)) != NULL && !term) {
468 if (dp->d_ino == 0)
415c8aef 469 continue;
555a248b
BJ
470 if (!strcmp(".", dp->d_name) ||
471 !strcmp("..", dp->d_name))
415c8aef 472 continue;
aeb5c9b4
KM
473 strcpy(cp, dp->d_name);
474 i = telldir(dirp);
475 closedir(dirp);
f21b792d 476 putfile(buf, cp, newparent);
aeb5c9b4
KM
477 dirp = opendir(".");
478 seekdir(dirp, i);
415c8aef 479 }
aeb5c9b4 480 closedir(dirp);
f21b792d 481 chdir(parent);
654c61ee
SL
482 break;
483
484 case S_IFLNK:
485 tomodes(&stbuf);
486 if (strlen(longname) >= NAMSIZ) {
095da6e7
SL
487 fprintf(stderr, "tar: %s: file name too long\n",
488 longname);
654c61ee
SL
489 return;
490 }
491 strcpy(dblock.dbuf.name, longname);
555a248b 492 if (stbuf.st_size + 1 >= NAMSIZ) {
095da6e7
SL
493 fprintf(stderr, "tar: %s: symbolic link too long\n",
494 longname);
555a248b
BJ
495 return;
496 }
f21b792d 497 i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
555a248b 498 if (i < 0) {
f21b792d 499 perror(longname);
555a248b
BJ
500 return;
501 }
502 dblock.dbuf.linkname[i] = '\0';
503 dblock.dbuf.linkflag = '2';
504 if (vflag) {
505 fprintf(stderr, "a %s ", longname);
506 fprintf(stderr, "symbolic link to %s\n",
095da6e7 507 dblock.dbuf.linkname);
555a248b
BJ
508 }
509 sprintf(dblock.dbuf.size, "%11lo", 0);
510 sprintf(dblock.dbuf.chksum, "%6o", checksum());
511 writetape((char *)&dblock);
654c61ee
SL
512 break;
513
514 case S_IFREG:
515 if ((infile = open(shortname, 0)) < 0) {
516 fprintf(stderr, "tar: %s: cannot open file\n", longname);
415c8aef
BJ
517 return;
518 }
654c61ee
SL
519 tomodes(&stbuf);
520 if (strlen(longname) >= NAMSIZ) {
095da6e7
SL
521 fprintf(stderr, "tar: %s: file name too long\n",
522 longname);
654c61ee
SL
523 return;
524 }
525 strcpy(dblock.dbuf.name, longname);
526 if (stbuf.st_nlink > 1) {
527 struct linkbuf *lp;
528 int found = 0;
529
530 for (lp = ihead; lp != NULL; lp = lp->nextp)
531 if (lp->inum == stbuf.st_ino &&
532 lp->devnum == stbuf.st_dev) {
533 found++;
534 break;
535 }
536 if (found) {
537 strcpy(dblock.dbuf.linkname, lp->pathname);
538 dblock.dbuf.linkflag = '1';
539 sprintf(dblock.dbuf.chksum, "%6o", checksum());
540 writetape( (char *) &dblock);
541 if (vflag) {
542 fprintf(stderr, "a %s ", longname);
095da6e7
SL
543 fprintf(stderr, "link to %s\n",
544 lp->pathname);
654c61ee
SL
545 }
546 lp->count--;
547 close(infile);
548 return;
549 }
550 lp = (struct linkbuf *) malloc(sizeof(*lp));
551 if (lp == NULL) {
552 if (freemem) {
553 fprintf(stderr,
095da6e7 554 "tar: out of memory, link information lost\n");
654c61ee
SL
555 freemem = 0;
556 }
557 } else {
558 lp->nextp = ihead;
559 ihead = lp;
560 lp->inum = stbuf.st_ino;
561 lp->devnum = stbuf.st_dev;
562 lp->count = stbuf.st_nlink - 1;
563 strcpy(lp->pathname, longname);
415c8aef
BJ
564 }
565 }
654c61ee
SL
566 blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
567 if (vflag) {
568 fprintf(stderr, "a %s ", longname);
569 fprintf(stderr, "%ld blocks\n", blocks);
570 }
571 sprintf(dblock.dbuf.chksum, "%6o", checksum());
572 writetape((char *)&dblock);
415c8aef 573
654c61ee
SL
574 while ((i = read(infile, buf, TBLOCK)) > 0 && blocks > 0) {
575 writetape(buf);
576 blocks--;
577 }
578 close(infile);
579 if (blocks != 0 || i != 0)
095da6e7
SL
580 fprintf(stderr, "tar: %s: file changed size\n",
581 longname);
654c61ee
SL
582 while (--blocks >= 0)
583 putempty();
584 break;
585
586 default:
587 fprintf(stderr, "tar: %s is not a file. Not dumped\n",
095da6e7 588 longname);
654c61ee 589 break;
415c8aef 590 }
415c8aef
BJ
591}
592
415c8aef 593doxtract(argv)
555a248b 594 char *argv[];
415c8aef
BJ
595{
596 long blocks, bytes;
597 char buf[TBLOCK];
598 char **cp;
599 int ofile;
600
601 for (;;) {
602 getdir();
603 if (endtape())
604 break;
415c8aef
BJ
605 if (*argv == 0)
606 goto gotit;
415c8aef
BJ
607 for (cp = argv; *cp; cp++)
608 if (prefix(*cp, dblock.dbuf.name))
609 goto gotit;
610 passtape();
611 continue;
612
613gotit:
614 if (checkw('x', dblock.dbuf.name) == 0) {
615 passtape();
616 continue;
617 }
654c61ee
SL
618 if (Fflag) {
619 char *s;
620
621 if ((s = rindex(dblock.dbuf.name, '/')) == 0)
622 s = dblock.dbuf.name;
623 else
624 s++;
625 if (checkf(s, stbuf.st_mode, Fflag) == 0) {
626 passtape();
627 continue;
628 }
629 }
555a248b 630 if (checkdir(dblock.dbuf.name))
415c8aef 631 continue;
555a248b
BJ
632 if (dblock.dbuf.linkflag == '2') {
633 unlink(dblock.dbuf.name);
634 if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
095da6e7
SL
635 fprintf(stderr, "tar: %s: symbolic link failed\n",
636 dblock.dbuf.name);
555a248b
BJ
637 continue;
638 }
639 if (vflag)
640 fprintf(stderr, "x %s symbolic link to %s\n",
095da6e7 641 dblock.dbuf.name, dblock.dbuf.linkname);
dfdd2236
KM
642#ifdef notdef
643 /* ignore alien orders */
555a248b
BJ
644 chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
645 if (mflag == 0) {
828d5b76 646 struct timeval tv[2];
555a248b 647
828d5b76
SL
648 tv[0].tv_sec = time(0);
649 tv[0].tv_usec = 0;
650 tv[1].tv_sec = stbuf.st_mtime;
651 tv[1].tv_usec = 0;
652 utimes(dblock.dbuf.name, tv);
555a248b
BJ
653 }
654 if (pflag)
655 chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
dfdd2236 656#endif
555a248b
BJ
657 continue;
658 }
415c8aef
BJ
659 if (dblock.dbuf.linkflag == '1') {
660 unlink(dblock.dbuf.name);
661 if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) {
095da6e7
SL
662 fprintf(stderr, "tar: %s: cannot link\n",
663 dblock.dbuf.name);
415c8aef
BJ
664 continue;
665 }
666 if (vflag)
06eff236 667 fprintf(stderr, "%s linked to %s\n",
095da6e7 668 dblock.dbuf.name, dblock.dbuf.linkname);
415c8aef
BJ
669 continue;
670 }
555a248b
BJ
671 if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
672 fprintf(stderr, "tar: %s - cannot create\n",
095da6e7 673 dblock.dbuf.name);
415c8aef
BJ
674 passtape();
675 continue;
676 }
cd2661db 677 chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
415c8aef
BJ
678 blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
679 if (vflag)
06eff236 680 fprintf(stderr, "x %s, %ld bytes, %ld tape blocks\n",
095da6e7 681 dblock.dbuf.name, bytes, blocks);
555a248b 682 for (; blocks-- > 0; bytes -= TBLOCK) {
415c8aef
BJ
683 readtape(buf);
684 if (bytes > TBLOCK) {
685 if (write(ofile, buf, TBLOCK) < 0) {
555a248b
BJ
686 fprintf(stderr,
687 "tar: %s: HELP - extract write error\n",
095da6e7 688 dblock.dbuf.name);
415c8aef
BJ
689 done(2);
690 }
555a248b
BJ
691 continue;
692 }
693 if (write(ofile, buf, (int) bytes) < 0) {
694 fprintf(stderr,
095da6e7
SL
695 "tar: %s: HELP - extract write error\n",
696 dblock.dbuf.name);
555a248b
BJ
697 done(2);
698 }
415c8aef
BJ
699 }
700 close(ofile);
701 if (mflag == 0) {
828d5b76 702 struct timeval tv[2];
415c8aef 703
828d5b76
SL
704 tv[0].tv_sec = time(0);
705 tv[0].tv_usec = 0;
706 tv[1].tv_sec = stbuf.st_mtime;
707 tv[1].tv_usec = 0;
708 utimes(dblock.dbuf.name, tv);
415c8aef 709 }
cd2661db 710 if (pflag)
555a248b 711 chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
415c8aef
BJ
712 }
713}
714
715dotable()
716{
717 for (;;) {
718 getdir();
719 if (endtape())
720 break;
721 if (vflag)
722 longt(&stbuf);
723 printf("%s", dblock.dbuf.name);
724 if (dblock.dbuf.linkflag == '1')
725 printf(" linked to %s", dblock.dbuf.linkname);
555a248b
BJ
726 if (dblock.dbuf.linkflag == '2')
727 printf(" symbolic link to %s", dblock.dbuf.linkname);
415c8aef
BJ
728 printf("\n");
729 passtape();
730 }
731}
732
733putempty()
734{
735 char buf[TBLOCK];
415c8aef 736
095da6e7 737 bzero(buf, sizeof (buf));
415c8aef
BJ
738 writetape(buf);
739}
740
741longt(st)
555a248b 742 register struct stat *st;
415c8aef
BJ
743{
744 register char *cp;
745 char *ctime();
746
747 pmode(st);
748 printf("%3d/%1d", st->st_uid, st->st_gid);
749 printf("%7D", st->st_size);
750 cp = ctime(&st->st_mtime);
751 printf(" %-12.12s %-4.4s ", cp+4, cp+20);
752}
753
754#define SUID 04000
755#define SGID 02000
756#define ROWN 0400
757#define WOWN 0200
758#define XOWN 0100
759#define RGRP 040
760#define WGRP 020
761#define XGRP 010
762#define ROTH 04
763#define WOTH 02
764#define XOTH 01
765#define STXT 01000
766int m1[] = { 1, ROWN, 'r', '-' };
767int m2[] = { 1, WOWN, 'w', '-' };
768int m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
769int m4[] = { 1, RGRP, 'r', '-' };
770int m5[] = { 1, WGRP, 'w', '-' };
771int m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
772int m7[] = { 1, ROTH, 'r', '-' };
773int m8[] = { 1, WOTH, 'w', '-' };
774int m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
775
776int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
777
778pmode(st)
555a248b 779 register struct stat *st;
415c8aef
BJ
780{
781 register int **mp;
782
783 for (mp = &m[0]; mp < &m[9];)
784 select(*mp++, st);
785}
786
787select(pairp, st)
555a248b
BJ
788 int *pairp;
789 struct stat *st;
415c8aef
BJ
790{
791 register int n, *ap;
792
793 ap = pairp;
794 n = *ap++;
795 while (--n>=0 && (st->st_mode&*ap++)==0)
796 ap++;
797 printf("%c", *ap);
798}
799
800checkdir(name)
555a248b 801 register char *name;
415c8aef
BJ
802{
803 register char *cp;
555a248b 804
654c61ee
SL
805 /*
806 * Quick check for existance of directory.
807 */
808 if ((cp = rindex(name, '/')) == 0)
809 return (0);
810 *cp = '\0';
811 if (access(name, 0) >= 0) {
812 *cp = '/';
813 return (cp[1] == '\0');
814 }
815 *cp = '/';
816
817 /*
818 * No luck, try to make all directories in path.
819 */
415c8aef 820 for (cp = name; *cp; cp++) {
555a248b
BJ
821 if (*cp != '/')
822 continue;
823 *cp = '\0';
654c61ee 824 if (access(name, 0) < 0) {
13fc8845
SL
825 if (mkdir(name, 0777) < 0) {
826 perror(name);
654c61ee
SL
827 *cp = '/';
828 return (0);
415c8aef 829 }
555a248b 830 chown(name, stbuf.st_uid, stbuf.st_gid);
07269bf2 831 if (pflag && cp[1] == '\0')
13fc8845 832 chmod(name, stbuf.st_mode & 0777);
415c8aef 833 }
555a248b 834 *cp = '/';
415c8aef 835 }
555a248b 836 return (cp[-1]=='/');
415c8aef
BJ
837}
838
839onintr()
840{
841 signal(SIGINT, SIG_IGN);
842 term++;
843}
844
845onquit()
846{
847 signal(SIGQUIT, SIG_IGN);
848 term++;
849}
850
851onhup()
852{
853 signal(SIGHUP, SIG_IGN);
854 term++;
855}
856
857onterm()
858{
859 signal(SIGTERM, SIG_IGN);
860 term++;
861}
862
863tomodes(sp)
864register struct stat *sp;
865{
866 register char *cp;
867
868 for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
869 *cp = '\0';
870 sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777);
871 sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid);
872 sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid);
873 sprintf(dblock.dbuf.size, "%11lo ", sp->st_size);
874 sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime);
875}
876
877checksum()
878{
879 register i;
880 register char *cp;
881
555a248b
BJ
882 for (cp = dblock.dbuf.chksum;
883 cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
415c8aef
BJ
884 *cp = ' ';
885 i = 0;
886 for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
887 i += *cp;
555a248b 888 return (i);
415c8aef
BJ
889}
890
891checkw(c, name)
555a248b 892 char *name;
415c8aef 893{
555a248b
BJ
894 if (!wflag)
895 return (1);
896 printf("%c ", c);
897 if (vflag)
898 longt(&stbuf);
899 printf("%s: ", name);
900 return (response() == 'y');
415c8aef
BJ
901}
902
903response()
904{
905 char c;
906
907 c = getchar();
908 if (c != '\n')
555a248b
BJ
909 while (getchar() != '\n')
910 ;
911 else
912 c = 'n';
913 return (c);
415c8aef
BJ
914}
915
654c61ee
SL
916checkf(name, mode, howmuch)
917 char *name;
918 int mode, howmuch;
919{
920 int l;
921
922 if ((mode & S_IFMT) == S_IFDIR)
923 return (strcmp(name, "SCCS") != 0);
924 if ((l = strlen(name)) < 3)
925 return (1);
926 if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
927 return (0);
928 if (strcmp(name, "core") == 0 ||
929 strcmp(name, "errs") == 0 ||
930 (howmuch > 1 && strcmp(name, "a.out") == 0))
931 return (0);
932 /* SHOULD CHECK IF IT IS EXECUTABLE */
933 return (1);
934}
935
415c8aef 936checkupdate(arg)
555a248b 937 char *arg;
415c8aef
BJ
938{
939 char name[100];
555a248b 940 long mtime;
415c8aef
BJ
941 daddr_t seekp;
942 daddr_t lookup();
943
944 rewind(tfile);
945 for (;;) {
946 if ((seekp = lookup(arg)) < 0)
555a248b 947 return (1);
415c8aef
BJ
948 fseek(tfile, seekp, 0);
949 fscanf(tfile, "%s %lo", name, &mtime);
555a248b 950 return (stbuf.st_mtime > mtime);
415c8aef
BJ
951 }
952}
953
954done(n)
955{
956 unlink(tname);
957 exit(n);
958}
959
960prefix(s1, s2)
555a248b 961 register char *s1, *s2;
415c8aef
BJ
962{
963 while (*s1)
964 if (*s1++ != *s2++)
555a248b 965 return (0);
415c8aef 966 if (*s2)
555a248b
BJ
967 return (*s2 == '/');
968 return (1);
415c8aef
BJ
969}
970
415c8aef
BJ
971#define N 200
972int njab;
555a248b 973
415c8aef
BJ
974daddr_t
975lookup(s)
555a248b 976 char *s;
415c8aef
BJ
977{
978 register i;
979 daddr_t a;
980
981 for(i=0; s[i]; i++)
555a248b 982 if (s[i] == ' ')
415c8aef
BJ
983 break;
984 a = bsrch(s, i, low, high);
555a248b 985 return (a);
415c8aef
BJ
986}
987
988daddr_t
989bsrch(s, n, l, h)
555a248b
BJ
990 daddr_t l, h;
991 char *s;
415c8aef
BJ
992{
993 register i, j;
994 char b[N];
995 daddr_t m, m1;
996
997 njab = 0;
998
999loop:
555a248b
BJ
1000 if (l >= h)
1001 return (-1L);
415c8aef 1002 m = l + (h-l)/2 - N/2;
555a248b 1003 if (m < l)
415c8aef
BJ
1004 m = l;
1005 fseek(tfile, m, 0);
1006 fread(b, 1, N, tfile);
1007 njab++;
1008 for(i=0; i<N; i++) {
555a248b 1009 if (b[i] == '\n')
415c8aef
BJ
1010 break;
1011 m++;
1012 }
555a248b
BJ
1013 if (m >= h)
1014 return (-1L);
415c8aef
BJ
1015 m1 = m;
1016 j = i;
1017 for(i++; i<N; i++) {
1018 m1++;
555a248b 1019 if (b[i] == '\n')
415c8aef
BJ
1020 break;
1021 }
1022 i = cmp(b+j, s, n);
555a248b 1023 if (i < 0) {
415c8aef
BJ
1024 h = m;
1025 goto loop;
1026 }
555a248b 1027 if (i > 0) {
415c8aef
BJ
1028 l = m1;
1029 goto loop;
1030 }
555a248b 1031 return (m);
415c8aef
BJ
1032}
1033
1034cmp(b, s, n)
555a248b 1035 char *b, *s;
415c8aef
BJ
1036{
1037 register i;
1038
555a248b 1039 if (b[0] != '\n')
415c8aef
BJ
1040 exit(2);
1041 for(i=0; i<n; i++) {
555a248b
BJ
1042 if (b[i+1] > s[i])
1043 return (-1);
1044 if (b[i+1] < s[i])
1045 return (1);
415c8aef 1046 }
555a248b 1047 return (b[i+1] == ' '? 0 : -1);
415c8aef
BJ
1048}
1049
1050readtape(buffer)
555a248b 1051 char *buffer;
415c8aef 1052{
06eff236 1053 register int i;
415c8aef
BJ
1054
1055 if (recno >= nblock || first == 0) {
8678ae71 1056 if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) {
095da6e7 1057 fprintf(stderr, "tar: tape read error\n");
415c8aef
BJ
1058 done(3);
1059 }
1060 if (first == 0) {
1061 if ((i % TBLOCK) != 0) {
095da6e7 1062 fprintf(stderr, "tar: tape blocksize error\n");
415c8aef
BJ
1063 done(3);
1064 }
1065 i /= TBLOCK;
06eff236 1066 if (i != nblock) {
095da6e7 1067 fprintf(stderr, "tar: blocksize = %d\n", i);
415c8aef
BJ
1068 nblock = i;
1069 }
1070 }
1071 recno = 0;
1072 }
1073 first = 1;
095da6e7 1074 bcopy((char *)&tbuf[recno++], buffer, TBLOCK);
555a248b 1075 return (TBLOCK);
415c8aef
BJ
1076}
1077
1078writetape(buffer)
555a248b 1079 char *buffer;
415c8aef
BJ
1080{
1081 first = 1;
415c8aef
BJ
1082 if (recno >= nblock) {
1083 if (write(mt, tbuf, TBLOCK*nblock) < 0) {
095da6e7 1084 fprintf(stderr, "tar: tape write error\n");
415c8aef
BJ
1085 done(2);
1086 }
1087 recno = 0;
1088 }
095da6e7 1089 bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
415c8aef
BJ
1090 if (recno >= nblock) {
1091 if (write(mt, tbuf, TBLOCK*nblock) < 0) {
095da6e7 1092 fprintf(stderr, "tar: tape write error\n");
415c8aef
BJ
1093 done(2);
1094 }
1095 recno = 0;
1096 }
555a248b 1097 return (TBLOCK);
415c8aef
BJ
1098}
1099
1100backtape()
1101{
06eff236
BJ
1102 static int mtdev = 1;
1103 static struct mtop mtop = {MTBSR, 1};
1104 struct mtget mtget;
1105
1106 if (mtdev == 1)
1107 mtdev = ioctl(mt, MTIOCGET, &mtget);
1108 if (mtdev == 0) {
1109 if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
095da6e7 1110 fprintf(stderr, "tar: tape backspace error\n");
415c8aef
BJ
1111 done(4);
1112 }
06eff236
BJ
1113 } else
1114 lseek(mt, (long) -TBLOCK*nblock, 1);
1115 recno--;
415c8aef
BJ
1116}
1117
1118flushtape()
1119{
1120 write(mt, tbuf, TBLOCK*nblock);
1121}
1122
8678ae71
KM
1123bread(fd, buf, size)
1124 int fd;
1125 char *buf;
1126 int size;
1127{
1128 int count;
1129 static int lastread = 0;
1130
1131 if (!Bflag)
1132 return (read(fd, buf, size));
1133 for (count = 0; count < size; count += lastread) {
1134 if (lastread < 0) {
1135 if (count > 0)
1136 return (count);
1137 return (lastread);
1138 }
1139 lastread = read(fd, buf, size - count);
1140 buf += lastread;
1141 }
1142 return (count);
1143}
5ee3622a
SL
1144
1145char *
1146getcwd(buf)
1147 char *buf;
1148{
1149
1150 if (getwd(buf) == NULL) {
1151 fprintf(stderr, "tar: %s\n", buf);
1152 exit(1);
1153 }
1154 return (buf);
1155}