home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 404.lha / TAR / src / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-05  |  12.1 KB  |  537 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.31 11/5/87 Public Domain - gnu
  9.  */
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #if !defined(MSDOS) && !defined(AMIGA)
  15. #include <sys/file.h>
  16. #endif    /* MSDOS */
  17.  
  18. #ifdef USG
  19. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  20. #endif
  21.  
  22. char *ctime();                /* From libc.a */
  23.  
  24. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  25.  
  26. #include "tar.h"
  27. #include "port.h"
  28.  
  29. #ifdef AMIGA
  30. int made_on_amiga;        /* from extract.c */
  31. #endif
  32.  
  33. long from_oct();            /* Decode octal number */
  34. void demode();                /* Print file mode */
  35.  
  36. union record *head;            /* Points to current archive header */
  37. struct stat hstat;            /* Stat struct corresponding */
  38. int head_standard;            /* Tape header is in ANSI format */
  39.  
  40. void print_header();
  41. void skip_file();
  42.  
  43.  
  44. /*
  45.  * Main loop for reading an archive.
  46.  */
  47. void
  48. read_and(do_something)
  49.     void (*do_something)();
  50. {
  51.     int status = 3;            /* Initial status at start of archive */
  52.     int prev_status;
  53.  
  54.     name_gather();            /* Gather all the names */
  55.     open_archive(1);        /* Open for reading */
  56.  
  57.     for(;;) {
  58.         prev_status = status;
  59.         status = read_header();
  60.         switch (status) {
  61.  
  62.         case 1:            /* Valid header */
  63.             /* We should decode next field (mode) first... */
  64.             /* Ensure incoming names are null terminated. */
  65.             head->header.name[NAMSIZ-1] = '\0';
  66.             
  67.             if (!name_match(head->header.name)) {
  68.                 /* Skip past it in the archive */
  69.                 userec(head);
  70.                 /* Skip to the next header on the archive */
  71.                 skip_file((long)hstat.st_size);
  72.                 continue;
  73.             }
  74.  
  75.             (*do_something)();
  76.             continue;
  77.  
  78.             /*
  79.              * If the previous header was good, tell them
  80.              * that we are skipping bad ones.
  81.              */
  82.         case 0:            /* Invalid header */
  83.             userec(head);
  84.             switch (prev_status) {
  85.             case 3:        /* Error on first record */
  86.                 annorec(stderr, tar);
  87.                 fprintf(stderr, "Hmm, this doesn't look like a tar archive.\n");
  88.                 /* FALL THRU */
  89.             case 2:        /* Error after record of zeroes */
  90.             case 1:        /* Error after header rec */
  91.                 annorec(stderr, tar);
  92.                 fprintf(stderr,
  93.                     "Skipping to next file header...\n");
  94.             case 0:        /* Error after error */
  95.                 break;
  96.             }
  97.             continue;
  98.  
  99.         case 2:            /* Record of zeroes */
  100.             userec(head);
  101.             status = prev_status;    /* If error after 0's */
  102.             if (f_ignorez)    
  103.                 continue;
  104.             /* FALL THRU */
  105.         case EOF:        /* End of archive */
  106.             break;
  107.         }
  108.         break;
  109.     };
  110.  
  111.     close_archive();
  112.     names_notfound();        /* Print names not found */
  113. }        
  114.  
  115.  
  116. /*
  117.  * Print a header record, based on tar options.
  118.  */
  119. void
  120. list_archive()
  121. {
  122.  
  123.     /* Save the record */
  124.     saverec(&head);
  125.  
  126.     /* Print the header record */
  127.     if (f_verbose) {
  128.         if (f_verbose > 1)
  129.             decode_header(head, &hstat, &head_standard, 0);
  130.         print_header(stdout);
  131.     }
  132.  
  133.     /* Skip past it in the archive */
  134.     saverec((union record **) 0);    /* Unsave it */
  135.     userec(head);
  136.  
  137.     /* Skip to the next header on the archive */
  138.     skip_file((long)hstat.st_size);
  139. }
  140.  
  141.  
  142. /*
  143.  * Read a record that's supposed to be a header record.
  144.  * Return its address in "head", and if it is good, the file's
  145.  * size in hstat.st_size.
  146.  *
  147.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  148.  * 2 for a record full of zeros (EOF marker).
  149.  *
  150.  * You must always userec(head) to skip past the header which this
  151.  * routine reads.
  152.  */
  153. int
  154. read_header()
  155. {
  156.     register int    i;
  157.     register long    sum, recsum;
  158.     register char    *p;
  159.     register union record *header;
  160.  
  161.     header = findrec();
  162.     head = header;        /* This is our current header */
  163.     if (NULL == header) return EOF;
  164.  
  165.     recsum = from_oct(8,  header->header.chksum);
  166.  
  167.     sum = 0;
  168.     p = header->charptr;
  169.     for (i = sizeof(*header); --i >= 0;) {
  170.         /*
  171.          * We can't use unsigned char here because of old compilers,
  172.          * e.g. V7.
  173.          */
  174.         sum += 0xFF & *p++;
  175.     }
  176.  
  177.     /* Adjust checksum to count the "chksum" field as blanks. */
  178.     for (i = sizeof(header->header.chksum); --i >= 0;)
  179.         sum -= 0xFF & header->header.chksum[i];
  180.     sum += ' '* sizeof header->header.chksum;    
  181.  
  182.     if (sum == recsum) {
  183.         /*
  184.          * Good record.  Decode file size and return.
  185.          */
  186.         if (header->header.linkflag == LF_LINK)
  187.             hstat.st_size = 0;    /* Links 0 size on tape */
  188.         else
  189.             hstat.st_size = from_oct(1+12, header->header.size);
  190.         return 1;
  191.     }
  192.  
  193.     if (sum == 8*' ') {
  194.         /*
  195.          * This is a zeroed record...whole record is 0's except
  196.          * for the 8 blanks we faked for the checksum field.
  197.          */
  198.         return 2;
  199.     }
  200.  
  201.     return 0;
  202. }
  203.  
  204.  
  205. /* 
  206.  * Decode things from a file header record into a "struct stat".
  207.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  208.  * Standard" tar format or regular old tar format.
  209.  *
  210.  * read_header() has already decoded the checksum and length, so we don't.
  211.  *
  212.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  213.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  214.  *
  215.  * decode_header should NOT be called twice for the same record, since the
  216.  * two calls might use different "wantug" values and thus might end up with
  217.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  218.  * should decode it first, and other callers should decode it without uid/gid
  219.  * before calling a routine, e.g. print_header, that assumes decoded data.
  220.  */
  221. decode_header(header, st, stdp, wantug)
  222.     register union record    *header;
  223.     register struct stat    *st;
  224.     int    *stdp;
  225.     int    wantug;
  226. {
  227.     made_on_amiga = FALSE;
  228.     st->st_mode = from_oct(8,  header->header.mode);
  229.     st->st_mtime = from_oct(1+12, header->header.mtime);
  230. #ifdef AMIGA
  231.     /*
  232.      * Tar file made on Amiga, use the prot and comment in header
  233.      */
  234.     if (!strcmp("AmigaTar", header->header.magic_cookie))
  235.     {
  236.         made_on_amiga = TRUE;
  237.         st->st_prot = from_hex(header->header.amiga_modes);
  238.         strcpy(st->st_comment, header->header.comment);
  239.         st->st_date.ds_Days = from_hex(header->header.ds_Days);
  240.         st->st_date.ds_Minute = from_hex(header->header.ds_Minute);
  241.         st->st_date.ds_Tick = from_hex(header->header.ds_Tick);
  242.     }
  243. #endif
  244.     
  245.     if (0==strcmp(header->header.magic, TMAGIC)) {
  246.         /* Unix Standard tar archive */
  247.         *stdp = 1;
  248.         if (wantug) {
  249. #ifdef NONAMES
  250.             st->st_uid = from_oct(8,  header->header.uid);
  251.             st->st_gid = from_oct(8,  header->header.gid);
  252. #else
  253.             st->st_uid = finduid(header->header.uname);
  254.             st->st_gid = findgid(header->header.gname);
  255. #endif
  256.         }
  257.         switch  (header->header.linkflag) 
  258.         case LF_BLK: case LF_CHR:
  259.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  260.                        from_oct(8, header->header.devminor));
  261.     } else {
  262.         /* Old fashioned tar archive */
  263.         *stdp = 0;
  264.         st->st_uid = from_oct(8,  header->header.uid);
  265.         st->st_gid = from_oct(8,  header->header.gid);
  266.         st->st_rdev = 0;
  267.     }
  268. }
  269.  
  270.  
  271. /*
  272.  * Quick and dirty octal conversion.
  273.  *
  274.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  275.  */
  276. long
  277. from_oct(digs, where)
  278.     register int    digs;
  279.     register char    *where;
  280. {
  281.     register long    value;
  282.  
  283.     while (isspace(*where)) {        /* Skip spaces */
  284.         where++;
  285.         if (--digs <= 0)
  286.             return -1;        /* All blank field */
  287.     }
  288.     value = 0;
  289.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  290.         value = (value << 3) | (*where++ - '0');
  291.         --digs;
  292.     }
  293.  
  294.     if (digs > 0 && *where && !isspace(*where))
  295.         return -1;            /* Ended on non-space/nul */
  296.  
  297.     return value;
  298. }
  299.  
  300.  
  301. /*
  302.  * Actually print it.
  303.  *
  304.  * Plain and fancy file header block logging.
  305.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  306.  * This should just contain file names, so it can be fed back into tar
  307.  * with xargs or the "-T" option.  The verbose option can give a bunch
  308.  * of info, one line per file.  I doubt anybody tries to parse its
  309.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  310.  * anyway.
  311.  *
  312.  * Note that print_header uses the globals <head>, <hstat>, and
  313.  * <head_standard>, which must be set up in advance.  This is not very clean
  314.  * and should be cleaned up.  FIXME.
  315.  */
  316. #define    UGSWIDTH    11        /* min width of User, group, size */
  317. #define    DATEWIDTH    19        /* Last mod date */
  318. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  319.  
  320. void
  321. print_header(outfile)
  322.     FILE *outfile;
  323. {
  324.     char modes[11];
  325.     char *timestamp;
  326.     char uform[11], gform[11];    /* These hold formatted ints */
  327.     char *user, *group;
  328.     char size[24];        /* Holds a formatted long or maj, min */
  329.     long longie;        /* To make ctime() call portable */
  330.     int    pad;
  331.  
  332.     annofile(outfile, (char *)NULL);
  333.  
  334.     if (f_verbose <= 1) {
  335.         /* Just the fax, mam. */
  336.         fprintf(outfile, "%s\n", head->header.name);
  337.         return;
  338.     } else {
  339.         /* File type and modes */
  340.         modes[0] = '?';
  341.         switch (head->header.linkflag) {
  342.         case LF_NORMAL:
  343.         case LF_OLDNORMAL:
  344.         case LF_LINK:
  345.                 modes[0] = '-'; 
  346.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  347.                     modes[0] = 'd';
  348.                 break;
  349.         case LF_DIR:    modes[0] = 'd'; break;
  350.         case LF_SYMLINK:modes[0] = 'l'; break;
  351.         case LF_BLK:    modes[0] = 'b'; break;
  352.         case LF_CHR:    modes[0] = 'c'; break;
  353.         case LF_FIFO:    modes[0] = 'p'; break;    
  354.         case LF_CONTIG:    modes[0] = 'C'; break;
  355.         }
  356.  
  357.         demode((unsigned)hstat.st_mode, modes+1);
  358.  
  359.         /* Timestamp */
  360.         longie = hstat.st_mtime;
  361.         timestamp = ctime(&longie);
  362.         timestamp[16] = '\0';
  363.         timestamp[24] = '\0';
  364.  
  365.         /* User and group names */
  366.         if (*head->header.uname && head_standard) {
  367.             user  = head->header.uname;
  368.         } else {
  369.             user = uform;
  370.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  371.         }
  372.         if (*head->header.gname && head_standard) {
  373.             group = head->header.gname;
  374.         } else {
  375.             group = gform;
  376.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  377.         }
  378.  
  379.         /* Format the file size or major/minor device numbers */
  380.         switch (head->header.linkflag) {
  381.         case LF_CHR:
  382.         case LF_BLK:
  383.             (void)sprintf(size, "%d,%d",
  384.                     major(hstat.st_rdev),
  385.                     minor(hstat.st_rdev));
  386.             break;
  387.  
  388.         default:
  389.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  390.         }
  391.  
  392.         /* Figure out padding and print the whole line. */
  393.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  394.         if (pad > ugswidth) ugswidth = pad;
  395.  
  396.         fprintf(outfile, "%s %s/%s %*s%s %s %s %.*s",
  397.             modes,
  398.             user,
  399.             group,
  400.             ugswidth - pad,
  401.             "",
  402.             size,
  403.             timestamp+4, timestamp+20,
  404.             sizeof(head->header.name),
  405.             head->header.name);
  406.  
  407.         switch (head->header.linkflag) {
  408.         case LF_SYMLINK:
  409.             fprintf(outfile, " -> %s\n", head->header.linkname);
  410.             break;
  411.  
  412.         case LF_LINK:
  413.             fprintf(outfile, " link to %s\n", head->header.linkname);
  414.             break;
  415.  
  416.         default:
  417.             fprintf(outfile, " unknown file type '%c'\n",
  418.                 head->header.linkflag);
  419.             break;
  420.  
  421.         case LF_OLDNORMAL:
  422.         case LF_NORMAL:
  423.         case LF_CHR:
  424.         case LF_BLK:
  425.         case LF_DIR:
  426.         case LF_FIFO:
  427.         case LF_CONTIG:
  428.             putc('\n', outfile);
  429.             break;
  430.         }
  431.     }
  432. }
  433.  
  434. /*
  435.  * Print a similar line when we make a directory automatically.
  436.  */
  437. void
  438. pr_mkdir(pathname, length, mode, outfile)
  439.     char *pathname;
  440.     int length;
  441.     int mode;
  442.     FILE *outfile;
  443. {
  444.     char modes[11];
  445.  
  446.     if (f_verbose > 1) {
  447.         /* File type and modes */
  448.         modes[0] = 'd';
  449.         demode((unsigned)mode, modes+1);
  450.  
  451.         annofile(outfile, (char *)NULL);
  452.         fprintf(outfile, "%s %*s %.*s\n",
  453.             modes,
  454.             ugswidth+DATEWIDTH,
  455.             "Creating directory:",
  456.             length,
  457.             pathname);
  458.     }
  459. }
  460.  
  461.  
  462. /*
  463.  * Skip over <size> bytes of data in records in the archive.
  464.  */
  465. void
  466. skip_file(size)
  467.     register long size;
  468. {
  469.     union record *x;
  470.  
  471.     while (size > 0) {
  472.         x = findrec();
  473.         if (x == NULL) {    /* Check it... */
  474.             annorec(stderr, tar);
  475.             fprintf(stderr, "Unexpected EOF on archive file\n");
  476.             exit(EX_BADARCH);
  477.         }
  478.         userec(x);
  479.         size -= RECORDSIZE;
  480.     }
  481. }
  482.  
  483.  
  484. /*
  485.  * Decode the mode string from a stat entry into a 9-char string and a null.
  486.  */
  487. void
  488. demode(mode, string)
  489.     register unsigned mode;
  490.     register char *string;
  491. {
  492.     register unsigned mask;
  493.     register char *rwx = "rwxrwxrwx";
  494.  
  495.     for (mask = 0400; mask != 0; mask >>= 1) {
  496.         if (mode & mask)
  497.             *string++ = *rwx++;
  498.         else {
  499.             *string++ = '-';
  500.             rwx++;
  501.         }
  502.     }
  503.  
  504.     if (mode & S_ISUID)
  505.         if (string[-7] == 'x')
  506.             string[-7] = 's';
  507.         else
  508.             string[-7] = 'S';
  509.     if (mode & S_ISGID)
  510.         if (string[-4] == 'x')
  511.             string[-4] = 's';
  512.         else
  513.             string[-4] = 'S';
  514.     if (mode & S_ISVTX)
  515.         if (string[-1] == 'x')
  516.             string[-1] = 't';
  517.         else
  518.             string[-1] = 'T';
  519.     *string = '\0';
  520. }
  521.  
  522. #ifdef AMIGA
  523. from_hex(char *s)
  524. {
  525.     int i, val = 0;
  526.     char nibble;
  527.  
  528.     for (i = 0; i < 8; i++)
  529.     {
  530.     nibble = *s++;
  531.     nibble = (nibble >= 'a') ? 10 + (nibble - 'a') : nibble - '0';
  532.     val = (val << 4) + nibble;
  533.     }
  534.     return(val);
  535. }
  536. #endif
  537.