zlib/contrib/untgz/untgz.c

532 lines
13 KiB
C
Raw Normal View History

2011-09-10 02:17:33 -04:00
/*
* untgz.c -- Display contents and/or extract file from
* a gzip'd TAR file
* written by "Pedro A. Aranda Guti\irrez" <paag@tid.es>
* adaptation to Unix by Jean-loup Gailly <jloup@gzip.org>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#ifdef unix
# include <unistd.h>
#else
# include <direct.h>
# include <io.h>
#endif
#include "zlib.h"
2011-09-10 02:20:29 -04:00
#ifdef WIN32
2011-09-10 02:22:37 -04:00
#include <windows.h>
2011-09-10 02:20:29 -04:00
# ifndef F_OK
# define F_OK (0)
# endif
# ifdef _MSC_VER
# define mkdir(dirname,mode) _mkdir(dirname)
# define strdup(str) _strdup(str)
# define unlink(fn) _unlink(fn)
# define access(path,mode) _access(path,mode)
# else
# define mkdir(dirname,mode) _mkdir(dirname)
# endif
#else
# include <utime.h>
#endif
2011-09-10 02:17:33 -04:00
/* Values used in typeflag field. */
2011-09-10 02:22:37 -04:00
#define REGTYPE '0' /* regular file */
#define AREGTYPE '\0' /* regular file */
#define LNKTYPE '1' /* link */
#define SYMTYPE '2' /* reserved */
#define CHRTYPE '3' /* character special */
#define BLKTYPE '4' /* block special */
#define DIRTYPE '5' /* directory */
#define FIFOTYPE '6' /* FIFO special */
#define CONTTYPE '7' /* reserved */
2011-09-10 02:17:33 -04:00
#define BLOCKSIZE 512
struct tar_header
2011-09-10 02:22:37 -04:00
{ /* byte offset */
char name[100]; /* 0 */
char mode[8]; /* 100 */
char uid[8]; /* 108 */
char gid[8]; /* 116 */
char size[12]; /* 124 */
char mtime[12]; /* 136 */
char chksum[8]; /* 148 */
char typeflag; /* 156 */
char linkname[100]; /* 157 */
char magic[6]; /* 257 */
char version[2]; /* 263 */
char uname[32]; /* 265 */
char gname[32]; /* 297 */
char devmajor[8]; /* 329 */
char devminor[8]; /* 337 */
char prefix[155]; /* 345 */
/* 500 */
2011-09-10 02:17:33 -04:00
};
union tar_buffer {
char buffer[BLOCKSIZE];
struct tar_header header;
};
enum { TGZ_EXTRACT = 0, TGZ_LIST };
2011-09-10 02:22:37 -04:00
static char *TGZfname OF((const char *));
void TGZnotfound OF((const char *));
2011-09-10 02:17:33 -04:00
2011-09-10 02:22:37 -04:00
int getoct OF((char *, int));
char *strtime OF((time_t *));
int setftime OF((char *, time_t));
int ExprMatch OF((char *, char *));
2011-09-10 02:17:33 -04:00
2011-09-10 02:22:37 -04:00
int makedir OF((char *));
int matchname OF((int, int, char **, char *));
2011-09-10 02:17:33 -04:00
2011-09-10 02:22:37 -04:00
void error OF((const char *));
int tar OF((gzFile, int, int, int, char **));
2011-09-10 02:17:33 -04:00
2011-09-10 02:22:37 -04:00
void help OF((int));
int main OF((int, char **));
2011-09-10 02:17:33 -04:00
char *prog;
/* This will give a benign warning */
2011-09-10 02:22:37 -04:00
static char *TGZsuffix[] = { "\0", ".tar", ".tar.gz", ".taz", ".tgz", NULL };
2011-09-10 02:17:33 -04:00
/* Return the real name of the TGZ archive */
/* or NULL if it does not exist. */
2011-09-10 02:22:37 -04:00
static char *TGZfname (const char *fname)
2011-09-10 02:17:33 -04:00
{
static char buffer[1024];
int origlen,i;
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
strcpy(buffer,fname);
origlen = strlen(buffer);
2011-09-10 02:22:37 -04:00
for (i=0; TGZsuffix[i]; i++)
2011-09-10 02:17:33 -04:00
{
2011-09-10 02:22:37 -04:00
strcpy(buffer+origlen,TGZsuffix[i]);
2011-09-10 02:17:33 -04:00
if (access(buffer,F_OK) == 0)
return buffer;
}
return NULL;
}
/* error message for the filename */
2011-09-10 02:22:37 -04:00
void TGZnotfound (const char *fname)
2011-09-10 02:17:33 -04:00
{
int i;
fprintf(stderr,"%s : couldn't find ",prog);
2011-09-10 02:22:37 -04:00
for (i=0;TGZsuffix[i];i++)
fprintf(stderr,(TGZsuffix[i+1]) ? "%s%s, " : "or %s%s\n",
2011-09-10 02:17:33 -04:00
fname,
2011-09-10 02:22:37 -04:00
TGZsuffix[i]);
2011-09-10 02:17:33 -04:00
exit(1);
}
/* help functions */
2011-09-10 02:22:37 -04:00
int getoct (char *p,int width)
2011-09-10 02:17:33 -04:00
{
int result = 0;
char c;
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
while (width --)
{
c = *p++;
if (c == ' ')
2011-09-10 02:22:37 -04:00
continue;
2011-09-10 02:17:33 -04:00
if (c == 0)
2011-09-10 02:22:37 -04:00
break;
2011-09-10 02:17:33 -04:00
result = result * 8 + (c - '0');
}
return result;
}
char *strtime (time_t *t)
{
struct tm *local;
static char result[32];
local = localtime(t);
sprintf(result,"%2d/%02d/%4d %02d:%02d:%02d",
2011-09-10 02:22:37 -04:00
local->tm_mday, local->tm_mon+1, local->tm_year+1900,
local->tm_hour, local->tm_min, local->tm_sec);
return result;
}
int setftime (char *fname,time_t ftime)
{
#ifdef WIN32
SYSTEMTIME st;
FILETIME locft, modft;
struct tm *loctm;
HANDLE hFile;
int result;
loctm = localtime(&ftime);
if (loctm == NULL)
return -1;
st.wYear = (WORD)loctm->tm_year + 1900;
st.wMonth = (WORD)loctm->tm_mon + 1;
st.wDayOfWeek = (WORD)loctm->tm_wday;
st.wDay = (WORD)loctm->tm_mday;
st.wHour = (WORD)loctm->tm_hour;
st.wMinute = (WORD)loctm->tm_min;
st.wSecond = (WORD)loctm->tm_sec;
st.wMilliseconds = 0;
if (!SystemTimeToFileTime(&st, &locft) ||
!LocalFileTimeToFileTime(&locft, &modft))
return -1;
hFile = CreateFile(fname, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
return -1;
result = SetFileTime(hFile, NULL, NULL, &modft) ? 0 : -1;
CloseHandle(hFile);
2011-09-10 02:17:33 -04:00
return result;
2011-09-10 02:22:37 -04:00
#else
struct utimbuf settime;
settime.actime = settime.modtime = ftime;
return utime(fname,&settime);
#endif
2011-09-10 02:17:33 -04:00
}
/* regular expression matching */
#define ISSPECIAL(c) (((c) == '*') || ((c) == '/'))
2011-09-10 02:22:37 -04:00
int ExprMatch (char *string,char *expr)
2011-09-10 02:17:33 -04:00
{
while (1)
{
if (ISSPECIAL(*expr))
2011-09-10 02:22:37 -04:00
{
if (*expr == '/')
{
if (*string != '\\' && *string != '/')
return 0;
string ++; expr++;
}
else if (*expr == '*')
{
if (*expr ++ == 0)
return 1;
while (*++string != *expr)
if (*string == 0)
return 0;
}
}
2011-09-10 02:17:33 -04:00
else
2011-09-10 02:22:37 -04:00
{
if (*string != *expr)
return 0;
if (*expr++ == 0)
return 1;
string++;
}
2011-09-10 02:17:33 -04:00
}
}
/* recursive make directory */
/* abort if you get an ENOENT errno somewhere in the middle */
/* e.g. ignore error "mkdir on existing directory" */
/* */
/* return 1 if OK */
/* 0 on error */
int makedir (char *newdir)
{
char *buffer = strdup(newdir);
char *p;
int len = strlen(buffer);
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
if (len <= 0) {
free(buffer);
return 0;
}
if (buffer[len-1] == '/') {
buffer[len-1] = '\0';
}
if (mkdir(buffer, 0775) == 0)
{
free(buffer);
return 1;
}
p = buffer+1;
while (1)
{
char hold;
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
while(*p && *p != '\\' && *p != '/')
2011-09-10 02:22:37 -04:00
p++;
2011-09-10 02:17:33 -04:00
hold = *p;
*p = 0;
if ((mkdir(buffer, 0775) == -1) && (errno == ENOENT))
2011-09-10 02:22:37 -04:00
{
fprintf(stderr,"%s: couldn't create directory %s\n",prog,buffer);
free(buffer);
return 0;
}
2011-09-10 02:17:33 -04:00
if (hold == 0)
2011-09-10 02:22:37 -04:00
break;
2011-09-10 02:17:33 -04:00
*p++ = hold;
}
free(buffer);
return 1;
}
int matchname (int arg,int argc,char **argv,char *fname)
{
2011-09-10 02:22:37 -04:00
if (arg == argc) /* no arguments given (untgz tgzarchive) */
2011-09-10 02:17:33 -04:00
return 1;
while (arg < argc)
if (ExprMatch(fname,argv[arg++]))
return 1;
return 0; /* ignore this for the moment being */
}
/* Tar file list or extract */
int tar (gzFile in,int action,int arg,int argc,char **argv)
{
union tar_buffer buffer;
int len;
int err;
int getheader = 1;
int remaining = 0;
FILE *outfile = NULL;
char fname[BLOCKSIZE];
time_t tartime;
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
if (action == TGZ_LIST)
printf(" day time size file\n"
2011-09-10 02:22:37 -04:00
" ---------- -------- --------- -------------------------------------\n");
2011-09-10 02:17:33 -04:00
while (1)
{
len = gzread(in, &buffer, BLOCKSIZE);
if (len < 0)
2011-09-10 02:22:37 -04:00
error (gzerror(in, &err));
2011-09-10 02:17:33 -04:00
/*
* Always expect complete blocks to process
* the tar information.
*/
if (len != BLOCKSIZE)
2011-09-10 02:22:37 -04:00
error("gzread: incomplete block read");
2011-09-10 02:17:33 -04:00
/*
* If we have to get a tar header
*/
if (getheader == 1)
2011-09-10 02:22:37 -04:00
{
/*
* if we met the end of the tar
* or the end-of-tar block,
* we are done
*/
if ((len == 0) || (buffer.header.name[0]== 0)) break;
tartime = (time_t)getoct(buffer.header.mtime,12);
strcpy(fname,buffer.header.name);
switch (buffer.header.typeflag)
{
case DIRTYPE:
if (action == TGZ_LIST)
printf(" %s <dir> %s\n",strtime(&tartime),fname);
if (action == TGZ_EXTRACT)
makedir(fname);
break;
case REGTYPE:
case AREGTYPE:
remaining = getoct(buffer.header.size,12);
if (action == TGZ_LIST)
printf(" %s %9d %s\n",strtime(&tartime),remaining,fname);
if (action == TGZ_EXTRACT)
{
if ((remaining) && (matchname(arg,argc,argv,fname)))
{
outfile = fopen(fname,"wb");
if (outfile == NULL) {
/* try creating directory */
char *p = strrchr(fname, '/');
if (p != NULL) {
*p = '\0';
makedir(fname);
*p = '/';
outfile = fopen(fname,"wb");
}
}
fprintf(stderr,
"%s %s\n",
(outfile) ? "Extracting" : "Couldn't create",
fname);
}
else
outfile = NULL;
}
/*
* could have no contents
*/
getheader = (remaining) ? 0 : 1;
break;
default:
if (action == TGZ_LIST)
printf(" %s <---> %s\n",strtime(&tartime),fname);
break;
}
}
2011-09-10 02:17:33 -04:00
else
2011-09-10 02:22:37 -04:00
{
unsigned int bytes = (remaining > BLOCKSIZE) ? BLOCKSIZE : remaining;
if ((action == TGZ_EXTRACT) && (outfile != NULL))
{
if (fwrite(&buffer,sizeof(char),bytes,outfile) != bytes)
{
fprintf(stderr,"%s : error writing %s skipping...\n",prog,fname);
fclose(outfile);
unlink(fname);
}
}
remaining -= bytes;
if (remaining == 0)
{
getheader = 1;
if ((action == TGZ_EXTRACT) && (outfile != NULL))
{
fclose(outfile);
outfile = NULL;
setftime(fname,tartime);
}
}
}
2011-09-10 02:17:33 -04:00
}
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
if (gzclose(in) != Z_OK)
error("failed gzclose");
return 0;
}
/* =========================================================== */
void help(int exitval)
{
fprintf(stderr,
2011-09-10 02:22:37 -04:00
"untgz version 0.1\n"
" a sample application of zlib\n\n"
"Usage : untgz file.tgz to extract all files\n"
" untgz file.tgz fname ... to extract selected files\n"
" untgz -l file.tgz to list archive contents\n"
" untgz -h to display this help\n\n");
2011-09-10 02:17:33 -04:00
exit(exitval);
}
void error(const char *msg)
{
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
/* ====================================================================== */
2011-09-10 02:22:37 -04:00
int _CRT_glob = 0; /* disable globbing of the arguments */
2011-09-10 02:17:33 -04:00
int main(int argc,char **argv)
{
2011-09-10 02:22:37 -04:00
int action = TGZ_EXTRACT;
int arg = 1;
char *TGZfile;
gzFile *f;
2011-09-10 02:17:33 -04:00
prog = strrchr(argv[0],'\\');
if (prog == NULL)
{
2011-09-10 02:22:37 -04:00
prog = strrchr(argv[0],'/');
if (prog == NULL)
{
prog = strrchr(argv[0],':');
if (prog == NULL)
prog = argv[0];
else
prog++;
}
else
prog++;
2011-09-10 02:17:33 -04:00
}
else
prog++;
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
if (argc == 1)
help(0);
if (strcmp(argv[arg],"-l") == 0)
{
2011-09-10 02:22:37 -04:00
action = TGZ_LIST;
if (argc == ++arg)
help(0);
2011-09-10 02:17:33 -04:00
}
else if (strcmp(argv[arg],"-h") == 0)
{
2011-09-10 02:22:37 -04:00
help(0);
2011-09-10 02:17:33 -04:00
}
if ((TGZfile = TGZfname(argv[arg])) == NULL)
2011-09-10 02:22:37 -04:00
TGZnotfound(argv[arg]);
2011-09-10 02:17:33 -04:00
++arg;
if ((action == TGZ_LIST) && (arg != argc))
help(1);
/*
* Process the TGZ file
*/
switch(action)
{
case TGZ_LIST:
case TGZ_EXTRACT:
2011-09-10 02:22:37 -04:00
f = gzopen(TGZfile,"rb");
if (f == NULL)
{
fprintf(stderr,"%s: Couldn't gzopen %s\n",
prog,
TGZfile);
return 1;
}
exit(tar(f, action, arg, argc, argv));
2011-09-10 02:17:33 -04:00
break;
2011-09-10 02:22:37 -04:00
2011-09-10 02:17:33 -04:00
default:
2011-09-10 02:22:37 -04:00
error("Unknown option!");
exit(1);
2011-09-10 02:17:33 -04:00
}
return 0;
}