home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / amiga / amigazip.c < prev    next >
C/C++ Source or Header  |  1997-08-19  |  14KB  |  503 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden, Igor Mandrichenko, Paul Kienitz and
  5.  John Bush.
  6.  Permission is granted to any individual or institution to use, copy, or
  7.  redistribute this software so long as all of the original files are included,
  8.  that it is not sold for profit, and that this copyright notice is retained.
  9.  
  10. */
  11.  
  12. #include "zip.h"
  13. #include "amiga/amiga.h"
  14.  
  15. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  16.  
  17. #define MATCH shmatch
  18. #define utime FileDate
  19.  
  20. #define PAD 0
  21. #define PATH_END '/'
  22. #define ONENAMELEN 30
  23.  
  24. /* Local globals (kinda like "military intelligence" or "broadcast quality") */
  25.  
  26. extern char *label;             /* still declared in fileio.c */
  27. local ulg label_time = 0;
  28. local ulg label_mode = 0;
  29. local time_t label_utim = 0;
  30.  
  31. /* Local functions */
  32. local char *readd OF((DIR *));
  33. local int wild_recurse OF((char *, char *));
  34.  
  35.  
  36. #define opend opendir
  37.  
  38. local char *readd(d)
  39. DIR *d;                 /* directory stream to read from */
  40. /* Return a pointer to the next name in the directory stream d, or NULL if
  41.    no more entries or an error occurs. */
  42. {
  43.   struct dirent *e = readdir(d);
  44.   return e == NULL ? (char *) NULL : e->d_name;
  45. }
  46.  
  47.  
  48. /* What we have here is a mostly-generic routine using opendir()/readd() and */
  49. /* isshexp()/MATCH() to find all the files matching a multi-part filespec  */
  50. /* using the portable pattern syntax.  It shouldn't take too much fiddling */
  51. /* to make it usable for any other platform that has directory hierarchies */
  52. /* but no shell-level pattern matching.  It works for patterns throughout  */
  53. /* the pathname, such as "foo:*.?/source/x*.[ch]".                         */
  54.  
  55. #define ONENAMELEN 30
  56. /* the length of one filename component on the Amiga */
  57.  
  58. /* whole is a pathname with wildcards, wildtail points somewhere in the  */
  59. /* middle of it.  All wildcards to be expanded must come AFTER wildtail. */
  60.  
  61. local int wild_recurse(whole, wildtail) char *whole; char *wildtail;
  62. {
  63.     DIR *dir;
  64.     char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2;
  65.     ush newlen, amatch = 0;
  66.     BPTR lok;
  67.     int e = ZE_MISS;
  68.  
  69.     if (!isshexp(wildtail))
  70.         if (lok = Lock(whole, ACCESS_READ)) {       /* p exists? */
  71.             UnLock(lok);
  72.             return procname(whole);
  73.         } else
  74.             return ZE_MISS;                     /* woops, no wildcards! */
  75.  
  76.     /* back up thru path components till existing dir found */
  77.     do {
  78.         name = wildtail + strlen(wildtail) - 1;
  79.         for (;;)
  80.             if (name-- <= wildtail || *name == PATH_END) {
  81.                 subwild = name + 1;
  82.                 plug2 = *subwild;
  83.                 *subwild = 0;
  84.                 break;
  85.             }
  86.         if (glue)
  87.             *glue = plug;
  88.         glue = subwild;
  89.         plug = plug2;
  90.         dir = opendir(whole);
  91.     } while (!dir && !disk_not_mounted && subwild > wildtail);
  92.     wildtail = subwild;                 /* skip past non-wild components */
  93.  
  94.     if (subwild = strchr(wildtail + 1, PATH_END)) {
  95.         /* this "+ 1" dodges the  ^^^ hole left by *glue == 0 */
  96.         *(subwild++) = 0;               /* wildtail = one component pattern */
  97.         newlen = strlen(whole) + strlen(subwild) + ONENAMELEN + 2;
  98.     } else
  99.         newlen = strlen(whole) + ONENAMELEN + 1;
  100.     if (!dir || !(newwhole = malloc(newlen))) {
  101.         if (glue)
  102.             *glue = plug;
  103.  
  104.         e = dir ? ZE_MEM : ZE_MISS;
  105.         goto ohforgetit;
  106.     }
  107.     strcpy(newwhole, whole);
  108.     newlen = strlen(newwhole);
  109.     if (glue)
  110.         *glue = plug;                           /* repair damage to whole */
  111.     if (!isshexp(wildtail)) {
  112.         e = ZE_MISS;                            /* non-wild name not found */
  113.         goto ohforgetit;
  114.     }
  115.  
  116.     while (name = readd(dir)) {
  117.         if (MATCH(wildtail, name)) {
  118.             strcpy(newwhole + newlen, name);
  119.             if (subwild) {
  120.                 name = newwhole + strlen(newwhole);
  121.                 *(name++) = PATH_END;
  122.                 strcpy(name, subwild);
  123.                 e = wild_recurse(newwhole, name);
  124.             } else
  125.                 e = procname(newwhole);
  126.             newwhole[newlen] = 0;
  127.             if (e == ZE_OK)
  128.                 amatch = 1;
  129.             else if (e != ZE_MISS)
  130.                 break;
  131.         }
  132.     }
  133.  
  134.   ohforgetit:
  135.     if (dir) closedir(dir);
  136.     if (subwild) *--subwild = PATH_END;
  137.     if (newwhole) free(newwhole);
  138.     if (e == ZE_MISS && amatch)
  139.         e = ZE_OK;
  140.     return e;
  141. }
  142.  
  143. int wild(p) char *p;
  144. {
  145.     char *use;
  146.  
  147.     /* special handling of stdin request */
  148.     if (strcmp(p, "-") == 0)    /* if compressing stdin */
  149.         return newname(p, 0);
  150.  
  151.     /* wild_recurse() can't handle colons in wildcard part: */
  152.     if (use = strchr(p, ':')) {
  153.         if (strchr(++use, ':'))
  154.             return ZE_PARMS;
  155.     } else
  156.         use = p;
  157.  
  158.     return wild_recurse(p, use);
  159. }
  160.  
  161.  
  162. int procname(n)
  163. char *n;                /* name to process */
  164. /* Process a name or sh expression to operate on (or exclude).  Return
  165.    an error code in the ZE_ class. */
  166. {
  167.   char *a;              /* path and name for recursion */
  168.   DIR *d;               /* directory stream from opendir() */
  169.   char *e;              /* pointer to name from readd() */
  170.   int m;                /* matched flag */
  171.   char *p;              /* path for recursion */
  172.   struct stat s;        /* result of stat() */
  173.   struct zlist far *z;  /* steps through zfiles list */
  174.  
  175.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  176.     return newname(n, 0);
  177.   else if (LSSTAT(n, &s))
  178.   {
  179.     /* Not a file or directory--search for shell expression in zip file */
  180.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  181.     m = 1;
  182.     for (z = zfiles; z != NULL; z = z->nxt) {
  183.       if (MATCH(p, z->iname))
  184.       {
  185.         z->mark = pcount ? filter(z->zname) : 1;
  186.         if (verbose)
  187.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  188.                z->mark ? "in" : "ex", z->name);
  189.         m = 0;
  190.       }
  191.     }
  192.     free((zvoid *)p);
  193.     return m ? ZE_MISS : ZE_OK;
  194.   }
  195.  
  196.   /* Live name--use if file, recurse if directory */
  197.   if ((s.st_mode & S_IFDIR) == 0)
  198.   {
  199.     /* add or remove name of file */
  200.     if ((m = newname(n, 0)) != ZE_OK)
  201.       return m;
  202.   } else {
  203.     /* Add trailing / to the directory name */
  204.     if ((p = malloc(strlen(n)+2)) == NULL)
  205.       return ZE_MEM;
  206.     strcpy(p, n);
  207.     a = p + strlen(p);
  208.     if (*p && a[-1] != '/' && a[-1] != ':')
  209.       strcpy(a, "/");
  210.     if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  211.       free((zvoid *)p);
  212.       return m;
  213.     }
  214.     /* recurse into directory */
  215.     if (recurse && (d = opendir(n)) != NULL)
  216.     {
  217.       while ((e = readd(d)) != NULL) {
  218.         if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  219.         {
  220.           closedir(d);
  221.           free((zvoid *)p);
  222.           return ZE_MEM;
  223.         }
  224.         strcat(strcpy(a, p), e);
  225.         if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  226.         {
  227.           if (m == ZE_MISS)
  228.             zipwarn("name not matched: ", a);
  229.           else
  230.             ziperr(m, a);
  231.         }
  232.         free((zvoid *)a);
  233.       }
  234.       closedir(d);
  235.     }
  236.     free((zvoid *)p);
  237.   } /* (s.st_mode & S_IFDIR) == 0) */
  238.   return ZE_OK;
  239. }
  240.  
  241. char *ex2in(x, isdir, pdosflag)
  242. char *x;                /* external file name */
  243. int isdir;              /* input: x is a directory */
  244. int *pdosflag;          /* output: force MSDOS file attributes? */
  245. /* Convert the external file name to a zip file name, returning the malloc'ed
  246.    string or NULL if not enough memory. */
  247. {
  248.   char *n;              /* internal file name (malloc'ed) */
  249.   char *t;              /* shortened name */
  250.   int dosflag;
  251.  
  252.   dosflag = dosify;     /* default for non-DOS and non-OS/2 */
  253.  
  254.   /* Find starting point in name before doing malloc */
  255.   if ((t = strrchr(x, ':')) != NULL)    /* reject ":" */
  256.     t++;
  257.   else
  258.     t = x;
  259.   {                                     /* reject "//" */
  260.     char *tt = t;
  261.     while (tt = strchr(tt, '/'))
  262.       while (*++tt == '/')
  263.         t = tt;
  264.   }
  265.   while (*t == '/')             /* reject leading "/" on what's left */
  266.     t++;
  267.  
  268.   if (!pathput)
  269.     t = last(t, PATH_END);
  270.  
  271.   /* Malloc space for internal name and copy it */
  272.   if ((n = malloc(strlen(t) + 1)) == NULL)
  273.     return NULL;
  274.   strcpy(n, t);
  275.  
  276.   if (dosify)
  277.     msname(n);
  278.   /* Returned malloc'ed name */
  279.   if (pdosflag)
  280.     *pdosflag = dosflag;
  281.   return n;
  282. }
  283.  
  284. char *in2ex(n)
  285. char *n;                /* internal file name */
  286. /* Convert the zip file name to an external file name, returning the malloc'ed
  287.    string or NULL if not enough memory. */
  288. {
  289.   char *x;              /* external file name */
  290.  
  291.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  292.       return NULL;
  293.   strcpy(x, n);
  294.   return x;
  295. }
  296.  
  297. void stamp(f, d)
  298. char *f;                /* name of file to change */
  299. ulg d;                  /* dos-style time to change it to */
  300. /* Set last updated and accessed time of file f to the DOS time d. */
  301. {
  302.   time_t u[2];          /* argument for utime() */
  303.  
  304.   /* Convert DOS time to time_t format in u */
  305.   u[0] = u[1] = dos2unixtime(d);
  306.  
  307.   /* Set updated and accessed times of f */
  308.   utime(f, u);
  309. }
  310.  
  311. ulg filetime(f, a, n, t)
  312. char *f;                /* name of file to get info on */
  313. ulg *a;                 /* return value: file attributes */
  314. long *n;                /* return value: file size */
  315. iztimes *t;             /* return value: access, modific. and creation times */
  316. /* If file *f does not exist, return 0.  Else, return the file's last
  317.    modified date and time as an MSDOS date and time.  The date and
  318.    time is returned in a long with the date most significant to allow
  319.    unsigned integer comparison of absolute times.  Also, if a is not
  320.    a NULL pointer, store the file attributes there, with the high two
  321.    bytes being the Unix attributes, and the low byte being a mapping
  322.    of that to DOS attributes.  If n is not NULL, store the file size
  323.    there.  If t is not NULL, the file's access, modification and creation
  324.    times are stored there as UNIX time_t values.
  325.    If f is "-", use standard input as the file. If f is a device, return
  326.    a file size of -1 */
  327. {
  328.   struct stat s;        /* results of stat() */
  329.   char name[FNMAX];
  330.   int len = strlen(f);
  331.  
  332.   if (f == label) {
  333.     if (a != NULL)
  334.       *a = label_mode;
  335.     if (n != NULL)
  336.       *n = -2L; /* convention for a label name */
  337.     if (t != NULL)
  338.       t->atime = t->mtime = t->ctime = label_utim;
  339.     return label_time;
  340.   }
  341.   strcpy(name, f);
  342.   if (name[len - 1] == '/')
  343.     name[len - 1] = '\0';
  344.   /* not all systems allow stat'ing a file with / appended */
  345.  
  346.   if (strcmp(f, "-") == 0) {
  347.     if (fstat(fileno(stdin), &s) != 0)
  348.       error("fstat(stdin)");
  349.   } else if (SSTAT(name, &s) != 0)
  350.              /* Accept about any file kind including directories
  351.               * (stored with trailing / with -r option)
  352.               */
  353.     return 0;
  354.  
  355.   if (a != NULL) {
  356.     *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  357.     if ((s.st_mode & S_IFDIR) != 0) {
  358.       *a |= MSDOS_DIR_ATTR;
  359.     }
  360.   }
  361.   if (n != NULL)
  362.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  363.   if (t != NULL) {
  364.     t->atime = s.st_atime;
  365.     t->mtime = s.st_mtime;
  366.     t->ctime = s.st_ctime;
  367.   }
  368.  
  369.    return unix2dostime(&s.st_mtime);
  370. }
  371.  
  372. int set_extra_field(z, z_utim)
  373.   struct zlist far *z;
  374.   iztimes *z_utim;
  375.   /* create extra field and change z->att if desired */
  376. {
  377. #ifdef USE_EF_UT_TIME
  378.   if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
  379.     return ZE_MEM;
  380.  
  381.   z->extra[0]  = 'U';
  382.   z->extra[1]  = 'T';
  383.   z->extra[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */
  384.   z->extra[3]  = 0;
  385.   z->extra[4]  = EB_UT_FL_MTIME;
  386.   z->extra[5]  = (char)(z_utim->mtime);
  387.   z->extra[6]  = (char)(z_utim->mtime >> 8);
  388.   z->extra[7]  = (char)(z_utim->mtime >> 16);
  389.   z->extra[8]  = (char)(z_utim->mtime >> 24);
  390.  
  391.   z->cextra = z->extra;
  392.   z->cext = z->ext  = (EB_HEADSIZE+EB_UT_LEN(1));
  393.  
  394.   return ZE_OK;
  395. #else /* !USE_EF_UT_TIME */
  396.   return (int)(z-z);
  397. #endif /* ?USE_EF_UT_TIME */
  398. }
  399.  
  400. int deletedir(d)
  401. char *d;                /* directory to delete */
  402. /* Delete the directory *d if it is empty, do nothing otherwise.
  403.    Return the result of rmdir(), delete(), or system().
  404.    For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]).
  405.  */
  406. {
  407.     return rmdir(d);
  408. }
  409.  
  410. #endif /* !UTIL */
  411.  
  412.  
  413. /******************************/
  414. /*  Function version_local()  */
  415. /******************************/
  416.  
  417.  
  418. /* NOTE:  the following include depends upon the environment
  419.  *        variable $Workbench to be set correctly.  (Set by
  420.  *        default, by kickstart during startup)
  421.  */
  422. int WBversion = (int)
  423. #include "ENV:Workbench"
  424. ;
  425.  
  426. void version_local()
  427. {
  428.    static ZCONST char CompiledWith[] = "Compiled with %s%s under %s%s%s%s.\n\n";
  429.  
  430. /* Define buffers. */
  431.  
  432.    char buf1[16];  /* compiler name */
  433.    char buf2[16];  /* revstamp */
  434.    char buf3[16];  /* OS */
  435.    char buf4[16];  /* Date */
  436. /*   char buf5[16];  /* Time */
  437.  
  438. /* format "with" name strings */
  439.  
  440. #ifdef AMIGA
  441. # ifdef __SASC
  442.    strcpy(buf1,"SAS/C ");
  443. # else
  444. #  ifdef LATTICE
  445.     strcpy(buf1,"Lattice C ");
  446. #  else
  447. #   ifdef AZTEC_C
  448.      strcpy(buf1,"Manx Aztec C ");
  449. #   else
  450.      strcpy(buf1,"UNKNOWN ");
  451. #   endif
  452. #  endif
  453. # endif
  454. /* "under" */
  455.   sprintf(buf3,"AmigaDOS v%d",WBversion);
  456. #else
  457.   strcpy(buf1,"Unknown compiler ");
  458.   strcpy(buf3,"Unknown OS");
  459. #endif
  460.  
  461. /* Define revision, date, and time strings.
  462.  * NOTE:  Do not calculate run time, be sure to use time compiled.
  463.  * Pass these strings via your makefile if undefined.
  464.  */
  465.  
  466. #if defined(__VERSION__) && defined(__REVISION__)
  467.   sprintf(buf2,"version %d.%d",__VERSION__,__REVISION__);
  468. #else
  469. # ifdef __VERSION__
  470.   sprintf(buf2,"version %d",__VERSION__);
  471. # else
  472.   sprintf(buf2,"unknown version");
  473. # endif
  474. #endif
  475.  
  476. #ifdef __DATE__
  477.   sprintf(buf4," on %s",__DATE__);
  478. #else
  479.   strcpy(buf4," unknown date");
  480. #endif
  481.  
  482. /******
  483. #ifdef __TIME__
  484.   sprintf(buf5," at %s",__TIME__);
  485. #else
  486.   strcpy(buf5," unknown time");
  487. #endif
  488. ******/
  489.  
  490. /* Print strings using "CompiledWith" mask defined above.
  491.  *  ("Compiled with %s%s under %s%s%s%s.")
  492.  */
  493.  
  494.    printf(CompiledWith,
  495.      buf1,
  496.      buf2,
  497.      buf3,
  498.      buf4,
  499.      /* buf5, */ "",
  500.      "" );  /* buf6 not used */
  501.  
  502. } /* end function version_local() */
  503.