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)
48 for (cp=path; cp; cp = (char*)strchr(cp+1,'/')){
53 strncpy(tmp, path, cp-path);
70 main(int argc, char **argv)
72 char fullname[MAXNAMELEN];
82 gzFile *infile = (gzFile*)0;
83 FILE *outfile = (FILE*)0;
85 char block[BLOCKSIZE];
86 int nbytes, nread, nwritten;
91 if (argc < 2 || argc > 3)
98 if (! (infile = gzopen(tarfile,"rb"))){
99 fprintf(stderr,"Cannot open %s\n", tarfile);
106 nread = gzread(infile,block,512);
108 if (!in_block && nread == 0)
111 if (nread != BLOCKSIZE){
112 fprintf(stderr,"Error: incomplete block read. Exiting.\n");
117 if (block[0]=='\0') /* We're done */
120 strncpy(magic,block+257,6);
122 if (strcmp(magic,"ustar ")){
124 "Error: incorrect magic number in tar header. Exiting\n");
127 strncpy(name,block,100);
129 sprintf(fullname,"%s/%s",basedir,name);
130 printf("%s\n",fullname);
142 fprintf(stderr,"Error: unknown type flag %c. Exiting.\n",type);
149 /* makepath will ignore the final path component, so make sure
150 dirnames have a trailing slash */
152 if (fullname[strlen(fullname)-1] != '/')
153 strcat(fullname,"/");
154 if (makepath(fullname)){
155 fprintf(stderr, "Error: cannot create directory %s. Exiting.\n",
163 if (fclose(outfile)){
164 fprintf(stderr,"Error: cannot close file %s. Exiting.\n",
171 if ( !(outfile = fopen(fullname,"wb"))){
172 /*try creating the directory, maybe it's not there */
173 if (makepath(fullname)){
174 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
178 /* now try again to open the file */
179 if (!(outfile = fopen(fullname,"wb"))){
180 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
186 strncpy(osize,block+124,12);
190 fprintf(stderr,"Error: invalid size in tar header. Exiting.\n");
194 } else { /* write or continue writing file contents */
195 nbytes = size>512? 512:size;
197 nwritten = fwrite(block, 1, nbytes, outfile);
198 if (nwritten != nbytes){
199 fprintf(stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",