2 * This file is in the public domain. Use it as you see fit.
6 * "untar" is an extremely simple tar extractor:
7 * * A single C source file, so it should be easy to compile
8 * and run on any system with a C compiler.
9 * * Extremely portable standard C. The only non-ANSI function
11 * * Reads basic ustar tar archives.
12 * * Does not require libarchive or any other special library.
14 * To compile: cc -o untar untar.c
16 * Usage: untar <archive>
18 * In particular, this program should be sufficient to extract the
19 * distribution for libarchive, allowing people to bootstrap
20 * libarchive on systems that do not already have a tar program.
22 * To unpack libarchive-x.y.z.tar.gz:
23 * * gunzip libarchive-x.y.z.tar.gz
24 * * untar libarchive-x.y.z.tar
26 * Written by Tim Kientzle, March 2009.
28 * Released into the public domain.
31 /* These are all highly standard and portable headers. */
36 /* This is for mkdir(); this may need to be changed for some platforms. */
37 #include <sys/stat.h> /* For mkdir() */
39 /* Parse an octal number, ignoring leading and trailing nonsense. */
41 parseoct(const char *p, size_t n)
45 while ((*p < '0' || *p > '7') && n > 0) {
49 while (*p >= '0' && *p <= '7' && n > 0) {
58 /* Returns true if this is 512 zero bytes. */
60 is_end_of_archive(const char *p)
63 for (n = 511; n >= 0; --n)
69 /* Create a directory, including parent directories as necessary. */
71 create_dir(char *pathname, int mode)
76 /* Strip trailing '/' */
77 if (pathname[strlen(pathname) - 1] == '/')
78 pathname[strlen(pathname) - 1] = '\0';
80 /* Try creating the directory. */
81 r = mkdir(pathname, mode);
84 /* On failure, try creating parent directory. */
85 p = strrchr(pathname, '/');
88 create_dir(pathname, 0755);
90 r = mkdir(pathname, mode);
94 fprintf(stderr, "Could not create directory %s\n", pathname);
97 /* Create a file, including parent directory as necessary. */
99 create_file(char *pathname, int mode)
102 f = fopen(pathname, "wb+");
104 /* Try creating parent dir and then creating file. */
105 char *p = strrchr(pathname, '/');
108 create_dir(pathname, 0755);
110 f = fopen(pathname, "wb+");
116 /* Verify the tar checksum. */
118 verify_checksum(const char *p)
121 for (n = 0; n < 512; ++n) {
122 if (n < 148 || n > 155)
123 /* Standard tar checksum adds unsigned bytes. */
124 u += ((unsigned char *)p)[n];
129 return (u == parseoct(p + 148, 8));
132 /* Extract a tar archive. */
134 untar(FILE *a, const char *path)
141 printf("Extracting from %s\n", path);
143 bytes_read = fread(buff, 1, 512, a);
144 if (bytes_read < 512) {
146 "Short read on %s: expected 512, got %d\n",
147 path, (int)bytes_read);
150 if (is_end_of_archive(buff)) {
151 printf("End of %s\n", path);
154 if (!verify_checksum(buff)) {
155 fprintf(stderr, "Checksum failure\n");
158 filesize = parseoct(buff + 124, 12);
161 printf(" Ignoring hardlink %s\n", buff);
164 printf(" Ignoring symlink %s\n", buff);
167 printf(" Ignoring character device %s\n", buff);
170 printf(" Ignoring block device %s\n", buff);
173 printf(" Extracting dir %s\n", buff);
174 create_dir(buff, parseoct(buff + 100, 8));
178 printf(" Ignoring FIFO %s\n", buff);
181 printf(" Extracting file %s\n", buff);
182 f = create_file(buff, parseoct(buff + 100, 8));
185 while (filesize > 0) {
186 bytes_read = fread(buff, 1, 512, a);
187 if (bytes_read < 512) {
189 "Short read on %s: Expected 512, got %d\n",
190 path, (int)bytes_read);
194 bytes_read = filesize;
196 if (fwrite(buff, 1, bytes_read, f)
199 fprintf(stderr, "Failed write\n");
204 filesize -= bytes_read;
214 main(int argc, char **argv)
218 ++argv; /* Skip program name */
219 for ( ;*argv != NULL; ++argv) {
220 a = fopen(*argv, "rb");
222 fprintf(stderr, "Unable to open %s\n", *argv);