~ubuntu-branches/ubuntu/lucid/perl-tk/lucid

« back to all changes in this revision

Viewing changes to PNG/zlib/contrib/untgz/untgz.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Tuckley
  • Date: 2008-02-15 13:56:59 UTC
  • mfrom: (1.1.3 upstream) (4.1.1 hardy)
  • Revision ID: james.westby@ubuntu.com-20080215135659-ru2oqlykuju20fav
Tags: 1:804.028-1
* New Upstream Release (Closes: #463080).
* Update to Debhelper v5.
* Build with XFT=1 (Closes: #411129).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * untgz.c -- Display contents and/or extract file from
3
 
 * a gzip'd TAR file
4
 
 * written by "Pedro A. Aranda Guti\irrez" <paag@tid.es>
 
2
 * untgz.c -- Display contents and extract files from a gzip'd TAR file
 
3
 *
 
4
 * written by Pedro A. Aranda Gutierrez <paag@tid.es>
5
5
 * adaptation to Unix by Jean-loup Gailly <jloup@gzip.org>
 
6
 * various fixes by Cosmin Truta <cosmint@cs.ubbcluj.ro>
6
7
 */
7
8
 
8
9
#include <stdio.h>
10
11
#include <string.h>
11
12
#include <time.h>
12
13
#include <errno.h>
13
 
#include <fcntl.h>
 
14
 
 
15
#include "zlib.h"
 
16
 
14
17
#ifdef unix
15
 
# include <unistd.h>
 
18
#  include <unistd.h>
16
19
#else
17
 
# include <direct.h>
18
 
# include <io.h>
 
20
#  include <direct.h>
 
21
#  include <io.h>
19
22
#endif
20
23
 
21
 
#include "zlib.h"
22
 
 
23
24
#ifdef WIN32
 
25
#include <windows.h>
24
26
#  ifndef F_OK
25
 
#    define F_OK (0)
 
27
#    define F_OK  0
26
28
#  endif
 
29
#  define mkdir(dirname,mode)   _mkdir(dirname)
27
30
#  ifdef _MSC_VER
28
 
#    define mkdir(dirname,mode) _mkdir(dirname)
 
31
#    define access(path,mode)   _access(path,mode)
 
32
#    define chmod(path,mode)    _chmod(path,mode)
29
33
#    define strdup(str)         _strdup(str)
30
 
#    define unlink(fn)          _unlink(fn)
31
 
#    define access(path,mode)   _access(path,mode)
32
 
#  else
33
 
#    define mkdir(dirname,mode) _mkdir(dirname)
34
34
#  endif
35
35
#else
36
36
#  include <utime.h>
37
37
#endif
38
38
 
39
39
 
40
 
/* Values used in typeflag field.  */
41
 
 
42
 
#define REGTYPE  '0'            /* regular file */
43
 
#define AREGTYPE '\0'           /* regular file */
44
 
#define LNKTYPE  '1'            /* link */
45
 
#define SYMTYPE  '2'            /* reserved */
46
 
#define CHRTYPE  '3'            /* character special */
47
 
#define BLKTYPE  '4'            /* block special */
48
 
#define DIRTYPE  '5'            /* directory */
49
 
#define FIFOTYPE '6'            /* FIFO special */
50
 
#define CONTTYPE '7'            /* reserved */
51
 
 
52
 
#define BLOCKSIZE 512
 
40
/* values used in typeflag field */
 
41
 
 
42
#define REGTYPE  '0'            /* regular file */
 
43
#define AREGTYPE '\0'           /* regular file */
 
44
#define LNKTYPE  '1'            /* link */
 
45
#define SYMTYPE  '2'            /* reserved */
 
46
#define CHRTYPE  '3'            /* character special */
 
47
#define BLKTYPE  '4'            /* block special */
 
48
#define DIRTYPE  '5'            /* directory */
 
49
#define FIFOTYPE '6'            /* FIFO special */
 
50
#define CONTTYPE '7'            /* reserved */
 
51
 
 
52
/* GNU tar extensions */
 
53
 
 
54
#define GNUTYPE_DUMPDIR  'D'    /* file names from dumped directory */
 
55
#define GNUTYPE_LONGLINK 'K'    /* long link name */
 
56
#define GNUTYPE_LONGNAME 'L'    /* long file name */
 
57
#define GNUTYPE_MULTIVOL 'M'    /* continuation of file from another volume */
 
58
#define GNUTYPE_NAMES    'N'    /* file name that does not fit into main hdr */
 
59
#define GNUTYPE_SPARSE   'S'    /* sparse file */
 
60
#define GNUTYPE_VOLHDR   'V'    /* tape/volume header */
 
61
 
 
62
 
 
63
/* tar header */
 
64
 
 
65
#define BLOCKSIZE     512
 
66
#define SHORTNAMESIZE 100
53
67
 
54
68
struct tar_header
55
 
{                               /* byte offset */
56
 
  char name[100];               /*   0 */
57
 
  char mode[8];                 /* 100 */
58
 
  char uid[8];                  /* 108 */
59
 
  char gid[8];                  /* 116 */
60
 
  char size[12];                /* 124 */
61
 
  char mtime[12];               /* 136 */
62
 
  char chksum[8];               /* 148 */
63
 
  char typeflag;                /* 156 */
64
 
  char linkname[100];           /* 157 */
65
 
  char magic[6];                /* 257 */
66
 
  char version[2];              /* 263 */
67
 
  char uname[32];               /* 265 */
68
 
  char gname[32];               /* 297 */
69
 
  char devmajor[8];             /* 329 */
70
 
  char devminor[8];             /* 337 */
71
 
  char prefix[155];             /* 345 */
72
 
                                /* 500 */
 
69
{                               /* byte offset */
 
70
  char name[100];               /*   0 */
 
71
  char mode[8];                 /* 100 */
 
72
  char uid[8];                  /* 108 */
 
73
  char gid[8];                  /* 116 */
 
74
  char size[12];                /* 124 */
 
75
  char mtime[12];               /* 136 */
 
76
  char chksum[8];               /* 148 */
 
77
  char typeflag;                /* 156 */
 
78
  char linkname[100];           /* 157 */
 
79
  char magic[6];                /* 257 */
 
80
  char version[2];              /* 263 */
 
81
  char uname[32];               /* 265 */
 
82
  char gname[32];               /* 297 */
 
83
  char devmajor[8];             /* 329 */
 
84
  char devminor[8];             /* 337 */
 
85
  char prefix[155];             /* 345 */
 
86
                                /* 500 */
73
87
};
74
88
 
75
 
union tar_buffer {
 
89
union tar_buffer
 
90
{
76
91
  char               buffer[BLOCKSIZE];
77
92
  struct tar_header  header;
78
93
};
79
94
 
80
 
enum { TGZ_EXTRACT = 0, TGZ_LIST };
81
 
 
82
 
static char *TGZfname   OF((const char *));
83
 
void TGZnotfound        OF((const char *));
84
 
 
85
 
int getoct              OF((char *, int));
86
 
char *strtime           OF((time_t *));
87
 
int ExprMatch           OF((char *,char *));
88
 
 
89
 
int makedir             OF((char *));
90
 
int matchname           OF((int,int,char **,char *));
91
 
 
92
 
void error              OF((const char *));
93
 
int  tar                OF((gzFile, int, int, int, char **));
94
 
 
95
 
void help               OF((int));
96
 
int main                OF((int, char **));
 
95
struct attr_item
 
96
{
 
97
  struct attr_item  *next;
 
98
  char              *fname;
 
99
  int                mode;
 
100
  time_t             time;
 
101
};
 
102
 
 
103
enum { TGZ_EXTRACT, TGZ_LIST, TGZ_INVALID };
 
104
 
 
105
char *TGZfname          OF((const char *));
 
106
void TGZnotfound        OF((const char *));
 
107
 
 
108
int getoct              OF((char *, int));
 
109
char *strtime           OF((time_t *));
 
110
int setfiletime         OF((char *, time_t));
 
111
void push_attr          OF((struct attr_item **, char *, int, time_t));
 
112
void restore_attr       OF((struct attr_item **));
 
113
 
 
114
int ExprMatch           OF((char *, char *));
 
115
 
 
116
int makedir             OF((char *));
 
117
int matchname           OF((int, int, char **, char *));
 
118
 
 
119
void error              OF((const char *));
 
120
int tar                 OF((gzFile, int, int, int, char **));
 
121
 
 
122
void help               OF((int));
 
123
int main                OF((int, char **));
97
124
 
98
125
char *prog;
99
126
 
100
 
/* This will give a benign warning */
101
 
 
102
 
static char *TGZprefix[] = { "\0", ".tgz", ".tar.gz", ".tar", NULL };
103
 
 
104
 
/* Return the real name of the TGZ archive */
105
 
/* or NULL if it does not exist. */
106
 
 
107
 
static char *TGZfname OF((const char *fname))
 
127
const char *TGZsuffix[] = { "\0", ".tar", ".tar.gz", ".taz", ".tgz", NULL };
 
128
 
 
129
/* return the file name of the TGZ archive */
 
130
/* or NULL if it does not exist */
 
131
 
 
132
char *TGZfname (const char *arcname)
108
133
{
109
134
  static char buffer[1024];
110
135
  int origlen,i;
111
 
  
112
 
  strcpy(buffer,fname);
 
136
 
 
137
  strcpy(buffer,arcname);
113
138
  origlen = strlen(buffer);
114
139
 
115
 
  for (i=0; TGZprefix[i]; i++)
 
140
  for (i=0; TGZsuffix[i]; i++)
116
141
    {
117
 
       strcpy(buffer+origlen,TGZprefix[i]);
 
142
       strcpy(buffer+origlen,TGZsuffix[i]);
118
143
       if (access(buffer,F_OK) == 0)
119
144
         return buffer;
120
145
    }
121
146
  return NULL;
122
147
}
123
148
 
 
149
 
124
150
/* error message for the filename */
125
151
 
126
 
void TGZnotfound OF((const char *fname))
 
152
void TGZnotfound (const char *arcname)
127
153
{
128
154
  int i;
129
155
 
130
 
  fprintf(stderr,"%s : couldn't find ",prog);
131
 
  for (i=0;TGZprefix[i];i++)
132
 
    fprintf(stderr,(TGZprefix[i+1]) ? "%s%s, " : "or %s%s\n",
133
 
            fname,
134
 
            TGZprefix[i]);
 
156
  fprintf(stderr,"%s: Couldn't find ",prog);
 
157
  for (i=0;TGZsuffix[i];i++)
 
158
    fprintf(stderr,(TGZsuffix[i+1]) ? "%s%s, " : "or %s%s\n",
 
159
            arcname,
 
160
            TGZsuffix[i]);
135
161
  exit(1);
136
162
}
137
163
 
138
164
 
139
 
/* help functions */
 
165
/* convert octal digits to int */
 
166
/* on error return -1 */
140
167
 
141
 
int getoct(char *p,int width)
 
168
int getoct (char *p,int width)
142
169
{
143
170
  int result = 0;
144
171
  char c;
145
 
  
146
 
  while (width --)
 
172
 
 
173
  while (width--)
147
174
    {
148
175
      c = *p++;
 
176
      if (c == 0)
 
177
        break;
149
178
      if (c == ' ')
150
 
        continue;
151
 
      if (c == 0)
152
 
        break;
 
179
        continue;
 
180
      if (c < '0' || c > '7')
 
181
        return -1;
153
182
      result = result * 8 + (c - '0');
154
183
    }
155
184
  return result;
156
185
}
157
186
 
 
187
 
 
188
/* convert time_t to string */
 
189
/* use the "YYYY/MM/DD hh:mm:ss" format */
 
190
 
158
191
char *strtime (time_t *t)
159
192
{
160
193
  struct tm   *local;
161
194
  static char result[32];
162
195
 
163
196
  local = localtime(t);
164
 
  sprintf(result,"%2d/%02d/%4d %02d:%02d:%02d",
165
 
          local->tm_mday, local->tm_mon+1, local->tm_year+1900,
166
 
          local->tm_hour, local->tm_min,   local->tm_sec);
167
 
  return result;
168
 
}
169
 
 
170
 
 
171
 
/* regular expression matching */
 
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);
 
200
  return result;
 
201
}
 
202
 
 
203
 
 
204
/* set file time */
 
205
 
 
206
int setfiletime (char *fname,time_t ftime)
 
207
{
 
208
#ifdef WIN32
 
209
  static int isWinNT = -1;
 
210
  SYSTEMTIME st;
 
211
  FILETIME locft, modft;
 
212
  struct tm *loctm;
 
213
  HANDLE hFile;
 
214
  int result;
 
215
 
 
216
  loctm = localtime(&ftime);
 
217
  if (loctm == NULL)
 
218
    return -1;
 
219
 
 
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))
 
230
    return -1;
 
231
 
 
232
  if (isWinNT < 0)
 
233
    isWinNT = (GetVersion() < 0x80000000) ? 1 : 0;
 
234
  hFile = CreateFile(fname, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
 
235
                     (isWinNT ? FILE_FLAG_BACKUP_SEMANTICS : 0),
 
236
                     NULL);
 
237
  if (hFile == INVALID_HANDLE_VALUE)
 
238
    return -1;
 
239
  result = SetFileTime(hFile, NULL, NULL, &modft) ? 0 : -1;
 
240
  CloseHandle(hFile);
 
241
  return result;
 
242
#else
 
243
  struct utimbuf settime;
 
244
 
 
245
  settime.actime = settime.modtime = ftime;
 
246
  return utime(fname,&settime);
 
247
#endif
 
248
}
 
249
 
 
250
 
 
251
/* push file attributes */
 
252
 
 
253
void push_attr(struct attr_item **list,char *fname,int mode,time_t time)
 
254
{
 
255
  struct attr_item *item;
 
256
 
 
257
  item = (struct attr_item *)malloc(sizeof(struct attr_item));
 
258
  if (item == NULL)
 
259
    error("Out of memory");
 
260
  item->fname = strdup(fname);
 
261
  item->mode  = mode;
 
262
  item->time  = time;
 
263
  item->next  = *list;
 
264
  *list       = item;
 
265
}
 
266
 
 
267
 
 
268
/* restore file attributes */
 
269
 
 
270
void restore_attr(struct attr_item **list)
 
271
{
 
272
  struct attr_item *item, *prev;
 
273
 
 
274
  for (item = *list; item != NULL; )
 
275
    {
 
276
      setfiletime(item->fname,item->time);
 
277
      chmod(item->fname,item->mode);
 
278
      prev = item;
 
279
      item = item->next;
 
280
      free(prev);
 
281
    }
 
282
  *list = NULL;
 
283
}
 
284
 
 
285
 
 
286
/* match regular expression */
172
287
 
173
288
#define ISSPECIAL(c) (((c) == '*') || ((c) == '/'))
174
289
 
175
 
int ExprMatch(char *string,char *expr)
 
290
int ExprMatch (char *string,char *expr)
176
291
{
177
292
  while (1)
178
293
    {
179
294
      if (ISSPECIAL(*expr))
180
 
        {
181
 
          if (*expr == '/')
182
 
            {
183
 
              if (*string != '\\' && *string != '/')
184
 
                return 0;
185
 
              string ++; expr++;
186
 
            }
187
 
          else if (*expr == '*')
188
 
            {
189
 
              if (*expr ++ == 0)
190
 
                return 1;
191
 
              while (*++string != *expr)
192
 
                if (*string == 0)
193
 
                  return 0;
194
 
            }
195
 
        }
 
295
        {
 
296
          if (*expr == '/')
 
297
            {
 
298
              if (*string != '\\' && *string != '/')
 
299
                return 0;
 
300
              string ++; expr++;
 
301
            }
 
302
          else if (*expr == '*')
 
303
            {
 
304
              if (*expr ++ == 0)
 
305
                return 1;
 
306
              while (*++string != *expr)
 
307
                if (*string == 0)
 
308
                  return 0;
 
309
            }
 
310
        }
196
311
      else
197
 
        {
198
 
          if (*string != *expr)
199
 
            return 0;
200
 
          if (*expr++ == 0)
201
 
            return 1;
202
 
          string++;
203
 
        }
 
312
        {
 
313
          if (*string != *expr)
 
314
            return 0;
 
315
          if (*expr++ == 0)
 
316
            return 1;
 
317
          string++;
 
318
        }
204
319
    }
205
320
}
206
321
 
207
 
/* recursive make directory */
208
 
/* abort if you get an ENOENT errno somewhere in the middle */
209
 
/* e.g. ignore error "mkdir on existing directory" */
210
 
/* */
 
322
 
 
323
/* recursive mkdir */
 
324
/* abort on ENOENT; ignore other errors like "directory already exists" */
211
325
/* return 1 if OK */
212
326
/*        0 on error */
213
327
 
216
330
  char *buffer = strdup(newdir);
217
331
  char *p;
218
332
  int  len = strlen(buffer);
219
 
  
 
333
 
220
334
  if (len <= 0) {
221
335
    free(buffer);
222
336
    return 0;
224
338
  if (buffer[len-1] == '/') {
225
339
    buffer[len-1] = '\0';
226
340
  }
227
 
  if (mkdir(buffer, 0775) == 0)
 
341
  if (mkdir(buffer, 0755) == 0)
228
342
    {
229
343
      free(buffer);
230
344
      return 1;
234
348
  while (1)
235
349
    {
236
350
      char hold;
237
 
      
 
351
 
238
352
      while(*p && *p != '\\' && *p != '/')
239
 
        p++;
 
353
        p++;
240
354
      hold = *p;
241
355
      *p = 0;
242
 
      if ((mkdir(buffer, 0775) == -1) && (errno == ENOENT))
243
 
        {
244
 
          fprintf(stderr,"%s: couldn't create directory %s\n",prog,buffer);
245
 
          free(buffer);
246
 
          return 0;
247
 
        }
 
356
      if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT))
 
357
        {
 
358
          fprintf(stderr,"%s: Couldn't create directory %s\n",prog,buffer);
 
359
          free(buffer);
 
360
          return 0;
 
361
        }
248
362
      if (hold == 0)
249
 
        break;
 
363
        break;
250
364
      *p++ = hold;
251
365
    }
252
366
  free(buffer);
253
367
  return 1;
254
368
}
255
369
 
 
370
 
256
371
int matchname (int arg,int argc,char **argv,char *fname)
257
372
{
258
 
  if (arg == argc)              /* no arguments given (untgz tgzarchive) */
 
373
  if (arg == argc)      /* no arguments given (untgz tgzarchive) */
259
374
    return 1;
260
375
 
261
376
  while (arg < argc)
266
381
}
267
382
 
268
383
 
269
 
/* Tar file list or extract */
 
384
/* tar file list or extract */
270
385
 
271
386
int tar (gzFile in,int action,int arg,int argc,char **argv)
272
387
{
277
392
  int    remaining = 0;
278
393
  FILE   *outfile = NULL;
279
394
  char   fname[BLOCKSIZE];
 
395
  int    tarmode;
280
396
  time_t tartime;
281
 
  
 
397
  struct attr_item *attributes = NULL;
 
398
 
282
399
  if (action == TGZ_LIST)
283
 
    printf("     day      time     size                       file\n"
284
 
           " ---------- -------- --------- -------------------------------------\n");
 
400
    printf("    date      time     size                       file\n"
 
401
           " ---------- -------- --------- -------------------------------------\n");
285
402
  while (1)
286
403
    {
287
404
      len = gzread(in, &buffer, BLOCKSIZE);
288
405
      if (len < 0)
289
 
        error (gzerror(in, &err));
 
406
        error(gzerror(in, &err));
290
407
      /*
291
408
       * Always expect complete blocks to process
292
409
       * the tar information.
293
410
       */
294
411
      if (len != BLOCKSIZE)
295
 
        error("gzread: incomplete block read");
296
 
      
 
412
        {
 
413
          action = TGZ_INVALID; /* force error exit */
 
414
          remaining = 0;        /* force I/O cleanup */
 
415
        }
 
416
 
297
417
      /*
298
418
       * If we have to get a tar header
299
419
       */
300
 
      if (getheader == 1)
301
 
        {
302
 
          /*
303
 
           * if we met the end of the tar
304
 
           * or the end-of-tar block,
305
 
           * we are done
306
 
           */
307
 
          if ((len == 0)  || (buffer.header.name[0]== 0)) break;
308
 
 
309
 
          tartime = (time_t)getoct(buffer.header.mtime,12);
310
 
          strcpy(fname,buffer.header.name);
311
 
          
312
 
          switch (buffer.header.typeflag)
313
 
            {
314
 
            case DIRTYPE:
315
 
              if (action == TGZ_LIST)
316
 
                printf(" %s     <dir> %s\n",strtime(&tartime),fname);
317
 
              if (action == TGZ_EXTRACT)
318
 
                makedir(fname);
319
 
              break;
320
 
            case REGTYPE:
321
 
            case AREGTYPE:
322
 
              remaining = getoct(buffer.header.size,12);
323
 
              if (action == TGZ_LIST)
324
 
                printf(" %s %9d %s\n",strtime(&tartime),remaining,fname);
325
 
              if (action == TGZ_EXTRACT)
326
 
                {
327
 
                  if ((remaining) && (matchname(arg,argc,argv,fname)))
328
 
                    {
329
 
                      outfile = fopen(fname,"wb");
330
 
                      if (outfile == NULL) {
331
 
                        /* try creating directory */
332
 
                        char *p = strrchr(fname, '/');
333
 
                        if (p != NULL) {
334
 
                          *p = '\0';
335
 
                          makedir(fname);
336
 
                          *p = '/';
337
 
                          outfile = fopen(fname,"wb");
338
 
                        }
339
 
                      }
340
 
                      fprintf(stderr,
341
 
                              "%s %s\n",
342
 
                              (outfile) ? "Extracting" : "Couldn't create",
343
 
                              fname);
344
 
                    }
345
 
                  else
346
 
                    outfile = NULL;
347
 
                }
348
 
              /*
349
 
               * could have no contents
350
 
               */
351
 
              getheader = (remaining) ? 0 : 1;
352
 
              break;
353
 
            default:
354
 
              if (action == TGZ_LIST)
355
 
                printf(" %s     <---> %s\n",strtime(&tartime),fname);
356
 
              break;
357
 
            }
358
 
        }
 
420
      if (getheader >= 1)
 
421
        {
 
422
          /*
 
423
           * if we met the end of the tar
 
424
           * or the end-of-tar block,
 
425
           * we are done
 
426
           */
 
427
          if (len == 0 || buffer.header.name[0] == 0)
 
428
            break;
 
429
 
 
430
          tarmode = getoct(buffer.header.mode,8);
 
431
          tartime = (time_t)getoct(buffer.header.mtime,12);
 
432
          if (tarmode == -1 || tartime == (time_t)-1)
 
433
            {
 
434
              buffer.header.name[0] = 0;
 
435
              action = TGZ_INVALID;
 
436
            }
 
437
 
 
438
          if (getheader == 1)
 
439
            {
 
440
              strncpy(fname,buffer.header.name,SHORTNAMESIZE);
 
441
              if (fname[SHORTNAMESIZE-1] != 0)
 
442
                  fname[SHORTNAMESIZE] = 0;
 
443
            }
 
444
          else
 
445
            {
 
446
              /*
 
447
               * The file name is longer than SHORTNAMESIZE
 
448
               */
 
449
              if (strncmp(fname,buffer.header.name,SHORTNAMESIZE-1) != 0)
 
450
                  error("bad long name");
 
451
              getheader = 1;
 
452
            }
 
453
 
 
454
          /*
 
455
           * Act according to the type flag
 
456
           */
 
457
          switch (buffer.header.typeflag)
 
458
            {
 
459
            case DIRTYPE:
 
460
              if (action == TGZ_LIST)
 
461
                printf(" %s     <dir> %s\n",strtime(&tartime),fname);
 
462
              if (action == TGZ_EXTRACT)
 
463
                {
 
464
                  makedir(fname);
 
465
                  push_attr(&attributes,fname,tarmode,tartime);
 
466
                }
 
467
              break;
 
468
            case REGTYPE:
 
469
            case AREGTYPE:
 
470
              remaining = getoct(buffer.header.size,12);
 
471
              if (remaining == -1)
 
472
                {
 
473
                  action = TGZ_INVALID;
 
474
                  break;
 
475
                }
 
476
              if (action == TGZ_LIST)
 
477
                printf(" %s %9d %s\n",strtime(&tartime),remaining,fname);
 
478
              else if (action == TGZ_EXTRACT)
 
479
                {
 
480
                  if (matchname(arg,argc,argv,fname))
 
481
                    {
 
482
                      outfile = fopen(fname,"wb");
 
483
                      if (outfile == NULL) {
 
484
                        /* try creating directory */
 
485
                        char *p = strrchr(fname, '/');
 
486
                        if (p != NULL) {
 
487
                          *p = '\0';
 
488
                          makedir(fname);
 
489
                          *p = '/';
 
490
                          outfile = fopen(fname,"wb");
 
491
                        }
 
492
                      }
 
493
                      if (outfile != NULL)
 
494
                        printf("Extracting %s\n",fname);
 
495
                      else
 
496
                        fprintf(stderr, "%s: Couldn't create %s",prog,fname);
 
497
                    }
 
498
                  else
 
499
                    outfile = NULL;
 
500
                }
 
501
              getheader = 0;
 
502
              break;
 
503
            case GNUTYPE_LONGLINK:
 
504
            case GNUTYPE_LONGNAME:
 
505
              remaining = getoct(buffer.header.size,12);
 
506
              if (remaining < 0 || remaining >= BLOCKSIZE)
 
507
                {
 
508
                  action = TGZ_INVALID;
 
509
                  break;
 
510
                }
 
511
              len = gzread(in, fname, BLOCKSIZE);
 
512
              if (len < 0)
 
513
                error(gzerror(in, &err));
 
514
              if (fname[BLOCKSIZE-1] != 0 || (int)strlen(fname) > remaining)
 
515
                {
 
516
                  action = TGZ_INVALID;
 
517
                  break;
 
518
                }
 
519
              getheader = 2;
 
520
              break;
 
521
            default:
 
522
              if (action == TGZ_LIST)
 
523
                printf(" %s     <---> %s\n",strtime(&tartime),fname);
 
524
              break;
 
525
            }
 
526
        }
359
527
      else
360
 
        {
361
 
          unsigned int bytes = (remaining > BLOCKSIZE) ? BLOCKSIZE : remaining;
362
 
 
363
 
          if ((action == TGZ_EXTRACT) && (outfile != NULL))
364
 
            {
365
 
              if (fwrite(&buffer,sizeof(char),bytes,outfile) != bytes)
366
 
                {
367
 
                  fprintf(stderr,"%s : error writing %s skipping...\n",prog,fname);
368
 
                  fclose(outfile);
369
 
                  unlink(fname);
370
 
                }
371
 
            }
372
 
          remaining -= bytes;
373
 
          if (remaining == 0)
374
 
            {
375
 
              getheader = 1;
376
 
              if ((action == TGZ_EXTRACT) && (outfile != NULL))
377
 
                {
378
 
#ifdef WIN32
379
 
                  HANDLE hFile;
380
 
                  FILETIME ftm,ftLocal;
381
 
                  SYSTEMTIME st;
382
 
                  struct tm localt;
383
 
 
384
 
                  fclose(outfile);
385
 
 
386
 
                  localt = *localtime(&tartime);
387
 
 
388
 
                  hFile = CreateFile(fname, GENERIC_READ | GENERIC_WRITE,
389
 
                                     0, NULL, OPEN_EXISTING, 0, NULL);
390
 
                  
391
 
                  st.wYear = (WORD)localt.tm_year+1900;
392
 
                  st.wMonth = (WORD)localt.tm_mon;
393
 
                  st.wDayOfWeek = (WORD)localt.tm_wday;
394
 
                  st.wDay = (WORD)localt.tm_mday;
395
 
                  st.wHour = (WORD)localt.tm_hour;
396
 
                  st.wMinute = (WORD)localt.tm_min;
397
 
                  st.wSecond = (WORD)localt.tm_sec;
398
 
                  st.wMilliseconds = 0;
399
 
                  SystemTimeToFileTime(&st,&ftLocal);
400
 
                  LocalFileTimeToFileTime(&ftLocal,&ftm);
401
 
                  SetFileTime(hFile,&ftm,NULL,&ftm);
402
 
                  CloseHandle(hFile);
403
 
 
404
 
                  outfile = NULL;
405
 
#else
406
 
                  struct utimbuf settime;
407
 
 
408
 
                  settime.actime = settime.modtime = tartime;
409
 
 
410
 
                  fclose(outfile);
411
 
                  outfile = NULL;
412
 
                  utime(fname,&settime);
413
 
#endif
414
 
                }
415
 
            }
416
 
        }
 
528
        {
 
529
          unsigned int bytes = (remaining > BLOCKSIZE) ? BLOCKSIZE : remaining;
 
530
 
 
531
          if (outfile != NULL)
 
532
            {
 
533
              if (fwrite(&buffer,sizeof(char),bytes,outfile) != bytes)
 
534
                {
 
535
                  fprintf(stderr,
 
536
                    "%s: Error writing %s -- skipping\n",prog,fname);
 
537
                  fclose(outfile);
 
538
                  outfile = NULL;
 
539
                  remove(fname);
 
540
                }
 
541
            }
 
542
          remaining -= bytes;
 
543
        }
 
544
 
 
545
      if (remaining == 0)
 
546
        {
 
547
          getheader = 1;
 
548
          if (outfile != NULL)
 
549
            {
 
550
              fclose(outfile);
 
551
              outfile = NULL;
 
552
              if (action != TGZ_INVALID)
 
553
                push_attr(&attributes,fname,tarmode,tartime);
 
554
            }
 
555
        }
 
556
 
 
557
      /*
 
558
       * Abandon if errors are found
 
559
       */
 
560
      if (action == TGZ_INVALID)
 
561
        {
 
562
          error("broken archive");
 
563
          break;
 
564
        }
417
565
    }
418
 
  
 
566
 
 
567
  /*
 
568
   * Restore file modes and time stamps
 
569
   */
 
570
  restore_attr(&attributes);
 
571
 
419
572
  if (gzclose(in) != Z_OK)
420
573
    error("failed gzclose");
421
574
 
423
576
}
424
577
 
425
578
 
426
 
/* =========================================================== */
 
579
/* ============================================================ */
427
580
 
428
581
void help(int exitval)
429
582
{
430
 
  fprintf(stderr,
431
 
          "untgz v 0.1\n"
432
 
          " an sample application of zlib 1.0.4\n\n"
433
 
          "Usage : untgz TGZfile            to extract all files\n"
434
 
          "        untgz TGZfile fname ...  to extract selected files\n"
435
 
          "        untgz -l TGZfile         to list archive contents\n"
436
 
          "        untgz -h                 to display this help\n\n");
 
583
  printf("untgz version 0.2.1\n"
 
584
         "  using zlib version %s\n\n",
 
585
         zlibVersion());
 
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");
437
590
  exit(exitval);
438
591
}
439
592
 
440
593
void error(const char *msg)
441
594
{
442
 
    fprintf(stderr, "%s: %s\n", prog, msg);
443
 
    exit(1);
 
595
  fprintf(stderr, "%s: %s\n", prog, msg);
 
596
  exit(1);
444
597
}
445
598
 
446
599
 
447
 
/* ====================================================================== */
 
600
/* ============================================================ */
448
601
 
449
 
int _CRT_glob = 0;      /* disable globbing of the arguments */
 
602
#if defined(WIN32) && defined(__GNUC__)
 
603
int _CRT_glob = 0;      /* disable argument globbing in MinGW */
 
604
#endif
450
605
 
451
606
int main(int argc,char **argv)
452
607
{
453
 
    int         action = TGZ_EXTRACT;
454
 
    int         arg = 1;
455
 
    char        *TGZfile;
456
 
    gzFile      *f;
457
 
    
 
608
    int         action = TGZ_EXTRACT;
 
609
    int         arg = 1;
 
610
    char        *TGZfile;
 
611
    gzFile      *f;
458
612
 
459
613
    prog = strrchr(argv[0],'\\');
460
614
    if (prog == NULL)
461
615
      {
462
 
        prog = strrchr(argv[0],'/');
463
 
        if (prog == NULL)
464
 
          {
465
 
            prog = strrchr(argv[0],':');
466
 
            if (prog == NULL)
467
 
              prog = argv[0];
468
 
            else
469
 
              prog++;
470
 
          }
471
 
        else
472
 
          prog++;
 
616
        prog = strrchr(argv[0],'/');
 
617
        if (prog == NULL)
 
618
          {
 
619
            prog = strrchr(argv[0],':');
 
620
            if (prog == NULL)
 
621
              prog = argv[0];
 
622
            else
 
623
              prog++;
 
624
          }
 
625
        else
 
626
          prog++;
473
627
      }
474
628
    else
475
629
      prog++;
476
 
    
 
630
 
477
631
    if (argc == 1)
478
632
      help(0);
479
633
 
480
634
    if (strcmp(argv[arg],"-l") == 0)
481
635
      {
482
 
        action = TGZ_LIST;
483
 
        if (argc == ++arg)
484
 
          help(0);
 
636
        action = TGZ_LIST;
 
637
        if (argc == ++arg)
 
638
          help(0);
485
639
      }
486
640
    else if (strcmp(argv[arg],"-h") == 0)
487
641
      {
488
 
        help(0);
 
642
        help(0);
489
643
      }
490
644
 
491
645
    if ((TGZfile = TGZfname(argv[arg])) == NULL)
492
 
      TGZnotfound(argv[arg]);            
 
646
      TGZnotfound(argv[arg]);
493
647
 
494
648
    ++arg;
495
649
    if ((action == TGZ_LIST) && (arg != argc))
502
656
      {
503
657
      case TGZ_LIST:
504
658
      case TGZ_EXTRACT:
505
 
        f = gzopen(TGZfile,"rb");
506
 
        if (f == NULL)
507
 
          {
508
 
            fprintf(stderr,"%s: Couldn't gzopen %s\n",
509
 
                    prog,
510
 
                    TGZfile);
511
 
            return 1;
512
 
          }
513
 
        exit(tar(f, action, arg, argc, argv));
 
659
        f = gzopen(TGZfile,"rb");
 
660
        if (f == NULL)
 
661
          {
 
662
            fprintf(stderr,"%s: Couldn't gzopen %s\n",prog,TGZfile);
 
663
            return 1;
 
664
          }
 
665
        exit(tar(f, action, arg, argc, argv));
514
666
      break;
515
 
        
 
667
 
516
668
      default:
517
 
        error("Unknown option!");
518
 
        exit(1);
 
669
        error("Unknown option");
 
670
        exit(1);
519
671
      }
520
672
 
521
673
    return 0;