Don't build the gdb-related documents in binutils-2.15.
[dragonfly.git] / contrib / file / is_tar.c
1 /*
2  * is_tar() -- figure out whether file is a tar archive.
3  *
4  * Stolen (by the author!) from the public domain tar program:
5  * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
6  *
7  * @(#)list.c 1.18 9/23/86 Public Domain - gnu
8  * $Id: is_tar.c,v 1.17 2002/07/03 18:26:38 christos Exp $
9  *
10  * Comments changed and some code/comments reformatted
11  * for file command by Ian Darwin.
12  */
13
14 #include "file.h"
15 #include <string.h>
16 #include <ctype.h>
17 #include <sys/types.h>
18 #include "tar.h"
19
20 #ifndef lint
21 FILE_RCSID("@(#)$Id: is_tar.c,v 1.17 2002/07/03 18:26:38 christos Exp $")
22 #endif
23
24 #define isodigit(c)     ( ((c) >= '0') && ((c) <= '7') )
25
26 static int from_oct(int, char *);       /* Decode octal number */
27
28 /*
29  * Return 
30  *      0 if the checksum is bad (i.e., probably not a tar archive), 
31  *      1 for old UNIX tar file,
32  *      2 for Unix Std (POSIX) tar file.
33  */
34 int
35 is_tar(unsigned char *buf, int nbytes)
36 {
37         union record *header = (union record *)buf;
38         int     i;
39         int     sum, recsum;
40         char    *p;
41
42         if (nbytes < sizeof(union record))
43                 return 0;
44
45         recsum = from_oct(8,  header->header.chksum);
46
47         sum = 0;
48         p = header->charptr;
49         for (i = sizeof(union record); --i >= 0;) {
50                 /*
51                  * We can't use unsigned char here because of old compilers,
52                  * e.g. V7.
53                  */
54                 sum += 0xFF & *p++;
55         }
56
57         /* Adjust checksum to count the "chksum" field as blanks. */
58         for (i = sizeof(header->header.chksum); --i >= 0;)
59                 sum -= 0xFF & header->header.chksum[i];
60         sum += ' '* sizeof header->header.chksum;       
61
62         if (sum != recsum)
63                 return 0;       /* Not a tar archive */
64         
65         if (0==strcmp(header->header.magic, TMAGIC)) 
66                 return 2;               /* Unix Standard tar archive */
67
68         return 1;                       /* Old fashioned tar archive */
69 }
70
71
72 /*
73  * Quick and dirty octal conversion.
74  *
75  * Result is -1 if the field is invalid (all blank, or nonoctal).
76  */
77 static int
78 from_oct(int digs, char *where)
79 {
80         int     value;
81
82         while (isspace((unsigned char)*where)) {        /* Skip spaces */
83                 where++;
84                 if (--digs <= 0)
85                         return -1;              /* All blank field */
86         }
87         value = 0;
88         while (digs > 0 && isodigit(*where)) {  /* Scan til nonoctal */
89                 value = (value << 3) | (*where++ - '0');
90                 --digs;
91         }
92
93         if (digs > 0 && *where && !isspace((unsigned char)*where))
94                 return -1;                      /* Ended on non-space/nul */
95
96         return value;
97 }