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

  1. /*---------------------------------------------------------------------------
  2.  
  3.   vmmvs.c (for both VM/CMS and MVS)
  4.  
  5.   Contains:  vmmvs_open_infile()
  6.              open_outfile()
  7.              find_vms_attrs()
  8.              flush()
  9.              close_outfile()
  10.              getVMMVSexfield()
  11.              do_wild()
  12.              mapattr()
  13.              mapname()
  14.              checkdir()
  15.              check_for_newer()
  16.              stat()
  17.              version()
  18.  
  19.   ---------------------------------------------------------------------------*/
  20.  
  21.  
  22. #define UNZIP_INTERNAL
  23. #include "unzip.h"
  24.  
  25.  
  26. /********************************/
  27. /* Function vmmvs_open_infile() */
  28. /********************************/
  29.  
  30. FILE *vmmvs_open_infile(__G)
  31.    __GDEF
  32. {
  33.    FILE *fzip;
  34.  
  35.    if ((fzip = fopen(G.zipfn,"rb,recfm=fb")) == (FILE *)NULL) {
  36.       char *buf;
  37.       FILE *in, *out;
  38.  
  39.       if ((buf = (char *)malloc(32768)) == NULL) return NULL;
  40.       if ((in = fopen(G.zipfn,"rb")) != NULL &&
  41.           (out = fopen("tt$$zz.z$$","wb  ,recfm=fb")) != NULL) {
  42.          Trace((stdout,"Converting ZIP file to fixed record format...\n"));
  43.          while (!feof(in)) {
  44.             fread(buf,1,32768,in);
  45.             fwrite(buf,1,32768,out);
  46.          }
  47.       }
  48.       else {
  49.          fclose(out);
  50.          fclose(in);
  51.          return NULL;
  52.       }
  53.       fclose(out);
  54.       fclose(in);
  55.       if (remove(G.zipfn) == 0) {
  56.          rename("tt$$zz.z$$",G.zipfn);
  57.          fzip = fopen(G.zipfn,"rb,recfm=fb");
  58.       }
  59.       if (fzip == (FILE *)NULL) return NULL;
  60.  
  61.       /* Update the G.ziplen value, it might have changed after the
  62.          reformatting copying. */
  63.       fseek(fzip,0L,SEEK_SET);
  64.       fseek(fzip,0L,SEEK_END);
  65.       G.ziplen = ftell(fzip);
  66.    }
  67.    return fzip;
  68. }
  69.  
  70.  
  71. /***************************/
  72. /* Function open_outfile() */
  73. /***************************/
  74.  
  75. int open_outfile(__G)           /* return 1 if fail */
  76.     __GDEF
  77. {
  78.     char type[100];
  79.     char *mode = NULL;
  80.  
  81.     if (G.pInfo->textmode)
  82.         mode = FOPWT;
  83.     else {
  84.         if (G.lrec.extra_field_length > EB_HEADSIZE) {
  85.             ush leb_id = makeword(&G.extra_field[EB_ID]);
  86.             ush leb_dlen = makeword(&G.extra_field[EB_LEN]);
  87.  
  88.             if ((leb_id == EF_VMCMS || leb_id == EF_MVS) &&
  89.                 (leb_dlen <= (G.lrec.extra_field_length - EB_HEADSIZE)) &&
  90.                 (getVMMVSexfield(type, G.extra_field, (unsigned)leb_dlen) > 0))
  91.                 mode = type;
  92.         }
  93.     }
  94.     if (mode == NULL) mode = FOPW;
  95.  
  96.     if ((G.outfile = fopen(G.filename, mode)) == (FILE *)NULL) {
  97.         Info(slide, 0x401, ((char *)slide, "\nerror:  cannot create %s\n",
  98.              G.filename));
  99.         return 1;
  100.     }
  101.     return 0;
  102. } /* end function open_outfile() */
  103.  
  104.  
  105. /****************************/
  106. /* Function close_outfile() */
  107. /****************************/
  108.  
  109. void close_outfile(__G)
  110.    __GDEF
  111. {
  112.    fclose(G.outfile);
  113. } /* end function close_outfile() */
  114.  
  115.  
  116. /******************************/
  117. /* Function getVMMVSexfield() */
  118. /******************************/
  119.  
  120. extent getVMMVSexfield(type, ef_block, datalen)
  121.     char *type;
  122.     uch *ef_block;
  123.     unsigned datalen;
  124. {
  125.     fldata_t *fdata = (fldata_t *) &ef_block[4];
  126.  
  127.     if (datalen < sizeof(fldata_t))
  128.         return 0;
  129.  
  130.     strcpy(type, "w");
  131.     strcat(type,  fdata->__openmode == __TEXT   ? ""
  132.                  :fdata->__openmode == __BINARY ? "b"
  133.                  :fdata->__openmode == __RECORD ? "b,type=record"
  134.                  : "");
  135.     strcat(type, ",recfm=");
  136.     strcat(type,  fdata->__recfmF? "F"
  137.                  :fdata->__recfmV? "V"
  138.                  :fdata->__recfmU? "U"
  139.                  :                 "?");
  140.     if (fdata->__recfmBlk) strcat(type, "B");
  141.     if (fdata->__recfmS)   strcat(type, "S");
  142.     if (fdata->__recfmASA) strcat(type, "A");
  143.     if (fdata->__recfmM)   strcat(type, "M");
  144.     sprintf(type+strlen(type), ",lrecl=%ld", fdata->__recfmV
  145.                                               ? fdata->__maxreclen+4
  146.                                               : fdata->__maxreclen);
  147.     sprintf(type+strlen(type), ",blksize=%ld", fdata->__blksize);
  148.  
  149.     return strlen(type);
  150. } /* end function getVMMVSexfield() */
  151.  
  152.  
  153.  
  154. #ifndef SFX
  155.  
  156. /**********************/
  157. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  158. /**********************/
  159.  
  160. char *do_wild(__G__ wld)
  161.     __GDEF
  162.     char *wld;             /* only used first time on a given dir */
  163. {
  164.     static int First = 0;
  165.     static char filename[256];
  166.  
  167.     if (First == 0) {
  168.        First = 1;
  169.        strcpy( filename, wld );
  170.        return filename;
  171.     }
  172.     else
  173.        return (char *)NULL;
  174.  
  175. } /* end function do_wild() */
  176.  
  177. #endif /* !SFX */
  178.  
  179.  
  180.  
  181. /************************/
  182. /*  Function mapattr()  */
  183. /************************/
  184.  
  185. int mapattr(__G)
  186.      __GDEF
  187. {
  188.     return 0;
  189. }
  190.  
  191. /************************/
  192. /*  Function mapname()  */
  193. /************************/
  194.  
  195. int mapname(__G__ renamed)
  196.             /* returns: */
  197.             /* 0 (PK_COOL) if no error, */
  198.             /* 1 (PK_WARN) if caution (filename trunc), */
  199.             /* 2 (PK_ERR)  if warning (skip file because dir doesn't exist), */
  200.             /* 3 (PK_BADERR) if error (skip file), */
  201.             /* 10 if no memory (skip file) */
  202.     __GDEF
  203.     int renamed;
  204. {
  205.     char newname[68], *lbar;
  206. #ifdef MVS
  207.     char *pmember;
  208. #endif
  209.     int name_changed = 0;
  210.  
  211. #ifdef MVS
  212.     while ((lbar = strrchr(G.filename,'_')) != NULL) {
  213.        strcpy(lbar,(lbar)+1);
  214.        name_changed = 1;
  215.     }
  216. #endif
  217.     while ((lbar = strrchr(G.filename,'+')) != NULL) {
  218.        strcpy(lbar,(lbar)+1);
  219.        name_changed = 1;
  220.     }
  221.     while ((lbar = strrchr(G.filename,'-')) != NULL) {
  222.        strcpy(lbar,(lbar)+1);
  223.        name_changed = 1;
  224.     }
  225.     while ((lbar = strrchr(G.filename,'(')) != NULL) {
  226.        strcpy(lbar,(lbar)+1);
  227.        name_changed = 1;
  228.     }
  229.     while ((lbar = strrchr(G.filename,')')) != NULL) {
  230.        strcpy(lbar,(lbar)+1);
  231.        name_changed = 1;
  232.     }
  233. #ifdef VM_CMS
  234.     if ((lbar = strrchr(G.filename,'/')) != NULL) {
  235.         strcpy((char *)newname,(char *)((lbar)+1));
  236.         printf("WARNING: file '%s' renamed as '%s'\n",G.filename,newname);
  237.         strcpy(G.filename,(char *)newname);
  238.         name_changed = 1;
  239.     }
  240. #else /* MVS */
  241.     if ((pmember = strrchr(G.filename,'/')) == NULL)
  242.         pmember = G.filename;
  243.     else
  244.         pmember++;
  245.  
  246.     /* search for extension in file name */
  247.     if ((lbar = strrchr(pmember,'.')) != NULL) {
  248.         *lbar++ = '\0';
  249.         strcpy(newname, pmember);
  250.         strcpy(pmember, lbar);
  251.         strcat(pmember, "(");
  252.         strcat(pmember, newname);
  253.         strcat(pmember, ")");
  254.     }
  255.  
  256.     /* Remove all `internal' dots '.', to prevent false consideration as
  257.      * MVS path delimiters! */
  258.     while ((lbar = strrchr(G.filename,'.')) != NULL) {
  259.         strcpy(lbar,(lbar)+1);
  260.         name_changed = 1;
  261.     }
  262.  
  263.     /* Finally, convert path delimiters from internal '/' to external '.' */
  264.     while ((lbar = strchr(G.filename,'/')) != NULL)
  265.         *lbar = '.';
  266. #endif /* ?VM_CMS */
  267. #ifndef MVS
  268.     if ((lbar = strchr(G.filename,'.')) == (char *)NULL) {
  269.         printf("WARNING: file '%s' has NO extension - renamed as '%s.NONAME'\n"\
  270.               ,G.filename,G.filename);
  271.        strcat(G.filename,".NONAME");
  272.        name_changed = 1;
  273.     }
  274. #endif
  275.     checkdir(__G__ G.filename, GETPATH);
  276.  
  277.     return name_changed;
  278.  
  279. } /* end function mapname() */
  280.  
  281.  
  282. int checkdir(__G__ pathcomp, flag)
  283.     __GDEF
  284.     char *pathcomp;
  285.     int flag;
  286. /*
  287.  * returns:  1 - (on APPEND_NAME) truncated filename
  288.  *           2 - path doesn't exist, not allowed to create
  289.  *           3 - path doesn't exist, tried to create and failed; or
  290.  *               path exists and is not a directory, but is supposed to be
  291.  *           4 - path is too long
  292.  *          10 - can't allocate memory for filename buffers
  293.  */
  294. {
  295.     static int rootlen = 0;      /* length of rootpath */
  296.     static char *rootpath;       /* user's "extract-to" directory */
  297.  
  298. #   define FN_MASK   7
  299. #   define FUNCTION  (flag & FN_MASK)
  300.  
  301. #if (!defined(SFX) || defined(SFX_EXDIR))
  302.     if (FUNCTION == ROOT) {
  303.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  304.         if (pathcomp == (char *)NULL) {
  305.             rootlen = 0;
  306.         }
  307.         else if ((rootlen = strlen(pathcomp)) > 0) {
  308.             if ((rootpath = (char *)malloc(rootlen+1)) == NULL) {
  309.                 rootlen = 0;
  310.                 return 10;
  311.             }
  312.             strcpy(rootpath, pathcomp);
  313.             Trace((stderr, "rootpath now = [%s]\n", rootpath));
  314.         }
  315.         return 0;
  316.     }
  317. #endif /* !SFX || SFX_EXDIR */
  318.  
  319. /*---------------------------------------------------------------------------
  320.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  321.     buildpath.
  322.   ---------------------------------------------------------------------------*/
  323.  
  324.     if (FUNCTION == GETPATH) {
  325.         if (rootlen > 0) {
  326. #ifdef VM_CMS                     /* put the exdir after the filename */
  327.            strcat(pathcomp,".");       /* used as minidisk to be save on  */
  328.            strcat(pathcomp,rootpath);
  329. #else /* MVS */
  330.            char newfilename[PATH_MAX];
  331.            char *start_fname;
  332.  
  333.            strcpy(newfilename,rootpath);
  334.            if (strchr(pathcomp,'(') == NULL) {
  335.               if ((start_fname = strrchr(pathcomp,'.')) == NULL) {
  336.                  start_fname = pathcomp;
  337.               }
  338.               else {
  339.                  *start_fname++ = '\0';
  340.                  strcat(newfilename, ".");
  341.                  strcat(newfilename, pathcomp);
  342.               }
  343.               strcat(newfilename,"(");
  344.               strcat(newfilename,start_fname);
  345.               strcat(newfilename,")");
  346.            }
  347.            else {
  348.               strcat(newfilename,".");
  349.               strcat(newfilename,pathcomp);
  350.            }
  351.            Trace((stdout, "new dataset : %s\n", newfilename));
  352.            strcpy(pathcomp,newfilename);
  353. #endif /* ?VM_CMS */
  354.         }
  355.         return 0;
  356.     }
  357.  
  358. /*---------------------------------------------------------------------------
  359.     END:  free rootpath, immediately prior to program exit.
  360.   ---------------------------------------------------------------------------*/
  361.  
  362.     if (FUNCTION == END) {
  363.         Trace((stderr, "freeing rootpath\n"));
  364.         if (rootlen > 0)
  365.             free(rootpath);
  366.         return 0;
  367.     }
  368.  
  369.     return 99;  /* should never reach */
  370.  
  371. } /* end function checkdir() */
  372.  
  373.  
  374. /******************************/
  375. /* Function check_for_newer() */  /* used for overwriting/freshening/updating */
  376. /******************************/
  377.  
  378. int check_for_newer(__G__ filename)  /* return 1 if existing file is newer */
  379.     __GDEF                           /*  or equal; 0 if older; -1 if doesn't */
  380.     char *filename;                  /*  exist yet */
  381. {
  382.     FILE *stream;
  383.  
  384.     if ((stream = fopen(filename, "r")) != (FILE *)NULL) {
  385.        fclose(stream);
  386.        /* File exists, assume it is "newer" than archive entry. */
  387.        return EXISTS_AND_NEWER;
  388.     }
  389.     /* File does not exist. */
  390.     return DOES_NOT_EXIST;
  391. } /* end function check_for_newer() */
  392.  
  393.  
  394. /*********************/
  395. /*  Function stat()  */
  396. /*********************/
  397.  
  398. int stat(const char *path, struct stat *buf)
  399. {
  400.    FILE *fp;
  401.    char fname[PATH_MAX];
  402.    time_t ltime;
  403.  
  404.    if ((fp = fopen(path, "r")) != NULL) {
  405.       fldata_t fdata;
  406.       if (fldata( fp, fname, &fdata ) == 0) {
  407.          buf->st_dev  = fdata.__device;
  408.          buf->st_mode = *(short *)(&fdata);
  409.       }
  410.  
  411.       /* Determine file size by seeking to EOF */
  412.       fseek(fp,0L,SEEK_END);
  413.       buf->st_size = ftell(fp);
  414.       fclose(fp);
  415.  
  416.       /* set time fields in stat buf to current time. */
  417.       time(<ime);
  418.       buf->st_atime =
  419.       buf->st_mtime =
  420.       buf->st_ctime = ltime;
  421.  
  422.       /* File exists, return success */
  423.       return 0;
  424.    }
  425.    return 1;
  426. }
  427.  
  428.  
  429.  
  430. #ifndef SFX
  431.  
  432. /************************/
  433. /*  Function version()  */
  434. /************************/
  435.  
  436. void version(__G)
  437.     __GDEF
  438. {
  439.     int len;
  440.  
  441.     len = sprintf((char *)slide, LoadFarString(CompiledWith),
  442.  
  443.       "C/370", " 2.1",
  444. #ifdef VM_CMS
  445.       "VM/CMS",
  446. #else
  447.       "MVS",
  448. #endif
  449.       "",
  450.  
  451. #ifdef __DATE__
  452.       " on ", __DATE__
  453. #else
  454.       "", ""
  455. #endif
  456.     );
  457.  
  458.     (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  459.  
  460. } /* end function version() */
  461.  
  462. #endif /* !SFX */
  463.