2 /* Minitar: extract .tar.gz files on Win32 platforms.
3 Uses zlib for decompression.
5 This is very simple-minded, it ignores checksums, and any type of file
6 that is not a plain file or a directory. Nonetheless it is useful.
8 Author: Charles G. Waldman (cgw@pgt.com), Aug 4 1998
10 This file is placed in the public domain; you can
11 do whatever you like with it. There is NO WARRANTY.
12 If it breaks, you get to keep both pieces */
22 fprintf(stderr,"Usage: %s file.tar.gz [base-dir]\n",name);
23 fprintf(stderr,"\tExtracts the contents compressed tar file to base-dir\n");
29 #define MAXNAMELEN 1024
34 sscanf(str,"%o",&ret);
38 /* this is like mkdir -p, except if there is no trailing slash,
39 the final component is assumed to be a file, rather than a
40 path component, so it is not created as a directory */
42 int makepath(char *path)
47 for (cp=path; cp; cp = (char*)strchr(cp+1,'/')){
52 strncpy(tmp, path, cp-path);
69 main(int argc, char **argv)
71 char fullname[MAXNAMELEN];
81 gzFile *infile = (gzFile*)0;
82 FILE *outfile = (FILE*)0;
84 char block[BLOCKSIZE];
85 int nbytes, nread, nwritten;
90 if (argc < 2 || argc > 3)
97 if (! (infile = gzopen(tarfile,"rb"))){
98 fprintf(stderr,"Cannot open %s\n", tarfile);
105 nread = gzread(infile,block,512);
107 if (!in_block && nread == 0)
110 if (nread != BLOCKSIZE){
111 fprintf(stderr,"Error: incomplete block read. Exiting.\n");
116 if (block[0]=='\0') /* We're done */
119 strncpy(magic,block+257,6);
121 if (strcmp(magic,"ustar ")){
123 "Error: incorrect magic number in tar header. Exiting\n");
126 strncpy(name,block,100);
128 sprintf(fullname,"%s/%s",basedir,name);
129 printf("%s\n",fullname);
141 fprintf(stderr,"Error: unknown type flag %c. Exiting.\n",type);
148 /* makepath will ignore the final path component, so make sure
149 dirnames have a trailing slash */
151 if (fullname[strlen(fullname)-1] != '/')
152 strcat(fullname,"/");
153 if (makepath(fullname)){
154 fprintf(stderr, "Error: cannot create directory %s. Exiting.\n",
162 if (fclose(outfile)){
163 fprintf(stderr,"Error: cannot close file %s. Exiting.\n",
170 if ( !(outfile = fopen(fullname,"wb"))){
171 /*try creating the directory, maybe it's not there */
172 if (makepath(fullname)){
173 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
177 /* now try again to open the file */
178 if (!(outfile = fopen(fullname,"wb"))){
179 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
185 strncpy(osize,block+124,12);
189 fprintf(stderr,"Error: invalid size in tar header. Exiting.\n");
193 } else { /* write or continue writing file contents */
194 nbytes = size>512? 512:size;
196 nwritten = fwrite(block, 1, nbytes, outfile);
197 if (nwritten != nbytes){
198 fprintf(stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",