~ubuntu-branches/ubuntu/intrepid/unzip/intrepid

« back to all changes in this revision

Viewing changes to unix/unix.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2004-06-06 17:57:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040606175746-nl7p2dgp3aobyc2c
Tags: upstream-5.51
ImportĀ upstreamĀ versionĀ 5.51

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 1990-2004 Info-ZIP.  All rights reserved.
 
3
 
 
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
 
8
*/
 
9
/*---------------------------------------------------------------------------
 
10
 
 
11
  unix.c
 
12
 
 
13
  Unix-specific routines for use with Info-ZIP's UnZip 5.41 and later.
 
14
 
 
15
  Contains:  readdir()
 
16
             do_wild()           <-- generic enough to put in fileio.c?
 
17
             mapattr()
 
18
             mapname()
 
19
             checkdir()
 
20
             mkdir()
 
21
             close_outfile()
 
22
             defer_dir_attribs()
 
23
             set_direc_attribs()
 
24
             stamp_file()
 
25
             version()
 
26
 
 
27
  ---------------------------------------------------------------------------*/
 
28
 
 
29
 
 
30
#define UNZIP_INTERNAL
 
31
#include "unzip.h"
 
32
 
 
33
#ifdef SCO_XENIX
 
34
#  define SYSNDIR
 
35
#else  /* SCO Unix, AIX, DNIX, TI SysV, Coherent 4.x, ... */
 
36
#  if defined(__convexc__) || defined(SYSV) || defined(CRAY) || defined(BSD4_4)
 
37
#    define DIRENT
 
38
#  endif
 
39
#endif
 
40
#if defined(_AIX) || defined(__mpexl)
 
41
#  define DIRENT
 
42
#endif
 
43
#ifdef COHERENT
 
44
#  if defined(_I386) || (defined(__COHERENT__) && (__COHERENT__ >= 0x420))
 
45
#    define DIRENT
 
46
#  endif
 
47
#endif
 
48
 
 
49
#ifdef _POSIX_VERSION
 
50
#  ifndef DIRENT
 
51
#    define DIRENT
 
52
#  endif
 
53
#endif
 
54
 
 
55
#ifdef DIRENT
 
56
#  include <dirent.h>
 
57
#else
 
58
#  ifdef SYSV
 
59
#    ifdef SYSNDIR
 
60
#      include <sys/ndir.h>
 
61
#    else
 
62
#      include <ndir.h>
 
63
#    endif
 
64
#  else /* !SYSV */
 
65
#    ifndef NO_SYSDIR
 
66
#      include <sys/dir.h>
 
67
#    endif
 
68
#  endif /* ?SYSV */
 
69
#  ifndef dirent
 
70
#    define dirent direct
 
71
#  endif
 
72
#endif /* ?DIRENT */
 
73
 
 
74
#ifdef SET_DIR_ATTRIB
 
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 */
 
78
    union {
 
79
        iztimes t3;             /* mtime, atime, ctime */
 
80
        ztimbuf t2;             /* modtime, actime */
 
81
    } u;
 
82
    unsigned perms;             /* same as min_info.file_attr */
 
83
    int have_uidgid;            /* flag */
 
84
    ush uidgid[2];
 
85
    char fnbuf[1];              /* buffer stub for directory name */
 
86
} uxdirattr;
 
87
#define UxAtt(d)  ((uxdirattr *)d)    /* typecast shortcut */
 
88
#endif /* SET_DIR_ATTRIB */
 
89
 
 
90
#ifdef ACORN_FTYPE_NFS
 
91
/* Acorn bits for NFS filetyping */
 
92
typedef struct {
 
93
  uch ID[2];
 
94
  uch size[2];
 
95
  uch ID_2[4];
 
96
  uch loadaddr[4];
 
97
  uch execaddr[4];
 
98
  uch attr[4];
 
99
} RO_extra_block;
 
100
 
 
101
#endif /* ACORN_FTYPE_NFS */
 
102
 
 
103
/* static int created_dir;      */      /* used in mapname(), checkdir() */
 
104
/* static int renamed_fullpath; */      /* ditto */
 
105
 
 
106
 
 
107
#ifndef SFX
 
108
#ifdef NO_DIR                  /* for AT&T 3B1 */
 
109
 
 
110
#define opendir(path) fopen(path,"r")
 
111
#define closedir(dir) fclose(dir)
 
112
typedef FILE DIR;
 
113
typedef struct zdir {
 
114
    FILE *dirhandle;
 
115
    struct dirent *entry;
 
116
} DIR
 
117
DIR *opendir OF((ZCONST char *dirspec));
 
118
void closedir OF((DIR *dirp));
 
119
struct dirent *readdir OF((DIR *dirp));
 
120
 
 
121
DIR *opendir(dirspec)
 
122
    ZCONST char *dirspec;
 
123
{
 
124
    DIR *dirp;
 
125
 
 
126
    if ((dirp = malloc(sizeof(DIR)) != NULL) {
 
127
        if ((dirp->dirhandle = fopen(dirspec, "r")) == NULL) {
 
128
            free(dirp);
 
129
            dirp = NULL;
 
130
        }
 
131
    }
 
132
    return dirp;
 
133
}
 
134
 
 
135
void closedir(dirp)
 
136
    DIR *dirp;
 
137
{
 
138
    fclose(dirp->dirhandle);
 
139
    free(dirp);
 
140
}
 
141
 
 
142
/*
 
143
 *  Apparently originally by Rich Salz.
 
144
 *  Cleaned up and modified by James W. Birdsall.
 
145
 */
 
146
struct dirent *readdir(dirp)
 
147
    DIR *dirp;
 
148
{
 
149
 
 
150
    if (dirp == NULL)
 
151
        return NULL;
 
152
 
 
153
    for (;;)
 
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);
 
159
 
 
160
} /* end function readdir() */
 
161
 
 
162
#endif /* NO_DIR */
 
163
 
 
164
 
 
165
/**********************/
 
166
/* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
 
167
/**********************/
 
168
 
 
169
char *do_wild(__G__ wildspec)
 
170
    __GDEF
 
171
    ZCONST char *wildspec;  /* only used first time on a given dir */
 
172
{
 
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;
 
178
*/
 
179
    struct dirent *file;
 
180
 
 
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[].
 
184
     */
 
185
    if (!G.notfirstcall) {  /* first call:  must initialize everything */
 
186
        G.notfirstcall = TRUE;
 
187
 
 
188
        if (!iswild(wildspec)) {
 
189
            strcpy(G.matchname, wildspec);
 
190
            G.have_dirname = FALSE;
 
191
            G.wild_dir = NULL;
 
192
            return G.matchname;
 
193
        }
 
194
 
 
195
        /* break the wildspec into a directory part and a wildcard filename */
 
196
        if ((G.wildname = (ZCONST char *)strrchr(wildspec, '/')) == NULL) {
 
197
            G.dirname = ".";
 
198
            G.dirnamelen = 1;
 
199
            G.have_dirname = FALSE;
 
200
            G.wildname = wildspec;
 
201
        } else {
 
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 */
 
209
            }
 
210
            strncpy(G.dirname, wildspec, G.dirnamelen);
 
211
            G.dirname[G.dirnamelen] = '\0';   /* terminate for strcpy below */
 
212
            G.have_dirname = TRUE;
 
213
        }
 
214
 
 
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);
 
229
                    } else
 
230
                        strcpy(G.matchname, file->d_name);
 
231
                    return G.matchname;
 
232
                }
 
233
            }
 
234
            /* if we get to here directory is exhausted, so close it */
 
235
            closedir((DIR *)G.wild_dir);
 
236
            G.wild_dir = (zvoid *)NULL;
 
237
        }
 
238
        Trace((stderr, "do_wild:  opendir(%s) returns NULL\n",
 
239
          FnFilter1(G.dirname)));
 
240
 
 
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);
 
244
        return G.matchname;
 
245
    }
 
246
 
 
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 */
 
250
        if (G.have_dirname)
 
251
            free(G.dirname);
 
252
        return (char *)NULL;
 
253
    }
 
254
 
 
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
 
257
     * matchname already.
 
258
     */
 
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);
 
269
            } else
 
270
                strcpy(G.matchname, file->d_name);
 
271
            return G.matchname;
 
272
        }
 
273
    }
 
274
 
 
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 */
 
278
    if (G.have_dirname)
 
279
        free(G.dirname);
 
280
    return (char *)NULL;
 
281
 
 
282
} /* end function do_wild() */
 
283
 
 
284
#endif /* !SFX */
 
285
 
 
286
 
 
287
 
 
288
 
 
289
 
 
290
/**********************/
 
291
/* Function mapattr() */
 
292
/**********************/
 
293
 
 
294
int mapattr(__G)
 
295
    __GDEF
 
296
{
 
297
    ulg tmp = G.crec.external_file_attributes;
 
298
 
 
299
    G.pInfo->file_attr = 0;
 
300
    /* initialized to 0 for check in "default" branch below... */
 
301
 
 
302
    switch (G.pInfo->hostnum) {
 
303
        case AMIGA_:
 
304
            tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
 
305
            G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
 
306
            break;
 
307
        case THEOS_:
 
308
            tmp &= 0xF1FFFFFFL;
 
309
            if ((tmp & 0xF0000000L) != 0x40000000L)
 
310
                tmp &= 0x01FFFFFFL;     /* not a dir, mask all ftype bits */
 
311
            else
 
312
                tmp &= 0x41FFFFFFL;     /* leave directory bit as set */
 
313
            /* fall through! */
 
314
        case UNIX_:
 
315
        case VMS_:
 
316
        case ACORN_:
 
317
        case ATARI_:
 
318
        case BEOS_:
 
319
        case QDOS_:
 
320
        case TANDEM_:
 
321
            G.pInfo->file_attr = (unsigned)(tmp >> 16);
 
322
            if (G.pInfo->file_attr != 0 || !G.extra_field) {
 
323
                return 0;
 
324
            } else {
 
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!)
 
338
                 */
 
339
                ush ebID;
 
340
                unsigned ebLen;
 
341
                uch *ef = G.extra_field;
 
342
                unsigned ef_len = G.crec.extra_field_length;
 
343
                int r = FALSE;
 
344
 
 
345
                while (!r && ef_len >= EB_HEADSIZE) {
 
346
                    ebID = makeword(ef);
 
347
                    ebLen = (unsigned)makeword(ef+EB_LEN);
 
348
                    if (ebLen > (ef_len - EB_HEADSIZE))
 
349
                        /* discoverd some e.f. inconsistency! */
 
350
                        break;
 
351
                    switch (ebID) {
 
352
                      case EF_ASIUNIX:
 
353
                        if (ebLen >= (EB_ASI_MODE+2)) {
 
354
                            G.pInfo->file_attr =
 
355
                              (unsigned)makeword(ef+(EB_HEADSIZE+EB_ASI_MODE));
 
356
                            /* force stop of loop: */
 
357
                            ef_len = (ebLen + EB_HEADSIZE);
 
358
                            break;
 
359
                        }
 
360
                        /* else: fall through! */
 
361
                      case EF_PKVMS:
 
362
                        /* "found nondecypherable e.f. with perm. attr" */
 
363
                        r = TRUE;
 
364
                      default:
 
365
                        break;
 
366
                    }
 
367
                    ef_len -= (ebLen + EB_HEADSIZE);
 
368
                    ef += (ebLen + EB_HEADSIZE);
 
369
                }
 
370
                if (!r)
 
371
                    return 0;
 
372
            }
 
373
            /* fall through! */
 
374
        /* all remaining cases:  expand MSDOS read-only bit into write perms */
 
375
        case FS_FAT_:
 
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
 
380
             * bits (see below).
 
381
             */
 
382
            G.pInfo->file_attr = (unsigned)(tmp >> 16);
 
383
            /* fall through! */
 
384
        case FS_HPFS_:
 
385
        case FS_NTFS_:
 
386
        case MAC_:
 
387
        case TOPS20_:
 
388
        default:
 
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.
 
392
             */
 
393
            if ((tmp & 0x10) == 0) {
 
394
                extent fnlen = strlen(G.filename);
 
395
                if (fnlen > 0 && G.filename[fnlen-1] == '/')
 
396
                    tmp |= 0x10;
 
397
            }
 
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!
 
403
                 */
 
404
                return 0;
 
405
            G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
 
406
            break;
 
407
    } /* end switch (host-OS-created-by) */
 
408
 
 
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;
 
412
 
 
413
    return 0;
 
414
 
 
415
} /* end function mapattr() */
 
416
 
 
417
 
 
418
 
 
419
 
 
420
 
 
421
/************************/
 
422
/*  Function mapname()  */
 
423
/************************/
 
424
 
 
425
int mapname(__G__ renamed)
 
426
    __GDEF
 
427
    int renamed;
 
428
/*
 
429
 * returns:
 
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]
 
437
 */
 
438
{
 
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 */
 
445
#endif
 
446
    int killed_ddot = FALSE;       /* is set when skipping "../" pathcomp */
 
447
    int error = MPN_OK;
 
448
    register unsigned workch;      /* hold the character being tested */
 
449
 
 
450
 
 
451
/*---------------------------------------------------------------------------
 
452
    Initialize various pointers and counters and stuff.
 
453
  ---------------------------------------------------------------------------*/
 
454
 
 
455
    if (G.pInfo->vollabel)
 
456
        return MPN_VOL_LABEL;   /* can't set disk volume labels in Unix */
 
457
 
 
458
    /* can create path as long as not just freshening, or if user told us */
 
459
    G.create_dirs = (!uO.fflag || renamed);
 
460
 
 
461
    G.created_dir = FALSE;      /* not yet */
 
462
 
 
463
    /* user gave full pathname:  don't prepend rootpath */
 
464
    G.renamed_fullpath = (renamed && (*G.filename == '/'));
 
465
 
 
466
    if (checkdir(__G__ (char *)NULL, INIT) == MPN_NOMEM)
 
467
        return MPN_NOMEM;       /* initialize path buffer, unless no memory */
 
468
 
 
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 */
 
475
    else
 
476
        ++cp;                   /* point to start of last component of path */
 
477
 
 
478
/*---------------------------------------------------------------------------
 
479
    Begin main loop through characters in filename.
 
480
  ---------------------------------------------------------------------------*/
 
481
 
 
482
    while ((workch = (uch)*cp++) != 0) {
 
483
 
 
484
        switch (workch) {
 
485
            case '/':             /* can assume -j flag not given */
 
486
                *pp = '\0';
 
487
                if (strcmp(pathcomp, ".") == 0) {
 
488
                    /* don't bother appending "./" to the path */
 
489
                    *pathcomp = '\0';
 
490
                } else if (!uO.ddotflag && strcmp(pathcomp, "..") == 0) {
 
491
                    /* "../" dir traversal detected, skip over it */
 
492
                    *pathcomp = '\0';
 
493
                    killed_ddot = TRUE;     /* set "show message" flag */
 
494
                }
 
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)
 
499
                    return error;
 
500
                pp = pathcomp;    /* reset conversion buffer for next piece */
 
501
                lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
 
502
                break;
 
503
 
 
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 */
 
512
            case '*':
 
513
                *pp++ = '_';  /* these rules apply equally to FAT and NTFS */
 
514
                break;
 
515
#endif
 
516
            case ';':             /* VMS version (or DEC-20 attrib?) */
 
517
                lastsemi = pp;
 
518
                *pp++ = ';';      /* keep for now; remove VMS ";##" */
 
519
                break;            /*  later, if requested */
 
520
 
 
521
#ifdef ACORN_FTYPE_NFS
 
522
            case ',':             /* NFS filetype extension */
 
523
                lastcomma = pp;
 
524
                *pp++ = ',';      /* keep for now; may need to remove */
 
525
                break;            /*  later, if requested */
 
526
#endif
 
527
 
 
528
#ifdef MTS
 
529
            case ' ':             /* change spaces to underscore under */
 
530
                *pp++ = '_';      /*  MTS; leave as spaces under Unix */
 
531
                break;
 
532
#endif
 
533
 
 
534
            default:
 
535
                /* allow European characters in filenames: */
 
536
                if (isprint(workch) || (128 <= workch && workch <= 254))
 
537
                    *pp++ = (char)workch;
 
538
        } /* end switch */
 
539
 
 
540
    } /* end while loop */
 
541
 
 
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;
 
549
    }
 
550
 
 
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-
 
554
    fore exiting.
 
555
  ---------------------------------------------------------------------------*/
 
556
 
 
557
    if (G.filename[strlen(G.filename) - 1] == '/') {
 
558
        checkdir(__G__ G.filename, GETPATH);
 
559
        if (G.created_dir) {
 
560
            if (QCOND2) {
 
561
                Info(slide, 0, ((char *)slide, "   creating: %s\n",
 
562
                  FnFilter1(G.filename)));
 
563
            }
 
564
#ifndef NO_CHMOD
 
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");
 
568
#endif
 
569
            /* set dir time (note trailing '/') */
 
570
            return (error & ~MPN_MASK) | MPN_CREATED_DIR;
 
571
        }
 
572
        /* dir existed already; don't look for data to extract */
 
573
        return (error & ~MPN_MASK) | MPN_INF_SKIP;
 
574
    }
 
575
 
 
576
    *pp = '\0';                   /* done with pathcomp:  terminate it */
 
577
 
 
578
    /* if not saving them, remove VMS version numbers (appended ";###") */
 
579
    if (!uO.V_flag && lastsemi) {
 
580
        pp = lastsemi + 1;
 
581
        while (isdigit((uch)(*pp)))
 
582
            ++pp;
 
583
        if (*pp == '\0')          /* only digits between ';' and end:  nuke */
 
584
            *lastsemi = '\0';
 
585
    }
 
586
 
 
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)
 
593
    {
 
594
        /* file *must* have a RISC OS extra field */
 
595
        long ft = (long)makelong(ef_spark->loadaddr);
 
596
        /*32-bit*/
 
597
        if (lastcomma) {
 
598
            pp = lastcomma + 1;
 
599
            while (isxdigit((uch)(*pp))) ++pp;
 
600
            if (pp == lastcomma+4 && *pp == '\0') *lastcomma='\0'; /* nuke */
 
601
        }
 
602
        if ((ft & 1<<31)==0) ft=0x000FFD00;
 
603
        sprintf(pathcomp+strlen(pathcomp), ",%03x", (int)(ft>>8) & 0xFFF);
 
604
    }
 
605
#endif /* ACORN_FTYPE_NFS */
 
606
 
 
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;
 
611
    }
 
612
 
 
613
    checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
 
614
    checkdir(__G__ G.filename, GETPATH);
 
615
 
 
616
    return error;
 
617
 
 
618
} /* end function mapname() */
 
619
 
 
620
 
 
621
 
 
622
 
 
623
#if 0  /*========== NOTES ==========*/
 
624
 
 
625
  extract-to dir:      a:path/
 
626
  buildpath:           path1/path2/ ...   (NULL-terminated)
 
627
  pathcomp:                filename
 
628
 
 
629
  mapname():
 
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
 
638
 
 
639
#endif /* 0 */
 
640
 
 
641
 
 
642
 
 
643
 
 
644
/***********************/
 
645
/* Function checkdir() */
 
646
/***********************/
 
647
 
 
648
int checkdir(__G__ pathcomp, flag)
 
649
    __GDEF
 
650
    char *pathcomp;
 
651
    int flag;
 
652
/*
 
653
 * returns:
 
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
 
661
 */
 
662
{
 
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') */
 
667
 
 
668
#   define FN_MASK   7
 
669
#   define FUNCTION  (flag & FN_MASK)
 
670
 
 
671
 
 
672
 
 
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
  ---------------------------------------------------------------------------*/
 
678
 
 
679
    if (FUNCTION == APPEND_DIR) {
 
680
        int too_long = FALSE;
 
681
#ifdef SHORT_NAMES
 
682
        char *old_end = end;
 
683
#endif
 
684
 
 
685
        Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
 
686
        while ((*G.end = *pathcomp++) != '\0')
 
687
            ++G.end;
 
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';
 
691
#endif
 
692
 
 
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. */
 
697
 
 
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) */
 
703
                free(G.buildpath);
 
704
                return MPN_INF_SKIP;    /* path doesn't exist: nothing to do */
 
705
            }
 
706
            if (too_long) {
 
707
                Info(slide, 1, ((char *)slide,
 
708
                  "checkdir error:  path too long: %s\n",
 
709
                  FnFilter1(G.buildpath)));
 
710
                free(G.buildpath);
 
711
                /* no room for filenames:  fatal */
 
712
                return MPN_ERR_TOOLONG;
 
713
            }
 
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)));
 
719
                free(G.buildpath);
 
720
                /* path didn't exist, tried to create, failed */
 
721
                return MPN_ERR_SKIP;
 
722
            }
 
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)));
 
729
            free(G.buildpath);
 
730
            /* path existed but wasn't dir */
 
731
            return MPN_ERR_SKIP;
 
732
        }
 
733
        if (too_long) {
 
734
            Info(slide, 1, ((char *)slide,
 
735
              "checkdir error:  path too long: %s\n", FnFilter1(G.buildpath)));
 
736
            free(G.buildpath);
 
737
            /* no room for filenames:  fatal */
 
738
            return MPN_ERR_TOOLONG;
 
739
        }
 
740
        *G.end++ = '/';
 
741
        *G.end = '\0';
 
742
        Trace((stderr, "buildpath now = [%s]\n", FnFilter1(G.buildpath)));
 
743
        return MPN_OK;
 
744
 
 
745
    } /* end if (FUNCTION == APPEND_DIR) */
 
746
 
 
747
/*---------------------------------------------------------------------------
 
748
    GETPATH:  copy full path to the string pointed at by pathcomp, and free
 
749
    G.buildpath.
 
750
  ---------------------------------------------------------------------------*/
 
751
 
 
752
    if (FUNCTION == GETPATH) {
 
753
        strcpy(pathcomp, G.buildpath);
 
754
        Trace((stderr, "getting and freeing path [%s]\n",
 
755
          FnFilter1(pathcomp)));
 
756
        free(G.buildpath);
 
757
        G.buildpath = G.end = (char *)NULL;
 
758
        return MPN_OK;
 
759
    }
 
760
 
 
761
/*---------------------------------------------------------------------------
 
762
    APPEND_NAME:  assume the path component is the filename; append it and
 
763
    return without checking for existence.
 
764
  ---------------------------------------------------------------------------*/
 
765
 
 
766
    if (FUNCTION == APPEND_NAME) {
 
767
#ifdef SHORT_NAMES
 
768
        char *old_end = end;
 
769
#endif
 
770
 
 
771
        Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
 
772
        while ((*G.end = *pathcomp++) != '\0') {
 
773
            ++G.end;
 
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';
 
777
#endif
 
778
            if ((G.end-G.buildpath) >= FILNAMSIZ) {
 
779
                *--G.end = '\0';
 
780
                Info(slide, 0x201, ((char *)slide,
 
781
                  "checkdir warning:  path too long; truncating\n\
 
782
                   %s\n                -> %s\n",
 
783
                  FnFilter1(G.filename), FnFilter2(G.buildpath)));
 
784
                return MPN_INF_TRUNC;   /* filename truncated */
 
785
            }
 
786
        }
 
787
        Trace((stderr, "buildpath now = [%s]\n", FnFilter1(G.buildpath)));
 
788
        /* could check for existence here, prompt for new name... */
 
789
        return MPN_OK;
 
790
    }
 
791
 
 
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
 
795
    extract-to path.
 
796
  ---------------------------------------------------------------------------*/
 
797
 
 
798
/* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
 
799
 
 
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)))
 
805
#else
 
806
        if ((G.buildpath = (char *)malloc(strlen(G.filename)+G.rootlen+1))
 
807
#endif
 
808
            == (char *)NULL)
 
809
            return MPN_NOMEM;
 
810
        if ((G.rootlen > 0) && !G.renamed_fullpath) {
 
811
            strcpy(G.buildpath, G.rootpath);
 
812
            G.end = G.buildpath + G.rootlen;
 
813
        } else {
 
814
            *G.buildpath = '\0';
 
815
            G.end = G.buildpath;
 
816
        }
 
817
        Trace((stderr, "[%s]\n", FnFilter1(G.buildpath)));
 
818
        return MPN_OK;
 
819
    }
 
820
 
 
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
 
825
    on the command line.
 
826
  ---------------------------------------------------------------------------*/
 
827
 
 
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) {
 
833
            G.rootlen = 0;
 
834
            return MPN_OK;
 
835
        }
 
836
        if (G.rootlen > 0)      /* rootpath was already set, nothing to do */
 
837
            return MPN_OK;
 
838
        if ((G.rootlen = strlen(pathcomp)) > 0) {
 
839
            char *tmproot;
 
840
 
 
841
            if ((tmproot = (char *)malloc(G.rootlen+2)) == (char *)NULL) {
 
842
                G.rootlen = 0;
 
843
                return MPN_NOMEM;
 
844
            }
 
845
            strcpy(tmproot, pathcomp);
 
846
            if (tmproot[G.rootlen-1] == '/') {
 
847
                tmproot[--G.rootlen] = '\0';
 
848
            }
 
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) */ ) {
 
853
                    free(tmproot);
 
854
                    G.rootlen = 0;
 
855
                    /* skip (or treat as stored file) */
 
856
                    return MPN_INF_SKIP;
 
857
                }
 
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)));
 
864
                    free(tmproot);
 
865
                    G.rootlen = 0;
 
866
                    /* path didn't exist, tried to create, and failed: */
 
867
                    /* file exists, or 2+ subdir levels required */
 
868
                    return MPN_ERR_SKIP;
 
869
                }
 
870
            }
 
871
            tmproot[G.rootlen++] = '/';
 
872
            tmproot[G.rootlen] = '\0';
 
873
            if ((G.rootpath = (char *)realloc(tmproot, G.rootlen+1)) == NULL) {
 
874
                free(tmproot);
 
875
                G.rootlen = 0;
 
876
                return MPN_NOMEM;
 
877
            }
 
878
            Trace((stderr, "rootpath now = [%s]\n", FnFilter1(G.rootpath)));
 
879
        }
 
880
        return MPN_OK;
 
881
    }
 
882
#endif /* !SFX || SFX_EXDIR */
 
883
 
 
884
/*---------------------------------------------------------------------------
 
885
    END:  free rootpath, immediately prior to program exit.
 
886
  ---------------------------------------------------------------------------*/
 
887
 
 
888
    if (FUNCTION == END) {
 
889
        Trace((stderr, "freeing rootpath\n"));
 
890
        if (G.rootlen > 0) {
 
891
            free(G.rootpath);
 
892
            G.rootlen = 0;
 
893
        }
 
894
        return MPN_OK;
 
895
    }
 
896
 
 
897
    return MPN_INVALID; /* should never reach */
 
898
 
 
899
} /* end function checkdir() */
 
900
 
 
901
 
 
902
 
 
903
 
 
904
 
 
905
#ifdef NO_MKDIR
 
906
 
 
907
/********************/
 
908
/* Function mkdir() */
 
909
/********************/
 
910
 
 
911
int mkdir(path, mode)
 
912
    ZCONST char *path;
 
913
    int mode;   /* ignored */
 
914
/*
 
915
 * returns:   0 - successful
 
916
 *           -1 - failed (errno not set, however)
 
917
 */
 
918
{
 
919
    char command[FILNAMSIZ+40]; /* buffer for system() call */
 
920
 
 
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);
 
924
    if (system(command))
 
925
        return -1;
 
926
    return 0;
 
927
}
 
928
 
 
929
#endif /* NO_MKDIR */
 
930
 
 
931
 
 
932
 
 
933
 
 
934
#if (!defined(MTS) || defined(SET_DIR_ATTRIB))
 
935
static int get_extattribs OF((__GPRO__ iztimes *pzt, ush z_uidgid[2]));
 
936
 
 
937
static int get_extattribs(__G__ pzt, z_uidgid)
 
938
    __GDEF
 
939
    iztimes *pzt;
 
940
    ush z_uidgid[2];
 
941
{
 
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
  ---------------------------------------------------------------------------*/
 
950
    int have_uidgid_flg;
 
951
    unsigned eb_izux_flg;
 
952
 
 
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,
 
955
#ifdef IZ_CHECK_TZ
 
956
                   (G.tz_is_valid ? pzt : NULL),
 
957
#else
 
958
                   pzt,
 
959
#endif
 
960
                   z_uidgid) : 0);
 
961
    if (eb_izux_flg & EB_UT_FL_MTIME) {
 
962
        TTrace((stderr, "\nget_extattribs:  Unix e.f. modif. time = %ld\n",
 
963
          pzt->mtime));
 
964
    } else {
 
965
        pzt->mtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
 
966
    }
 
967
    if (eb_izux_flg & EB_UT_FL_ATIME) {
 
968
        TTrace((stderr, "get_extattribs:  Unix e.f. access time = %ld\n",
 
969
          pzt->atime));
 
970
    } else {
 
971
        pzt->atime = pzt->mtime;
 
972
        TTrace((stderr, "\nget_extattribs:  modification/access times = %ld\n",
 
973
          pzt->mtime));
 
974
    }
 
975
 
 
976
    /* if -X option was specified and we have UID/GID info, restore it */
 
977
    have_uidgid_flg =
 
978
#ifdef RESTORE_UIDGID
 
979
            (uO.X_flag && (eb_izux_flg & EB_UX2_VALID));
 
980
#else
 
981
            0;
 
982
#endif
 
983
    return have_uidgid_flg;
 
984
}
 
985
#endif /* !MTS || SET_DIR_ATTRIB */
 
986
 
 
987
 
 
988
 
 
989
#ifndef MTS
 
990
 
 
991
/****************************/
 
992
/* Function close_outfile() */
 
993
/****************************/
 
994
 
 
995
void close_outfile(__G)    /* GRR: change to return PK-style warning level */
 
996
    __GDEF
 
997
{
 
998
    union {
 
999
        iztimes t3;             /* mtime, atime, ctime */
 
1000
        ztimbuf t2;             /* modtime, actime */
 
1001
    } zt;
 
1002
    ush z_uidgid[2];
 
1003
    int have_uidgid_flg;
 
1004
 
 
1005
    fclose(G.outfile);
 
1006
 
 
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
  ---------------------------------------------------------------------------*/
 
1014
 
 
1015
#ifdef SYMLINKS
 
1016
    if (G.symlnk) {
 
1017
        unsigned ucsize = (unsigned)G.lrec.ucsize;
 
1018
        extent slnk_entrysize = sizeof(slinkentry) + ucsize +
 
1019
                                strlen(G.filename);
 
1020
        slinkentry *slnk_entry;
 
1021
 
 
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)));
 
1026
            return;
 
1027
        }
 
1028
 
 
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)));
 
1033
            return;
 
1034
        }
 
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);
 
1041
 
 
1042
        /* reopen the "link data" file for reading */
 
1043
        G.outfile = fopen(G.filename, FOPR);
 
1044
 
 
1045
        if (!G.outfile ||
 
1046
            fread(slnk_entry->target, 1, ucsize, G.outfile) != (int)ucsize)
 
1047
        {
 
1048
            Info(slide, 0x201, ((char *)slide,
 
1049
              "warning:  symbolic link (%s) failed\n",
 
1050
              FnFilter1(G.filename)));
 
1051
            free(slnk_entry);
 
1052
            fclose(G.outfile);
 
1053
            return;
 
1054
        }
 
1055
        fclose(G.outfile);                  /* close "link" file for good... */
 
1056
        slnk_entry->target[ucsize] = '\0';
 
1057
        if (QCOND2)
 
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;
 
1063
        else
 
1064
            G.slink_head = slnk_entry;
 
1065
        G.slink_last = slnk_entry;
 
1066
        return;
 
1067
    }
 
1068
#endif /* SYMLINKS */
 
1069
 
 
1070
#ifdef QLZIP
 
1071
    if (G.extra_field) {
 
1072
        static void qlfix OF((__GPRO__ uch *ef_ptr, unsigned ef_len));
 
1073
 
 
1074
        qlfix(__G__ G.extra_field, G.lrec.extra_field_length);
 
1075
    }
 
1076
#endif
 
1077
 
 
1078
    have_uidgid_flg = get_extattribs(__G__ &(zt.t3), z_uidgid);
 
1079
 
 
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]))
 
1084
        {
 
1085
            if (uO.qflag)
 
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)));
 
1089
            else
 
1090
                Info(slide, 0x201, ((char *)slide,
 
1091
                  " (warning) cannot set UID %d and/or GID %d",
 
1092
                  z_uidgid[0], z_uidgid[1]));
 
1093
        }
 
1094
    }
 
1095
 
 
1096
    /* set the file's access and modification times */
 
1097
    if (utime(G.filename, &(zt.t2))) {
 
1098
#ifdef AOS_VS
 
1099
        if (uO.qflag)
 
1100
            Info(slide, 0x201, ((char *)slide, "... cannot set time for %s\n",
 
1101
              FnFilter1(G.filename)));
 
1102
        else
 
1103
            Info(slide, 0x201, ((char *)slide, "... cannot set time"));
 
1104
#else
 
1105
        if (uO.qflag)
 
1106
            Info(slide, 0x201, ((char *)slide,
 
1107
              "warning:  cannot set times for %s\n", FnFilter1(G.filename)));
 
1108
        else
 
1109
            Info(slide, 0x201, ((char *)slide,
 
1110
              " (warning) cannot set times"));
 
1111
#endif /* ?AOS_VS */
 
1112
    }
 
1113
 
 
1114
/*---------------------------------------------------------------------------
 
1115
    Change the file permissions from default ones to those stored in the
 
1116
    zipfile.
 
1117
  ---------------------------------------------------------------------------*/
 
1118
 
 
1119
#ifndef NO_CHMOD
 
1120
    if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
 
1121
        perror("chmod (file attributes) error");
 
1122
#endif
 
1123
 
 
1124
} /* end function close_outfile() */
 
1125
 
 
1126
#endif /* !MTS */
 
1127
 
 
1128
 
 
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";
 
1135
#  ifndef NO_CHMOD
 
1136
  static ZCONST char Far DirlistChmodFailed[] =
 
1137
    "warning:  cannot set permissions for %s\n";
 
1138
#  endif
 
1139
 
 
1140
 
 
1141
int defer_dir_attribs(__G__ pd)
 
1142
    __GDEF
 
1143
    direntry **pd;
 
1144
{
 
1145
    uxdirattr *d_entry;
 
1146
 
 
1147
    d_entry = (uxdirattr *)malloc(sizeof(uxdirattr) + strlen(G.filename));
 
1148
    *pd = (direntry *)d_entry;
 
1149
    if (d_entry == (uxdirattr *)NULL) {
 
1150
        return PK_MEM;
 
1151
    }
 
1152
    d_entry->fn = d_entry->fnbuf;
 
1153
    strcpy(d_entry->fn, G.filename);
 
1154
 
 
1155
    d_entry->perms = G.pInfo->file_attr;
 
1156
 
 
1157
    d_entry->have_uidgid = get_extattribs(__G__ &(d_entry->u.t3),
 
1158
                                          d_entry->uidgid);
 
1159
    return PK_OK;
 
1160
} /* end function defer_dir_attribs() */
 
1161
 
 
1162
 
 
1163
int set_direc_attribs(__G__ d)
 
1164
    __GDEF
 
1165
    direntry *d;
 
1166
{
 
1167
    int errval = PK_OK;
 
1168
 
 
1169
    if (UxAtt(d)->have_uidgid &&
 
1170
        chown(UxAtt(d)->fn, (uid_t)UxAtt(d)->uidgid[0],
 
1171
              (gid_t)UxAtt(d)->uidgid[1]))
 
1172
    {
 
1173
        Info(slide, 0x201, ((char *)slide,
 
1174
          LoadFarString(DirlistUidGidFailed),
 
1175
          UxAtt(d)->uidgid[0], UxAtt(d)->uidgid[1], FnFilter1(d->fn)));
 
1176
        if (!errval)
 
1177
            errval = PK_WARN;
 
1178
    }
 
1179
    if (utime(d->fn, &UxAtt(d)->u.t2)) {
 
1180
        Info(slide, 0x201, ((char *)slide,
 
1181
          LoadFarString(DirlistUtimeFailed), FnFilter1(d->fn)));
 
1182
        if (!errval)
 
1183
            errval = PK_WARN;
 
1184
    }
 
1185
#ifndef NO_CHMOD
 
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"); */
 
1190
        if (!errval)
 
1191
            errval = PK_WARN;
 
1192
    }
 
1193
#endif /* !NO_CHMOD */
 
1194
    return errval;
 
1195
} /* end function set_direc_attribs() */
 
1196
 
 
1197
#endif /* SET_DIR_ATTRIB */
 
1198
 
 
1199
 
 
1200
 
 
1201
 
 
1202
#ifdef TIMESTAMP
 
1203
 
 
1204
/***************************/
 
1205
/*  Function stamp_file()  */
 
1206
/***************************/
 
1207
 
 
1208
int stamp_file(fname, modtime)
 
1209
    ZCONST char *fname;
 
1210
    time_t modtime;
 
1211
{
 
1212
    ztimbuf tp;
 
1213
 
 
1214
    tp.modtime = tp.actime = modtime;
 
1215
    return (utime(fname, &tp));
 
1216
 
 
1217
} /* end function stamp_file() */
 
1218
 
 
1219
#endif /* TIMESTAMP */
 
1220
 
 
1221
 
 
1222
 
 
1223
 
 
1224
#ifndef SFX
 
1225
 
 
1226
/************************/
 
1227
/*  Function version()  */
 
1228
/************************/
 
1229
 
 
1230
void version(__G)
 
1231
    __GDEF
 
1232
{
 
1233
#if (defined(__GNUC__) && defined(NX_CURRENT_COMPILER_RELEASE))
 
1234
    char cc_namebuf[40];
 
1235
    char cc_versbuf[40];
 
1236
#else
 
1237
#if (defined(CRAY) && defined(_RELEASE))
 
1238
    char cc_versbuf[40];
 
1239
#endif
 
1240
#endif
 
1241
#if ((defined(CRAY) || defined(cray)) && defined(_UNICOS))
 
1242
    char os_namebuf[40];
 
1243
#else
 
1244
#if defined(__NetBSD__)
 
1245
    char os_namebuf[40];
 
1246
#endif
 
1247
#endif
 
1248
 
 
1249
    /* Pyramid, NeXT have problems with huge macro expansion, too:  no Info() */
 
1250
    sprintf((char *)slide, LoadFarString(CompiledWith),
 
1251
 
 
1252
#ifdef __GNUC__
 
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),
 
1256
       cc_namebuf),
 
1257
      (strlen(__VERSION__) > 8)? "(gcc)" :
 
1258
        (sprintf(cc_versbuf, "(gcc %s)", __VERSION__), cc_versbuf),
 
1259
#  else
 
1260
      "gcc ", __VERSION__,
 
1261
#  endif
 
1262
#else
 
1263
#  if defined(CRAY) && defined(_RELEASE)
 
1264
      "cc ", (sprintf(cc_versbuf, "version %d", _RELEASE), cc_versbuf),
 
1265
#  else
 
1266
#  ifdef __VERSION__
 
1267
#   ifndef IZ_CC_NAME
 
1268
#    define IZ_CC_NAME "cc "
 
1269
#   endif
 
1270
      IZ_CC_NAME, __VERSION__
 
1271
#  else
 
1272
#   ifndef IZ_CC_NAME
 
1273
#    define IZ_CC_NAME "cc"
 
1274
#   endif
 
1275
      IZ_CC_NAME, "",
 
1276
#  endif
 
1277
#  endif
 
1278
#endif /* ?__GNUC__ */
 
1279
 
 
1280
#ifndef IZ_OS_NAME
 
1281
#  define IZ_OS_NAME "Unix"
 
1282
#endif
 
1283
      IZ_OS_NAME,
 
1284
 
 
1285
#if defined(sgi) || defined(__sgi)
 
1286
      " (Silicon Graphics IRIX)",
 
1287
#else
 
1288
#ifdef sun
 
1289
#  ifdef sparc
 
1290
#    ifdef __SVR4
 
1291
      " (Sun SPARC/Solaris)",
 
1292
#    else /* may or may not be SunOS */
 
1293
      " (Sun SPARC)",
 
1294
#    endif
 
1295
#  else
 
1296
#  if defined(sun386) || defined(i386)
 
1297
      " (Sun 386i)",
 
1298
#  else
 
1299
#  if defined(mc68020) || defined(__mc68020__)
 
1300
      " (Sun 3)",
 
1301
#  else /* mc68010 or mc68000:  Sun 2 or earlier */
 
1302
      " (Sun 2)",
 
1303
#  endif
 
1304
#  endif
 
1305
#  endif
 
1306
#else
 
1307
#ifdef __hpux
 
1308
      " (HP/UX)",
 
1309
#else
 
1310
#ifdef __osf__
 
1311
      " (DEC OSF/1)",
 
1312
#else
 
1313
#ifdef _AIX
 
1314
      " (IBM AIX)",
 
1315
#else
 
1316
#ifdef aiws
 
1317
      " (IBM RT/AIX)",
 
1318
#else
 
1319
#if defined(CRAY) || defined(cray)
 
1320
#  ifdef _UNICOS
 
1321
      (sprintf(os_namebuf, " (Cray UNICOS release %d)", _UNICOS), os_namebuf),
 
1322
#  else
 
1323
      " (Cray UNICOS)",
 
1324
#  endif
 
1325
#else
 
1326
#if defined(uts) || defined(UTS)
 
1327
      " (Amdahl UTS)",
 
1328
#else
 
1329
#ifdef NeXT
 
1330
#  ifdef mc68000
 
1331
      " (NeXTStep/black)",
 
1332
#  else
 
1333
      " (NeXTStep for Intel)",
 
1334
#  endif
 
1335
#else              /* the next dozen or so are somewhat order-dependent */
 
1336
#ifdef LINUX
 
1337
#  ifdef __ELF__
 
1338
      " (Linux ELF)",
 
1339
#  else
 
1340
      " (Linux a.out)",
 
1341
#  endif
 
1342
#else
 
1343
#ifdef MINIX
 
1344
      " (Minix)",
 
1345
#else
 
1346
#ifdef M_UNIX
 
1347
      " (SCO Unix)",
 
1348
#else
 
1349
#ifdef M_XENIX
 
1350
      " (SCO Xenix)",
 
1351
#else
 
1352
#ifdef __NetBSD__
 
1353
#  ifdef NetBSD0_8
 
1354
      (sprintf(os_namebuf, " (NetBSD 0.8%c)", (char)(NetBSD0_8 - 1 + 'A')),
 
1355
       os_namebuf),
 
1356
#  else
 
1357
#  ifdef NetBSD0_9
 
1358
      (sprintf(os_namebuf, " (NetBSD 0.9%c)", (char)(NetBSD0_9 - 1 + 'A')),
 
1359
       os_namebuf),
 
1360
#  else
 
1361
#  ifdef NetBSD1_0
 
1362
      (sprintf(os_namebuf, " (NetBSD 1.0%c)", (char)(NetBSD1_0 - 1 + 'A')),
 
1363
       os_namebuf),
 
1364
#  else
 
1365
      (BSD4_4 == 0.5)? " (NetBSD before 0.9)" : " (NetBSD 1.1 or later)",
 
1366
#  endif
 
1367
#  endif
 
1368
#  endif
 
1369
#else
 
1370
#ifdef __FreeBSD__
 
1371
      (BSD4_4 == 0.5)? " (FreeBSD 1.x)" : " (FreeBSD 2.0 or later)",
 
1372
#else
 
1373
#ifdef __bsdi__
 
1374
      (BSD4_4 == 0.5)? " (BSD/386 1.0)" : " (BSD/386 1.1 or later)",
 
1375
#else
 
1376
#ifdef __386BSD__
 
1377
      (BSD4_4 == 1)? " (386BSD, post-4.4 release)" : " (386BSD)",
 
1378
#else
 
1379
#ifdef __CYGWIN__
 
1380
      " (Cygwin)",
 
1381
#else
 
1382
#if defined(i686) || defined(__i686) || defined(__i686__)
 
1383
      " (Intel 686)",
 
1384
#else
 
1385
#if defined(i586) || defined(__i586) || defined(__i586__)
 
1386
      " (Intel 586)",
 
1387
#else
 
1388
#if defined(i486) || defined(__i486) || defined(__i486__)
 
1389
      " (Intel 486)",
 
1390
#else
 
1391
#if defined(i386) || defined(__i386) || defined(__i386__)
 
1392
      " (Intel 386)",
 
1393
#else
 
1394
#ifdef pyr
 
1395
      " (Pyramid)",
 
1396
#else
 
1397
#ifdef ultrix
 
1398
#  ifdef mips
 
1399
      " (DEC/MIPS)",
 
1400
#  else
 
1401
#  ifdef vax
 
1402
      " (DEC/VAX)",
 
1403
#  else /* __alpha? */
 
1404
      " (DEC/Alpha)",
 
1405
#  endif
 
1406
#  endif
 
1407
#else
 
1408
#ifdef gould
 
1409
      " (Gould)",
 
1410
#else
 
1411
#ifdef MTS
 
1412
      " (MTS)",
 
1413
#else
 
1414
#ifdef __convexc__
 
1415
      " (Convex)",
 
1416
#else
 
1417
#ifdef __QNX__
 
1418
      " (QNX 4)",
 
1419
#else
 
1420
#ifdef __QNXNTO__
 
1421
      " (QNX Neutrino)",
 
1422
#else
 
1423
#ifdef Lynx
 
1424
      " (LynxOS)",
 
1425
#else
 
1426
      "",
 
1427
#endif /* Lynx */
 
1428
#endif /* QNX Neutrino */
 
1429
#endif /* QNX 4 */
 
1430
#endif /* Convex */
 
1431
#endif /* MTS */
 
1432
#endif /* Gould */
 
1433
#endif /* DEC */
 
1434
#endif /* Pyramid */
 
1435
#endif /* 386 */
 
1436
#endif /* 486 */
 
1437
#endif /* 586 */
 
1438
#endif /* 686 */
 
1439
#endif /* Cygwin */
 
1440
#endif /* 386BSD */
 
1441
#endif /* BSDI BSD/386 */
 
1442
#endif /* NetBSD */
 
1443
#endif /* FreeBSD */
 
1444
#endif /* SCO Xenix */
 
1445
#endif /* SCO Unix */
 
1446
#endif /* Minix */
 
1447
#endif /* Linux */
 
1448
#endif /* NeXT */
 
1449
#endif /* Amdahl */
 
1450
#endif /* Cray */
 
1451
#endif /* RT/AIX */
 
1452
#endif /* AIX */
 
1453
#endif /* OSF/1 */
 
1454
#endif /* HP/UX */
 
1455
#endif /* Sun */
 
1456
#endif /* SGI */
 
1457
 
 
1458
#ifdef __DATE__
 
1459
      " on ", __DATE__
 
1460
#else
 
1461
      "", ""
 
1462
#endif
 
1463
    );
 
1464
 
 
1465
    (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
 
1466
 
 
1467
} /* end function version() */
 
1468
 
 
1469
#endif /* !SFX */
 
1470
 
 
1471
 
 
1472
 
 
1473
 
 
1474
#ifdef QLZIP
 
1475
 
 
1476
struct qdirect  {
 
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 */
 
1487
};
 
1488
 
 
1489
#define LONGID  "QDOS02"
 
1490
#define EXTRALEN (sizeof(struct qdirect) + 8)
 
1491
#define JBLONGID    "QZHD"
 
1492
#define JBEXTRALEN  (sizeof(jbextra)  - 4 * sizeof(char))
 
1493
 
 
1494
typedef struct {
 
1495
    char        eb_header[4] __attribute__ ((packed));  /* place_holder */
 
1496
    char        longid[8] __attribute__ ((packed));
 
1497
    struct      qdirect     header __attribute__ ((packed));
 
1498
} qdosextra;
 
1499
 
 
1500
typedef struct {
 
1501
    char        eb_header[4];                           /* place_holder */
 
1502
    char        longid[4];
 
1503
    struct      qdirect     header;
 
1504
} jbextra;
 
1505
 
 
1506
 
 
1507
 
 
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.
 
1511
 */
 
1512
static ush SH(ush val)
 
1513
{
 
1514
    uch swapbuf[2];
 
1515
 
 
1516
    swapbuf[1] = (uch)(val & 0xff);
 
1517
    swapbuf[0] = (uch)(val >> 8);
 
1518
    return (*(ush *)swapbuf);
 
1519
}
 
1520
 
 
1521
 
 
1522
 
 
1523
static ulg LG(ulg val)
 
1524
{
 
1525
    /*  convert the big-endian unsigned long number `val' to the machine
 
1526
     *  dependant representation
 
1527
     */
 
1528
    ush swapbuf[2];
 
1529
 
 
1530
    swapbuf[1] = SH((ush)(val & 0xffff));
 
1531
    swapbuf[0] = SH((ush)(val >> 16));
 
1532
    return (*(ulg *)swapbuf);
 
1533
}
 
1534
 
 
1535
 
 
1536
 
 
1537
static void qlfix(__G__ ef_ptr, ef_len)
 
1538
    __GDEF
 
1539
    uch *ef_ptr;
 
1540
    unsigned ef_len;
 
1541
{
 
1542
    while (ef_len >= EB_HEADSIZE)
 
1543
    {
 
1544
        unsigned    eb_id  = makeword(EB_ID + ef_ptr);
 
1545
        unsigned    eb_len = makeword(EB_LEN + ef_ptr);
 
1546
 
 
1547
        if (eb_len > (ef_len - EB_HEADSIZE)) {
 
1548
            /* discovered some extra field inconsistency! */
 
1549
            Trace((stderr,
 
1550
              "qlfix: block length %u > rest ef_size %u\n", eb_len,
 
1551
              ef_len - EB_HEADSIZE));
 
1552
            break;
 
1553
        }
 
1554
 
 
1555
        switch (eb_id) {
 
1556
          case EF_QDOS:
 
1557
          {
 
1558
            struct _ntc_
 
1559
            {
 
1560
                long id;
 
1561
                long dlen;
 
1562
            } ntc;
 
1563
            long dlen = 0;
 
1564
 
 
1565
            qdosextra   *extra = (qdosextra *)ef_ptr;
 
1566
            jbextra     *jbp   = (jbextra   *)ef_ptr;
 
1567
 
 
1568
            if (!strncmp(extra->longid, LONGID, strlen(LONGID)))
 
1569
            {
 
1570
                if (eb_len != EXTRALEN)
 
1571
                    if (uO.qflag)
 
1572
                        Info(slide, 0x201, ((char *)slide,
 
1573
                          "warning:  invalid length in Qdos field for %s\n",
 
1574
                          FnFilter1(G.filename)));
 
1575
                    else
 
1576
                        Info(slide, 0x201, ((char *)slide,
 
1577
                          "warning:  invalid length in Qdos field"));
 
1578
 
 
1579
                if (extra->header.d_type)
 
1580
                {
 
1581
                    dlen = extra->header.d_datalen;
 
1582
                }
 
1583
            }
 
1584
 
 
1585
            if (!strncmp(jbp->longid, JBLONGID, strlen(JBLONGID)))
 
1586
            {
 
1587
                if (eb_len != JBEXTRALEN)
 
1588
                    if (uO.qflag)
 
1589
                        Info(slide, 0x201, ((char *)slide,
 
1590
                          "warning:  invalid length in QZ field for %s\n",
 
1591
                          FnFilter1(G.filename)));
 
1592
                    else
 
1593
                        Info(slide, 0x201, ((char *)slide,
 
1594
                          "warning:  invalid length in QZ field"));
 
1595
                if(jbp->header.d_type)
 
1596
                {
 
1597
                    dlen = jbp->header.d_datalen;
 
1598
                }
 
1599
            }
 
1600
 
 
1601
            if ((long)LG(dlen) > 0)
 
1602
            {
 
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")
 
1607
                {
 
1608
                    ntc.id = *(long *)"XTcc";
 
1609
                    ntc.dlen = dlen;
 
1610
                    fwrite (&ntc, 8, 1, G.outfile);
 
1611
                }
 
1612
                Info(slide, 0x201, ((char *)slide, "QData = %d", LG(dlen)));
 
1613
                fclose(G.outfile);
 
1614
            }
 
1615
            return;     /* finished, cancel further extra field scanning */
 
1616
          }
 
1617
 
 
1618
          default:
 
1619
            Trace((stderr,"qlfix: unknown extra field block, ID=%d\n",
 
1620
               eb_id));
 
1621
        }
 
1622
 
 
1623
        /* Skip this extra field block */
 
1624
        ef_ptr += (eb_len + EB_HEADSIZE);
 
1625
        ef_len -= (eb_len + EB_HEADSIZE);
 
1626
    }
 
1627
}
 
1628
#endif /* QLZIP */