home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / PISA_TAR.TAR / tar / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-10  |  10.0 KB  |  465 lines

  1. /*
  2.  * List a tar archive.
  3.  *
  4.  * Also includes support routines for reading a tar archive.
  5.  *
  6.  * Pubic Domain version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  7.  *
  8.  * @(#)list.c 1.18 9/23/86 Public Domain - gnu
  9.  */
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15.  
  16. char *ctime();                /* From libc.a */
  17.  
  18. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  19.  
  20. #include <tar.h>
  21.  
  22. long from_oct();            /* Decode octal number */
  23. void demode();                /* Print file mode */
  24.  
  25. union record *head;            /* Points to current archive header */
  26. struct stat hstat[1];            /* Stat struct corresponding */
  27.  
  28. void print_header();
  29. void skip_file();
  30.  
  31.  
  32. /*
  33.  * Main loop for reading an archive.
  34.  */
  35. void
  36. read_and(do_something)
  37.     void (*do_something)();
  38. {
  39.     int status = 1;
  40.     int prev_status;
  41.  
  42.     name_gather();            /* Gather all the names */
  43.     open_archive(1);        /* Open for reading */
  44.  
  45.     for(;;) {
  46.         prev_status = status;
  47.         status = read_header();
  48.         switch (status) {
  49.  
  50.         case 1:            /* Valid header */
  51.             /* We should decode next field (mode) first... */
  52.             /* Ensure incoming names are null terminated. */
  53.             head->header.name[NAMSIZ-1] = '\0';
  54.             
  55.             if (!name_match(head->header.name)) {
  56.                 /* Skip past it in the archive */
  57.                 userec(head);
  58.                 /* Skip to the next header on the archive */
  59.                 skip_file((long)hstat->st_size);
  60.                 continue;
  61.             }
  62.  
  63.             (*do_something)();
  64.             continue;
  65.  
  66.             /*
  67.              * If the previous header was good, tell them
  68.              * that we are skipping bad ones.
  69.              */
  70.         case 0:            /* Invalid header */
  71.         case0:
  72.             userec(head);
  73.             if (prev_status == 1) {
  74.                 annorec(stderr, tar);
  75.                 fprintf(stderr,
  76.                     "Skipping to next file header...\n");
  77.             }
  78.             continue;
  79.  
  80.         case 2:            /* Block of zeroes */
  81.             if (f_ignorez)    
  82.                 goto case0;    /* Just skip if asked */
  83.             /* FALL THRU */
  84.         case EOF:        /* End of archive */
  85.             break;
  86.         }
  87.         break;
  88.     };
  89.  
  90.     close_archive();
  91.     names_notfound();        /* Print names not found */
  92. }        
  93.  
  94.  
  95. /*
  96.  * Print a header record, based on tar options.
  97.  */
  98. void
  99. list_archive()
  100. {
  101.  
  102.     /* Save the record */
  103.     saverec(&head);
  104.  
  105.     /* Print the header record */
  106.     print_header();
  107.  
  108.     /* Skip past it in the archive */
  109.     saverec((union record **) 0);    /* Unsave it */
  110.     userec(head);
  111.  
  112.     /* Skip to the next header on the archive */
  113.     skip_file((long)hstat->st_size);
  114. }
  115.  
  116.  
  117. /*
  118.  * Read a record that's supposed to be a header record.
  119.  * Return its address in "head", and if it is good, the file's
  120.  * size in hstat->st_size.
  121.  *
  122.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  123.  * 2 for a block full of zeros (EOF marker).
  124.  *
  125.  * You must always userec(head) to skip past the header which this
  126.  * routine reads.
  127.  */
  128. int
  129. read_header()
  130. {
  131.     register int    i;
  132.     register long    sum, recsum;
  133.     register char    *p;
  134.     register union record *header;
  135.  
  136.     header = findrec();
  137.     head = header;        /* This is our current header */
  138.     if (NULL == header) return EOF;
  139.  
  140.     recsum = from_oct(8,  header->header.chksum);
  141.  
  142.     sum = 0;
  143.     p = header->charptr;
  144.     for (i = sizeof(*header); --i >= 0;) {
  145.         /*
  146.          * We can't use unsigned char here because of old compilers,
  147.          * e.g. V7.
  148.          */
  149.         sum += 0xFF & *p++;
  150.     }
  151.  
  152.     /* Adjust checksum to count the "chksum" field as blanks. */
  153.     for (i = sizeof(header->header.chksum); --i >= 0;)
  154.         sum -= 0xFF & header->header.chksum[i];
  155.     sum += ' '* sizeof header->header.chksum;    
  156.  
  157.     if (sum == recsum) {
  158.         /*
  159.          * Good record.  Decode file size and return.
  160.          */
  161.         if (header->header.linkflag == LF_LINK)
  162.             hstat->st_size = 0;    /* Links 0 size on tape */
  163.         else
  164.             hstat->st_size = from_oct(1+12, header->header.size);
  165.         return 1;
  166.     }
  167.  
  168.     if (sum == 8*' ') {
  169.         /*
  170.          * This is a zeroed block...whole block is 0's except
  171.          * for the 8 blanks we faked for the checksum field.
  172.          */
  173.         return 2;
  174.     }
  175.  
  176.     return 0;
  177. }
  178.  
  179.  
  180. /* 
  181.  * Decode things from a file header record into a "struct stat".
  182.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  183.  * Standard" tar format or regular old tar format.
  184.  *
  185.  * read_header() has already decoded the checksum and length, so we don't.
  186.  *
  187.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  188.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  189.  */
  190. decode_header(header, st, stdp, wantug)
  191.     register union record    *header;
  192.     register struct stat    *st;
  193.     int    *stdp;
  194.     int    wantug;
  195. {
  196.  
  197.     st->st_mode = from_oct(8,  header->header.mode);
  198.     st->st_mtime = from_oct(1+12, header->header.mtime);
  199.     
  200.     if (0==strcmp(header->header.magic, TMAGIC)) {
  201.         /* Unix Standard tar archive */
  202.         *stdp = 1;
  203. #ifndef    NONAMES
  204.         if (wantug) {
  205.             st->st_uid = finduid(header->header.uname);
  206.             st->st_gid = findgid(header->header.gname);
  207.         }
  208. #endif
  209.         switch  (header->header.linkflag) 
  210.         case LF_BLK: case LF_CHR:
  211.             st->st_dev = makedev(from_oct(8, header->header.devmajor),
  212.                       from_oct(8, header->header.devminor));
  213.     } else {
  214.         /* Old fashioned tar archive */
  215.         *stdp = 0;
  216.         st->st_uid = from_oct(8,  header->header.uid);
  217.         st->st_gid = from_oct(8,  header->header.gid);
  218.         st->st_dev = 0;
  219.     }
  220. }
  221.  
  222.  
  223. /*
  224.  * Quick and dirty octal conversion.
  225.  *
  226.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  227.  */
  228. long
  229. from_oct(digs, where)
  230.     register int    digs;
  231.     register char    *where;
  232. {
  233.     register long    value;
  234.  
  235.     while (isspace(*where)) {        /* Skip spaces */
  236.         where++;
  237.         if (--digs <= 0)
  238.             return -1;        /* All blank field */
  239.     }
  240.     value = 0;
  241.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  242.         value = (value << 3) | (*where++ - '0');
  243.         --digs;
  244.     }
  245.  
  246.     if (digs > 0 && *where && !isspace(*where))
  247.         return -1;            /* Ended on non-space/nul */
  248.  
  249.     return value;
  250. }
  251.  
  252.  
  253. /*
  254.  * Actually print it.
  255.  */
  256. #define    UGSWIDTH    11        /* min width of User, group, size */
  257. #define    DATEWIDTH    19        /* Last mod date */
  258. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  259.  
  260. void
  261. print_header()
  262. {
  263.     char modes[11];
  264.     char *timestamp;
  265.     char uform[11], gform[11];    /* These hold formatted ints */
  266.     char *user, *group;
  267.     char size[24];        /* Holds a formatted long or maj, min */
  268.     long longie;        /* To make ctime() call portable */
  269.     int    pad;
  270.     int    header_std;    /* Is header standard or not? */
  271.  
  272.     annofile(stdout, (char *)NULL);
  273.  
  274.     if (f_verbose) {
  275.         decode_header(head, hstat, &header_std, 0);
  276.  
  277.         /* File type and modes */
  278.         modes[0] = '?';
  279.         switch (head->header.linkflag) {
  280.         case LF_NORMAL:
  281.         case LF_OLDNORMAL:
  282.         case LF_LINK:
  283.                 modes[0] = '-'; 
  284.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  285.                     modes[0] = 'd';
  286.                 break;
  287.         case LF_DIR:    modes[0] = 'd'; break;
  288.         case LF_SYMLINK:modes[0] = 'l'; break;
  289.         case LF_BLK:    modes[0] = 'b'; break;
  290.         case LF_CHR:    modes[0] = 'c'; break;
  291.         case LF_FIFO:    modes[0] = 'f'; break;
  292.         case LF_CONTIG:    modes[0] = '='; break;
  293.         }
  294.  
  295.         demode((unsigned)hstat->st_mode, modes+1);
  296.  
  297.         /* Timestamp */
  298.         longie = hstat->st_mtime;
  299.         timestamp = ctime(&longie);
  300.         timestamp[16] = '\0';
  301.         timestamp[24] = '\0';
  302.  
  303.         /* User and group names */
  304.         if (*head->header.uname && header_std) {
  305.             user  = head->header.uname;
  306.         } else {
  307.             user = uform;
  308.             (void)sprintf(uform, "%d", (int)hstat->st_uid);
  309.         }
  310.         if (*head->header.gname && header_std) {
  311.             group = head->header.gname;
  312.         } else {
  313.             group = gform;
  314.             (void)sprintf(gform, "%d", (int)hstat->st_gid);
  315.         }
  316.  
  317.         /* Format the file size or major/minor device numbers */
  318.         switch (head->header.linkflag) {
  319.         case LF_CHR:
  320.         case LF_BLK:
  321.             (void)sprintf(size, "%d, %d",
  322.                     major(hstat->st_dev),
  323.                     minor(hstat->st_dev));
  324.             break;
  325.  
  326.         default:
  327.             (void)sprintf(size, "%ld", (long)hstat->st_size);
  328.         }
  329.  
  330.         /* Figure out padding and print the whole line. */
  331.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  332.         if (pad > ugswidth) ugswidth = pad;
  333.  
  334.         printf("%s %s/%s %*s%s %s %s %.*s",
  335.             modes,
  336.             user,
  337.             group,
  338.             ugswidth - pad,
  339.             "",
  340.             size,
  341.             timestamp+4, timestamp+20,
  342.             sizeof(head->header.name),
  343.             head->header.name);
  344.     } else {
  345.         printf("%s", head->header.name);
  346.     }
  347.  
  348.     if (f_verbose) switch (head->header.linkflag) {
  349.     case LF_SYMLINK:
  350.         printf(" -> %s\n", head->header.linkname);
  351.         break;
  352.  
  353.     case LF_LINK:
  354.         printf(" link to %s\n", head->header.linkname);
  355.         break;
  356.  
  357.     default:
  358.         printf(" unknown file type '%c'\n", head->header.linkflag);
  359.         break;
  360.  
  361.     case LF_OLDNORMAL:
  362.     case LF_NORMAL:
  363.     case LF_CHR:
  364.     case LF_BLK:
  365.     case LF_DIR:
  366.     case LF_FIFO:
  367.     case LF_CONTIG:
  368.         putc('\n', stdout);
  369.         break;
  370.     } else {
  371.         putc('\n', stdout);
  372.     }
  373.  
  374.     /* FIXME: we don't print major/minor device numbers */
  375. }
  376.  
  377. /*
  378.  * Print a similar line when we make a directory automatically.
  379.  */
  380. void
  381. pr_mkdir(pathname, length, mode)
  382.     char *pathname;
  383.     int length;
  384.     int mode;
  385. {
  386.     char modes[11];
  387.  
  388.     if (f_verbose) {
  389.         /* File type and modes */
  390.         modes[0] = 'd';
  391.         demode((unsigned)mode, modes+1);
  392.  
  393.         annofile(stdout, (char *)NULL);
  394.         printf("%s %*s %.*s\n",
  395.             modes,
  396.             ugswidth+DATEWIDTH,
  397.             "Creating directory:",
  398.             length,
  399.             pathname);
  400.     }
  401. }
  402.  
  403.  
  404. /*
  405.  * Skip over <size> bytes of data in records in the archive.
  406.  */
  407. void
  408. skip_file(size)
  409.     register long size;
  410. {
  411.     union record *x;
  412.  
  413.     while (size > 0) {
  414.         x = findrec();
  415.         if (x == NULL) {    /* Check it... */
  416.             annorec(stderr, tar);
  417.             fprintf(stderr, "Unexpected EOF on archive file\n");
  418.             exit(EX_BADARCH);
  419.         }
  420.         userec(x);
  421.         size -= RECORDSIZE;
  422.     }
  423. }
  424.  
  425.  
  426. /*
  427.  * Decode the mode string from a stat entry into a 9-char string and a null.
  428.  */
  429. void
  430. demode(mode, string)
  431.     register unsigned mode;
  432.     register char *string;
  433. {
  434.     register unsigned mask;
  435.     register char *rwx = "rwxrwxrwx";
  436.  
  437.     for (mask = 0400; mask != 0; mask >>= 1) {
  438.         if (mode & mask)
  439.             *string++ = *rwx++;
  440.         else {
  441.             *string++ = '-';
  442.             rwx++;
  443.         }
  444.     }
  445.  
  446. #ifdef S_ISUID
  447.     if (mode & S_ISUID)
  448.         if (string[-7] == 'x')
  449.             string[-7] = 's';
  450.         else
  451.             string[-7] = 'S';
  452.     if (mode & S_ISGID)
  453.         if (string[-4] == 'x')
  454.             string[-4] = 's';
  455.         else
  456.             string[-4] = 'S';
  457.     if (mode & S_ISVTX)
  458.         if (string[-1] == 'x')
  459.             string[-1] = 't';
  460.         else
  461.             string[-1] = 'T';
  462. #endif
  463.     *string = '\0';
  464. }
  465.