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

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 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. #define MATCH shmatch
  16.  
  17. #include <jctype.h>
  18. #include <dirent.h>
  19.  
  20. #define PAD 0
  21. #define PATH_END '/'
  22.  
  23. int h68_stat(char *name, struct stat *st)
  24. {
  25.   int ret;
  26.  
  27. #if 0   /* This is not needed.  LIBC is debugged. */
  28.   if (strchr(name, '*') != NULL || strchr(name, '?') != NULL)
  29.     return -1;
  30. #endif
  31.   ret = stat(name, st);
  32.   if (ret == -1)
  33.     return -1;
  34.   if (S_ISEXBIT(st->st_mode))
  35.     st->st_mode |= S_IEXEC;
  36.   else
  37.     st->st_mode &= ~S_IEXEC;
  38.   st->st_mode &= (~022 & 0177777);
  39.   return ret;
  40. }
  41.  
  42. #ifndef UTIL
  43.  
  44. #include <sys/dos.h>
  45.  
  46. /* Library functions not in (most) header files */
  47.  
  48. int utime OF((char *, ztimbuf *));
  49.  
  50. extern char *label;
  51. local ulg label_time = 0;
  52. local ulg label_mode = 0;
  53. local time_t label_utim = 0;
  54.  
  55. /* Local functions */
  56. local char *readd OF((DIR *));
  57.  
  58. local char *readd(d)
  59. DIR *d;
  60. {
  61.    struct dirent *e;
  62.  
  63.    e = readdir(d);
  64.    return e == NULL ? (char *) NULL : e->d_name;
  65. }
  66.  
  67. int wild(w)
  68. char *w;
  69. {
  70.   struct _filbuf inf;
  71.   int r;
  72.   extern int _hupair;
  73.   char name[FNMAX];
  74.   char *p;
  75.  
  76.   if (_hupair)
  77.     return procname(w);         /* if argv's passed hupair, don't glob */
  78.   if (strcmp(w, "-") == 0)   /* if compressing stdin */
  79.     return newname(w, 0);
  80.   strcpy(name, w);
  81.   _toslash(name);
  82.   if ((p = strrchr(name, '/')) == NULL && (p = strrchr(name, ':')) == NULL)
  83.     p = name;
  84.   else
  85.     p++;
  86.   if (_dos_files (&inf, w, 0xff) < 0)
  87.     return ZE_MISS;
  88.   do {
  89.     strcpy(p, inf.name);
  90.     r = procname(name);
  91.     if (r != ZE_OK)
  92.       return r;
  93.   } while (_dos_nfiles(&inf) >= 0);
  94.  
  95.   return ZE_OK;
  96. }
  97.  
  98. int procname(n)
  99. char *n;                /* name to process */
  100. /* Process a name or sh expression to operate on (or exclude).  Return
  101.    an error code in the ZE_ class. */
  102. {
  103.   char *a;              /* path and name for recursion */
  104.   DIR *d;               /* directory stream from opendir() */
  105.   char *e;              /* pointer to name from readd() */
  106.   int m;                /* matched flag */
  107.   char *p;              /* path for recursion */
  108.   struct stat s;        /* result of stat() */
  109.   struct zlist far *z;  /* steps through zfiles list */
  110.  
  111.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  112.     return newname(n, 0);
  113.   else if (LSSTAT(n, &s))
  114.   {
  115.     /* Not a file or directory--search for shell expression in zip file */
  116.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  117.     m = 1;
  118.     for (z = zfiles; z != NULL; z = z->nxt) {
  119.       if (MATCH(p, z->iname))
  120.       {
  121.         z->mark = pcount ? filter(z->zname) : 1;
  122.         if (verbose)
  123.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  124.                z->mark ? "in" : "ex", z->name);
  125.         m = 0;
  126.       }
  127.     }
  128.     free((zvoid *)p);
  129.     return m ? ZE_MISS : ZE_OK;
  130.   }
  131.  
  132.   /* Live name--use if file, recurse if directory */
  133.   _toslash(n);
  134.   if ((s.st_mode & S_IFDIR) == 0)
  135.   {
  136.     /* add or remove name of file */
  137.     if ((m = newname(n, 0)) != ZE_OK)
  138.       return m;
  139.   } else {
  140.     /* Add trailing / to the directory name */
  141.     if ((p = malloc(strlen(n)+2)) == NULL)
  142.       return ZE_MEM;
  143.     if (strcmp(n, ".") == 0) {
  144.       *p = '\0';  /* avoid "./" prefix and do not create zip entry */
  145.     } else {
  146.       strcpy(p, n);
  147.       a = p + strlen(p);
  148.       if (a[-1] != '/')
  149.         strcpy(a, "/");
  150.       if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  151.         free((zvoid *)p);
  152.         return m;
  153.       }
  154.     }
  155.     /* recurse into directory */
  156.     if (recurse && (d = opendir(n)) != NULL)
  157.     {
  158.       while ((e = readd(d)) != NULL) {
  159.         if (strcmp(e, ".") && strcmp(e, ".."))
  160.         {
  161.           if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  162.           {
  163.             closedir(d);
  164.             free((zvoid *)p);
  165.             return ZE_MEM;
  166.           }
  167.           strcat(strcpy(a, p), e);
  168.           if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  169.           {
  170.             if (m == ZE_MISS)
  171.               zipwarn("name not matched: ", a);
  172.             else
  173.               ziperr(m, a);
  174.           }
  175.           free((zvoid *)a);
  176.         }
  177.       }
  178.       closedir(d);
  179.     }
  180.     free((zvoid *)p);
  181.   } /* (s.st_mode & S_IFDIR) == 0) */
  182.   return ZE_OK;
  183. }
  184.  
  185. char *ex2in(x, isdir, pdosflag)
  186. char *x;                /* external file name */
  187. int isdir;              /* input: x is a directory */
  188. int *pdosflag;          /* output: force MSDOS file attributes? */
  189. /* Convert the external file name to a zip file name, returning the malloc'ed
  190.    string or NULL if not enough memory. */
  191. {
  192.   char *n;              /* internal file name (malloc'ed) */
  193.   char *t;              /* shortened name */
  194.   int dosflag;
  195.  
  196.   dosflag = dosify; /* default for non-DOS and non-OS/2 */
  197.  
  198.   /* Find starting point in name before doing malloc */
  199.   t = *x && *(x + 1) == ':' ? x + 2 : x;
  200.   while (*t == '/' || *t == '\\')
  201.     t++;
  202.  
  203.   /* Make changes, if any, to the copied name (leave original intact) */
  204.   _toslash(t);
  205.  
  206.   if (!pathput)
  207.     t = last(t, PATH_END);
  208.  
  209.   /* Malloc space for internal name and copy it */
  210.   if ((n = malloc(strlen(t) + 1)) == NULL)
  211.     return NULL;
  212.   strcpy(n, t);
  213.  
  214.   if (dosify)
  215.     msname(n);
  216.   /* Returned malloc'ed name */
  217.   if (pdosflag)
  218.     *pdosflag = dosflag;
  219.   return n;
  220. }
  221.  
  222.  
  223. char *in2ex(n)
  224. char *n;                /* internal file name */
  225. /* Convert the zip file name to an external file name, returning the malloc'ed
  226.    string or NULL if not enough memory. */
  227. {
  228.   char *x;              /* external file name */
  229.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  230.       return NULL;
  231.   strcpy(x, n);
  232.  
  233.   return x;
  234. }
  235.  
  236. void stamp(f, d)
  237. char *f;                /* name of file to change */
  238. ulg d;                  /* dos-style time to change it to */
  239. /* Set last updated and accessed time of file f to the DOS time d. */
  240. {
  241.   ztimbuf u;            /* argument for utime() */
  242.  
  243.   /* Convert DOS time to time_t format in u */
  244.   u.actime = u.modtime = dos2unixtime(d);
  245.  
  246.   /* Set updated and accessed times of f */
  247.   utime(f, &u);
  248. }
  249.  
  250. #define GetFileMode(name) (_dos_chmod((name), -1) & 0x3f)
  251.  
  252. ulg filetime(f, a, n, t)
  253. char *f;                /* name of file to get info on */
  254. ulg *a;                 /* return value: file attributes */
  255. long *n;                /* return value: file size */
  256. iztimes *t;             /* return value: access, modific. and creation times */
  257. /* If file *f does not exist, return 0.  Else, return the file's last
  258.    modified date and time as an MSDOS date and time.  The date and
  259.    time is returned in a long with the date most significant to allow
  260.    unsigned integer comparison of absolute times.  Also, if a is not
  261.    a NULL pointer, store the file attributes there, with the high two
  262.    bytes being the Unix attributes, and the low byte being a mapping
  263.    of that to DOS attributes.  If n is not NULL, store the file size
  264.    there.  If t is not NULL, the file's access, modification and creation
  265.    times are stored there as UNIX time_t values.
  266.    If f is "-", use standard input as the file. If f is a device, return
  267.    a file size of -1 */
  268. {
  269.   struct stat s;        /* results of stat() */
  270.   char name[FNMAX];
  271.   int len = strlen(f), isstdin = !strcmp(f, "-");
  272.  
  273.   if (f == label) {
  274.     if (a != NULL)
  275.       *a = label_mode;
  276.     if (n != NULL)
  277.       *n = -2L; /* convention for a label name */
  278.     if (t != NULL)
  279.       t->atime = t->mtime = t->ctime = label_utim;
  280.     return label_time;
  281.   }
  282.   strcpy(name, f);
  283.   if (name[len - 1] == '/')
  284.     name[len - 1] = '\0';
  285.   /* not all systems allow stat'ing a file with / appended */
  286.  
  287.   if (isstdin) {
  288.     if (fstat(fileno(stdin), &s) != 0)
  289.       error("fstat(stdin)");
  290.   } else if (LSSTAT(name, &s)) != 0)
  291.              /* Accept about any file kind including directories
  292.               * (stored with trailing / with -r option)
  293.               */
  294.     return 0;
  295.  
  296.   if (a != NULL) {
  297.     *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));
  298.   }
  299.   if (n != NULL)
  300.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  301.   if (t != NULL) {
  302.     t->atime = s.st_atime;
  303.     t->mtime = s.st_mtime;
  304.     t->ctime = s.st_ctime;
  305.   }
  306.  
  307.   return unix2dostime(&s.st_mtime);
  308. }
  309.  
  310. int set_extra_field(z, z_utim)
  311.   struct zlist far *z;
  312.   iztimes *z_utim;
  313.   /* create extra field and change z->att if desired */
  314. {
  315. #ifdef USE_EF_UT_TIME
  316.   if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
  317.     return ZE_MEM;
  318.  
  319.   z->extra[0]  = 'U';
  320.   z->extra[1]  = 'T';
  321.   z->extra[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */
  322.   z->extra[3]  = 0;
  323.   z->extra[4]  = EB_UT_FL_MTIME;
  324.   z->extra[5]  = (char)(z_utim->mtime);
  325.   z->extra[6]  = (char)(z_utim->mtime >> 8);
  326.   z->extra[7]  = (char)(z_utim->mtime >> 16);
  327.   z->extra[8]  = (char)(z_utim->mtime >> 24);
  328.  
  329.   z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));
  330.   z->cextra = z->extra;
  331.  
  332.   return ZE_OK;
  333. #else /* !USE_EF_UT_TIME */
  334.   return (int)(z-z);
  335. #endif /* ?USE_EF_UT_TIME */
  336. }
  337.  
  338. int deletedir(d)
  339. char *d;                /* directory to delete */
  340. /* Delete the directory *d if it is empty, do nothing otherwise.
  341.    Return the result of rmdir(), delete(), or system().
  342.    For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]).
  343.  */
  344. {
  345.     return rmdir(d);
  346. }
  347.  
  348. #endif /* !UTIL */
  349.  
  350.  
  351. /******************************/
  352. /*  Function version_local()  */
  353. /******************************/
  354.  
  355. void version_local()
  356. {
  357.     static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  358. #if 0
  359.     char buf[40];
  360. #endif
  361.  
  362.     printf(CompiledWith,
  363.  
  364. #ifdef __GNUC__
  365.       "gcc ", __VERSION__,
  366. #else
  367. #  if 0
  368.       "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
  369. #  else
  370.       "unknown compiler", "",
  371. #  endif
  372. #endif
  373.  
  374.       "Human68k", " (X68000)",
  375.  
  376. #ifdef __DATE__
  377.       " on ", __DATE__
  378. #else
  379.       "", ""
  380. #endif
  381.       );
  382.  
  383. } /* end function version_local() */
  384.