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 */
25 fprintf(stderr,"Usage: %s file.tar.gz [base-dir]\n",name);
26 fprintf(stderr,"\tExtracts the contents compressed tar file to base-dir\n");
32 #define MAXNAMELEN 1024
38 sscanf(str,"%o",&ret);
42 /* this is like mkdir -p, except if there is no trailing slash,
43 the final component is assumed to be a file, rather than a
44 path component, so it is not created as a directory */
52 for (cp=path; cp; cp = (char*)strchr(cp+1,'/')){
57 strncpy(tmp, path, cp-path);
79 main(int argc, char **argv)
81 char fullname[MAXNAMELEN];
90 gzFile *infile = (gzFile*)0;
91 FILE *outfile = (FILE*)0;
93 char block[BLOCKSIZE];
94 int nbytes, nread, nwritten;
99 if (argc < 2 || argc > 3)
106 if (! (infile = gzopen(tarfile,"rb"))){
107 fprintf(stderr,"Cannot open %s\n", tarfile);
114 nread = gzread(infile,block,512);
116 if (!in_block && nread == 0)
119 if (nread != BLOCKSIZE){
120 fprintf(stderr,"Error: incomplete block read. Exiting.\n");
125 if (block[0]=='\0') /* We're done */
128 strncpy(magic,block+257,6);
130 if (strcmp(magic,"ustar ")){
132 "Error: incorrect magic number in tar header. Exiting\n");
135 strncpy(name,block,100);
137 sprintf(fullname,"%s/%s",basedir,name);
138 printf("%s\n",fullname);
150 fprintf(stderr,"Error: unknown type flag %c. Exiting.\n",type);
157 /* makepath will ignore the final path component, so make sure
158 dirnames have a trailing slash */
160 if (fullname[strlen(fullname)-1] != '/')
161 strcat(fullname,"/");
162 if (makepath(fullname)){
163 fprintf(stderr, "Error: cannot create directory %s. Exiting.\n",
171 if (fclose(outfile)){
172 fprintf(stderr,"Error: cannot close file %s. Exiting.\n",
179 if ( !(outfile = fopen(fullname,"wb"))){
180 /*try creating the directory, maybe it's not there */
181 if (makepath(fullname)){
182 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
186 /* now try again to open the file */
187 if (!(outfile = fopen(fullname,"wb"))){
188 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
194 strncpy(osize,block+124,12);
198 fprintf(stderr,"Error: invalid size in tar header. Exiting.\n");
202 } else { /* write or continue writing file contents */
203 nbytes = size>512? 512:size;
205 nwritten = fwrite(block, 1, nbytes, outfile);
206 if (nwritten != nbytes){
207 fprintf(stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",