1 /* $Source: /u/mark/src/pax/RCS/list.c,v $
5 * list.c - List all files on an archive
9 * These function are needed to support archive table of contents and
10 * verbose mode during extraction and creation of achives.
14 * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
16 * Sponsored by The USENIX Association for public distribution.
18 * Copyright (c) 1989 Mark H. Colburn.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms are permitted
22 * provided that the above copyright notice is duplicated in all such
23 * forms and that any documentation, advertising materials, and other
24 * materials related to such distribution and use acknowledge that the
25 * software was developed * by Mark H. Colburn and sponsored by The
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33 * Revision 1.2 89/02/12 10:04:43 mark
36 * Revision 1.1 88/12/23 18:02:14 mark
42 static char *ident = "$Id: list.c,v 1.2 89/02/12 10:04:43 mark Exp $";
43 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
55 * isodigit returns non zero iff argument is an octal digit, zero otherwise
57 #define ISODIGIT(c) (((c) >= '0') && ((c) <= '7'))
60 /* Function Prototypes */
64 static void cpio_entry(char *, Stat *);
65 static void tar_entry(char *, Stat *);
66 static void pax_entry(char *, Stat *);
67 static void print_mode(ushort);
68 static long from_oct(int digs, char *where);
72 static void cpio_entry();
73 static void tar_entry();
74 static void pax_entry();
75 static void print_mode();
76 static long from_oct();
81 /* Internal Identifiers */
83 static char *monnames[] = {
84 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
85 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
89 /* read_header - read a header record
93 * Read a record that's supposed to be a header record. Return its
94 * address in "head", and if it is good, the file's size in
95 * asb->sb_size. Decode things from a file header record into a "Stat".
96 * Also set "head_standard" to !=0 or ==0 depending whether header record
97 * is "Unix Standard" tar format or regular old tar format.
101 * char *name - pointer which will contain name of file
102 * Stat *asb - pointer which will contain stat info
106 * Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
107 * record full of zeros (EOF marker).
112 int read_header(char *name, Stat *asb)
116 int read_header(name, asb)
127 char hdrbuf[BLOCKSIZE];
129 memset((char *)asb, 0, sizeof(Stat));
130 /* read the header from the buffer */
131 if (buf_read(hdrbuf, BLOCKSIZE) != 0) {
135 strcpy(name, hdrbuf);
137 recsum = from_oct(8, &hdrbuf[148]);
140 for (i = 0 ; i < 500; i++) {
143 * We can't use unsigned char here because of old compilers, e.g. V7.
148 /* Adjust checksum to count the "chksum" field as blanks. */
149 for (i = 0; i < 8; i++) {
150 sum -= 0xFF & hdrbuf[148 + i];
154 if (sum == 8 * ' ') {
157 * This is a zeroed record...whole record is 0's except for the 8
158 * blanks we faked for the checksum field.
164 * Good record. Decode file size and return.
166 if (hdrbuf[156] != LNKTYPE) {
167 asb->sb_size = from_oct(1 + 12, &hdrbuf[124]);
169 asb->sb_mtime = from_oct(1 + 12, &hdrbuf[136]);
170 asb->sb_mode = from_oct(8, &hdrbuf[100]);
172 if (strcmp(&hdrbuf[257], TMAGIC) == 0) {
173 /* Unix Standard tar archive */
176 asb->sb_uid = from_oct(8, &hdrbuf[108]);
177 asb->sb_gid = from_oct(8, &hdrbuf[116]);
179 asb->sb_uid = finduid(&hdrbuf[265]);
180 asb->sb_gid = findgid(&hdrbuf[297]);
182 switch (hdrbuf[156]) {
185 #ifndef _POSIX_SOURCE
186 asb->sb_rdev = makedev(from_oct(8, &hdrbuf[329]),
187 from_oct(8, &hdrbuf[337]));
195 /* Old fashioned tar archive */
197 asb->sb_uid = from_oct(8, &hdrbuf[108]);
198 asb->sb_gid = from_oct(8, &hdrbuf[116]);
201 switch (hdrbuf[156]) {
205 * Berkeley tar stores directories as regular files with a
208 if (name[strlen(name) - 1] == '/') {
209 name[strlen(name) - 1] = '\0';
210 asb->sb_mode |= S_IFDIR;
212 asb->sb_mode |= S_IFREG;
217 linkto(&hdrbuf[157], asb);
219 asb->sb_mode |= S_IFREG;
222 asb->sb_mode |= S_IFBLK;
225 asb->sb_mode |= S_IFCHR;
228 asb->sb_mode |= S_IFDIR;
232 asb->sb_mode |= S_IFLNK;
233 strcpy(asb->sb_link, &hdrbuf[157]);
238 asb->sb_mode |= S_IFIFO;
243 asb->sb_mode |= S_IFCTG;
253 /* print_entry - print a single table-of-contents entry
257 * Print_entry prints a single line of file information. The format
258 * of the line is the same as that used by the LS command. For some
259 * archive formats, various fields may not make any sense, such as
260 * the link count on tar archives. No error checking is done for bad
265 * char *name - pointer to name to print an entry for
266 * Stat *asb - pointer to the stat structure for the file
271 void print_entry(char *name, Stat *asb)
275 void print_entry(name, asb)
281 switch (ar_interface) {
283 tar_entry(name, asb);
286 cpio_entry(name, asb);
288 case PAX: pax_entry(name, asb);
294 /* cpio_entry - print a verbose cpio-style entry
298 * Print_entry prints a single line of file information. The format
299 * of the line is the same as that used by the traditional cpio
300 * command. No error checking is done for bad or invalid data.
304 * char *name - pointer to name to print an entry for
305 * Stat *asb - pointer to the stat structure for the file
310 static void cpio_entry(char *name, Stat *asb)
314 static void cpio_entry(name, asb)
325 if (f_list && f_verbose) {
326 fprintf(msgfile, "%-7o", asb->sb_mode);
327 atm = localtime(&asb->sb_mtime);
328 if (pwp = getpwuid((int) USH(asb->sb_uid))) {
329 fprintf(msgfile, "%-6s", pwp->pw_name);
331 fprintf(msgfile, "%-6u", USH(asb->sb_uid));
333 fprintf(msgfile,"%7ld %3s %2d %02d:%02d:%02d %4d ",
334 asb->sb_size, monnames[atm->tm_mon],
335 atm->tm_mday, atm->tm_hour, atm->tm_min,
336 atm->tm_sec, atm->tm_year + 1900);
338 fprintf(msgfile, "%s", name);
339 if ((asb->sb_nlink > 1) && (from = islink(name, asb))) {
340 fprintf(msgfile, " linked to %s", from->l_name);
343 if ((asb->sb_mode & S_IFMT) == S_IFLNK) {
344 fprintf(msgfile, " symbolic link to %s", asb->sb_link);
351 /* tar_entry - print a tar verbose mode entry
355 * Print_entry prints a single line of tar file information. The format
356 * of the line is the same as that produced by the traditional tar
357 * command. No error checking is done for bad or invalid data.
361 * char *name - pointer to name to print an entry for
362 * Stat *asb - pointer to the stat structure for the file
367 static void tar_entry(char *name, Stat *asb)
371 static void tar_entry(name, asb)
380 char *symnam = "NULL";
383 if ((mode = asb->sb_mode & S_IFMT) == S_IFDIR) {
384 return; /* don't print directories */
389 case S_IFLNK: /* This file is a symbolic link */
390 i = readlink(name, symnam, PATH_MAX - 1);
391 if (i < 0) { /* Could not find symbolic link */
392 warn("can't read symbolic link", strerror());
393 } else { /* Found symbolic link filename */
395 fprintf(msgfile, "x %s symbolic link to %s\n", name, symnam);
399 case S_IFREG: /* It is a link or a file */
400 if ((asb->sb_nlink > 1) && (link = islink(name, asb))) {
401 fprintf(msgfile, "%s linked to %s\n", name, link->l_name);
403 fprintf(msgfile, "x %s, %ld bytes, %d tape blocks\n",
404 name, asb->sb_size, ROUNDUP(asb->sb_size,
405 BLOCKSIZE) / BLOCKSIZE);
408 } else if (f_append || f_create) {
411 case S_IFLNK: /* This file is a symbolic link */
412 i = readlink(name, symnam, PATH_MAX - 1);
413 if (i < 0) { /* Could not find symbolic link */
414 warn("can't read symbolic link", strerror());
415 } else { /* Found symbolic link filename */
417 fprintf(msgfile, "a %s symbolic link to %s\n", name, symnam);
421 case S_IFREG: /* It is a link or a file */
422 fprintf(msgfile, "a %s ", name);
423 if ((asb->sb_nlink > 1) && (link = islink(name, asb))) {
424 fprintf(msgfile, "link to %s\n", link->l_name);
426 fprintf(msgfile, "%ld Blocks\n",
427 ROUNDUP(asb->sb_size, BLOCKSIZE) / BLOCKSIZE);
433 atm = localtime(&asb->sb_mtime);
434 print_mode(asb->sb_mode);
435 fprintf(msgfile," %d/%d %6d %3s %2d %02d:%02d %4d %s",
436 asb->sb_uid, asb->sb_gid, asb->sb_size,
437 monnames[atm->tm_mon], atm->tm_mday, atm->tm_hour,
438 atm->tm_min, atm->tm_year + 1900, name);
440 fprintf(msgfile, "%s", name);
444 case S_IFLNK: /* This file is a symbolic link */
445 i = readlink(name, symnam, PATH_MAX - 1);
446 if (i < 0) { /* Could not find symbolic link */
447 warn("can't read symbolic link", strerror());
448 } else { /* Found symbolic link filename */
450 fprintf(msgfile, " symbolic link to %s", symnam);
454 case S_IFREG: /* It is a link or a file */
455 if ((asb->sb_nlink > 1) && (link = islink(name, asb))) {
456 fprintf(msgfile, " linked to %s", link->l_name);
458 break; /* Do not print out directories */
460 fputc('\n', msgfile);
462 fprintf(msgfile, "? %s %ld blocks\n", name,
463 ROUNDUP(asb->sb_size, BLOCKSIZE) / BLOCKSIZE);
468 /* pax_entry - print a verbose cpio-style entry
472 * Print_entry prints a single line of file information. The format
473 * of the line is the same as that used by the LS command.
474 * No error checking is done for bad or invalid data.
478 * char *name - pointer to name to print an entry for
479 * Stat *asb - pointer to the stat structure for the file
484 static void pax_entry(char *name, Stat *asb)
488 static void pax_entry(name, asb)
499 if (f_list && f_verbose) {
500 print_mode(asb->sb_mode);
501 fprintf(msgfile, " %2d", asb->sb_nlink);
502 atm = localtime(&asb->sb_mtime);
503 if (pwp = getpwuid((int) USH(asb->sb_uid))) {
504 fprintf(msgfile, " %-8s", pwp->pw_name);
506 fprintf(msgfile, " %-8u", USH(asb->sb_uid));
508 if (grp = getgrgid((int) USH(asb->sb_gid))) {
509 fprintf(msgfile, " %-8s", grp->gr_name);
511 fprintf(msgfile, " %-8u", USH(asb->sb_gid));
513 switch (asb->sb_mode & S_IFMT) {
516 fprintf(msgfile, "\t%3d, %3d",
517 major(asb->sb_rdev), minor(asb->sb_rdev));
520 fprintf(msgfile, "\t%8ld", asb->sb_size);
523 fprintf(msgfile, "\t ");
525 fprintf(msgfile," %3s %2d %02d:%02d ",
526 monnames[atm->tm_mon], atm->tm_mday,
527 atm->tm_hour, atm->tm_min);
529 fprintf(msgfile, "%s", name);
530 if ((asb->sb_nlink > 1) && (from = islink(name, asb))) {
531 fprintf(msgfile, " == %s", from->l_name);
534 if ((asb->sb_mode & S_IFMT) == S_IFLNK) {
535 fprintf(msgfile, " -> %s", asb->sb_link);
542 /* print_mode - fancy file mode display
546 * Print_mode displays a numeric file mode in the standard unix
547 * representation, ala ls (-rwxrwxrwx). No error checking is done
548 * for bad mode combinations. FIFOS, sybmbolic links, sticky bits,
549 * block- and character-special devices are supported if supported
550 * by the hosting implementation.
554 * ushort mode - The integer representation of the mode to print.
559 static void print_mode(ushort mode)
563 static void print_mode(mode)
568 /* Tar does not print the leading identifier... */
569 if (ar_interface != TAR) {
570 switch (mode & S_IFMT) {
596 putc(mode & 0400 ? 'r' : '-', msgfile);
597 putc(mode & 0200 ? 'w' : '-', msgfile);
599 ? mode & 04000 ? 's' : 'x'
600 : mode & 04000 ? 'S' : '-', msgfile);
601 putc(mode & 0040 ? 'r' : '-', msgfile);
602 putc(mode & 0020 ? 'w' : '-', msgfile);
604 ? mode & 02000 ? 's' : 'x'
605 : mode & 02000 ? 'S' : '-', msgfile);
606 putc(mode & 0004 ? 'r' : '-', msgfile);
607 putc(mode & 0002 ? 'w' : '-', msgfile);
609 ? mode & 01000 ? 't' : 'x'
610 : mode & 01000 ? 'T' : '-', msgfile);
614 /* from_oct - quick and dirty octal conversion
618 * From_oct will convert an ASCII representation of an octal number
619 * to the numeric representation. The number of characters to convert
620 * is given by the parameter "digs". If there are less numbers than
621 * specified by "digs", then the routine returns -1.
625 * int digs - Number to of digits to convert
626 * char *where - Character representation of octal number
630 * The value of the octal number represented by the first digs
631 * characters of the string where. Result is -1 if the field
632 * is invalid (all blank, or nonoctal).
636 * If the field is all blank, then the value returned is -1.
642 static long from_oct(int digs, char *where)
646 static long from_oct(digs, where)
647 int digs; /* number of characters to convert */
648 char *where; /* character representation of octal number */
654 while (isspace(*where)) { /* Skip spaces */
657 return(-1); /* All blank field */
661 while (digs > 0 && ISODIGIT(*where)) { /* Scan til nonoctal */
662 value = (value << 3) | (*where++ - '0');
666 if (digs > 0 && *where && !isspace(*where)) {
667 return(-1); /* Ended on non-space/nul */