home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / acorn / acornzip.c next >
C/C++ Source or Header  |  1996-04-05  |  15KB  |  557 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden, Sergio Monesi, Karl Davis and
  5.  Igor Mandrichenko.
  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 <stdlib.h>
  13. #include <string.h>
  14. #include "zip.h"
  15.  
  16. #define MATCH shmatch
  17. #define opend opendir
  18. #define closed closedir
  19. #define PAD 0
  20. #define PATH_END '/'
  21.  
  22.  
  23. #ifndef UTIL
  24.  
  25. extern char *label;
  26. local ulg label_time = 0;
  27. local ulg label_mode = 0;
  28. local time_t label_utim = 0;
  29.  
  30. char *readd(DIR *d)
  31. /* Return a pointer to the next name in the directory stream d, or NULL if
  32.    no more entries or an error occurs. */
  33. {
  34.    struct dirent *e;
  35.  
  36.    e = readdir(d);
  37.    return (e == NULL ? (char *) NULL : e->d_name);
  38. }
  39.  
  40. /* What we have here is a mostly-generic routine using opend()/readd() and */
  41. /* isshexp()/MATCH() to find all the files matching a multi-part filespec  */
  42. /* using the portable pattern syntax.  It shouldn't take too much fiddling */
  43. /* to make it usable for any other platform that has directory hierarchies */
  44. /* but no shell-level pattern matching.  It works for patterns throughout  */
  45. /* the pathname, such as "foo:*.?/source/x*.[ch]".                         */
  46.  
  47. /* whole is a pathname with wildcards, wildtail points somewhere in the  */
  48. /* middle of it.  All wildcards to be expanded must come AFTER wildtail. */
  49.  
  50. local int wild_recurse(whole, wildtail) char *whole; char *wildtail;
  51. {
  52.     dstrm *dir;
  53.     char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2;
  54.     ush newlen, amatch = 0;
  55.     struct stat statb;
  56.     int disk_not_mounted=0;
  57.     int e = ZE_MISS;
  58.  
  59.     if (!isshexp(wildtail)) {
  60.         if (stat(whole,&statb)==0 && (statb.st_mode & S_IREAD)!=0) {
  61.             return procname(whole);
  62.         } else
  63.             return ZE_MISS;                     /* woops, no wildcards! */
  64.     }
  65.  
  66.     /* back up thru path components till existing dir found */
  67.     do {
  68.         name = wildtail + strlen(wildtail) - 1;
  69.         for (;;)
  70.             if (name-- <= wildtail || *name == '.') {
  71.                 subwild = name + 1;
  72.                 plug2 = *subwild;
  73.                 *subwild = 0;
  74.                 break;
  75.             }
  76.         if (glue)
  77.             *glue = plug;
  78.         glue = subwild;
  79.         plug = plug2;
  80.         dir = opend(whole);
  81.     } while (!dir && !disk_not_mounted && subwild > wildtail);
  82.     wildtail = subwild;                 /* skip past non-wild components */
  83.  
  84.     if (subwild = strchr(wildtail + 1, '.')) {
  85.         /* this "+ 1" dodges the  ^^^ hole left by *glue == 0 */
  86.         *(subwild++) = 0;               /* wildtail = one component pattern */
  87.         newlen = strlen(whole) + strlen(subwild) + 32;
  88.     } else
  89.         newlen = strlen(whole) + 31;
  90.     if (!dir || !(newwhole = malloc(newlen))) {
  91.         if (glue)
  92.             *glue = plug;
  93.         e = dir ? ZE_MEM : ZE_MISS;
  94.         goto ohforgetit;
  95.     }
  96.     strcpy(newwhole, whole);
  97.     newlen = strlen(newwhole);
  98.     if (glue)
  99.         *glue = plug;                           /* repair damage to whole */
  100.     if (!isshexp(wildtail)) {
  101.         e = ZE_MISS;                            /* non-wild name not found */
  102.         goto ohforgetit;
  103.     }
  104.  
  105.     while (name = readd(dir)) {
  106.         if (MATCH(wildtail, name)) {
  107.             strcpy(newwhole + newlen, name);
  108.             if (subwild) {
  109.                 name = newwhole + strlen(newwhole);
  110.                 *(name++) = '.';
  111.                 strcpy(name, subwild);
  112.                 e = wild_recurse(newwhole, name);
  113.             } else
  114.                 e = procname(newwhole);
  115.             newwhole[newlen] = 0;
  116.             if (e == ZE_OK)
  117.                 amatch = 1;
  118.             else if (e != ZE_MISS)
  119.                 break;
  120.         }
  121.     }
  122.  
  123.   ohforgetit:
  124.     if (dir) closed(dir);
  125.     if (subwild) *--subwild = '.';
  126.     if (newwhole) free(newwhole);
  127.     if (e == ZE_MISS && amatch)
  128.         e = ZE_OK;
  129.     return e;
  130. }
  131.  
  132. int wild(p)
  133. char *p;
  134. {
  135.   char *path;
  136.   int toret;
  137.  
  138.   /* special handling of stdin request */
  139.   if (strcmp(p, "-") == 0)   /* if compressing stdin */
  140.     return newname(p, 0);
  141.  
  142.   path=p;
  143.   if (strchr(p, ':')==NULL && *p!='@') {
  144.     if (!(path=malloc(strlen(p)+3))) {
  145.       return ZE_MEM;
  146.     }
  147.     strcpy(path,"@.");
  148.     strcat(path,p);
  149.   }
  150.  
  151.   toret=wild_recurse(path, path);
  152.  
  153.   if (path!=p) {
  154.     free(path);
  155.   }
  156.   return toret;
  157. }
  158.  
  159. int procname(n)
  160. char *n;                /* name to process */
  161. /* Process a name or sh expression to operate on (or exclude).  Return
  162.    an error code in the ZE_ class. */
  163. {
  164.   char *a;              /* path and name for recursion */
  165.   DIR *d;               /* directory stream from opendir() */
  166.   char *e;              /* pointer to name from readd() */
  167.   int m;                /* matched flag */
  168.   char *p;              /* path for recursion */
  169.   struct stat s;        /* result of stat() */
  170.   struct zlist far *z;  /* steps through zfiles list */
  171.  
  172.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  173.     return newname(n, 0);
  174.   else if (LSSTAT(n, &s))
  175.   {
  176.     /* Not a file or directory--search for shell expression in zip file */
  177.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  178.     m = 1;
  179.     for (z = zfiles; z != NULL; z = z->nxt) {
  180.       if (MATCH(p, z->zname))
  181.       {
  182.         z->mark = pcount ? filter(z->zname) : 1;
  183.         if (verbose)
  184.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  185.                z->mark ? "in" : "ex", z->name);
  186.         m = 0;
  187.       }
  188.     }
  189.     free((zvoid *)p);
  190.     return m ? ZE_MISS : ZE_OK;
  191.   }
  192.  
  193.   /* Live name--use if file, recurse if directory */
  194.   if ((s.st_mode & S_IFDIR) == 0)
  195.   {
  196.     /* add or remove name of file */
  197.     if ((m = newname(n, 0)) != ZE_OK)
  198.       return m;
  199.   } else {
  200.     /* Add trailing / to the directory name */
  201.     if ((p = malloc(strlen(n)+2)) == NULL)
  202.       return ZE_MEM;
  203.     if (strcmp(n, ".") == 0) {
  204.       *p = '\0';  /* avoid "./" prefix and do not create zip entry */
  205.     } else {
  206.       strcpy(p, n);
  207.       a = p + strlen(p);
  208.       if (a[-1] != '.')
  209.         strcpy(a, ".");
  210.       if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  211.         free((zvoid *)p);
  212.         return m;
  213.       }
  214.     }
  215.     /* recurse into directory */
  216.     if (recurse && (d = opendir(n)) != NULL)
  217.     {
  218.       while ((e = readd(d)) != NULL) {
  219.         if (strcmp(e, ".") && strcmp(e, ".."))
  220.         {
  221.           if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  222.           {
  223.             closedir(d);
  224.             free((zvoid *)p);
  225.             return ZE_MEM;
  226.           }
  227.           strcat(strcpy(a, p), e);
  228.           if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  229.           {
  230.             if (m == ZE_MISS)
  231.               zipwarn("name not matched: ", a);
  232.             else
  233.               ziperr(m, a);
  234.           }
  235.           free((zvoid *)a);
  236.         }
  237.       }
  238.       closedir(d);
  239.     }
  240.     free((zvoid *)p);
  241.   } /* (s.st_mode & S_IFDIR) == 0) */
  242.   return ZE_OK;
  243. }
  244.  
  245. char *ex2in(x, isdir, pdosflag)
  246. char *x;                /* external file name */
  247. int isdir;              /* input: x is a directory */
  248. int *pdosflag;          /* output: force MSDOS file attributes? */
  249. /* Convert the external file name to a zip file name, returning the malloc'ed
  250.    string or NULL if not enough memory. */
  251. {
  252.   char *n;              /* internal file name (malloc'ed) */
  253.   char *t;              /* shortened name */
  254.   char *tmp;
  255.   int dosflag;
  256.   char *lastlastdir=NULL;   /* pointer to 2 dirs before... */
  257.   char *lastdir=NULL;       /* pointer to last dir... */
  258.  
  259.   /* Malloc space for internal name and copy it */
  260.   if ((tmp = malloc(strlen(x) + 1)) == NULL)
  261.     return NULL;
  262.   strcpy(tmp, x);
  263.  
  264.   dosflag = dosify; /* default for non-DOS and non-OS/2 */
  265.  
  266.   /* Find starting point in name before doing malloc */
  267.   for(t=tmp;*t;t++) {
  268.     if (*t=='/') {
  269.       *t='.';
  270.     }
  271.     else if (*t=='.') {
  272.       *t='/';
  273.       lastlastdir=lastdir;
  274.       lastdir=t+1;
  275.     }
  276.   }
  277.  
  278.   t=strchr(tmp,'$');      /* skip FS name */
  279.   if (t!=NULL)
  280.     t+=2;                 /* skip '.' after '$' */
  281.   else
  282.     t=tmp;
  283.   if (*t=='@')            /* skip @. at the beginning of filenames */
  284.     t+=2;
  285.  
  286.   /* Make changes, if any, to the copied name (leave original intact) */
  287.  
  288.   /* return a pointer to '\0' if the file is a directory with the same
  289.      same name as an extension to swap (eg. 'c', 'h', etc.) */
  290.   if (isdir && exts2swap!=NULL) {
  291.     if (lastlastdir==NULL)
  292.       lastlastdir=t;
  293.     if (checkext(lastlastdir)) {
  294.       free((void *)tmp);
  295.       n=malloc(1);
  296.       if (n!=NULL)
  297.         *n='\0';
  298.       return n;
  299.     }
  300.   }
  301.  
  302.   if (exts2swap!=NULL && lastdir!=NULL) {
  303.     if (lastlastdir==NULL)
  304.       lastlastdir=t;
  305.     if (checkext(lastlastdir)) {
  306.       swapext(lastlastdir,lastdir-1);
  307.     }
  308.   }
  309.  
  310.   if (!pathput)
  311.     t = last(t, PATH_END);
  312.  
  313.   /* Malloc space for internal name and copy it */
  314.   if ((n = malloc(strlen(t) + 1)) == NULL) {
  315.     free((void *)tmp);
  316.     return NULL;
  317.   }
  318.   strcpy(n, t);
  319.  
  320.   free((void *)tmp);
  321.  
  322.   if (dosify)
  323.     msname(n);
  324.  
  325.   /* Returned malloc'ed name */
  326.   if (pdosflag)
  327.     *pdosflag = dosflag;
  328.   return n;
  329. }
  330.  
  331. char *in2ex(n)
  332. char *n;                /* internal file name */
  333. /* Convert the zip file name to an external file name, returning the malloc'ed
  334.    string or NULL if not enough memory. */
  335. {
  336.   char *x;              /* external file name */
  337.   char *t;              /* scans name */
  338.   char *lastext=NULL;   /* pointer to last extension */
  339.   char *lastdir=NULL;   /* pointer to last dir */
  340.  
  341.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  342.     return NULL;
  343.   strcpy(x, n);
  344.  
  345.   for(t=x;*t;t++) {
  346.     if (*t=='.') {
  347.       *t='/';
  348.       lastext=t+1;
  349.     }
  350.     else if (*t=='/') {
  351.       *t='.';
  352.       lastdir=t+1;
  353.     }
  354.   }
  355.  
  356.   if (exts2swap!=NULL && (int)lastext>(int)lastdir) {
  357.     if (lastdir==NULL)
  358.       lastdir=x;
  359.     if (checkext(lastext)) {
  360.       swapext(lastdir,lastext-1);
  361.     }
  362.   }
  363.  
  364.   return x;
  365. }
  366.  
  367. void stamp(f, d)
  368. char *f;                /* name of file to change */
  369. ulg d;                  /* dos-style time to change it to */
  370. /* Set last updated and accessed time of file f to the DOS time d. */
  371. {
  372. #if 0
  373.   zipwarn("timestamp not implemented yet", "");
  374. #else
  375.   time_t m_time;
  376.   int loadaddr,execaddr,attr;
  377.  
  378.   /* Convert DOS time to time_t format in m_time */
  379.   m_time = dos2unixtime(d);
  380.  
  381.   /* set the file's access and modification times */
  382.   SWI_OS_File_5(f,NULL,&loadaddr,NULL,NULL,&attr);
  383.  
  384.   loadaddr=0xfff00000U | ((((m_time>>8) * 100)>>24) + 0x33);
  385.   execaddr=m_time * 100 + 0x6e996a00U;
  386.  
  387.   SWI_OS_File_1(f,loadaddr,execaddr,attr);
  388. #endif /* ?never */
  389. }
  390.  
  391. ulg filetime(f, a, n, t)
  392. char *f;                /* name of file to get info on */
  393. ulg *a;                 /* return value: file attributes */
  394. long *n;                /* return value: file size */
  395. ztimbuf *t;             /* return value: access and modification time */
  396. /* If file *f does not exist, return 0.  Else, return the file's last
  397.    modified date and time as an MSDOS date and time.  The date and
  398.    time is returned in a long with the date most significant to allow
  399.    unsigned integer comparison of absolute times.  Also, if a is not
  400.    a NULL pointer, store the file attributes there, with the high two
  401.    bytes being the Unix attributes, and the low byte being a mapping
  402.    of that to DOS attributes.  If n is not NULL, store the file size
  403.    there.  If t is not NULL, the file's access and modification time
  404.    are stored there as UNIX time_t values.
  405.    If f is "-", use standard input as the file. If f is a device, return
  406.    a file size of -1 */
  407. {
  408.   struct stat s;        /* results of stat() */
  409.   char name[FNMAX];
  410.   int len = strlen(f);
  411.  
  412.   if (f == label) {
  413.     if (a != NULL)
  414.       *a = label_mode;
  415.     if (n != NULL)
  416.       *n = -2L; /* convention for a label name */
  417.     if (t != NULL)
  418.       t->actime = t->modtime = label_utim;
  419.     return label_time;
  420.   }
  421.   strcpy(name, f);
  422.   if (name[len - 1] == '.')
  423.     name[len - 1] = '\0';
  424.   /* not all systems allow stat'ing a file with / appended */
  425.  
  426.   if (strcmp(f, "-") == 0) {
  427.   /* forge stat values for stdin since Amiga and RISCOS have no fstat() */
  428.     s.st_mode = (S_IREAD|S_IWRITE|S_IFREG);
  429.     s.st_size = -1;
  430.     s.st_mtime = time(&s.st_mtime);
  431.   } else if (LSSTAT(name, &s) != 0)
  432.              /* Accept about any file kind including directories
  433.               * (stored with trailing / with -r option)
  434.               */
  435.     return 0;
  436.  
  437.   if (a != NULL) {
  438.     *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  439.     if ((s.st_mode & S_IFDIR) != 0) {
  440.       *a |= MSDOS_DIR_ATTR;
  441.     }
  442.   }
  443.   if (n != NULL)
  444.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  445.   if (t != NULL) {
  446.     t->actime = s.st_atime;
  447.     t->modtime = s.st_mtime;
  448.   }
  449.  
  450.   return unix2dostime((time_t *) &s.st_mtime);
  451. }
  452.  
  453. int set_extra_field(z, z_utim)
  454.         struct zlist far *z;
  455.         ztimbuf *z_utim;
  456. {
  457. #ifdef USE_EF_UX_TIME
  458.   char *eb_ptr;
  459. #endif /* USE_EF_UX_TIME */
  460.   char *name;
  461.   extra_block *block;
  462.  
  463. #define EB_SPARK_LEN    20
  464. #ifdef USE_EF_UX_TIME
  465. #  define EF_SPARK_SIZ  ((EB_HEADSIZE+EB_SPARK_LEN)+(EB_HEADSIZE+EB_UX_MINLEN))
  466. #else
  467. #  define EF_SPARK_SIZ  (EB_HEADSIZE+EB_SPARK_LEN)
  468. #endif
  469.  
  470.   if ((name=(char *)malloc(strlen(z->name)+1))==NULL) {
  471.     fprintf(stderr," set_extra_field: not enough memory for directory name\n");
  472.     return ZE_MEM;
  473.   }
  474.  
  475.   strcpy(name,z->name);
  476.  
  477.   if (name[strlen(name)-1]=='.') {  /* remove the last '.' in directory names */
  478.     name[strlen(name)-1]=0;
  479.   }
  480.  
  481.   z->extra=(char *)malloc(EF_SPARK_SIZ);
  482.   if (z->extra==NULL) {
  483.     fprintf(stderr," set_extra_field: not enough memory\n");
  484.     free(name);
  485.     return ZE_MEM;
  486.   }
  487.   z->cextra = z->extra;
  488.   z->cext = z->ext = EF_SPARK_SIZ;
  489.  
  490.   block=(extra_block *)z->extra;
  491.   block->ID=SPARKID;
  492.   block->size=EB_SPARK_LEN;
  493.   block->ID_2=SPARKID_2;
  494.   block->zero=0;
  495.  
  496.   if (SWI_OS_File_5(name,NULL,&block->loadaddr,&block->execaddr,
  497.                     NULL,&block->attr) != NULL) {
  498.     fprintf(stderr," OS error while set_extra_field of %s\n",name);
  499.   }
  500.  
  501.   free(name);
  502.  
  503. #ifdef USE_EF_UX_TIME
  504.     eb_ptr = z->extra + (EB_HEADSIZE+EB_SPARK_LEN);
  505.  
  506.     eb_ptr[0]  = 'U';
  507.     eb_ptr[1]  = 'X';
  508.     eb_ptr[2]  = EB_UX_MINLEN;          /* length of data part of e.f. */
  509.     eb_ptr[3]  = 0;
  510.     eb_ptr[4]  = (char)(z_utim->actime);
  511.     eb_ptr[5]  = (char)(z_utim->actime >> 8);
  512.     eb_ptr[6]  = (char)(z_utim->actime >> 16);
  513.     eb_ptr[7]  = (char)(z_utim->actime >> 24);
  514.     eb_ptr[8]  = (char)(z_utim->modtime);
  515.     eb_ptr[9]  = (char)(z_utim->modtime >> 8);
  516.     eb_ptr[10] = (char)(z_utim->modtime >> 16);
  517.     eb_ptr[11] = (char)(z_utim->modtime >> 24);
  518. #endif /* USE_EF_UX_TIME */
  519.  
  520.   return ZE_OK;
  521. }
  522.  
  523. /******************************/
  524. /*  Function version_local()  */
  525. /******************************/
  526.  
  527. void version_local()
  528. {
  529.     static const char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  530.  
  531.     printf(CompiledWith,
  532. #ifdef __GNUC__
  533.       "gcc ", __VERSION__,
  534. #else
  535. #  ifdef __CC_NORCROFT
  536.       "Norcroft ", "cc",
  537. #  else
  538.       "cc",""
  539. #  endif
  540. #endif
  541.  
  542.       "RISC OS",
  543.  
  544.       " (Acorn Computers Ltd)",
  545.  
  546. #ifdef __DATE__
  547.       " on ", __DATE__
  548. #else
  549.       "", ""
  550. #endif
  551.       );
  552.  
  553. } /* end function version_local() */
  554.  
  555.  
  556. #endif /* !UTIL */
  557.