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 92 struct tar_header header;
97 struct attr_item *next;
111 void push_attr OF((
struct attr_item **,
char *,
int, time_t));
123 int main OF((
int,
char **));
127 const char *
TGZsuffix[] = {
"\0",
".tar",
".tar.gz",
".taz",
".tgz",
NULL };
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",
182 result = result * 8 + (c -
'0');
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);
255 struct attr_item *item;
257 item = (
struct attr_item *)
malloc(
sizeof(
struct attr_item));
259 error(
"Out of memory");
260 item->fname = strdup(fname);
272 struct attr_item *item, *prev;
274 for (item = *list; item !=
NULL; )
277 chmod(item->fname,item->mode);
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);
388 union tar_buffer buffer;
397 struct attr_item *attributes =
NULL;
400 printf(
" date time size file\n" 401 " ---------- -------- --------- -------------------------------------\n");
427 if (len == 0 || buffer.header.name[0] == 0)
430 tarmode =
getoct(buffer.header.mode,8);
431 tartime = (time_t)
getoct(buffer.header.mtime,12);
432 if (tarmode == -1 || tartime == (time_t)-1)
434 buffer.header.name[0] = 0;
450 error(
"bad long name");
457 switch (buffer.header.typeflag)
461 printf(
" %s <dir> %s\n",
strtime(&tartime),fname);
465 push_attr(&attributes,fname,tarmode,tartime);
470 remaining =
getoct(buffer.header.size,12);
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);
505 remaining =
getoct(buffer.header.size,12);
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)
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)
GLuint GLint GLboolean GLint GLenum access
int makedir(char *newdir)
int ZEXPORT gzclose(gzFile file)
GLint GLint GLsizei width
unsigned short prefix[65536]
#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)
const char *ZEXPORT zlibVersion()
GLuint const GLchar * name
if(!abbox) return FT_THROW(Invalid_Argument)
const char *ZEXPORT gzerror(gzFile file, int *errnum)
int getoct(char *p, int width)
gzFile ZEXPORT gzopen(char *path, const char *mode) const
int ExprMatch(char *string, char *expr)
void TGZnotfound(const char *arcname)
char *TGZfname OF((const char *))
char * TGZfname(const char *arcname)
int ZEXPORT gzread(gzFile file, voidp buf, unsigned len)
int tar(gzFile in, int action, int arg, int argc, char **argv)
void restore_attr(struct attr_item **list)