home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / tops20 / tops20.c < prev    next >
C/C++ Source or Header  |  1997-08-19  |  15KB  |  563 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. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  14.  
  15. #include <time.h>
  16.  
  17. #define PATH_START '<'
  18. #define PATH_END   '>'
  19. #define MATCH shmatch
  20.  
  21. /* Extra malloc() space in names for cutpath() */
  22. #define PAD 5         /* may have to change .FOO] to ]FOO.DIR;1 */
  23.  
  24.  
  25. #define TRUE 1
  26. #define FALSE 0
  27. #define O_RDONLY (0)
  28. #define O_T20_WILD (1<<18)
  29. #include <monsym.h>    /* Get amazing monsym() macro */
  30. extern int jsys(), fstat();
  31. extern char *getcwd();
  32. extern int _gtjfn(), _rljfn();
  33. #define JSYS_CLASS    0070000000000
  34. #define FLD(val,mask) (((unsigned)(val)*((mask)&(-(mask))))&(mask))
  35. #define _DEFJS(name,class) (FLD(class, JSYS_CLASS) | (monsym(name)&0777777))
  36. #define JFNS  _DEFJS("JFNS%", 1)
  37. #define GNJFN _DEFJS("GNJFN%", 0)
  38. static int wfopen(), wfnext(), strlower(), strupper();
  39. static char *wfname();
  40. typedef struct {
  41.    int  wfjfn;
  42.    int  more;
  43. } DIR;
  44.  
  45. /* Library functions not in (most) header files */
  46.  
  47. extern int stat(), chmod(), toupper(), tolower();
  48.  
  49. int utime OF((char *, ztimbuf *));
  50.  
  51. extern char *label;
  52. local ulg label_time = 0;
  53. local ulg label_mode = 0;
  54. local time_t label_utim = 0;
  55.  
  56. /* Local functions */
  57. local char *readd OF((DIR *));
  58.  
  59.  
  60. local DIR *opendir(n)
  61. char *n;                /* directory name to open */
  62. /* Open the directory *n, returning a pointer to an allocated DIR, or
  63.    NULL if error. */
  64. {
  65.     DIR *d;             /* pointer to malloc'ed directory stream */
  66.     char    *c;                         /* scans TOPS20 path */
  67.     int     m;                          /* length of name */
  68.     char    *p;                         /* temp string */
  69.  
  70.     if (((d = (DIR *)malloc(sizeof(DIR))) == NULL) ||
  71.         ((p = (char *)malloc((m = strlen(n)) + 4)) == NULL)) {
  72.             return NULL;
  73.     }
  74.  
  75. /* Directory may be in form "<DIR.SUB1.SUB2>" or "<DIR.SUB1>SUB2.DIRECTORY".
  76. ** If latter, convert to former. */
  77.  
  78.     if ((m > 0)  &&  (*(c = strcpy(p,n) + m-1) != '>')) {
  79.         c -= 10;
  80.         *c-- = '\0';        /* terminate at "DIRECTORY.1" */
  81.         *c = '>';           /* "." --> ">" */
  82.         while ((c > p)  &&  (*--c != '>'));
  83.         *c = '.';           /* ">" --> "." */
  84.     }
  85.     strcat(p, "*.*");
  86.     if ((d->wfjfn = wfopen(p)) == 0) {
  87.         free((zvoid *)d);
  88.         free((zvoid *)p);
  89.         return NULL;
  90.     }
  91.     free((zvoid *)p);
  92.     d->more = TRUE;
  93.     return (d);
  94. }
  95.  
  96. local char *readd(d)
  97. DIR *d;                 /* directory stream to read from */
  98. /* Return a pointer to the next name in the directory stream d, or NULL if
  99.    no more entries or an error occurs. */
  100. {
  101.   char    *p;
  102.   if ((d->more == FALSE) || ((p = wfname(d->wfjfn)) == NULL)) {
  103.       return NULL;
  104.   }
  105.   if (wfnext(d->wfjfn) == 0) {
  106.       d->more = FALSE;
  107.   }
  108.   return p;
  109. }
  110.  
  111.  
  112. local void closedir(d)
  113. DIR *d;                 /* directory stream to close */
  114. /* Close the directory stream */
  115. {
  116.   free((zvoid *)d);
  117. }
  118.  
  119. /* Wildcard filename routines */
  120.  
  121. /* WFOPEN - open wild card filename
  122. **      Returns wild JFN for filespec, 0 if failure.
  123. */
  124. static int
  125. wfopen(name)
  126. char *name;
  127. {
  128.     return (_gtjfn(name, (O_RDONLY | O_T20_WILD)));
  129. }
  130.  
  131. /* WFNAME - Return filename for wild JFN
  132. **      Returns pointer to dynamically allocated filename string
  133. */
  134. static char *
  135. wfname(jfn)
  136. int jfn;
  137. {
  138.     char *fp, fname[200];
  139.     int ablock[5];
  140.  
  141.     ablock[1] = (int) (fname - 1);
  142.     ablock[2] = jfn & 0777777;  /* jfn, no flags */
  143.     ablock[3] = 0111110000001;  /* DEV+DIR+NAME+TYPE+GEN, punctuate */
  144.     if (!jsys(JFNS, ablock))
  145.         return NULL;            /* something bad happened */
  146.     if ((fp = (char *)malloc(strlen(fname) + 1)) == NULL) {
  147.         return NULL;
  148.     }
  149.     strcpy(fp, fname);          /* copy the file name here */
  150.     return fp;
  151. }
  152.  
  153. /* WFNEXT - Make wild JFN point to next real file
  154. **      Returns success or failure (not JFN)
  155. */
  156. static int
  157. wfnext(jfn)
  158. int jfn;
  159. {
  160.     int ablock[5];
  161.  
  162.     ablock[1] = jfn;            /* save jfn and flags */
  163.     return jsys(GNJFN, ablock);
  164. }
  165.  
  166.  
  167. static int
  168. strupper(s)     /* Returns s in uppercase */
  169. char *s;        /* String to be uppercased */
  170. {
  171.     char    *p;
  172.  
  173.     p = s;
  174.     for (; *p; p++)
  175.         *p = toupper (*p);
  176. }
  177.  
  178. static int
  179. strlower(s)     /* Returns s in lowercase. */
  180. char *s;        /* String to be lowercased */
  181. {
  182.     char    *p;
  183.  
  184.     p = s;
  185.     for (; *p; p++)
  186.         *p = tolower (*p);
  187. }
  188.  
  189. int procname(n)
  190. char *n;                /* name to process */
  191. /* Process a name or sh expression to operate on (or exclude).  Return
  192.    an error code in the ZE_ class. */
  193. {
  194.   DIR *d;               /* directory stream from opendir() */
  195.   char *e;              /* pointer to name from readd() */
  196.   int m;                /* matched flag */
  197.   char *p;              /* path for recursion */
  198.   struct stat s;        /* result of stat() */
  199.   struct zlist far *z;  /* steps through zfiles list */
  200.  
  201.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  202.     return newname(n, 0);
  203.   else if (LSSTAT(n, &s))
  204.   {
  205.     /* Not a file or directory--search for shell expression in zip file */
  206.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  207.     m = 1;
  208.     for (z = zfiles; z != NULL; z = z->nxt) {
  209.       if (MATCH(p, z->iname))
  210.       {
  211.         z->mark = pcount ? filter(z->zname) : 1;
  212.         if (verbose)
  213.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  214.                z->mark ? "in" : "ex", z->name);
  215.         m = 0;
  216.       }
  217.     }
  218.     free((zvoid *)p);
  219.     return m ? ZE_MISS : ZE_OK;
  220.   }
  221.  
  222.   /* Live name--use if file, recurse if directory */
  223.   if ((s.st_mode & S_IFDIR) == 0)
  224.   {
  225.     /* add or remove name of file */
  226.     if ((m = newname(n, 0)) != ZE_OK)
  227.       return m;
  228.   } else {
  229.     if (dirnames && (m = newname(n, 1)) != ZE_OK) {
  230.       return m;
  231.     }
  232.     /* recurse into directory */
  233.     if (recurse && (d = opendir(n)) != NULL)
  234.     {
  235.       while ((e = readd(d)) != NULL) {
  236.         if ((m = procname(e)) != ZE_OK)     /* recurse on name */
  237.         {
  238.           closedir(d);
  239.           return m;
  240.         }
  241.       }
  242.       closedir(d);
  243.     }
  244.   } /* (s.st_mode & S_IFDIR) == 0) */
  245.   return ZE_OK;
  246. }
  247.  
  248. char *ex2in(x, isdir, pdosflag)
  249. char *x;                /* external file name */
  250. int isdir;              /* input: x is a directory */
  251. int *pdosflag;          /* output: force MSDOS file attributes? */
  252. /* Convert the external file name to a zip file name, returning the malloc'ed
  253.    string or NULL if not enough memory. */
  254. {
  255.   char *n;              /* internal file name (malloc'ed) */
  256.   char *t;              /* shortened name */
  257.   int dosflag;
  258.  
  259.   int jfn;
  260.   char *fp, fname[200];
  261.   int ablock[5];
  262.  
  263.   jfn = _gtjfn(x, (O_RDONLY));
  264.   ablock[1] = (int) (fname - 1);
  265.   ablock[2] = jfn & 0777777;    /* jfn, no flags */
  266.   ablock[3] = 0111100000001;    /* DEV+DIR+NAME+TYPE, punctuate */
  267.   if (!jsys(JFNS, ablock)) {
  268.       _rljfn(jfn);
  269.       return NULL;              /* something bad happened */
  270.   }
  271.   _rljfn(jfn);
  272.   if ((fp = (char *)malloc(strlen(fname) + 1)) == NULL) {
  273.       return NULL;
  274.   }
  275.   strcpy(fp, fname);            /* copy the file name here */
  276.   x = fp;
  277.  
  278.   dosflag = dosify; /* default for non-DOS and non-OS/2 */
  279.  
  280.   /* Find starting point in name before doing malloc */
  281.   t = x;
  282.   if ((n = strrchr(t, ':')) != NULL)
  283.     t = n + 1;
  284.   if (*t == PATH_START && (n = strrchr(t, PATH_END)) != NULL)
  285.     if (*(++t) == '.')
  286.       /* path is relative to current directory, skip leading '.' */
  287.       t++;
  288.  
  289.   /* Make changes, if any, to the copied name (leave original intact) */
  290.   if (!pathput)
  291.     t = last(t, PATH_END);
  292.  
  293.   /* Malloc space for internal name and copy it */
  294.   if ((n = malloc(strlen(t) + 1)) == NULL)
  295.     return NULL;
  296.   strcpy(n, t);
  297.  
  298.   if ((t = strrchr(n, PATH_END)) != NULL)
  299.   {
  300.     *t = '/';
  301.     while (--t > n)
  302.       if (*t == '.')
  303.         *t = '/';
  304.   }
  305.  
  306.   /* Fix from Greg Roelofs: */
  307.   /* Get current working directory and strip from n (t now = n) */
  308.   {
  309.     char cwd[256], *p, *q;
  310.     int c;
  311.  
  312.     if (getcwd(cwd, 256) && ((p = strchr(cwd, PATH_START)) != NULL))
  313.     {
  314.       if (*(++p) == '.')
  315.         p++;
  316.       if ((q = strrchr(p, PATH_END)) != NULL)
  317.       {
  318.         *q = '/';
  319.         while (--q > p)
  320.           if (*q == '.')
  321.             *q = '/';
  322.  
  323.         /* strip bogus path parts from n */
  324.         if (strncmp(n, p, (c=strlen(p))) == 0)
  325.         {
  326.           q = n + c;
  327.           while (*t++ = *q++)
  328.             ;
  329.         }
  330.       }
  331.     }
  332.   }
  333.   strlower(n);
  334.  
  335.   if (isdir)
  336.   {
  337.     if (strcmp((t=n+strlen(n)-6), ".dir;1"))
  338.       error("directory not version 1");
  339.     else
  340.       strcpy(t, "/");
  341.   }
  342.  
  343.   if ((t = strrchr(n, '.')) != NULL)
  344.   {
  345.     if ( t[1] == '\0')             /* "filename." -> "filename" */
  346.       *t = '\0';
  347.   }
  348.  
  349.   if (dosify)
  350.     msname(n);
  351.  
  352.   /* Returned malloc'ed name */
  353.   if (pdosflag)
  354.     *pdosflag = dosflag;
  355.   return n;
  356. }
  357.  
  358.  
  359. char *in2ex(n)
  360. char *n;                /* internal file name */
  361. /* Convert the zip file name to an external file name, returning the malloc'ed
  362.    string or NULL if not enough memory. */
  363. {
  364.   char *x;              /* external file name */
  365.   char *t;              /* scans name */
  366.  
  367.   if ((t = strrchr(n, '/')) == NULL)
  368.   {
  369.     if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  370.       return NULL;
  371.     strcpy(x, n);
  372.   }
  373.   else
  374.   {
  375.     if ((x = malloc(strlen(n) + 3 + PAD)) == NULL)
  376.       return NULL;
  377.     x[0] = PATH_START;
  378.     x[1] = '.';
  379.     strcpy(x + 2, n);
  380.     *(t = x + 2 + (t - n)) = PATH_END;
  381.     while (--t > x)
  382.       if (*t == '/')
  383.         *t = '.';
  384.   }
  385.   strupper(x);
  386.  
  387.   return x;
  388. }
  389.  
  390. void stamp(f, d)
  391. char *f;                /* name of file to change */
  392. ulg d;                  /* dos-style time to change it to */
  393. /* Set last updated and accessed time of file f to the DOS time d. */
  394. {
  395.   ztimbuf u;            /* argument for utime() */
  396.  
  397.   /* Convert DOS time to time_t format in u */
  398.   u.actime = u.modtime = dos2unixtime(d);
  399.  
  400.   /* Set updated and accessed times of f */
  401.   utime(f, &u);
  402. }
  403.  
  404. ulg filetime(f, a, n, t)
  405. char *f;                /* name of file to get info on */
  406. ulg *a;                 /* return value: file attributes */
  407. long *n;                /* return value: file size */
  408. iztimes *t;             /* return value: access, modific. and creation times */
  409. /* If file *f does not exist, return 0.  Else, return the file's last
  410.    modified date and time as an MSDOS date and time.  The date and
  411.    time is returned in a long with the date most significant to allow
  412.    unsigned integer comparison of absolute times.  Also, if a is not
  413.    a NULL pointer, store the file attributes there, with the high two
  414.    bytes being the Unix attributes, and the low byte being a mapping
  415.    of that to DOS attributes.  If n is not NULL, store the file size
  416.    there.  If t is not NULL, the file's access, modification and creation
  417.    times are stored there as UNIX time_t values.
  418.    If f is "-", use standard input as the file. If f is a device, return
  419.    a file size of -1 */
  420. {
  421.   struct stat s;        /* results of stat() */
  422.   char name[FNMAX];
  423.   int len = strlen(f);
  424.  
  425.   if (f == label) {
  426.     if (a != NULL)
  427.       *a = label_mode;
  428.     if (n != NULL)
  429.       *n = -2L; /* convention for a label name */
  430.     if (t != NULL)
  431.       t->atime = t->mtime = t->ctime = label_utim;
  432.     return label_time;
  433.   }
  434.   strcpy(name, f);
  435.   if (name[len - 1] == '/')
  436.     name[len - 1] = '\0';
  437.   /* not all systems allow stat'ing a file with / appended */
  438.  
  439.   if (strcmp(f, "-") == 0) {
  440.     if (fstat(fileno(stdin), &s) != 0)
  441.       error("fstat(stdin)");
  442.   } else if (LSSTAT(name, &s) != 0)
  443.              /* Accept about any file kind including directories
  444.               * (stored with trailing / with -r option)
  445.               */
  446.     return 0;
  447.  
  448.   if (a != NULL) {
  449.     *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  450.     if ((s.st_mode & S_IFDIR) != 0) {
  451.       *a |= MSDOS_DIR_ATTR;
  452.     }
  453.   }
  454.   if (n != NULL)
  455.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  456.   if (t != NULL) {
  457.     t->atime = s.st_atime;
  458.     t->mtime = s.st_mtime;
  459.     t->ctime = s.st_ctime;
  460.   }
  461.  
  462.   return unix2dostime(&s.st_mtime);
  463. }
  464.  
  465. #include <monsym.h>   /* Get amazing monsym() macro */
  466. #define       _FBBYV  monsym(".FBBYV")
  467. #define         FBBSZ_S       -24     /* Obsolete, replace by FLDGET! */
  468. #define         FBBSZ_M       077     /* ditto */
  469.  
  470. extern int _gtjfn(), _rljfn(), _gtfdb(), stat();
  471.  
  472. int set_extra_field(z, z_utim)
  473.   struct zlist far *z;
  474.   iztimes *z_utim;
  475.   /* create extra field and change z->att if desired */
  476. {
  477.   int jfn;
  478.  
  479.   translate_eol = 0;
  480.   jfn = _gtjfn(z->name, O_RDONLY);
  481.   z->att = (((_gtfdb (jfn, _FBBYV) << FBBSZ_S) & FBBSZ_M) != 8) ?
  482.            ASCII :BINARY;
  483.   _rljfn(jfn);
  484.  
  485. #ifdef USE_EF_UT_TIME
  486.   if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
  487.     return ZE_MEM;
  488.  
  489.   z->extra[0]  = 'U';
  490.   z->extra[1]  = 'T';
  491.   z->extra[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */
  492.   z->extra[3]  = 0;
  493.   z->extra[4]  = EB_UT_FL_MTIME;
  494.   z->extra[5]  = (char)(z_utim->mtime);
  495.   z->extra[6]  = (char)(z_utim->mtime >> 8);
  496.   z->extra[7]  = (char)(z_utim->mtime >> 16);
  497.   z->extra[8]  = (char)(z_utim->mtime >> 24);
  498.  
  499.   z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));
  500.   z->cextra = z->extra;
  501. #endif /* USE_EF_UT_TIME */
  502.  
  503.   return ZE_OK;
  504. }
  505.  
  506. int deletedir(d)
  507. char *d;                /* directory to delete */
  508. /* Delete the directory *d if it is empty, do nothing otherwise.
  509.    Return the result of rmdir(), delete(), or system().
  510.    For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]).
  511.  */
  512. {
  513.     zipwarn("deletedir not implemented yet", "");
  514.     return 127;
  515. }
  516.  
  517. #endif /* !UTIL */
  518.  
  519.  
  520. /******************************/
  521. /*  Function version_local()  */
  522. /******************************/
  523.  
  524. void version_local()
  525. {
  526.     static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  527. #if 0
  528.     char buf[40];
  529. #endif
  530.  
  531.     printf(CompiledWith,
  532.  
  533. #ifdef __GNUC__
  534.       "gcc ", __VERSION__,
  535. #else
  536. #  if 0
  537.       "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
  538. #  else
  539. #  ifdef __COMPILER_KCC__
  540.       "KCC", "",
  541. #  else
  542.       "unknown compiler", "",
  543. #  endif
  544. #  endif
  545. #endif
  546.  
  547.       "TOPS-20",
  548.  
  549. #if defined(foobar) || defined(FOOBAR)
  550.       " (Foo BAR)",   /* OS version or hardware */
  551. #else
  552.       "",
  553. #endif /* Foo BAR */
  554.  
  555. #ifdef __DATE__
  556.       " on ", __DATE__
  557. #else
  558.       "", ""
  559. #endif
  560.       );
  561.  
  562. } /* end function version_local() */
  563.