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

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 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. #ifndef UTIL
  17.  
  18. #define MATCH shmatch
  19. #define opend opendir
  20. #define closed closedir
  21. #define PAD 0
  22. #define PATH_END '/'
  23.  
  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->iname))
  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 modification time */
  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. iztimes *t;             /* return value: access, modific. and creation times */
  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, modification and creation
  404.    times 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->atime = t->mtime = t->ctime = 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->atime = s.st_atime;
  447.     t->mtime = s.st_mtime;
  448.     t->ctime = s.st_ctime;
  449.   }
  450.  
  451.   return unix2dostime((time_t *) &s.st_mtime);
  452. }
  453.  
  454. int set_extra_field(z, z_utim)
  455.         struct zlist far *z;
  456.         iztimes *z_utim;
  457. {
  458. #ifdef USE_EF_UT_TIME
  459.   char *eb_ptr;
  460. #endif /* USE_EF_UT_TIME */
  461.   char *name;
  462.   extra_block *block;
  463.  
  464. #define EB_SPARK_LEN    20
  465. #define EB_SPARK_SIZE (EB_HEADSIZE+EB_SPARK_LEN)
  466. #ifdef USE_EF_UT_TIME
  467. #  define EB_UTTIME_SIZE (EB_HEADSIZE+EB_UT_LEN(1))
  468. #else
  469. #  define EB_UTTIME_SIZE 0
  470. #endif
  471. #define EF_SPARK_TOTALSIZE (EB_SPARK_SIZE + EB_UTTIME_SIZE)
  472.  
  473.   if ((name=(char *)malloc(strlen(z->name)+1))==NULL) {
  474.     fprintf(stderr," set_extra_field: not enough memory for directory name\n");
  475.     return ZE_MEM;
  476.   }
  477.  
  478.   strcpy(name,z->name);
  479.  
  480.   if (name[strlen(name)-1]=='.') {  /* remove the last '.' in directory names */
  481.     name[strlen(name)-1]=0;
  482.   }
  483.  
  484.   z->extra=(char *)malloc(EF_SPARK_TOTALSIZE);
  485.   if (z->extra==NULL) {
  486.     fprintf(stderr," set_extra_field: not enough memory\n");
  487.     free(name);
  488.     return ZE_MEM;
  489.   }
  490.   z->cextra = z->extra;
  491.   z->cext = z->ext = EF_SPARK_TOTALSIZE;
  492.  
  493.   block=(extra_block *)z->extra;
  494.   block->ID=SPARKID;
  495.   block->size=EB_SPARK_LEN;
  496.   block->ID_2=SPARKID_2;
  497.   block->zero=0;
  498.  
  499.   if (SWI_OS_File_5(name,NULL,&block->loadaddr,&block->execaddr,
  500.                     NULL,&block->attr) != NULL) {
  501.     fprintf(stderr," OS error while set_extra_field of %s\n",name);
  502.   }
  503.  
  504.   free(name);
  505.  
  506. #ifdef USE_EF_UT_TIME
  507.     eb_ptr = z->extra + EB_SPARK_SIZE;
  508.  
  509.     eb_ptr[0]  = 'U';
  510.     eb_ptr[1]  = 'T';
  511.     eb_ptr[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */
  512.     eb_ptr[3]  = 0;
  513.     eb_ptr[4]  = EB_UT_FL_MTIME;
  514.     eb_ptr[5]  = (char)(z_utim->mtime);
  515.     eb_ptr[6]  = (char)(z_utim->mtime >> 8);
  516.     eb_ptr[7]  = (char)(z_utim->mtime >> 16);
  517.     eb_ptr[8]  = (char)(z_utim->mtime >> 24);
  518. #endif /* USE_EF_UT_TIME */
  519.  
  520.   return ZE_OK;
  521. }
  522.  
  523. #endif /* !UTIL */
  524.  
  525.  
  526. /******************************/
  527. /*  Function version_local()  */
  528. /******************************/
  529.  
  530. void version_local()
  531. {
  532.     static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  533.  
  534.     printf(CompiledWith,
  535. #ifdef __GNUC__
  536.       "gcc ", __VERSION__,
  537. #else
  538. #  ifdef __CC_NORCROFT
  539.       "Norcroft ", "cc",
  540. #  else
  541.       "cc", "",
  542. #  endif
  543. #endif
  544.  
  545.       "RISC OS",
  546.  
  547.       " (Acorn Computers Ltd)",
  548.  
  549. #ifdef __DATE__
  550.       " on ", __DATE__
  551. #else
  552.       "", ""
  553. #endif
  554.       );
  555.  
  556. } /* end function version_local() */
  557.