home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / cmsmvs / cmsmvs.c < prev    next >
C/C++ Source or Header  |  1996-04-05  |  7KB  |  293 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden, George Petrov 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. /*
  12.  * routines common to VM/CMS and MVS
  13.  */
  14.  
  15. #include "zip.h"
  16.  
  17. #include <time.h>
  18.  
  19. #define MATCH shmatch
  20.  
  21.  
  22. #define PAD 0
  23. #define PATH_END '/'
  24.  
  25. /* Library functions not in (most) header files */
  26.  
  27. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  28.  
  29. int utime OF((char *, ztimbuf *));
  30.  
  31. extern char *label;
  32. local ulg label_time = 0;
  33. local ulg label_mode = 0;
  34. local time_t label_utim = 0;
  35.  
  36. int stat(const char *path, struct stat *buf)
  37. {
  38.    if ((buf->fp = fopen(path, "r")) != NULL) {
  39.       fldata_t fdata;
  40.       if (fldata( buf->fp, buf->fname, &fdata ) == 0) {
  41.          buf->st_dev  = fdata.__device;
  42.          buf->st_mode = *(short *)(&fdata);
  43.       }
  44.       strcpy( buf->fname, path );
  45.       fclose(buf->fp);
  46.    }
  47.    return (buf->fp != NULL ? 0 : 1);
  48. }
  49.  
  50. int fstat(int fd, struct stat *buf)
  51. {
  52.    fldata_t fdata;
  53.  
  54.    if ((fd != -1) && (fldata( (FILE *)fd, buf->fname, &fdata ) == 0)) {
  55.       buf->st_dev  = fdata.__device;
  56.       buf->st_mode = *(short *)(&fdata);
  57.       buf->fp      = (FILE *)fd;
  58.       return 0;
  59.    }
  60.    return -1;
  61. }
  62.  
  63.  
  64. char *ex2in(x, isdir, pdosflag)
  65. char *x;                /* external file name */
  66. int isdir;              /* input: x is a directory */
  67. int *pdosflag;          /* output: force MSDOS file attributes? */
  68. /* Convert the external file name to a zip file name, returning the malloc'ed
  69.    string or NULL if not enough memory. */
  70. {
  71.   char *n;              /* internal file name (malloc'ed) */
  72.   char *t;              /* shortened name */
  73.   int dosflag;
  74.   char mem[10] = "";    /* member name */
  75.   char ext[10] = "";     /* extension name */
  76.  
  77.   dosflag = dosify;  /* default for non-DOS non-OS/2 */
  78.  
  79.   /* Find starting point in name before doing malloc */
  80.   for (t = x; *t == '/'; t++)
  81.     ;
  82.  
  83.   /* Make changes, if any, to the copied name (leave original intact) */
  84.   if (!pathput)
  85.     t = last(t, PATH_END);
  86.  
  87.   /* Malloc space for internal name and copy it */
  88.   if ((n = malloc(strlen(t) + 1)) == NULL)
  89.     return NULL;
  90.   strcpy(n, t);
  91.   if (t = strrchr(n, '(')) {
  92.      *t = '\0';
  93.      strcpy(mem,t+1);
  94.      if (t = strchr(mem, ')')) *t = '\0';
  95.      if (t = strrchr(n, '.')) t++;
  96.      else t = n;
  97.      strcpy(ext,t);
  98.      strcpy(t,mem);
  99.      strcat(t,".");
  100.      strcat(t,ext);
  101.   }
  102.   if (t = strrchr(n, '.')) {
  103.      while (--t > n)
  104.         if (*t == '.')
  105.           *t = '/';
  106.   }
  107.   n = strtoasc(n,t);
  108.  
  109.   if (isdir == 42) return n;      /* avoid warning on unused variable */
  110.  
  111.   if (dosify)
  112.     msname(n);
  113.  
  114.   /* Returned malloc'ed name */
  115.   if (pdosflag)
  116.     *pdosflag = dosflag;
  117.   return n;
  118. }
  119.  
  120.  
  121. char *in2ex(n)
  122. char *n;                /* internal file name */
  123. /* Convert the zip file name to an external file name, returning the malloc'ed
  124.    string or NULL if not enough memory. */
  125. {
  126.   char *x;              /* external file name */
  127.  
  128.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  129.     return NULL;
  130.   strtoebc(x, n);
  131.   return x;
  132. }
  133.  
  134. void stamp(f, d)
  135. char *f;                /* name of file to change */
  136. ulg d;                  /* dos-style time to change it to */
  137. /* Set last updated and accessed time of file f to the DOS time d. */
  138. {
  139.   ztimbuf u;            /* argument for utime() */
  140.  
  141.   /* Convert DOS time to time_t format in u.actime and u.modtime */
  142.   u.actime = u.modtime = dos2unixtime(d);
  143.  
  144.   utime(f, &u);
  145. }
  146.  
  147. ulg filetime(f, a, n, t)
  148. char *f;                /* name of file to get info on */
  149. ulg *a;                 /* return value: file attributes */
  150. long *n;                /* return value: file size */
  151. ztimbuf *t;             /* return value: access and modification time */
  152. {
  153.   FILE *stream;
  154.   time_t ltime;
  155.  
  156.   if (strcmp(f, "-") != 0) {    /* if not compressing stdin */
  157.      if ((stream = fopen(f, "r")) == (FILE *)NULL) {
  158.         return 0;
  159.      } else {
  160.         if (n != NULL) {
  161.            *n = -1L;
  162. /*         fseek(stream,0L,SEEK_END);
  163.            *n = ftell(stream);  - don't work with "r" */
  164.         }
  165.         fclose(stream);
  166.      }
  167.   }
  168.   else {
  169.      if (n != NULL) {
  170.         *n = -1L;
  171.      }
  172.   }
  173.  
  174.   time(<ime);
  175.   if (t != NULL)
  176.      t->actime = t->modtime = ltime;
  177.  
  178.   return unix2dostime(<ime);
  179. }
  180.  
  181. int set_extra_field(z, z_utim)
  182. struct zlist far *z;
  183. ztimbuf *z_utim;
  184. /* create extra field and change z->att if desired */
  185. {
  186.    fldata_t fdata;
  187.    FILE *stream;
  188.    char fname[65];
  189.    char type[60];
  190.    char *eb_ptr;
  191. #ifdef USE_EF_UX_TIME
  192.    extent ef_l_len = (EB_HEADSIZE+EB_UX_MINLEN);
  193. #else /* !USE_EF_UX_TIME */
  194.    extent ef_l_len = 0;
  195. #endif /* ?USE_EF_UX_TIME */
  196.    int set_cmsmvs_eb = 0;
  197.  
  198. /*translate_eol = 0;*/
  199.   if (aflag == ASCII) {
  200.      z->att = ASCII;
  201.   } else {
  202.     if (bflag)
  203.       z->att = BINARY;
  204.     else
  205.       z->att = __EBCDIC;
  206.     ef_l_len += sizeof(fdata)+EB_HEADSIZE;
  207.     set_cmsmvs_eb = 1;
  208.   }
  209.  
  210.   if (ef_l_len > 0) {
  211.     z->extra = (char *)malloc(ef_l_len);
  212.     if (z->extra == NULL) {
  213.        printf("FLDATA : Unable to allocate memory !\n");
  214.        return ZE_MEM;
  215.     }
  216.     z->cext = z->ext = ef_l_len;
  217.     eb_ptr = z->cextra = z->extra;
  218.  
  219.     if (set_cmsmvs_eb) {
  220.       strtoebc(fname,z->zname);
  221.       if (bflag)
  222.         stream = fopen(fname,"rb,type=record");
  223.       else
  224.         stream = fopen(fname,"r");
  225.       if (stream == NULL) {
  226.         printf("FLDATA : Could not open file : %s !\n",fname);
  227.         return ZE_NONE;
  228.       }
  229.  
  230.       fldata(stream,fname,&fdata);
  231.       /*put the system ID */
  232. #ifdef VM_CMS
  233.       *(eb_ptr) = EF_VMCMS & 0xFF;
  234.       *(eb_ptr+1) = EF_VMCMS >> 8;
  235. #else
  236.       *(eb_ptr) = EF_MVS & 0xFF;
  237.       *(eb_ptr+1) = EF_MVS >> 8;
  238. #endif
  239.       *(eb_ptr+2) = sizeof(fdata) & 0xFF;
  240.       *(eb_ptr+3) = sizeof(fdata) >> 8;
  241.  
  242.       memcpy(eb_ptr+EB_HEADSIZE,&fdata,sizeof(fdata));
  243.       fclose(stream);
  244. #ifdef USE_EF_UX_TIME
  245.       eb_ptr += (sizeof(fdata)+EB_HEADSIZE);
  246. #endif /* USE_EF_UX_TIME */
  247.     }
  248. #ifdef USE_EF_UX_TIME
  249.     eb_ptr[0]  = 'U';
  250.     eb_ptr[1]  = 'X';
  251.     eb_ptr[2]  = EB_UX_MINLEN;          /* length of data part of e.f. */
  252.     eb_ptr[3]  = 0;
  253.     eb_ptr[4]  = (char)(z_utim->actime);
  254.     eb_ptr[5]  = (char)(z_utim->actime >> 8);
  255.     eb_ptr[6]  = (char)(z_utim->actime >> 16);
  256.     eb_ptr[7]  = (char)(z_utim->actime >> 24);
  257.     eb_ptr[8]  = (char)(z_utim->modtime);
  258.     eb_ptr[9]  = (char)(z_utim->modtime >> 8);
  259.     eb_ptr[10] = (char)(z_utim->modtime >> 16);
  260.     eb_ptr[11] = (char)(z_utim->modtime >> 24);
  261. #endif /* USE_EF_UX_TIME */
  262.   }
  263.  
  264.   return ZE_OK;
  265. }
  266.  
  267. int deletedir(d)
  268. char *d;                /* directory to delete */
  269. /* Delete the directory *d if it is empty, do nothing otherwise.
  270.    Return the result of rmdir(), delete(), or system().
  271.    For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]).
  272.  */
  273. {
  274. #ifdef VM_CMS
  275.     return 0;
  276. #else
  277.     return rmdir(d);
  278. #endif /* VM_CMS */
  279. }
  280.  
  281. void version_local()
  282. {
  283.     printf("Compiled with %s under %s.\n", "C/370 2.1",
  284. #ifdef VM_CMS
  285.       "VM/CMS"
  286. #else
  287.       "MVS"
  288. #endif
  289.     );
  290. }
  291.  
  292. #endif /* !UTIL */
  293.