home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / unix / unix.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  16KB  |  623 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. #include "zip.h"
  12.  
  13. #include <time.h>
  14.  
  15. #ifdef MINIX
  16. #  ifdef S_IWRITE
  17. #    undef S_IWRITE
  18. #  endif /* S_IWRITE */
  19. #  define S_IWRITE S_IWUSR
  20. #endif /* S_IWUSR */
  21.  
  22. #define MATCH shmatch
  23.  
  24. #if defined(HAVE_DIRENT_H) || defined(_POSIX_VERSION)
  25. #  include <dirent.h>
  26. #else /* !HAVE_DIRENT_H */
  27. #  ifdef HAVE_NDIR_H
  28. #    include <ndir.h>
  29. #  endif /* HAVE_NDIR_H */
  30. #  ifdef HAVE_SYS_NDIR_H
  31. #    include <sys/ndir.h>
  32. #  endif /* HAVE_SYS_NDIR_H */
  33. #  ifdef HAVE_SYS_DIR_H
  34. #    include <sys/dir.h>
  35. #  endif /* HAVE_SYS_DIR_H */
  36. #  ifndef dirent
  37. #    define dirent direct
  38. #  endif
  39. #endif /* HAVE_DIRENT_H || _POSIX_VERSION */
  40.  
  41. #define PAD 0
  42. #define PATH_END '/'
  43.  
  44. /* Library functions not in (most) header files */
  45.  
  46. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  47.  
  48. #ifdef _POSIX_VERSION
  49. #  include <utime.h>
  50. #else
  51.    int utime OF((char *, time_t *));
  52. #endif
  53.  
  54. extern char *label;
  55. local ulg label_time = 0;
  56. local ulg label_mode = 0;
  57. local time_t label_utim = 0;
  58.  
  59. /* Local functions */
  60. local char *readd OF((DIR *));
  61.  
  62.  
  63. #ifdef NO_DIR                    /* for AT&T 3B1 */
  64. #define direct dirent
  65. #include <sys/dir.h>
  66. typedef FILE DIR
  67. /*
  68. **  Apparently originally by Rich Salz.
  69. **  Cleaned up and modified by James W. Birdsall.
  70. */
  71.  
  72. #define opendir(path) fopen(path, "r")
  73.  
  74. struct dirent *readdir(dirp)
  75. DIR *dirp;
  76. {
  77.   static struct dirent entry;
  78.  
  79.   if (dirp == NULL)
  80.     return NULL;
  81.   for (;;)
  82.     if (fread (&entry, sizeof (struct dirent), 1, dirp) == 0)
  83.       return NULL;
  84.     else if (entry.d_ino)
  85.       return (&entry);
  86. } /* end of readdir() */
  87.  
  88. #define closedir(dirp) fclose(dirp)
  89. #endif /* NO_DIR */
  90.  
  91.  
  92. local char *readd(d)
  93. DIR *d;                 /* directory stream to read from */
  94. /* Return a pointer to the next name in the directory stream d, or NULL if
  95.    no more entries or an error occurs. */
  96. {
  97.   struct dirent *e;
  98.  
  99.   e = readdir(d);
  100.   return e == NULL ? (char *) NULL : e->d_name;
  101. }
  102.  
  103. int procname(n)
  104. char *n;                /* name to process */
  105. /* Process a name or sh expression to operate on (or exclude).  Return
  106.    an error code in the ZE_ class. */
  107. {
  108.   char *a;              /* path and name for recursion */
  109.   DIR *d;               /* directory stream from opendir() */
  110.   char *e;              /* pointer to name from readd() */
  111.   int m;                /* matched flag */
  112.   char *p;              /* path for recursion */
  113.   struct stat s;        /* result of stat() */
  114.   struct zlist far *z;  /* steps through zfiles list */
  115.  
  116.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  117.     return newname(n, 0);
  118.   else if (LSSTAT(n, &s))
  119.   {
  120.     /* Not a file or directory--search for shell expression in zip file */
  121.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  122.     m = 1;
  123.     for (z = zfiles; z != NULL; z = z->nxt) {
  124.       if (MATCH(p, z->zname))
  125.       {
  126.         z->mark = pcount ? filter(z->zname) : 1;
  127.         if (verbose)
  128.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  129.                z->mark ? "in" : "ex", z->name);
  130.         m = 0;
  131.       }
  132.     }
  133.     free((zvoid *)p);
  134.     return m ? ZE_MISS : ZE_OK;
  135.   }
  136.  
  137.   /* Live name--use if file, recurse if directory */
  138.   if ((s.st_mode & S_IFDIR) == 0)
  139.   {
  140.     /* add or remove name of file */
  141.     if ((m = newname(n, 0)) != ZE_OK)
  142.       return m;
  143.   } else {
  144.     /* Add trailing / to the directory name */
  145.     if ((p = malloc(strlen(n)+2)) == NULL)
  146.       return ZE_MEM;
  147.     if (strcmp(n, ".") == 0) {
  148.       *p = '\0';  /* avoid "./" prefix and do not create zip entry */
  149.     } else {
  150.       strcpy(p, n);
  151.       a = p + strlen(p);
  152.       if (a[-1] != '/')
  153.         strcpy(a, "/");
  154.       if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  155.         free((zvoid *)p);
  156.         return m;
  157.       }
  158.     }
  159.     /* recurse into directory */
  160.     if (recurse && (d = opendir(n)) != NULL)
  161.     {
  162.       while ((e = readd(d)) != NULL) {
  163.         if (strcmp(e, ".") && strcmp(e, ".."))
  164.         {
  165.           if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  166.           {
  167.             closedir(d);
  168.             free((zvoid *)p);
  169.             return ZE_MEM;
  170.           }
  171.           strcat(strcpy(a, p), e);
  172.           if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  173.           {
  174.             if (m == ZE_MISS)
  175.               zipwarn("name not matched: ", a);
  176.             else
  177.               ziperr(m, a);
  178.           }
  179.           free((zvoid *)a);
  180.         }
  181.       }
  182.       closedir(d);
  183.     }
  184.     free((zvoid *)p);
  185.   } /* (s.st_mode & S_IFDIR) == 0) */
  186.   return ZE_OK;
  187. }
  188.  
  189. char *ex2in(x, isdir, pdosflag)
  190. char *x;                /* external file name */
  191. int isdir;              /* input: x is a directory */
  192. int *pdosflag;          /* output: force MSDOS file attributes? */
  193. /* Convert the external file name to a zip file name, returning the malloc'ed
  194.    string or NULL if not enough memory. */
  195. {
  196.   char *n;              /* internal file name (malloc'ed) */
  197.   char *t;              /* shortened name */
  198.   int dosflag;
  199.  
  200.   dosflag = dosify;  /* default for non-DOS and non-OS/2 */
  201.  
  202.   /* Find starting point in name before doing malloc */
  203.   for (t = x; *t == '/'; t++)
  204.     ;
  205.  
  206.   /* Make changes, if any, to the copied name (leave original intact) */
  207.   if (!pathput)
  208.     t = last(t, PATH_END);
  209.  
  210.   /* Malloc space for internal name and copy it */
  211.   if ((n = malloc(strlen(t) + 1)) == NULL)
  212.     return NULL;
  213.   strcpy(n, t);
  214.  
  215.   if (isdir == 42) return n;      /* avoid warning on unused variable */
  216.  
  217.   if (dosify)
  218.     msname(n);
  219.  
  220.   /* Returned malloc'ed name */
  221.   if (pdosflag)
  222.     *pdosflag = dosflag;
  223.   return n;
  224. }
  225.  
  226.  
  227. char *in2ex(n)
  228. char *n;                /* internal file name */
  229. /* Convert the zip file name to an external file name, returning the malloc'ed
  230.    string or NULL if not enough memory. */
  231. {
  232.   char *x;              /* external file name */
  233.  
  234.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  235.     return NULL;
  236.   strcpy(x, n);
  237.   return x;
  238. }
  239.  
  240. /*
  241.  * use ztimbuf in both POSIX and non POSIX cases ?
  242.  */
  243. void stamp(f, d)
  244. char *f;                /* name of file to change */
  245. ulg d;                  /* dos-style time to change it to */
  246. /* Set last updated and accessed time of file f to the DOS time d. */
  247. {
  248. #ifdef _POSIX_VERSION
  249.   struct utimbuf u;     /* argument for utime()  const ?? */
  250. #else
  251.   time_t u[2];          /* argument for utime() */
  252. #endif
  253.  
  254.   /* Convert DOS time to time_t format in u */
  255. #ifdef _POSIX_VERSION
  256.   u.actime = u.modtime = dos2unixtime(d);
  257.   utime(f, &u);
  258. #else
  259.   u[0] = u[1] = dos2unixtime(d);
  260.   utime(f, u);
  261. #endif
  262.  
  263. }
  264.  
  265. ulg filetime(f, a, n, t)
  266. char *f;                /* name of file to get info on */
  267. ulg *a;                 /* return value: file attributes */
  268. long *n;                /* return value: file size */
  269. ztimbuf *t;             /* return value: access and modification time */
  270. /* If file *f does not exist, return 0.  Else, return the file's last
  271.    modified date and time as an MSDOS date and time.  The date and
  272.    time is returned in a long with the date most significant to allow
  273.    unsigned integer comparison of absolute times.  Also, if a is not
  274.    a NULL pointer, store the file attributes there, with the high two
  275.    bytes being the Unix attributes, and the low byte being a mapping
  276.    of that to DOS attributes.  If n is not NULL, store the file size
  277.    there.  If t is not NULL, the file's access and modification time
  278.    are stored there as UNIX time_t values.
  279.    If f is "-", use standard input as the file. If f is a device, return
  280.    a file size of -1 */
  281. {
  282.   struct stat s;        /* results of stat() */
  283.   char name[FNMAX];
  284.   int len = strlen(f);
  285.  
  286.   if (f == label) {
  287.     if (a != NULL)
  288.       *a = label_mode;
  289.     if (n != NULL)
  290.       *n = -2L; /* convention for a label name */
  291.     if (t != NULL)
  292.       t->actime = t->modtime = label_utim;
  293.     return label_time;
  294.   }
  295.   strcpy(name, f);
  296.   if (name[len - 1] == '/')
  297.     name[len - 1] = '\0';
  298.   /* not all systems allow stat'ing a file with / appended */
  299.   if (strcmp(f, "-") == 0) {
  300.     if (fstat(fileno(stdin), &s) != 0)
  301.       error("fstat(stdin)");
  302.   } else if (LSSTAT(name, &s) != 0)
  303.              /* Accept about any file kind including directories
  304.               * (stored with trailing / with -r option)
  305.               */
  306.     return 0;
  307.  
  308.   if (a != NULL) {
  309.     *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  310.     if ((s.st_mode & S_IFMT) == S_IFDIR) {
  311.       *a |= MSDOS_DIR_ATTR;
  312.     }
  313.   }
  314.   if (n != NULL)
  315.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  316.   if (t != NULL) {
  317.     t->actime = s.st_atime;
  318.     t->modtime = s.st_mtime;
  319.   }
  320.  
  321.   return unix2dostime(&s.st_mtime);
  322. }
  323.  
  324. int set_extra_field(z, z_utim)
  325.   struct zlist far *z;
  326.   ztimbuf *z_utim;
  327.   /* store full data in local header but just time stamp info in central */
  328. {
  329.   struct stat s;
  330.  
  331.   /* For the full sized UX local field including the UID/GID fields, we
  332.    * have to stat the file, again.  */
  333.   if (stat(z->name, &s))
  334.     return ZE_OPEN;
  335.  
  336. #define EF_L_UNIX_LEN (EB_HEADSIZE + EB_UX_FULLSIZE)
  337. #define EF_C_UNIX_LEN (EB_HEADSIZE + EB_UX_MINLEN)
  338.   if ((z->extra = (char *)malloc(EF_L_UNIX_LEN)) == NULL)
  339.     return ZE_MEM;
  340.   if ((z->cextra = (char *)malloc(EF_C_UNIX_LEN)) == NULL)
  341.     return ZE_MEM;
  342.  
  343.   z->extra[0]  = 'U';
  344.   z->extra[1]  = 'X';
  345.   z->extra[2]  = EB_UX_FULLSIZE;        /* length of data part of local e.f. */
  346.   z->extra[3]  = 0;
  347.   z->extra[4]  = (char)(s.st_atime);
  348.   z->extra[5]  = (char)(s.st_atime >> 8);
  349.   z->extra[6]  = (char)(s.st_atime >> 16);
  350.   z->extra[7]  = (char)(s.st_atime >> 24);
  351.   z->extra[8]  = (char)(s.st_mtime);
  352.   z->extra[9]  = (char)(s.st_mtime >> 8);
  353.   z->extra[10] = (char)(s.st_mtime >> 16);
  354.   z->extra[11] = (char)(s.st_mtime >> 24);
  355.   z->extra[12] = (char)(s.st_uid);
  356.   z->extra[13] = (char)(s.st_uid >> 8);
  357.   z->extra[14] = (char)(s.st_gid);
  358.   z->extra[15] = (char)(s.st_gid >> 8);
  359.   z->ext = EF_L_UNIX_LEN;
  360.  
  361.   memcpy(z->cextra, z->extra, EF_C_UNIX_LEN);
  362.   z->cextra[2] = EB_UX_MINLEN;
  363.   z->cext = EF_C_UNIX_LEN;
  364.  
  365.   /* lower-middle external-attribute byte (unused until now):
  366.    *   high bit        => (have GMT mod/acc times) >>> NO LONGER USED! <<<
  367.    *   second-high bit => have Unix UID/GID info
  368.    * NOTE: The high bit was NEVER used in any official Info-Zip release,
  369.    *       but its future use should be avoided (if possible), since it
  370.    *       was used as "GMT mod/acc times local extra field" flags in Zip beta
  371.    *       versions 2.0j up to 2.0v, for about 1.5 years.
  372.    */
  373.   z->atx |= 0x4000;
  374.   return ZE_OK;
  375. }
  376.  
  377. int deletedir(d)
  378. char *d;                /* directory to delete */
  379. /* Delete the directory *d if it is empty, do nothing otherwise.
  380.    Return the result of rmdir(), delete(), or system().
  381.    For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]).
  382.  */
  383. {
  384. # ifdef NO_RMDIR
  385.     /* code from Greg Roelofs, who horked it from Mark Edwards (unzip) */
  386.     int r, len;
  387.     char *s;              /* malloc'd string for system command */
  388.  
  389.     len = strlen(d);
  390.     if ((s = malloc(len + 34)) == NULL)
  391.       return 127;
  392.  
  393.     sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);
  394.     r = system(s);
  395.     free(s);
  396.     return r;
  397. # else /* !NO_RMDIR */
  398.     return rmdir(d);
  399. # endif /* ?NO_RMDIR */
  400. }
  401.  
  402. /******************************/
  403. /*  Function version_local()  */
  404. /******************************/
  405.  
  406. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__386BSD__) || \
  407.     defined(__bsdi__)
  408. #include <sys/param.h> /* for the BSD4_4 define */
  409. #endif
  410.  
  411. void version_local()
  412. {
  413.     static const char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  414. #if defined(CRAY) || defined(NX_CURRENT_COMPILER_RELEASE) || defined(NetBSD)
  415.     char buf1[40];
  416. #if defined(CRAY) || defined(NX_CURRENT_COMPILER_RELEASE)
  417.     char buf2[40];
  418. #endif
  419. #endif
  420.  
  421.     /* Pyramid, NeXT have problems with huge macro expansion, too:  no Info() */
  422.     printf(CompiledWith,
  423.  
  424. #ifdef __GNUC__
  425. #  ifdef NX_CURRENT_COMPILER_RELEASE
  426.       (sprintf(buf1, "NeXT DevKit %d.%02d ", NX_CURRENT_COMPILER_RELEASE/100,
  427.         NX_CURRENT_COMPILER_RELEASE%100), buf1),
  428.       (strlen(__VERSION__) > 8)? "(gcc)" :
  429.         (sprintf(buf2, "(gcc %s)", __VERSION__), buf2),
  430. #  else
  431.       "gcc ", __VERSION__,
  432. #  endif
  433. #else
  434. #  if defined(CRAY) && defined(_RELEASE)
  435.       "cc ", (sprintf(buf1, "version %d", _RELEASE), buf1),
  436. #  else
  437. #  ifdef __VERSION__
  438.       "cc ", __VERSION__,
  439. #  else
  440.       "cc", "",
  441. #  endif
  442. #  endif
  443. #endif
  444.  
  445.       "Unix",
  446.  
  447. #if defined(sgi) || defined(__sgi)
  448.       " (Silicon Graphics IRIX)",
  449. #else
  450. #ifdef sun
  451. #  ifdef sparc
  452. #    ifdef __SVR4
  453.       " (Sun Sparc/Solaris)",
  454. #    else /* may or may not be SunOS */
  455.       " (Sun Sparc)",
  456. #    endif
  457. #  else
  458. #  if defined(sun386) || defined(i386)
  459.       " (Sun 386i)",
  460. #  else
  461. #  if defined(mc68020) || defined(__mc68020__)
  462.       " (Sun 3)",
  463. #  else /* mc68010 or mc68000:  Sun 2 or earlier */
  464.       " (Sun 2)",
  465. #  endif
  466. #  endif
  467. #  endif
  468. #else
  469. #ifdef __hpux
  470.       " (HP/UX)",
  471. #else
  472. #ifdef __osf__
  473.       " (DEC OSF/1)",
  474. #else
  475. #ifdef _AIX
  476.       " (IBM AIX)",
  477. #else
  478. #ifdef aiws
  479.       " (IBM RT/AIX)",
  480. #else
  481. #if defined(CRAY) || defined(cray)
  482. #  ifdef _UNICOS
  483.       (sprintf(buf2, " (Cray UNICOS release %d)", _UNICOS), buf2),
  484. #  else
  485.       " (Cray UNICOS)",
  486. #  endif
  487. #else
  488. #if defined(uts) || defined(UTS)
  489.       " (Amdahl UTS)",
  490. #else
  491. #ifdef NeXT
  492. #  ifdef mc68000
  493.       " (NeXTStep/black)",
  494. #  else
  495.       " (NeXTStep for Intel)",
  496. #  endif
  497. #else              /* the next dozen or so are somewhat order-dependent */
  498. #if defined(linux) || defined(__linux__)
  499. #  ifdef __ELF__
  500.       " (Linux ELF)",
  501. #  else
  502.       " (Linux a.out)",
  503. #  endif
  504. #else
  505. #ifdef MINIX
  506.       " (Minix)",
  507. #else
  508. #ifdef M_UNIX
  509.       " (SCO Unix)",
  510. #else
  511. #ifdef M_XENIX
  512.       " (SCO Xenix)",
  513. #else
  514. #ifdef __NetBSD__
  515. #  ifdef NetBSD0_8
  516. #    if NetBSD0_8 == 1
  517.         " (NetBSD 0.8)",
  518. #    else
  519.           (sprintf(buf1, " (NetBSD 0.8%c)", (char) (NetBSD0_8 + '@')), buf1),
  520. #    endif
  521. #  else
  522. #  ifdef NetBSD0_9
  523. #    if NetBSD0_9 == 1
  524.         " (NetBSD 0.9)",
  525. #    else
  526.           (sprintf(buf1, " (NetBSD 0.9%c)", (char)(NetBSD0_9 + 'A' - 2)), buf1),
  527. #    endif
  528. #  else
  529. #  ifdef NetBSD1_0
  530. #    if NetBSD1_0 == 1
  531.         " (NetBSD 1.0)",
  532. #    else
  533.           (sprintf(buf1, " (NetBSD 1.0%c)", (char)(NetBSD1_0 + 'A' - 2)), buf1),
  534. #    endif
  535. #  else
  536. #  ifdef NetBSD1_1
  537. #    if NetBSD1_1 == 1
  538.         " (NetBSD 1.1)",
  539. #    else
  540.           (sprintf(buf1, " (NetBSD 1.1%c)", (char)(NetBSD1_1 + 'A' - 2)), buf1),
  541. #    endif
  542. #  else
  543.       (BSD4_4 == 0.5)? " (NetBSD before 0.9)" : " (NetBSD 1.2 or later)",
  544. #  endif
  545. #  endif
  546. #  endif
  547. #  endif
  548. #else
  549. #ifdef __FreeBSD__
  550.       (BSD4_4 == 0.5)? " (FreeBSD 1.x)" : " (FreeBSD 2.0 or later)",
  551. #else
  552. #ifdef __bsdi__
  553.       (BSD4_4 == 0.5)? " (BSD/386 1.0)" : " (BSD/386 1.1 or later)",
  554. #else
  555. #ifdef __386BSD__
  556.       (BSD4_4 == 1)? " (386BSD, post-4.4 release)" : " (386BSD)",
  557. #else
  558. #if defined(i486) || defined(__i486) || defined(__i486__)
  559.       " (Intel 486)",
  560. #else
  561. #if defined(i386) || defined(__i386) || defined(__i386__)
  562.       " (Intel 386)",
  563. #else
  564. #ifdef pyr
  565.       " (Pyramid)",
  566. #else
  567. #if defined(ultrix) || defined(__ultrix)
  568. #  if defined(mips) || defined(__mips)
  569.       " (DEC/MIPS)",
  570. #  else
  571. #  if defined(vax) || defined(__vax)
  572.       " (DEC/VAX)",
  573. #  else /* __alpha? */
  574.       " (DEC/Alpha)",
  575. #  endif
  576. #  endif
  577. #else
  578. #ifdef gould
  579.       " (Gould)",
  580. #else
  581. #ifdef MTS
  582.       " (MTS)",
  583. #else
  584. #ifdef __convexc__
  585.       " (Convex)",
  586. #else
  587.       "",
  588. #endif /* Convex */
  589. #endif /* MTS */
  590. #endif /* Gould */
  591. #endif /* DEC */
  592. #endif /* Pyramid */
  593. #endif /* 386 */
  594. #endif /* 486 */
  595. #endif /* 386BSD */
  596. #endif /* BSDI BSD/386 */
  597. #endif /* NetBSD */
  598. #endif /* FreeBSD */
  599. #endif /* SCO Xenix */
  600. #endif /* SCO Unix */
  601. #endif /* Minix */
  602. #endif /* Linux */
  603. #endif /* NeXT */
  604. #endif /* Amdahl */
  605. #endif /* Cray */
  606. #endif /* RT/AIX */
  607. #endif /* AIX */
  608. #endif /* OSF/1 */
  609. #endif /* HP/UX */
  610. #endif /* Sun */
  611. #endif /* SGI */
  612.  
  613. #ifdef __DATE__
  614.       " on ", __DATE__
  615. #else
  616.       "", ""
  617. #endif
  618.     );
  619.  
  620. } /* end function version_local() */
  621.  
  622. #endif /* !UTIL */
  623.