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 */
21 # include <direct.h> /* For mkdir */
29 fprintf(stderr,"Usage: %s file.tar.gz [base-dir]\n",name);
30 fprintf(stderr,"\tExtracts the contents compressed tar file to base-dir\n");
36 #define MAXNAMELEN 1024
42 sscanf(str,"%o",&ret);
46 /* this is like mkdir -p, except if there is no trailing slash,
47 the final component is assumed to be a file, rather than a
48 path component, so it is not created as a directory */
56 for (cp=path; cp; cp = (char*)strchr(cp+1,'/')){
61 strncpy(tmp, path, cp-path);
83 main(int argc, char **argv)
85 char fullname[MAXNAMELEN];
94 gzFile *infile = (gzFile*)0;
95 FILE *outfile = (FILE*)0;
97 char block[BLOCKSIZE];
98 int nbytes, nread, nwritten;
103 if (argc < 2 || argc > 3)
110 if (! (infile = gzopen(tarfile,"rb"))){
111 fprintf(stderr,"Cannot open %s\n", tarfile);
118 nread = gzread(infile,block,512);
120 if (!in_block && nread == 0)
123 if (nread != BLOCKSIZE){
124 fprintf(stderr,"Error: incomplete block read. Exiting.\n");
129 if (block[0]=='\0') /* We're done */
132 strncpy(magic,block+257,6);
134 if (strcmp(magic,"ustar ")){
136 "Error: incorrect magic number in tar header. Exiting\n");
139 strncpy(name,block,100);
141 sprintf(fullname,"%s/%s",basedir,name);
142 printf("%s\n",fullname);
154 fprintf(stderr,"Error: unknown type flag %c. Exiting.\n",type);
161 /* makepath will ignore the final path component, so make sure
162 dirnames have a trailing slash */
164 if (fullname[strlen(fullname)-1] != '/')
165 strcat(fullname,"/");
166 if (makepath(fullname)){
167 fprintf(stderr, "Error: cannot create directory %s. Exiting.\n",
175 if (fclose(outfile)){
176 fprintf(stderr,"Error: cannot close file %s. Exiting.\n",
183 if ( !(outfile = fopen(fullname,"wb"))){
184 /*try creating the directory, maybe it's not there */
185 if (makepath(fullname)){
186 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
190 /* now try again to open the file */
191 if (!(outfile = fopen(fullname,"wb"))){
192 fprintf(stderr,"Error: cannot create file %s. Exiting.\n",
198 strncpy(osize,block+124,12);
202 fprintf(stderr,"Error: invalid size in tar header. Exiting.\n");
206 } else { /* write or continue writing file contents */
207 nbytes = size>512? 512:size;
209 nwritten = fwrite(block, 1, nbytes, outfile);
210 if (nwritten != nbytes){
211 fprintf(stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",