29 # define mkdir(dirname,mode) _mkdir(dirname) 31 # define access(path,mode) _access(path,mode) 32 # define chmod(path,mode) _chmod(path,mode) 33 # define strdup(str) _strdup(str) 54 #define GNUTYPE_DUMPDIR 'D' 55 #define GNUTYPE_LONGLINK 'K' 56 #define GNUTYPE_LONGNAME 'L' 57 #define GNUTYPE_MULTIVOL 'M' 58 #define GNUTYPE_NAMES 'N' 59 #define GNUTYPE_SPARSE 'S' 60 #define GNUTYPE_VOLHDR 'V' 66 #define SHORTNAMESIZE 100 120 int tar OF((gzFile,
int,
int,
int,
char **));
123 int main OF((
int,
char **));
127 const char *
TGZsuffix[] = {
"\0",
".tar",
".tar.gz",
".taz",
".tgz", NULL };
134 static char buffer[1024];
137 strcpy(buffer,arcname);
138 origlen = strlen(buffer);
143 if (
access(buffer,F_OK) == 0)
156 fprintf(stderr,
"%s: Couldn't find ",
prog);
158 fprintf(stderr,(
TGZsuffix[i+1]) ?
"%s%s, " :
"or %s%s\n",
180 if (c < '0' || c >
'7')
182 result = result * 8 + (c -
'0');
194 static char result[32];
196 local = localtime(t);
197 sprintf(result,
"%4d/%02d/%02d %02d:%02d:%02d",
198 local->tm_year+1900, local->tm_mon+1, local->tm_mday,
199 local->tm_hour, local->tm_min, local->tm_sec);
209 static int isWinNT = -1;
211 FILETIME locft, modft;
216 loctm = localtime(&ftime);
220 st.wYear = (WORD)loctm->tm_year + 1900;
221 st.wMonth = (WORD)loctm->tm_mon + 1;
222 st.wDayOfWeek = (WORD)loctm->tm_wday;
223 st.wDay = (WORD)loctm->tm_mday;
224 st.wHour = (WORD)loctm->tm_hour;
225 st.wMinute = (WORD)loctm->tm_min;
226 st.wSecond = (WORD)loctm->tm_sec;
227 st.wMilliseconds = 0;
228 if (!SystemTimeToFileTime(&st, &locft) ||
229 !LocalFileTimeToFileTime(&locft, &modft))
233 isWinNT = (GetVersion() < 0x80000000) ? 1 : 0;
234 hFile = CreateFile(fname, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
235 (isWinNT ? FILE_FLAG_BACKUP_SEMANTICS : 0),
239 result = SetFileTime(hFile, NULL, NULL, &modft) ? 0 : -1;
243 struct utimbuf settime;
245 settime.actime = settime.modtime = ftime;
246 return utime(fname,&settime);
259 error(
"Out of memory");
260 item->
fname = strdup(fname);
274 for (item = *list; item != NULL; )
288 #define ISSPECIAL(c) (((c) == '*') || ((c) == '/')) 298 if (*
string !=
'\\' && *
string !=
'/')
302 else if (*expr ==
'*')
306 while (*++
string != *expr)
313 if (*
string != *expr)
330 char *buffer = strdup(newdir);
332 int len = strlen(buffer);
338 if (buffer[len-1] ==
'/') {
339 buffer[len-1] =
'\0';
341 if (mkdir(buffer, 0755) == 0)
352 while(*p && *p !=
'\\' && *p !=
'/')
356 if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT))
358 fprintf(stderr,
"%s: Couldn't create directory %s\n",
prog,buffer);
386 int tar (gzFile
in,
int action,
int arg,
int argc,
char **argv)
393 FILE *outfile = NULL;
400 printf(
" date time size file\n" 401 " ---------- -------- --------- -------------------------------------\n");
432 if (tarmode == -1 || tartime == (time_t)-1)
450 error(
"bad long name");
461 printf(
" %s <dir> %s\n",
strtime(&tartime),fname);
465 push_attr(&attributes,fname,tarmode,tartime);
477 printf(
" %s %9d %s\n",
strtime(&tartime),remaining,fname);
482 outfile = fopen(fname,
"wb");
483 if (outfile == NULL) {
485 char *p = strrchr(fname,
'/');
490 outfile = fopen(fname,
"wb");
494 printf(
"Extracting %s\n",fname);
496 fprintf(stderr,
"%s: Couldn't create %s",
prog,fname);
506 if (remaining < 0 || remaining >=
BLOCKSIZE)
514 if (fname[
BLOCKSIZE-1] != 0 || (
int)strlen(fname) > remaining)
523 printf(
" %s <---> %s\n",
strtime(&tartime),fname);
533 if (fwrite(&buffer,
sizeof(
char),bytes,outfile) != bytes)
536 "%s: Error writing %s -- skipping\n",
prog,fname);
553 push_attr(&attributes,fname,tarmode,tartime);
562 error(
"broken archive");
573 error(
"failed gzclose");
583 printf(
"untgz version 0.2.1\n" 584 " using zlib version %s\n\n",
586 printf(
"Usage: untgz file.tgz extract all files\n" 587 " untgz file.tgz fname ... extract selected files\n" 588 " untgz -l file.tgz list archive contents\n" 589 " untgz -h display this help\n");
595 fprintf(stderr,
"%s: %s\n",
prog, msg);
602 #if defined(WIN32) && defined(__GNUC__) 613 prog = strrchr(argv[0],
'\\');
616 prog = strrchr(argv[0],
'/');
619 prog = strrchr(argv[0],
':');
634 if (strcmp(argv[arg],
"-l") == 0)
640 else if (strcmp(argv[arg],
"-h") == 0)
645 if ((TGZfile =
TGZfname(argv[arg])) == NULL)
649 if ((action ==
TGZ_LIST) && (arg != argc))
662 fprintf(stderr,
"%s: Couldn't gzopen %s\n",
prog,TGZfile);
665 exit(
tar(f, action, arg, argc, argv));
669 error(
"Unknown option");
int matchname(int arg, int argc, char **argv, char *fname)
int makedir(char *newdir)
int gzread(gzFile file, voidp buf, unsigned len)
gzFile gzopen(char *path, char *mode)
#define INVALID_HANDLE_VALUE
char * strtime(time_t *t)
int main(int argc, char **argv)
void push_attr(struct attr_item **list, char *fname, int mode, time_t time)
void error(const char *msg)
int setfiletime(char *fname, time_t ftime)
int getoct(char *p, int width)
static unsigned in(void *in_desc, z_const unsigned char **buf)
int ExprMatch(char *string, char *expr)
void TGZnotfound(const char *arcname)
char *TGZfname OF((const char *))
char * TGZfname(const char *arcname)
int tar(gzFile in, int action, int arg, int argc, char **argv)
char * gzerror(gzFile file, int *errnum)
void restore_attr(struct attr_item **list)