comparison nt/minitar.c @ 359:8e84bee8ddd0 r21-1-9

Import from CVS: tag r21-1-9
author cvs
date Mon, 13 Aug 2007 10:57:55 +0200
parents 03446687b7cc
children
comparison
equal deleted inserted replaced
358:fed6e0f6a03a 359:8e84bee8ddd0
1 1
2 /* Minitar: extract .tar.gz files on Win32 platforms. 2 /* Minitar: extract .tar.gz files on Win32 platforms.
3 Uses zlib for decompression. 3 Uses zlib for decompression.
4 4
5 This is very simple-minded, it ignores checksums, and any type of file 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. 6 that is not a plain file or a directory. Nonetheless it is useful.
7 7
8 Author: Charles G. Waldman (cgw@pgt.com), Aug 4 1998 8 Author: Charles G. Waldman (cgw@pgt.com), Aug 4 1998
9 9
10 This file is placed in the public domain; you can 10 This file is placed in the public domain; you can
11 do whatever you like with it. There is NO WARRANTY. 11 do whatever you like with it. There is NO WARRANTY.
12 If it breaks, you get to keep both pieces */ 12 If it breaks, you get to keep both pieces */
13 13
14 14
15 #include <stdio.h> 15 #include <stdio.h>
16 #include <Errno.h> 16 #include <Errno.h>
62 } 62 }
63 } 63 }
64 return 0; 64 return 0;
65 } 65 }
66 66
67 67
68 68
69 69
70 main(int argc, char **argv) 70 main(int argc, char **argv)
71 { 71 {
72 char fullname[MAXNAMELEN]; 72 char fullname[MAXNAMELEN];
73 char *basedir = "."; 73 char *basedir = ".";
76 int size; 76 int size;
77 char osize[13]; 77 char osize[13];
78 char name[101]; 78 char name[101];
79 char magic[7]; 79 char magic[7];
80 char type; 80 char type;
81 81
82 gzFile *infile = (gzFile*)0; 82 gzFile *infile = (gzFile*)0;
83 FILE *outfile = (FILE*)0; 83 FILE *outfile = (FILE*)0;
84 84
85 char block[BLOCKSIZE]; 85 char block[BLOCKSIZE];
86 int nbytes, nread, nwritten; 86 int nbytes, nread, nwritten;
97 97
98 if (! (infile = gzopen(tarfile,"rb"))){ 98 if (! (infile = gzopen(tarfile,"rb"))){
99 fprintf(stderr,"Cannot open %s\n", tarfile); 99 fprintf(stderr,"Cannot open %s\n", tarfile);
100 exit(-2); 100 exit(-2);
101 } 101 }
102 102
103 while (1){ 103 while (1){
104 104
105 105
106 nread = gzread(infile,block,512); 106 nread = gzread(infile,block,512);
107 107
108 if (!in_block && nread == 0) 108 if (!in_block && nread == 0)
109 break; 109 break;
127 strncpy(name,block,100); 127 strncpy(name,block,100);
128 name[100] = '\0'; 128 name[100] = '\0';
129 sprintf(fullname,"%s/%s",basedir,name); 129 sprintf(fullname,"%s/%s",basedir,name);
130 printf("%s\n",fullname); 130 printf("%s\n",fullname);
131 type = block[156]; 131 type = block[156];
132 132
133 switch(type){ 133 switch(type){
134 case '0': 134 case '0':
135 case '\0': 135 case '\0':
136 directory = 0; 136 directory = 0;
137 break; 137 break;
140 break; 140 break;
141 default: 141 default:
142 fprintf(stderr,"Error: unknown type flag %c. Exiting.\n",type); 142 fprintf(stderr,"Error: unknown type flag %c. Exiting.\n",type);
143 break; 143 break;
144 } 144 }
145 145
146 if (directory){ 146 if (directory){
147 in_block = 0; 147 in_block = 0;
148 148
149 /* makepath will ignore the final path component, so make sure 149 /* makepath will ignore the final path component, so make sure
150 dirnames have a trailing slash */ 150 dirnames have a trailing slash */
151 151
152 if (fullname[strlen(fullname)-1] != '/') 152 if (fullname[strlen(fullname)-1] != '/')
153 strcat(fullname,"/"); 153 strcat(fullname,"/");
154 if (makepath(fullname)){ 154 if (makepath(fullname)){
191 exit(-2); 191 exit(-2);
192 } 192 }
193 } 193 }
194 } else { /* write or continue writing file contents */ 194 } else { /* write or continue writing file contents */
195 nbytes = size>512? 512:size; 195 nbytes = size>512? 512:size;
196 196
197 nwritten = fwrite(block, 1, nbytes, outfile); 197 nwritten = fwrite(block, 1, nbytes, outfile);
198 if (nwritten != nbytes){ 198 if (nwritten != nbytes){
199 fprintf(stderr, "Error: only wrote %d bytes to file %s. Exiting.\n", 199 fprintf(stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",
200 nwritten, fullname); 200 nwritten, fullname);
201 } 201 }
202 size -= nbytes; 202 size -= nbytes;
203 if (size==0) 203 if (size==0)
204 in_block = 0; 204 in_block = 0;
205 } 205 }
206 } 206 }
207 } 207
208 208 exit(0);
209 209 }
210 210
211 211
212
213