2
Copyright (c) 1990-2004 Info-ZIP. All rights reserved.
4
See the accompanying file LICENSE, version 2000-Apr-09 or later
5
(the contents of which are also included in unzip.h) for terms of use.
6
If, for some reason, all these files are missing, the Info-ZIP license
7
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
9
/*---------------------------------------------------------------------------
13
Unix-specific routines for use with Info-ZIP's UnZip 5.41 and later.
16
do_wild() <-- generic enough to put in fileio.c?
27
---------------------------------------------------------------------------*/
30
#define UNZIP_INTERNAL
35
#else /* SCO Unix, AIX, DNIX, TI SysV, Coherent 4.x, ... */
36
# if defined(__convexc__) || defined(SYSV) || defined(CRAY) || defined(BSD4_4)
40
#if defined(_AIX) || defined(__mpexl)
44
# if defined(_I386) || (defined(__COHERENT__) && (__COHERENT__ >= 0x420))
60
# include <sys/ndir.h>
70
# define dirent direct
75
typedef struct uxdirattr { /* struct for holding unix style directory */
76
struct uxdirattr *next; /* info until can be sorted and set at end */
77
char *fn; /* filename of directory */
79
iztimes t3; /* mtime, atime, ctime */
80
ztimbuf t2; /* modtime, actime */
82
unsigned perms; /* same as min_info.file_attr */
83
int have_uidgid; /* flag */
85
char fnbuf[1]; /* buffer stub for directory name */
87
#define UxAtt(d) ((uxdirattr *)d) /* typecast shortcut */
88
#endif /* SET_DIR_ATTRIB */
90
#ifdef ACORN_FTYPE_NFS
91
/* Acorn bits for NFS filetyping */
101
#endif /* ACORN_FTYPE_NFS */
103
/* static int created_dir; */ /* used in mapname(), checkdir() */
104
/* static int renamed_fullpath; */ /* ditto */
108
#ifdef NO_DIR /* for AT&T 3B1 */
110
#define opendir(path) fopen(path,"r")
111
#define closedir(dir) fclose(dir)
113
typedef struct zdir {
115
struct dirent *entry;
117
DIR *opendir OF((ZCONST char *dirspec));
118
void closedir OF((DIR *dirp));
119
struct dirent *readdir OF((DIR *dirp));
121
DIR *opendir(dirspec)
122
ZCONST char *dirspec;
126
if ((dirp = malloc(sizeof(DIR)) != NULL) {
127
if ((dirp->dirhandle = fopen(dirspec, "r")) == NULL) {
138
fclose(dirp->dirhandle);
143
* Apparently originally by Rich Salz.
144
* Cleaned up and modified by James W. Birdsall.
146
struct dirent *readdir(dirp)
154
if (fread(&(dirp->entry), sizeof (struct dirent), 1,
155
dirp->dirhandle) == 0)
156
return (struct dirent *)NULL;
157
else if ((dirp->entry).d_ino)
158
return &(dirp->entry);
160
} /* end function readdir() */
165
/**********************/
166
/* Function do_wild() */ /* for porting: dir separator; match(ignore_case) */
167
/**********************/
169
char *do_wild(__G__ wildspec)
171
ZCONST char *wildspec; /* only used first time on a given dir */
173
/* these statics are now declared in SYSTEM_SPECIFIC_GLOBALS in unxcfg.h:
174
static DIR *wild_dir = (DIR *)NULL;
175
static ZCONST char *wildname;
176
static char *dirname, matchname[FILNAMSIZ];
177
static int notfirstcall=FALSE, have_dirname, dirnamelen;
181
/* Even when we're just returning wildspec, we *always* do so in
182
* matchname[]--calling routine is allowed to append four characters
183
* to the returned string, and wildspec may be a pointer to argv[].
185
if (!G.notfirstcall) { /* first call: must initialize everything */
186
G.notfirstcall = TRUE;
188
if (!iswild(wildspec)) {
189
strcpy(G.matchname, wildspec);
190
G.have_dirname = FALSE;
195
/* break the wildspec into a directory part and a wildcard filename */
196
if ((G.wildname = (ZCONST char *)strrchr(wildspec, '/')) == NULL) {
199
G.have_dirname = FALSE;
200
G.wildname = wildspec;
202
++G.wildname; /* point at character after '/' */
203
G.dirnamelen = G.wildname - wildspec;
204
if ((G.dirname = (char *)malloc(G.dirnamelen+1)) == (char *)NULL) {
205
Info(slide, 0x201, ((char *)slide,
206
"warning: cannot allocate wildcard buffers\n"));
207
strcpy(G.matchname, wildspec);
208
return G.matchname; /* but maybe filespec was not a wildcard */
210
strncpy(G.dirname, wildspec, G.dirnamelen);
211
G.dirname[G.dirnamelen] = '\0'; /* terminate for strcpy below */
212
G.have_dirname = TRUE;
215
if ((G.wild_dir = (zvoid *)opendir(G.dirname)) != (zvoid *)NULL) {
216
while ((file = readdir((DIR *)G.wild_dir)) !=
217
(struct dirent *)NULL) {
218
Trace((stderr, "do_wild: readdir returns %s\n",
219
FnFilter1(file->d_name)));
220
if (file->d_name[0] == '.' && G.wildname[0] != '.')
221
continue; /* Unix: '*' and '?' do not match leading dot */
222
if (match(file->d_name, G.wildname, 0) && /* 0 == case sens. */
223
/* skip "." and ".." directory entries */
224
strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
225
Trace((stderr, "do_wild: match() succeeds\n"));
226
if (G.have_dirname) {
227
strcpy(G.matchname, G.dirname);
228
strcpy(G.matchname+G.dirnamelen, file->d_name);
230
strcpy(G.matchname, file->d_name);
234
/* if we get to here directory is exhausted, so close it */
235
closedir((DIR *)G.wild_dir);
236
G.wild_dir = (zvoid *)NULL;
238
Trace((stderr, "do_wild: opendir(%s) returns NULL\n",
239
FnFilter1(G.dirname)));
241
/* return the raw wildspec in case that works (e.g., directory not
242
* searchable, but filespec was not wild and file is readable) */
243
strcpy(G.matchname, wildspec);
247
/* last time through, might have failed opendir but returned raw wildspec */
248
if ((DIR *)G.wild_dir == (DIR *)NULL) {
249
G.notfirstcall = FALSE; /* nothing left--reset for new wildspec */
255
/* If we've gotten this far, we've read and matched at least one entry
256
* successfully (in a previous call), so dirname has been copied into
259
while ((file = readdir((DIR *)G.wild_dir)) != (struct dirent *)NULL) {
260
Trace((stderr, "do_wild: readdir returns %s\n",
261
FnFilter1(file->d_name)));
262
if (file->d_name[0] == '.' && G.wildname[0] != '.')
263
continue; /* Unix: '*' and '?' do not match leading dot */
264
if (match(file->d_name, G.wildname, 0)) { /* 0 == don't ignore case */
265
Trace((stderr, "do_wild: match() succeeds\n"));
266
if (G.have_dirname) {
267
/* strcpy(G.matchname, G.dirname); */
268
strcpy(G.matchname+G.dirnamelen, file->d_name);
270
strcpy(G.matchname, file->d_name);
275
closedir((DIR *)G.wild_dir); /* at least one entry read; nothing left */
276
G.wild_dir = (zvoid *)NULL;
277
G.notfirstcall = FALSE; /* reset for new wildspec */
282
} /* end function do_wild() */
290
/**********************/
291
/* Function mapattr() */
292
/**********************/
297
ulg tmp = G.crec.external_file_attributes;
299
G.pInfo->file_attr = 0;
300
/* initialized to 0 for check in "default" branch below... */
302
switch (G.pInfo->hostnum) {
304
tmp = (unsigned)(tmp>>17 & 7); /* Amiga RWE bits */
305
G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
309
if ((tmp & 0xF0000000L) != 0x40000000L)
310
tmp &= 0x01FFFFFFL; /* not a dir, mask all ftype bits */
312
tmp &= 0x41FFFFFFL; /* leave directory bit as set */
321
G.pInfo->file_attr = (unsigned)(tmp >> 16);
322
if (G.pInfo->file_attr != 0 || !G.extra_field) {
325
/* Some (non-Info-ZIP) implementations of Zip for Unix and
326
* VMS (and probably others ??) leave 0 in the upper 16-bit
327
* part of the external_file_attributes field. Instead, they
328
* store file permission attributes in some extra field.
329
* As a work-around, we search for the presence of one of
330
* these extra fields and fall back to the MSDOS compatible
331
* part of external_file_attributes if one of the known
332
* e.f. types has been detected.
333
* Later, we might implement extraction of the permission
334
* bits from the VMS extra field. But for now, the work-around
335
* should be sufficient to provide "readable" extracted files.
336
* (For ASI Unix e.f., an experimental remap from the e.f.
337
* mode value IS already provided!)
341
uch *ef = G.extra_field;
342
unsigned ef_len = G.crec.extra_field_length;
345
while (!r && ef_len >= EB_HEADSIZE) {
347
ebLen = (unsigned)makeword(ef+EB_LEN);
348
if (ebLen > (ef_len - EB_HEADSIZE))
349
/* discoverd some e.f. inconsistency! */
353
if (ebLen >= (EB_ASI_MODE+2)) {
355
(unsigned)makeword(ef+(EB_HEADSIZE+EB_ASI_MODE));
356
/* force stop of loop: */
357
ef_len = (ebLen + EB_HEADSIZE);
360
/* else: fall through! */
362
/* "found nondecypherable e.f. with perm. attr" */
367
ef_len -= (ebLen + EB_HEADSIZE);
368
ef += (ebLen + EB_HEADSIZE);
374
/* all remaining cases: expand MSDOS read-only bit into write perms */
376
/* PKWARE's PKZip for Unix marks entries as FS_FAT_, but stores the
377
* Unix attributes in the upper 16 bits of the external attributes
378
* field, just like Info-ZIP's Zip for Unix. We try to use that
379
* value, after a check for consistency with the MSDOS attribute
382
G.pInfo->file_attr = (unsigned)(tmp >> 16);
389
/* Ensure that DOS subdir bit is set when the entry's name ends
390
* in a '/'. Some third-party Zip programs fail to set the subdir
391
* bit for directory entries.
393
if ((tmp & 0x10) == 0) {
394
extent fnlen = strlen(G.filename);
395
if (fnlen > 0 && G.filename[fnlen-1] == '/')
398
/* read-only bit --> write perms; subdir bit --> dir exec bit */
399
tmp = !(tmp & 1) << 1 | (tmp & 0x10) >> 4;
400
if ((G.pInfo->file_attr & 0700) == (unsigned)(0400 | tmp<<6))
401
/* keep previous G.pInfo->file_attr setting, when its "owner"
402
* part appears to be consistent with DOS attribute flags!
405
G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
407
} /* end switch (host-OS-created-by) */
409
/* for originating systems with no concept of "group," "other," "system": */
410
umask( (int)(tmp=umask(0)) ); /* apply mask to expanded r/w(/x) perms */
411
G.pInfo->file_attr &= ~tmp;
415
} /* end function mapattr() */
421
/************************/
422
/* Function mapname() */
423
/************************/
425
int mapname(__G__ renamed)
430
* MPN_OK - no problem detected
431
* MPN_INF_TRUNC - caution (truncated filename)
432
* MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
433
* MPN_ERR_SKIP - error -> skip entry
434
* MPN_ERR_TOOLONG - error -> path is too long
435
* MPN_NOMEM - error (memory allocation failed) -> skip entry
436
* [also MPN_VOL_LABEL, MPN_CREATED_DIR]
439
char pathcomp[FILNAMSIZ]; /* path-component buffer */
440
char *pp, *cp=(char *)NULL; /* character pointers */
441
char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
442
#ifdef ACORN_FTYPE_NFS
443
char *lastcomma=(char *)NULL; /* pointer to last comma in pathcomp */
444
RO_extra_block *ef_spark; /* pointer Acorn FTYPE ef block */
446
int killed_ddot = FALSE; /* is set when skipping "../" pathcomp */
448
register unsigned workch; /* hold the character being tested */
451
/*---------------------------------------------------------------------------
452
Initialize various pointers and counters and stuff.
453
---------------------------------------------------------------------------*/
455
if (G.pInfo->vollabel)
456
return MPN_VOL_LABEL; /* can't set disk volume labels in Unix */
458
/* can create path as long as not just freshening, or if user told us */
459
G.create_dirs = (!uO.fflag || renamed);
461
G.created_dir = FALSE; /* not yet */
463
/* user gave full pathname: don't prepend rootpath */
464
G.renamed_fullpath = (renamed && (*G.filename == '/'));
466
if (checkdir(__G__ (char *)NULL, INIT) == MPN_NOMEM)
467
return MPN_NOMEM; /* initialize path buffer, unless no memory */
469
*pathcomp = '\0'; /* initialize translation buffer */
470
pp = pathcomp; /* point to translation buffer */
471
if (uO.jflag) /* junking directories */
472
cp = (char *)strrchr(G.filename, '/');
473
if (cp == (char *)NULL) /* no '/' or not junking dirs */
474
cp = G.filename; /* point to internal zipfile-member pathname */
476
++cp; /* point to start of last component of path */
478
/*---------------------------------------------------------------------------
479
Begin main loop through characters in filename.
480
---------------------------------------------------------------------------*/
482
while ((workch = (uch)*cp++) != 0) {
485
case '/': /* can assume -j flag not given */
487
if (strcmp(pathcomp, ".") == 0) {
488
/* don't bother appending "./" to the path */
490
} else if (!uO.ddotflag && strcmp(pathcomp, "..") == 0) {
491
/* "../" dir traversal detected, skip over it */
493
killed_ddot = TRUE; /* set "show message" flag */
495
/* when path component is not empty, append it now */
496
if (*pathcomp != '\0' &&
497
((error = checkdir(__G__ pathcomp, APPEND_DIR))
498
& MPN_MASK) > MPN_INF_TRUNC)
500
pp = pathcomp; /* reset conversion buffer for next piece */
501
lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
504
#ifdef __CYGWIN__ /* Cygwin runs on Win32, apply FAT/NTFS filename rules */
505
case ':': /* drive spec not stored, so no colon allowed */
506
case '\\': /* '\\' may come as normal filename char (not */
507
case '<': /* dir sep char!) from unix-like file system */
508
case '>': /* no redirection symbols allowed either */
509
case '|': /* no pipe signs allowed */
510
case '"': /* no double quotes allowed */
511
case '?': /* no wildcards allowed */
513
*pp++ = '_'; /* these rules apply equally to FAT and NTFS */
516
case ';': /* VMS version (or DEC-20 attrib?) */
518
*pp++ = ';'; /* keep for now; remove VMS ";##" */
519
break; /* later, if requested */
521
#ifdef ACORN_FTYPE_NFS
522
case ',': /* NFS filetype extension */
524
*pp++ = ','; /* keep for now; may need to remove */
525
break; /* later, if requested */
529
case ' ': /* change spaces to underscore under */
530
*pp++ = '_'; /* MTS; leave as spaces under Unix */
535
/* allow European characters in filenames: */
536
if (isprint(workch) || (128 <= workch && workch <= 254))
537
*pp++ = (char)workch;
540
} /* end while loop */
542
/* Show warning when stripping insecure "parent dir" path components */
543
if (killed_ddot && QCOND2) {
544
Info(slide, 0, ((char *)slide,
545
"warning: skipped \"../\" path component(s) in %s\n",
546
FnFilter1(G.filename)));
547
if (!(error & ~MPN_MASK))
548
error = (error & MPN_MASK) | PK_WARN;
551
/*---------------------------------------------------------------------------
552
Report if directory was created (and no file to create: filename ended
553
in '/'), check name to be sure it exists, and combine path and name be-
555
---------------------------------------------------------------------------*/
557
if (G.filename[strlen(G.filename) - 1] == '/') {
558
checkdir(__G__ G.filename, GETPATH);
561
Info(slide, 0, ((char *)slide, " creating: %s\n",
562
FnFilter1(G.filename)));
565
/* set approx. dir perms (make sure can still read/write in dir) */
566
if (chmod(G.filename, (0xffff & G.pInfo->file_attr) | 0700))
567
perror("chmod (directory attributes) error");
569
/* set dir time (note trailing '/') */
570
return (error & ~MPN_MASK) | MPN_CREATED_DIR;
572
/* dir existed already; don't look for data to extract */
573
return (error & ~MPN_MASK) | MPN_INF_SKIP;
576
*pp = '\0'; /* done with pathcomp: terminate it */
578
/* if not saving them, remove VMS version numbers (appended ";###") */
579
if (!uO.V_flag && lastsemi) {
581
while (isdigit((uch)(*pp)))
583
if (*pp == '\0') /* only digits between ';' and end: nuke */
587
#ifdef ACORN_FTYPE_NFS
588
/* translate Acorn filetype information if asked to do so */
589
if (uO.acorn_nfs_ext &&
590
(ef_spark = (RO_extra_block *)
591
getRISCOSexfield(G.extra_field, G.lrec.extra_field_length))
592
!= (RO_extra_block *)NULL)
594
/* file *must* have a RISC OS extra field */
595
long ft = (long)makelong(ef_spark->loadaddr);
599
while (isxdigit((uch)(*pp))) ++pp;
600
if (pp == lastcomma+4 && *pp == '\0') *lastcomma='\0'; /* nuke */
602
if ((ft & 1<<31)==0) ft=0x000FFD00;
603
sprintf(pathcomp+strlen(pathcomp), ",%03x", (int)(ft>>8) & 0xFFF);
605
#endif /* ACORN_FTYPE_NFS */
607
if (*pathcomp == '\0') {
608
Info(slide, 1, ((char *)slide, "mapname: conversion of %s failed\n",
609
FnFilter1(G.filename)));
610
return (error & ~MPN_MASK) | MPN_ERR_SKIP;
613
checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
614
checkdir(__G__ G.filename, GETPATH);
618
} /* end function mapname() */
623
#if 0 /*========== NOTES ==========*/
625
extract-to dir: a:path/
626
buildpath: path1/path2/ ... (NULL-terminated)
630
loop over chars in zipfile member name
631
checkdir(path component, COMPONENT | CREATEDIR) --> map as required?
632
(d:/tmp/unzip/) (disk:[tmp.unzip.)
633
(d:/tmp/unzip/jj/) (disk:[tmp.unzip.jj.)
634
(d:/tmp/unzip/jj/temp/) (disk:[tmp.unzip.jj.temp.)
635
finally add filename itself and check for existence? (could use with rename)
636
(d:/tmp/unzip/jj/temp/msg.outdir) (disk:[tmp.unzip.jj.temp]msg.outdir)
637
checkdir(name, GETPATH) --> copy path to name and free space
644
/***********************/
645
/* Function checkdir() */
646
/***********************/
648
int checkdir(__G__ pathcomp, flag)
654
* MPN_OK - no problem detected
655
* MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
656
* MPN_INF_SKIP - path doesn't exist, not allowed to create
657
* MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
658
* exists and is not a directory, but is supposed to be
659
* MPN_ERR_TOOLONG - path is too long
660
* MPN_NOMEM - can't allocate memory for filename buffers
663
/* static int rootlen = 0; */ /* length of rootpath */
664
/* static char *rootpath; */ /* user's "extract-to" directory */
665
/* static char *buildpath; */ /* full path (so far) to extracted file */
666
/* static char *end; */ /* pointer to end of buildpath ('\0') */
669
# define FUNCTION (flag & FN_MASK)
673
/*---------------------------------------------------------------------------
674
APPEND_DIR: append the path component to the path being built and check
675
for its existence. If doesn't exist and we are creating directories, do
676
so for this one; else signal success or error as appropriate.
677
---------------------------------------------------------------------------*/
679
if (FUNCTION == APPEND_DIR) {
680
int too_long = FALSE;
685
Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
686
while ((*G.end = *pathcomp++) != '\0')
688
#ifdef SHORT_NAMES /* path components restricted to 14 chars, typically */
689
if ((G.end-old_end) > FILENAME_MAX) /* GRR: proper constant? */
690
*(G.end = old_end + FILENAME_MAX) = '\0';
693
/* GRR: could do better check, see if overrunning buffer as we go:
694
* check end-buildpath after each append, set warning variable if
695
* within 20 of FILNAMSIZ; then if var set, do careful check when
696
* appending. Clear variable when begin new path. */
698
/* next check: need to append '/', at least one-char name, '\0' */
699
if ((G.end-G.buildpath) > FILNAMSIZ-3)
700
too_long = TRUE; /* check if extracting dir? */
701
if (SSTAT(G.buildpath, &G.statbuf)) { /* path doesn't exist */
702
if (!G.create_dirs) { /* told not to create (freshening) */
704
return MPN_INF_SKIP; /* path doesn't exist: nothing to do */
707
Info(slide, 1, ((char *)slide,
708
"checkdir error: path too long: %s\n",
709
FnFilter1(G.buildpath)));
711
/* no room for filenames: fatal */
712
return MPN_ERR_TOOLONG;
714
if (mkdir(G.buildpath, 0777) == -1) { /* create the directory */
715
Info(slide, 1, ((char *)slide,
716
"checkdir error: cannot create %s\n\
717
unable to process %s.\n",
718
FnFilter2(G.buildpath), FnFilter1(G.filename)));
720
/* path didn't exist, tried to create, failed */
723
G.created_dir = TRUE;
724
} else if (!S_ISDIR(G.statbuf.st_mode)) {
725
Info(slide, 1, ((char *)slide,
726
"checkdir error: %s exists but is not directory\n\
727
unable to process %s.\n",
728
FnFilter2(G.buildpath), FnFilter1(G.filename)));
730
/* path existed but wasn't dir */
734
Info(slide, 1, ((char *)slide,
735
"checkdir error: path too long: %s\n", FnFilter1(G.buildpath)));
737
/* no room for filenames: fatal */
738
return MPN_ERR_TOOLONG;
742
Trace((stderr, "buildpath now = [%s]\n", FnFilter1(G.buildpath)));
745
} /* end if (FUNCTION == APPEND_DIR) */
747
/*---------------------------------------------------------------------------
748
GETPATH: copy full path to the string pointed at by pathcomp, and free
750
---------------------------------------------------------------------------*/
752
if (FUNCTION == GETPATH) {
753
strcpy(pathcomp, G.buildpath);
754
Trace((stderr, "getting and freeing path [%s]\n",
755
FnFilter1(pathcomp)));
757
G.buildpath = G.end = (char *)NULL;
761
/*---------------------------------------------------------------------------
762
APPEND_NAME: assume the path component is the filename; append it and
763
return without checking for existence.
764
---------------------------------------------------------------------------*/
766
if (FUNCTION == APPEND_NAME) {
771
Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
772
while ((*G.end = *pathcomp++) != '\0') {
774
#ifdef SHORT_NAMES /* truncate name at 14 characters, typically */
775
if ((G.end-old_end) > FILENAME_MAX) /* GRR: proper constant? */
776
*(G.end = old_end + FILENAME_MAX) = '\0';
778
if ((G.end-G.buildpath) >= FILNAMSIZ) {
780
Info(slide, 0x201, ((char *)slide,
781
"checkdir warning: path too long; truncating\n\
783
FnFilter1(G.filename), FnFilter2(G.buildpath)));
784
return MPN_INF_TRUNC; /* filename truncated */
787
Trace((stderr, "buildpath now = [%s]\n", FnFilter1(G.buildpath)));
788
/* could check for existence here, prompt for new name... */
792
/*---------------------------------------------------------------------------
793
INIT: allocate and initialize buffer space for the file currently being
794
extracted. If file was renamed with an absolute path, don't prepend the
796
---------------------------------------------------------------------------*/
798
/* GRR: for VMS and TOPS-20, add up to 13 to strlen */
800
if (FUNCTION == INIT) {
801
Trace((stderr, "initializing buildpath to "));
802
#ifdef ACORN_FTYPE_NFS
803
if ((G.buildpath = (char *)malloc(strlen(G.filename)+G.rootlen+
804
(uO.acorn_nfs_ext ? 5 : 1)))
806
if ((G.buildpath = (char *)malloc(strlen(G.filename)+G.rootlen+1))
810
if ((G.rootlen > 0) && !G.renamed_fullpath) {
811
strcpy(G.buildpath, G.rootpath);
812
G.end = G.buildpath + G.rootlen;
817
Trace((stderr, "[%s]\n", FnFilter1(G.buildpath)));
821
/*---------------------------------------------------------------------------
822
ROOT: if appropriate, store the path in rootpath and create it if
823
necessary; else assume it's a zipfile member and return. This path
824
segment gets used in extracting all members from every zipfile specified
826
---------------------------------------------------------------------------*/
828
#if (!defined(SFX) || defined(SFX_EXDIR))
829
if (FUNCTION == ROOT) {
830
Trace((stderr, "initializing root path to [%s]\n",
831
FnFilter1(pathcomp)));
832
if (pathcomp == (char *)NULL) {
836
if (G.rootlen > 0) /* rootpath was already set, nothing to do */
838
if ((G.rootlen = strlen(pathcomp)) > 0) {
841
if ((tmproot = (char *)malloc(G.rootlen+2)) == (char *)NULL) {
845
strcpy(tmproot, pathcomp);
846
if (tmproot[G.rootlen-1] == '/') {
847
tmproot[--G.rootlen] = '\0';
849
if (G.rootlen > 0 && (SSTAT(tmproot, &G.statbuf) ||
850
!S_ISDIR(G.statbuf.st_mode)))
851
{ /* path does not exist */
852
if (!G.create_dirs /* || iswild(tmproot) */ ) {
855
/* skip (or treat as stored file) */
858
/* create the directory (could add loop here scanning tmproot
859
* to create more than one level, but why really necessary?) */
860
if (mkdir(tmproot, 0777) == -1) {
861
Info(slide, 1, ((char *)slide,
862
"checkdir: cannot create extraction directory: %s\n",
863
FnFilter1(tmproot)));
866
/* path didn't exist, tried to create, and failed: */
867
/* file exists, or 2+ subdir levels required */
871
tmproot[G.rootlen++] = '/';
872
tmproot[G.rootlen] = '\0';
873
if ((G.rootpath = (char *)realloc(tmproot, G.rootlen+1)) == NULL) {
878
Trace((stderr, "rootpath now = [%s]\n", FnFilter1(G.rootpath)));
882
#endif /* !SFX || SFX_EXDIR */
884
/*---------------------------------------------------------------------------
885
END: free rootpath, immediately prior to program exit.
886
---------------------------------------------------------------------------*/
888
if (FUNCTION == END) {
889
Trace((stderr, "freeing rootpath\n"));
897
return MPN_INVALID; /* should never reach */
899
} /* end function checkdir() */
907
/********************/
908
/* Function mkdir() */
909
/********************/
911
int mkdir(path, mode)
913
int mode; /* ignored */
915
* returns: 0 - successful
916
* -1 - failed (errno not set, however)
919
char command[FILNAMSIZ+40]; /* buffer for system() call */
921
/* GRR 930416: added single quotes around path to avoid bug with
922
* creating directories with ampersands in name; not yet tested */
923
sprintf(command, "IFS=\" \t\n\" /bin/mkdir '%s' 2>/dev/null", path);
929
#endif /* NO_MKDIR */
934
#if (!defined(MTS) || defined(SET_DIR_ATTRIB))
935
static int get_extattribs OF((__GPRO__ iztimes *pzt, ush z_uidgid[2]));
937
static int get_extattribs(__G__ pzt, z_uidgid)
942
/*---------------------------------------------------------------------------
943
Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
944
time: adjust base year from 1980 to 1970, do usual conversions from
945
yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
946
light savings time differences. If we have a Unix extra field, however,
947
we're laughing: both mtime and atime are ours. On the other hand, we
948
then have to check for restoration of UID/GID.
949
---------------------------------------------------------------------------*/
951
unsigned eb_izux_flg;
953
eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
954
G.lrec.extra_field_length, 0, G.lrec.last_mod_dos_datetime,
956
(G.tz_is_valid ? pzt : NULL),
961
if (eb_izux_flg & EB_UT_FL_MTIME) {
962
TTrace((stderr, "\nget_extattribs: Unix e.f. modif. time = %ld\n",
965
pzt->mtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
967
if (eb_izux_flg & EB_UT_FL_ATIME) {
968
TTrace((stderr, "get_extattribs: Unix e.f. access time = %ld\n",
971
pzt->atime = pzt->mtime;
972
TTrace((stderr, "\nget_extattribs: modification/access times = %ld\n",
976
/* if -X option was specified and we have UID/GID info, restore it */
978
#ifdef RESTORE_UIDGID
979
(uO.X_flag && (eb_izux_flg & EB_UX2_VALID));
983
return have_uidgid_flg;
985
#endif /* !MTS || SET_DIR_ATTRIB */
991
/****************************/
992
/* Function close_outfile() */
993
/****************************/
995
void close_outfile(__G) /* GRR: change to return PK-style warning level */
999
iztimes t3; /* mtime, atime, ctime */
1000
ztimbuf t2; /* modtime, actime */
1003
int have_uidgid_flg;
1007
/*---------------------------------------------------------------------------
1008
If symbolic links are supported, allocate storage for a symlink control
1009
structure, put the uncompressed "data" and other required info in it, and
1010
add the structure to the "deferred symlinks" chain. Since we know it's a
1011
symbolic link to start with, we shouldn't have to worry about overflowing
1012
unsigned ints with unsigned longs.
1013
---------------------------------------------------------------------------*/
1017
unsigned ucsize = (unsigned)G.lrec.ucsize;
1018
extent slnk_entrysize = sizeof(slinkentry) + ucsize +
1020
slinkentry *slnk_entry;
1022
if ((unsigned)slnk_entrysize < ucsize) {
1023
Info(slide, 0x201, ((char *)slide,
1024
"warning: symbolic link (%s) failed: mem alloc overflow\n",
1025
FnFilter1(G.filename)));
1029
if ((slnk_entry = (slinkentry *)malloc(slnk_entrysize)) == NULL) {
1030
Info(slide, 0x201, ((char *)slide,
1031
"warning: symbolic link (%s) failed: no mem\n",
1032
FnFilter1(G.filename)));
1035
slnk_entry->next = NULL;
1036
slnk_entry->targetlen = ucsize;
1037
slnk_entry->attriblen = 0; /* don't set attributes for symlinks */
1038
slnk_entry->target = slnk_entry->buf;
1039
slnk_entry->fname = slnk_entry->target + ucsize + 1;
1040
strcpy(slnk_entry->fname, G.filename);
1042
/* reopen the "link data" file for reading */
1043
G.outfile = fopen(G.filename, FOPR);
1046
fread(slnk_entry->target, 1, ucsize, G.outfile) != (int)ucsize)
1048
Info(slide, 0x201, ((char *)slide,
1049
"warning: symbolic link (%s) failed\n",
1050
FnFilter1(G.filename)));
1055
fclose(G.outfile); /* close "link" file for good... */
1056
slnk_entry->target[ucsize] = '\0';
1058
Info(slide, 0, ((char *)slide, "-> %s ",
1059
FnFilter1(slnk_entry->target)));
1060
/* add this symlink record to the list of deferred symlinks */
1061
if (G.slink_last != NULL)
1062
G.slink_last->next = slnk_entry;
1064
G.slink_head = slnk_entry;
1065
G.slink_last = slnk_entry;
1068
#endif /* SYMLINKS */
1071
if (G.extra_field) {
1072
static void qlfix OF((__GPRO__ uch *ef_ptr, unsigned ef_len));
1074
qlfix(__G__ G.extra_field, G.lrec.extra_field_length);
1078
have_uidgid_flg = get_extattribs(__G__ &(zt.t3), z_uidgid);
1080
/* if -X option was specified and we have UID/GID info, restore it */
1081
if (have_uidgid_flg) {
1082
TTrace((stderr, "close_outfile: restoring Unix UID/GID info\n"));
1083
if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
1086
Info(slide, 0x201, ((char *)slide,
1087
"warning: cannot set UID %d and/or GID %d for %s\n",
1088
z_uidgid[0], z_uidgid[1], FnFilter1(G.filename)));
1090
Info(slide, 0x201, ((char *)slide,
1091
" (warning) cannot set UID %d and/or GID %d",
1092
z_uidgid[0], z_uidgid[1]));
1096
/* set the file's access and modification times */
1097
if (utime(G.filename, &(zt.t2))) {
1100
Info(slide, 0x201, ((char *)slide, "... cannot set time for %s\n",
1101
FnFilter1(G.filename)));
1103
Info(slide, 0x201, ((char *)slide, "... cannot set time"));
1106
Info(slide, 0x201, ((char *)slide,
1107
"warning: cannot set times for %s\n", FnFilter1(G.filename)));
1109
Info(slide, 0x201, ((char *)slide,
1110
" (warning) cannot set times"));
1111
#endif /* ?AOS_VS */
1114
/*---------------------------------------------------------------------------
1115
Change the file permissions from default ones to those stored in the
1117
---------------------------------------------------------------------------*/
1120
if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
1121
perror("chmod (file attributes) error");
1124
} /* end function close_outfile() */
1129
#ifdef SET_DIR_ATTRIB
1130
/* messages of code for setting directory attributes */
1131
static ZCONST char Far DirlistUidGidFailed[] =
1132
"warning: cannot set UID %d and/or GID %d for %s\n";
1133
static ZCONST char Far DirlistUtimeFailed[] =
1134
"warning: cannot set modification, access times for %s\n";
1136
static ZCONST char Far DirlistChmodFailed[] =
1137
"warning: cannot set permissions for %s\n";
1141
int defer_dir_attribs(__G__ pd)
1147
d_entry = (uxdirattr *)malloc(sizeof(uxdirattr) + strlen(G.filename));
1148
*pd = (direntry *)d_entry;
1149
if (d_entry == (uxdirattr *)NULL) {
1152
d_entry->fn = d_entry->fnbuf;
1153
strcpy(d_entry->fn, G.filename);
1155
d_entry->perms = G.pInfo->file_attr;
1157
d_entry->have_uidgid = get_extattribs(__G__ &(d_entry->u.t3),
1160
} /* end function defer_dir_attribs() */
1163
int set_direc_attribs(__G__ d)
1169
if (UxAtt(d)->have_uidgid &&
1170
chown(UxAtt(d)->fn, (uid_t)UxAtt(d)->uidgid[0],
1171
(gid_t)UxAtt(d)->uidgid[1]))
1173
Info(slide, 0x201, ((char *)slide,
1174
LoadFarString(DirlistUidGidFailed),
1175
UxAtt(d)->uidgid[0], UxAtt(d)->uidgid[1], FnFilter1(d->fn)));
1179
if (utime(d->fn, &UxAtt(d)->u.t2)) {
1180
Info(slide, 0x201, ((char *)slide,
1181
LoadFarString(DirlistUtimeFailed), FnFilter1(d->fn)));
1186
if (chmod(d->fn, 0xffff & UxAtt(d)->perms)) {
1187
Info(slide, 0x201, ((char *)slide,
1188
LoadFarString(DirlistChmodFailed), FnFilter1(d->fn)));
1189
/* perror("chmod (file attributes) error"); */
1193
#endif /* !NO_CHMOD */
1195
} /* end function set_direc_attribs() */
1197
#endif /* SET_DIR_ATTRIB */
1204
/***************************/
1205
/* Function stamp_file() */
1206
/***************************/
1208
int stamp_file(fname, modtime)
1214
tp.modtime = tp.actime = modtime;
1215
return (utime(fname, &tp));
1217
} /* end function stamp_file() */
1219
#endif /* TIMESTAMP */
1226
/************************/
1227
/* Function version() */
1228
/************************/
1233
#if (defined(__GNUC__) && defined(NX_CURRENT_COMPILER_RELEASE))
1234
char cc_namebuf[40];
1235
char cc_versbuf[40];
1237
#if (defined(CRAY) && defined(_RELEASE))
1238
char cc_versbuf[40];
1241
#if ((defined(CRAY) || defined(cray)) && defined(_UNICOS))
1242
char os_namebuf[40];
1244
#if defined(__NetBSD__)
1245
char os_namebuf[40];
1249
/* Pyramid, NeXT have problems with huge macro expansion, too: no Info() */
1250
sprintf((char *)slide, LoadFarString(CompiledWith),
1253
# ifdef NX_CURRENT_COMPILER_RELEASE
1254
(sprintf(cc_namebuf, "NeXT DevKit %d.%02d ",
1255
NX_CURRENT_COMPILER_RELEASE/100, NX_CURRENT_COMPILER_RELEASE%100),
1257
(strlen(__VERSION__) > 8)? "(gcc)" :
1258
(sprintf(cc_versbuf, "(gcc %s)", __VERSION__), cc_versbuf),
1260
"gcc ", __VERSION__,
1263
# if defined(CRAY) && defined(_RELEASE)
1264
"cc ", (sprintf(cc_versbuf, "version %d", _RELEASE), cc_versbuf),
1268
# define IZ_CC_NAME "cc "
1270
IZ_CC_NAME, __VERSION__
1273
# define IZ_CC_NAME "cc"
1278
#endif /* ?__GNUC__ */
1281
# define IZ_OS_NAME "Unix"
1285
#if defined(sgi) || defined(__sgi)
1286
" (Silicon Graphics IRIX)",
1291
" (Sun SPARC/Solaris)",
1292
# else /* may or may not be SunOS */
1296
# if defined(sun386) || defined(i386)
1299
# if defined(mc68020) || defined(__mc68020__)
1301
# else /* mc68010 or mc68000: Sun 2 or earlier */
1319
#if defined(CRAY) || defined(cray)
1321
(sprintf(os_namebuf, " (Cray UNICOS release %d)", _UNICOS), os_namebuf),
1326
#if defined(uts) || defined(UTS)
1331
" (NeXTStep/black)",
1333
" (NeXTStep for Intel)",
1335
#else /* the next dozen or so are somewhat order-dependent */
1354
(sprintf(os_namebuf, " (NetBSD 0.8%c)", (char)(NetBSD0_8 - 1 + 'A')),
1358
(sprintf(os_namebuf, " (NetBSD 0.9%c)", (char)(NetBSD0_9 - 1 + 'A')),
1362
(sprintf(os_namebuf, " (NetBSD 1.0%c)", (char)(NetBSD1_0 - 1 + 'A')),
1365
(BSD4_4 == 0.5)? " (NetBSD before 0.9)" : " (NetBSD 1.1 or later)",
1371
(BSD4_4 == 0.5)? " (FreeBSD 1.x)" : " (FreeBSD 2.0 or later)",
1374
(BSD4_4 == 0.5)? " (BSD/386 1.0)" : " (BSD/386 1.1 or later)",
1377
(BSD4_4 == 1)? " (386BSD, post-4.4 release)" : " (386BSD)",
1382
#if defined(i686) || defined(__i686) || defined(__i686__)
1385
#if defined(i586) || defined(__i586) || defined(__i586__)
1388
#if defined(i486) || defined(__i486) || defined(__i486__)
1391
#if defined(i386) || defined(__i386) || defined(__i386__)
1403
# else /* __alpha? */
1428
#endif /* QNX Neutrino */
1434
#endif /* Pyramid */
1441
#endif /* BSDI BSD/386 */
1443
#endif /* FreeBSD */
1444
#endif /* SCO Xenix */
1445
#endif /* SCO Unix */
1465
(*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
1467
} /* end function version() */
1477
long d_length __attribute__ ((packed)); /* file length */
1478
unsigned char d_access __attribute__ ((packed)); /* file access type */
1479
unsigned char d_type __attribute__ ((packed)); /* file type */
1480
long d_datalen __attribute__ ((packed)); /* data length */
1481
long d_reserved __attribute__ ((packed));/* Unused */
1482
short d_szname __attribute__ ((packed)); /* size of name */
1483
char d_name[36] __attribute__ ((packed));/* name area */
1484
long d_update __attribute__ ((packed)); /* last update */
1485
long d_refdate __attribute__ ((packed));
1486
long d_backup __attribute__ ((packed)); /* EOD */
1489
#define LONGID "QDOS02"
1490
#define EXTRALEN (sizeof(struct qdirect) + 8)
1491
#define JBLONGID "QZHD"
1492
#define JBEXTRALEN (sizeof(jbextra) - 4 * sizeof(char))
1495
char eb_header[4] __attribute__ ((packed)); /* place_holder */
1496
char longid[8] __attribute__ ((packed));
1497
struct qdirect header __attribute__ ((packed));
1501
char eb_header[4]; /* place_holder */
1503
struct qdirect header;
1508
/* The following two functions SH() and LG() convert big-endian short
1509
* and long numbers into native byte order. They are some kind of
1510
* counterpart to the generic UnZip's makeword() and makelong() functions.
1512
static ush SH(ush val)
1516
swapbuf[1] = (uch)(val & 0xff);
1517
swapbuf[0] = (uch)(val >> 8);
1518
return (*(ush *)swapbuf);
1523
static ulg LG(ulg val)
1525
/* convert the big-endian unsigned long number `val' to the machine
1526
* dependant representation
1530
swapbuf[1] = SH((ush)(val & 0xffff));
1531
swapbuf[0] = SH((ush)(val >> 16));
1532
return (*(ulg *)swapbuf);
1537
static void qlfix(__G__ ef_ptr, ef_len)
1542
while (ef_len >= EB_HEADSIZE)
1544
unsigned eb_id = makeword(EB_ID + ef_ptr);
1545
unsigned eb_len = makeword(EB_LEN + ef_ptr);
1547
if (eb_len > (ef_len - EB_HEADSIZE)) {
1548
/* discovered some extra field inconsistency! */
1550
"qlfix: block length %u > rest ef_size %u\n", eb_len,
1551
ef_len - EB_HEADSIZE));
1565
qdosextra *extra = (qdosextra *)ef_ptr;
1566
jbextra *jbp = (jbextra *)ef_ptr;
1568
if (!strncmp(extra->longid, LONGID, strlen(LONGID)))
1570
if (eb_len != EXTRALEN)
1572
Info(slide, 0x201, ((char *)slide,
1573
"warning: invalid length in Qdos field for %s\n",
1574
FnFilter1(G.filename)));
1576
Info(slide, 0x201, ((char *)slide,
1577
"warning: invalid length in Qdos field"));
1579
if (extra->header.d_type)
1581
dlen = extra->header.d_datalen;
1585
if (!strncmp(jbp->longid, JBLONGID, strlen(JBLONGID)))
1587
if (eb_len != JBEXTRALEN)
1589
Info(slide, 0x201, ((char *)slide,
1590
"warning: invalid length in QZ field for %s\n",
1591
FnFilter1(G.filename)));
1593
Info(slide, 0x201, ((char *)slide,
1594
"warning: invalid length in QZ field"));
1595
if(jbp->header.d_type)
1597
dlen = jbp->header.d_datalen;
1601
if ((long)LG(dlen) > 0)
1603
G.outfile = fopen(G.filename,"r+");
1604
fseek(G.outfile, -8, SEEK_END);
1605
fread(&ntc, 8, 1, G.outfile);
1606
if(ntc.id != *(long *)"XTcc")
1608
ntc.id = *(long *)"XTcc";
1610
fwrite (&ntc, 8, 1, G.outfile);
1612
Info(slide, 0x201, ((char *)slide, "QData = %d", LG(dlen)));
1615
return; /* finished, cancel further extra field scanning */
1619
Trace((stderr,"qlfix: unknown extra field block, ID=%d\n",
1623
/* Skip this extra field block */
1624
ef_ptr += (eb_len + EB_HEADSIZE);
1625
ef_len -= (eb_len + EB_HEADSIZE);