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

  1. /*---------------------------------------------------------------------------
  2.  
  3.   vms.c                                        Igor Mandrichenko and others
  4.  
  5.   This file contains routines to extract VMS file attributes from a zipfile
  6.   extra field and create a file with these attributes.  The code was almost
  7.   entirely written by Igor, with a couple of routines by CN and lots of
  8.   modifications by Christian Spieler.
  9.  
  10.   Contains:  check_format()
  11.              open_outfile()
  12.              find_vms_attrs()
  13.              flush()
  14.              close_outfile()
  15.              do_wild()
  16.              mapattr()
  17.              mapname()
  18.              checkdir()
  19.              check_for_newer()
  20.              return_VMS
  21.              screenlines()
  22.              version()
  23.  
  24.   ---------------------------------------------------------------------------
  25.  
  26.      Copyright (C) 1992-93 Igor Mandrichenko.
  27.      Permission is granted to any individual or institution to use, copy,
  28.      or redistribute this software so long as all of the original files
  29.      are included unmodified and that this copyright notice is retained.
  30.  
  31.   Revision history:
  32.  
  33.      1.x   [moved to History.510 for brevity]
  34.      2.x   [moved to History.513 for brevity]
  35.      3.0   Cave Newt           4 Sep 94
  36.             relocated version history; renamed secinf to X_flag
  37.  
  38.   ---------------------------------------------------------------------------*/
  39.  
  40. #ifdef VMS                      /* VMS only! */
  41.  
  42. #define UNZIP_INTERNAL
  43. #include "unzip.h"
  44. #include "vms.h"
  45. #include "vmsdefs.h"
  46. #include <lib$routines.h>
  47. #include <unixlib.h>
  48.  
  49. #define BUFS512 8192*2          /* Must be a multiple of 512 */
  50.  
  51. #define OK(s)   ((s)&1)         /* VMS success or warning status */
  52. #define STRICMP(s1,s2)  STRNICMP(s1,s2,2147483647)
  53.  
  54. /*
  55. *   Local static storage
  56. */
  57. static struct FAB       fileblk;
  58. static struct XABDAT    dattim;
  59. static struct XABRDT    rdt;
  60. static struct RAB       rab;
  61. static struct NAM       nam;
  62.  
  63. static struct FAB *outfab = NULL;
  64. static struct RAB *outrab = NULL;
  65. static struct XABFHC *xabfhc = NULL;
  66. static struct XABDAT *xabdat = NULL;
  67. static struct XABRDT *xabrdt = NULL;
  68. static struct XABPRO *xabpro = NULL;
  69. static struct XABKEY *xabkey = NULL;
  70. static struct XABALL *xaball = NULL;
  71. static struct XAB *first_xab = NULL, *last_xab = NULL;
  72.  
  73. static char query = 0;
  74. static int  text_output = 0,
  75.             bin_fixed = 0;
  76. #ifdef USE_ORIG_DOS
  77. static int  hostnum;
  78. #endif
  79.  
  80. static uch rfm;
  81.  
  82. static uch locbuf[BUFS512];
  83. static int loccnt = 0;
  84. static uch *locptr;
  85. static char got_eol = 0;
  86.  
  87. static int  _flush_blocks(__GPRO__ uch *rawbuf, unsigned size, int final_flag),
  88.             _flush_stream(__GPRO__ uch *rawbuf, unsigned size, int final_flag),
  89.             _flush_varlen(__GPRO__ uch *rawbuf, unsigned size, int final_flag),
  90.             _flush_qio(__GPRO__ uch *rawbuf, unsigned size, int final_flag),
  91.             _close_rms(__GPRO),
  92.             _close_qio(__GPRO),
  93.             WriteBuffer(__GPRO__ uch *buf, int len),
  94.             WriteRecord(__GPRO__ uch *rec, int len),
  95.             find_eol(uch *p, int n, int *l);
  96.  
  97. static int  (*_flush_routine)(__GPRO__ uch *rawbuf, unsigned size,
  98.                               int final_flag),
  99.             (*_close_routine)(__GPRO);
  100.  
  101. static void init_buf_ring(void);
  102. static void set_default_datetime_XABs(__GPRO);
  103. static int  replace(__GPRO);
  104. static void free_up(void);
  105. #ifdef CHECK_VERSIONS
  106. static int  get_vms_version(char *verbuf, int len);
  107. #endif /* CHECK_VERSIONS */
  108. static uch  *extract_block(__GPRO__ struct IZ_block *p, int *retlen,
  109.                            uch *init, int needlen);
  110. static void decompress_bits(uch *outptr, int needlen, uch *bitptr);
  111. static void vms_msg(__GPRO__ char *string, int status);
  112.  
  113. struct bufdsc
  114. {
  115.     struct bufdsc *next;
  116.     uch *buf;
  117.     int bufcnt;
  118. };
  119.  
  120. static struct bufdsc b1, b2, *curbuf;
  121. static uch buf1[BUFS512];
  122.  
  123. int check_format(__G)
  124.     __GDEF
  125. {
  126.     int rtype;
  127.     struct FAB fab;
  128.  
  129.     fab = cc$rms_fab;
  130.     fab.fab$l_fna = G.zipfn;
  131.     fab.fab$b_fns = strlen(G.zipfn);
  132.  
  133.     if ((sys$open(&fab) & 1) == 0)
  134.     {
  135.         Info(slide, 1, ((char *)slide, "\n\
  136.      error:  cannot open zipfile [ %s ] (access denied?).\n\n",
  137.           G.zipfn));
  138.         return PK_ERR;
  139.     }
  140.     rtype = fab.fab$b_rfm;
  141.     sys$close(&fab);
  142.  
  143.     if (rtype == FAB$C_VAR || rtype == FAB$C_VFC)
  144.     {
  145.         Info(slide, 1, ((char *)slide, "\n\
  146.      Error:  zipfile is in variable-length record format.  Please\n\
  147.      run \"bilf l %s\" to convert the zipfile to stream-LF\n\
  148.      record format.  (BILF is available at various VMS archives.)\n\n",
  149.           G.zipfn));
  150.         return PK_ERR;
  151.     }
  152.  
  153.     return PK_COOL;
  154. }
  155.  
  156.  
  157.  
  158. #define PRINTABLE_FORMAT(x)      ( (x) == FAB$C_VAR     \
  159.                                 || (x) == FAB$C_STMLF   \
  160.                                 || (x) == FAB$C_STMCR   \
  161.                                 || (x) == FAB$C_STM     )
  162.  
  163. /* VMS extra field types */
  164. #define VAT_NONE    0
  165. #define VAT_IZ      1   /* old INFO-ZIP format */
  166. #define VAT_PK      2   /* PKWARE format */
  167.  
  168. static int  vet;
  169.  
  170. static int  create_default_output(__GPRO),
  171.             create_rms_output(__GPRO),
  172.             create_qio_output(__GPRO);
  173.  
  174. /*
  175.  *  open_outfile() assignments:
  176.  *
  177.  *  VMS attributes ?        create_xxx      _flush_xxx
  178.  *  ----------------        ----------      ----------
  179.  *  not found               'default'       text mode ?
  180.  *                                          yes -> 'stream'
  181.  *                                          no  -> 'block'
  182.  *
  183.  *  yes, in IZ format       'rms'           G.cflag ?
  184.  *                                          yes -> switch(fab.rfm)
  185.  *                                              VAR  -> 'varlen'
  186.  *                                              STM* -> 'stream'
  187.  *                                              default -> 'block'
  188.  *                                          no -> 'block'
  189.  *
  190.  *  yes, in PK format       'qio'           G.cflag ?
  191.  *                                          yes -> switch(pka_rattr)
  192.  *                                              VAR  -> 'varlen'
  193.  *                                              STM* -> 'stream'
  194.  *                                              default -> 'block'
  195.  *                                          no -> 'qio'
  196.  *
  197.  *  "text mode" == G.pInfo->textmode || G.cflag
  198.  */
  199.  
  200. int open_outfile(__G)           /* return 1 (PK_WARN) if fail */
  201.     __GDEF
  202. {
  203.     switch(vet = find_vms_attrs(__G))
  204.     {
  205.         case VAT_NONE:
  206.         default:
  207.             return  create_default_output(__G);
  208.         case VAT_IZ:
  209.             return  create_rms_output(__G);
  210.         case VAT_PK:
  211.             return  create_qio_output(__G);
  212.     }
  213. }
  214.  
  215. static void init_buf_ring()
  216. {
  217.     locptr = &locbuf[0];
  218.     loccnt = 0;
  219.  
  220.     b1.buf = &locbuf[0];
  221.     b1.bufcnt = 0;
  222.     b1.next = &b2;
  223.     b2.buf = &buf1[0];
  224.     b2.bufcnt = 0;
  225.     b2.next = &b1;
  226.     curbuf = &b1;
  227. }
  228.  
  229.  
  230. /* Static data storage for time conversion: */
  231.  
  232. /*   string constants for month names */
  233. static const char *month[] =
  234.             {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  235.              "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
  236.  
  237. /*   buffer for time string */
  238. static char timbuf[24];         /* length = first entry in "stupid" + 1 */
  239.  
  240. /*   fixed-length string descriptor for timbuf: */
  241. static const struct dsc$descriptor stupid =
  242.             {sizeof(timbuf)-1, DSC$K_DTYPE_T, DSC$K_CLASS_S, timbuf};
  243.  
  244.  
  245. static void set_default_datetime_XABs(__GPRO)
  246. {
  247.     unsigned yr, mo, dy, hh, mm, ss;
  248. #ifdef USE_EF_UX_TIME
  249.     ztimbuf z_utime;
  250.  
  251.     if (G.extra_field &&
  252.         ef_scan_for_izux(G.extra_field, G.crec.extra_field_length,
  253.                          &z_utime, NULL) > 0)
  254.     {
  255.         struct tm *t = localtime(&(z_utime.modtime));
  256.  
  257.         yr = t->tm_year + 1900;
  258.         mo = t->tm_mon;
  259.         dy = t->tm_mday;
  260.         hh = t->tm_hour;
  261.         mm = t->tm_min;
  262.         ss = t->tm_sec;
  263.     }
  264.     else
  265.     {
  266.         yr = ((G.lrec.last_mod_file_date >> 9) & 0x7f) + 1980;
  267.         mo = ((G.lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  268.         dy = (G.lrec.last_mod_file_date & 0x1f);
  269.         hh = (G.lrec.last_mod_file_time >> 11) & 0x1f;
  270.         mm = (G.lrec.last_mod_file_time >> 5) & 0x3f;
  271.         ss = (G.lrec.last_mod_file_time & 0x1f) * 2;
  272.     }
  273. #else /* !USE_EF_UX_TIME */
  274.  
  275.     yr = ((G.lrec.last_mod_file_date >> 9) & 0x7f) + 1980;
  276.     mo = ((G.lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  277.     dy = (G.lrec.last_mod_file_date & 0x1f);
  278.     hh = (G.lrec.last_mod_file_time >> 11) & 0x1f;
  279.     mm = (G.lrec.last_mod_file_time >> 5) & 0x3f;
  280.     ss = (G.lrec.last_mod_file_time & 0x1f) * 2;
  281. #endif /* ?USE_EF_UX_TIME */
  282.  
  283.     dattim = cc$rms_xabdat;     /* fill XABs with default values */
  284.     rdt = cc$rms_xabrdt;
  285.     sprintf(timbuf, "%02u-%3s-%04u %02u:%02u:%02u.00", dy, month[mo],
  286.             yr, hh, mm, ss);
  287.     sys$bintim(&stupid, &dattim.xab$q_cdt);
  288.     memcpy(&rdt.xab$q_rdt, &dattim.xab$q_cdt, sizeof(rdt.xab$q_rdt));
  289. }
  290.  
  291.  
  292. static int create_default_output(__GPRO)        /* return 1 (PK_WARN) if fail */
  293. {
  294.     int ierr;
  295.  
  296.     text_output = G.pInfo->textmode ||
  297.                   G.cflag;      /* extract the file in text
  298.                                  * (variable-length) format */
  299.     bin_fixed = text_output || (G.bflag == 0)
  300.                 ? 0
  301.                 : ((G.bflag - 1) ? 1 : !G.pInfo->textfile);
  302. #ifdef USE_ORIG_DOS
  303.     hostnum = G.pInfo -> hostnum;
  304. #endif
  305.  
  306.     rfm = FAB$C_STMLF;  /* Default, stream-LF format from VMS or UNIX */
  307.  
  308.     if (!G.cflag)               /* Redirect output */
  309.     {
  310.         rab = cc$rms_rab;       /* fill RAB with default values */
  311.         fileblk = cc$rms_fab;   /* fill FAB with default values */
  312.  
  313.         outfab = &fileblk;
  314.         outfab->fab$l_xab = NULL;
  315.  
  316.         if (text_output)
  317.         {   /* Default format for output `real' text file */
  318.  
  319.             outfab->fab$b_rfm = FAB$C_VAR;      /* variable length records */
  320.             outfab->fab$b_rat = FAB$M_CR;       /* implied (CR) carriage ctrl */
  321.         }
  322.         else if (bin_fixed)
  323.         {   /* Default format for output `real' binary file */
  324.  
  325.             outfab->fab$b_rfm = FAB$C_FIX;      /* fixed length record format */
  326.             outfab->fab$w_mrs = 512;            /* record size 512 bytes */
  327.             outfab->fab$b_rat = 0;              /* no carriage ctrl */
  328.         }
  329.         else
  330.         {   /* Default format for output misc (bin or text) file */
  331.  
  332.             outfab->fab$b_rfm = FAB$C_STMLF;    /* stream-LF record format */
  333.             outfab->fab$b_rat = FAB$M_CR;       /* implied (CR) carriage ctrl */
  334.         }
  335.  
  336.         outfab->fab$l_fna = G.filename;
  337.         outfab->fab$b_fns = strlen(outfab->fab$l_fna);
  338.  
  339.         {
  340.             set_default_datetime_XABs(__G);
  341.  
  342.             dattim.xab$l_nxt = outfab->fab$l_xab;
  343.             outfab->fab$l_xab = (void *) &dattim;
  344.         }
  345.  
  346.         outfab->fab$w_ifi = 0;  /* Clear IFI. It may be nonzero after ZIP */
  347.  
  348.         ierr = sys$create(outfab);
  349.         if (ierr == RMS$_FEX)
  350.             ierr = replace(__G);
  351.  
  352.         if (ierr == 0)          /* Canceled */
  353.             return (free_up(), PK_WARN);
  354.  
  355.         if (ERR(ierr))
  356.         {
  357.             char buf[256];
  358.  
  359.             sprintf(buf, "[ Cannot create output file %s ]\n", G.filename);
  360.             vms_msg(__G__ buf, ierr);
  361.             vms_msg(__G__ "", outfab->fab$l_stv);
  362.             free_up();
  363.             return PK_WARN;
  364.         }
  365.  
  366.         if (!text_output)    /* Do not reopen text files and stdout
  367.                               *  Just open them in right mode         */
  368.         {
  369.             /*
  370.              *      Reopen file for Block I/O with no XABs.
  371.              */
  372.             if ((ierr = sys$close(outfab)) != RMS$_NORMAL)
  373.             {
  374. #ifdef DEBUG
  375.                 vms_msg(__G__ "[ create_default_output: sys$close failed ]\n",
  376.                         ierr);
  377.                 vms_msg(__G__ "", outfab->fab$l_stv);
  378. #endif
  379.                 Info(slide, 1, ((char *)slide,
  380.                      "Can't create output file:  %s\n", G.filename));
  381.                 free_up();
  382.                 return PK_WARN;
  383.             }
  384.  
  385.  
  386.             outfab->fab$b_fac = FAB$M_BIO | FAB$M_PUT;  /* Get ready for block
  387.                                                          * output */
  388.             outfab->fab$l_xab = NULL;   /* Unlink all XABs */
  389.  
  390.             if ((ierr = sys$open(outfab)) != RMS$_NORMAL)
  391.             {
  392.                 char buf[256];
  393.  
  394.                 sprintf(buf, "[ Cannot open output file %s ]\n", G.filename);
  395.                 vms_msg(__G__ buf, ierr);
  396.                 vms_msg(__G__ "", outfab->fab$l_stv);
  397.                 free_up();
  398.                 return PK_WARN;
  399.             }
  400.         }
  401.  
  402.         outrab = &rab;
  403.         rab.rab$l_fab = outfab;
  404.         if (!text_output)
  405.         {
  406.             rab.rab$l_rop |= RAB$M_BIO;
  407.             rab.rab$l_rop |= RAB$M_ASY;
  408.         }
  409.         rab.rab$b_rac = RAB$C_SEQ;
  410.  
  411.         if ((ierr = sys$connect(outrab)) != RMS$_NORMAL)
  412.         {
  413. #ifdef DEBUG
  414.             vms_msg(__G__ "create_default_output: sys$connect failed.\n", ierr);
  415.             vms_msg(__G__ "", outfab->fab$l_stv);
  416. #endif
  417.             Info(slide, 1, ((char *)slide,
  418.                  "Can't create output file:  %s\n", G.filename));
  419.             free_up();
  420.             return PK_WARN;
  421.         }
  422.     }                   /* end if (!G.cflag) */
  423.  
  424.     init_buf_ring();
  425.  
  426.     _flush_routine = text_output? got_eol=0,_flush_stream : _flush_blocks;
  427.     _close_routine = _close_rms;
  428.     return PK_COOL;
  429. }
  430.  
  431.  
  432.  
  433. static int create_rms_output(__GPRO)           /* return 1 (PK_WARN) if fail */
  434. {
  435.     int ierr;
  436.  
  437.     text_output = G.cflag;      /* extract the file in text
  438.                                  * (variable-length) format;
  439.                                  * we ignore "-a" when attributes saved */
  440. #ifdef USE_ORIG_DOS
  441.     hostnum = G.pInfo -> hostnum;
  442. #endif
  443.  
  444.     rfm = outfab->fab$b_rfm;    /* Use record format from VMS extra field */
  445.  
  446.     if (G.cflag)
  447.     {
  448.         if (!PRINTABLE_FORMAT(rfm))
  449.         {
  450.             Info(slide, 1, ((char *)slide,
  451.                "[ File %s has illegal record format to put to screen ]\n",
  452.                G.filename));
  453.             free_up();
  454.             return PK_WARN;
  455.         }
  456.     }
  457.     else                        /* Redirect output */
  458.     {
  459.         rab = cc$rms_rab;       /* fill RAB with default values */
  460.  
  461.         /* The output FAB has already been initialized with the values
  462.          * found in the Zip file's "VMS attributes" extra field */
  463.  
  464.         outfab->fab$l_fna = G.filename;
  465.         outfab->fab$b_fns = strlen(outfab->fab$l_fna);
  466.  
  467.         if (!(xabdat && xabrdt))        /* Use date/time info
  468.                                          *  from zipfile if
  469.                                          *  no attributes given
  470.                                          */
  471.         {
  472.             set_default_datetime_XABs(__G);
  473.  
  474.             if (xabdat == NULL)
  475.             {
  476.                 dattim.xab$l_nxt = outfab->fab$l_xab;
  477.                 outfab->fab$l_xab = (void *) &dattim;
  478.             }
  479.         }
  480.  
  481.         outfab->fab$w_ifi = 0;  /* Clear IFI. It may be nonzero after ZIP */
  482.  
  483.         ierr = sys$create(outfab);
  484.         if (ierr == RMS$_FEX)
  485.             ierr = replace(__G);
  486.  
  487.         if (ierr == 0)          /* Canceled */
  488.             return (free_up(), PK_WARN);
  489.  
  490.         if (ERR(ierr))
  491.         {
  492.             char buf[256];
  493.  
  494.             sprintf(buf, "[ Cannot create output file %s ]\n", G.filename);
  495.             vms_msg(__G__ buf, ierr);
  496.             vms_msg(__G__ "", outfab->fab$l_stv);
  497.             free_up();
  498.             return PK_WARN;
  499.         }
  500.  
  501.         if (!text_output)    /* Do not reopen text files and stdout
  502.                               *  Just open them in right mode         */
  503.         {
  504.             /*
  505.              *      Reopen file for Block I/O with no XABs.
  506.              */
  507.             if ((ierr = sys$close(outfab)) != RMS$_NORMAL)
  508.             {
  509. #ifdef DEBUG
  510.                 vms_msg(__G__ "[ create_rms_output: sys$close failed ]\n",
  511.                         ierr);
  512.                 vms_msg(__G__ "", outfab->fab$l_stv);
  513. #endif
  514.                 Info(slide, 1, ((char *)slide,
  515.                      "Can't create output file:  %s\n", G.filename));
  516.                 free_up();
  517.                 return PK_WARN;
  518.             }
  519.  
  520.  
  521.             outfab->fab$b_fac = FAB$M_BIO | FAB$M_PUT;  /* Get ready for block
  522.                                                          * output */
  523.             outfab->fab$l_xab = NULL;   /* Unlink all XABs */
  524.  
  525.             if ((ierr = sys$open(outfab)) != RMS$_NORMAL)
  526.             {
  527.                 char buf[256];
  528.  
  529.                 sprintf(buf, "[ Cannot open output file %s ]\n", G.filename);
  530.                 vms_msg(__G__ buf, ierr);
  531.                 vms_msg(__G__ "", outfab->fab$l_stv);
  532.                 free_up();
  533.                 return PK_WARN;
  534.             }
  535.         }
  536.  
  537.         outrab = &rab;
  538.         rab.rab$l_fab = outfab;
  539.         if (!text_output)
  540.         {
  541.             rab.rab$l_rop |= RAB$M_BIO;
  542.             rab.rab$l_rop |= RAB$M_ASY;
  543.         }
  544.         rab.rab$b_rac = RAB$C_SEQ;
  545.  
  546.         if ((ierr = sys$connect(outrab)) != RMS$_NORMAL)
  547.         {
  548. #ifdef DEBUG
  549.             vms_msg(__G__ "create_rms_output: sys$connect failed.\n", ierr);
  550.             vms_msg(__G__ "", outfab->fab$l_stv);
  551. #endif
  552.             Info(slide, 1, ((char *)slide,
  553.                  "Can't create output file:  %s\n", G.filename));
  554.             free_up();
  555.             return PK_WARN;
  556.         }
  557.     }                   /* end if (!G.cflag) */
  558.  
  559.     init_buf_ring();
  560.  
  561.     if ( text_output )
  562.         switch(rfm)
  563.         {
  564.             case FAB$C_VAR:
  565.                 _flush_routine = _flush_varlen;
  566.                 break;
  567.             case FAB$C_STM:
  568.             case FAB$C_STMCR:
  569.             case FAB$C_STMLF:
  570.                 _flush_routine = _flush_stream;
  571.                 got_eol = 0;
  572.                 break;
  573.             default:
  574.                 _flush_routine = _flush_blocks;
  575.                 break;
  576.         }
  577.     else
  578.         _flush_routine = _flush_blocks;
  579.     _close_routine = _close_rms;
  580.     return PK_COOL;
  581. }
  582.  
  583.  
  584.  
  585. static  int pka_devchn;
  586. static  int pka_vbn;
  587.  
  588. static struct
  589. {
  590.     short   status;
  591.     long    count;
  592.     short   dummy;
  593. } pka_io_sb;
  594.  
  595. static struct
  596. {
  597.     short   status;
  598.     short   dummy;
  599.     void    *addr;
  600. } pka_acp_sb;
  601.  
  602. static struct fibdef    pka_fib;
  603. static struct atrdef    pka_atr[VMS_MAX_ATRCNT];
  604. static int              pka_idx;
  605. static ulg              pka_uchar;
  606. static struct fatdef    pka_rattr;
  607.  
  608. static struct dsc$descriptor    pka_fibdsc =
  609. {   sizeof(pka_fib), DSC$K_DTYPE_Z, DSC$K_CLASS_S, (void *) &pka_fib  };
  610.  
  611. static struct dsc$descriptor_s  pka_devdsc =
  612. {   0, DSC$K_DTYPE_T, DSC$K_CLASS_S, &nam.nam$t_dvi[1]  };
  613.  
  614. static struct dsc$descriptor_s  pka_fnam =
  615. {   0, DSC$K_DTYPE_T, DSC$K_CLASS_S, NULL  };
  616.  
  617. #define PK_PRINTABLE_RECTYP(x)   ( (x) == FAT$C_VARIABLE \
  618.                                 || (x) == FAT$C_STREAMLF \
  619.                                 || (x) == FAT$C_STREAMCR \
  620.                                 || (x) == FAT$C_STREAM   )
  621.  
  622.  
  623. static int create_qio_output(__GPRO)            /* return 1 (PK_WARN) if fail */
  624. {
  625.     int status;
  626.     static char exp_nam[NAM$C_MAXRSS];
  627.     static char res_nam[NAM$C_MAXRSS];
  628.     int i;
  629.  
  630.     if ( G.cflag )
  631.     {
  632.         int rtype = pka_rattr.fat$v_rtype;
  633.         if (!PK_PRINTABLE_RECTYP(rtype))
  634.         {
  635.             Info(slide, 1, ((char *)slide,
  636.                "[ File %s has illegal record format to put to screen ]\n",
  637.                G.filename));
  638.             return PK_WARN;
  639.         }
  640.  
  641.         init_buf_ring();
  642.  
  643.         switch(rtype)
  644.         {
  645.             case FAT$C_VARIABLE:
  646.                 _flush_routine = _flush_varlen;
  647.                 break;
  648.             case FAT$C_STREAM:
  649.             case FAT$C_STREAMCR:
  650.             case FAT$C_STREAMLF:
  651.                 _flush_routine = _flush_stream;
  652.                 got_eol = 0;
  653.                 break;
  654.             default:
  655.                 _flush_routine = _flush_blocks;
  656.                 break;
  657.         }
  658.         _close_routine = _close_rms;
  659.     }
  660.     else                        /* !(G.cflag) : redirect output */
  661.     {
  662.  
  663.         fileblk = cc$rms_fab;
  664.         fileblk.fab$l_fna = G.filename;
  665.         fileblk.fab$b_fns = strlen(G.filename);
  666.  
  667.         nam = cc$rms_nam;
  668.         fileblk.fab$l_nam = &nam;
  669.         nam.nam$l_esa = exp_nam;
  670.         nam.nam$b_ess = sizeof(exp_nam);
  671.         nam.nam$l_rsa = res_nam;
  672.         nam.nam$b_rss = sizeof(res_nam);
  673.  
  674.         if ( ERR(status = sys$parse(&fileblk)) )
  675.         {
  676.             vms_msg(__G__ "create_qio_output: sys$parse failed.\n", status);
  677.             return PK_WARN;
  678.         }
  679.  
  680.         pka_devdsc.dsc$w_length = (unsigned short)nam.nam$t_dvi[0];
  681.  
  682.         if ( ERR(status = sys$assign(&pka_devdsc,&pka_devchn,0,0)) )
  683.         {
  684.             vms_msg(__G__ "sys$assign failed.\n",status);
  685.             return PK_WARN;
  686.         }
  687.  
  688.         pka_fnam.dsc$a_pointer = nam.nam$l_name;
  689.         pka_fnam.dsc$w_length  = nam.nam$b_name + nam.nam$b_type;
  690.         if ( G.V_flag /* keep versions */ )
  691.             pka_fnam.dsc$w_length += nam.nam$b_ver;
  692.  
  693.         for (i=0;i<3;i++)
  694.         {
  695.             pka_fib.FIB$W_DID[i]=nam.nam$w_did[i];
  696.             pka_fib.FIB$W_FID[i]=0;
  697.         }
  698.  
  699.         pka_fib.FIB$L_ACCTL = FIB$M_WRITE;
  700.         /* Allocate space for the file */
  701.         pka_fib.FIB$W_EXCTL = FIB$M_EXTEND;
  702.         if ( pka_uchar & FCH$M_CONTIG )
  703.             pka_fib.FIB$W_EXCTL |= FIB$M_ALCON | FIB$M_FILCON;
  704.         if ( pka_uchar & FCH$M_CONTIGB )
  705.             pka_fib.FIB$W_EXCTL |= FIB$M_ALCONB;
  706.  
  707. #define SWAPW(x)        ( (((x)>>16)&0xFFFF) + ((x)<<16) )
  708.  
  709.         pka_fib.fib$l_exsz = SWAPW(pka_rattr.fat$l_hiblk);
  710.  
  711.         status = sys$qiow(0, pka_devchn, IO$_CREATE|IO$M_CREATE|IO$M_ACCESS,
  712.                           &pka_acp_sb, 0, 0,
  713.                           &pka_fibdsc, &pka_fnam, 0, 0, &pka_atr, 0);
  714.  
  715.         if ( !ERR(status) )
  716.             status = pka_acp_sb.status;
  717.  
  718.         if ( ERR(status) )
  719.         {
  720.             vms_msg(__G__ "[ Create file QIO failed.\n",status);
  721.             return PK_WARN;
  722.             sys$dassgn(pka_devchn);
  723.         }
  724.  
  725.         locptr = locbuf;
  726.         loccnt = 0;
  727.         pka_vbn = 1;
  728.         _flush_routine = _flush_qio;
  729.         _close_routine = _close_qio;
  730.     }                   /* end if (!G.cflag) */
  731.     return PK_COOL;
  732. }
  733.  
  734.  
  735.  
  736. static int replace(__GPRO)
  737. {                       /*
  738.                          *      File exists. Inquire user about further action.
  739.                          */
  740.     char answ[10];
  741.     struct NAM nam;
  742.     int ierr;
  743.  
  744.     if (query == 0)
  745.     {
  746.         do
  747.         {
  748.             Info(slide, 1, ((char *)slide,
  749.                  "%s exists:  [o]verwrite, new [v]ersion or [n]o extract?\n\
  750.   (uppercase response [O,V,N] = do same for all files): ",
  751.                  G.filename));
  752.             fflush(stderr);
  753.         } while (fgets(answ, 9, stderr) == NULL && !isalpha(answ[0])
  754.                  && tolower(answ[0]) != 'o'
  755.                  && tolower(answ[0]) != 'v'
  756.                  && tolower(answ[0]) != 'n');
  757.  
  758.         if (isupper(answ[0]))
  759.             query = answ[0] = tolower(answ[0]);
  760.     }
  761.     else
  762.         answ[0] = query;
  763.  
  764.     switch (answ[0])
  765.     {
  766.         case 'n':
  767.             ierr = 0;
  768.             break;
  769.         case 'v':
  770.             nam = cc$rms_nam;
  771.             nam.nam$l_rsa = G.filename;
  772.             nam.nam$b_rss = FILNAMSIZ - 1;
  773.  
  774.             outfab->fab$l_fop |= FAB$M_MXV;
  775.             outfab->fab$l_nam = &nam;
  776.  
  777.             ierr = sys$create(outfab);
  778.             if (!ERR(ierr))
  779.             {
  780.                 outfab->fab$l_nam = NULL;
  781.                 G.filename[outfab->fab$b_fns = nam.nam$b_rsl] = '\0';
  782.             }
  783.             break;
  784.         case 'o':
  785.             outfab->fab$l_fop |= FAB$M_SUP;
  786.             ierr = sys$create(outfab);
  787.             break;
  788.     }
  789.     return ierr;
  790. }
  791.  
  792.  
  793.  
  794. #define W(p)    (*(unsigned short*)(p))
  795. #define L(p)    (*(unsigned long*)(p))
  796. #define EQL_L(a,b)      ( L(a) == L(b) )
  797. #define EQL_W(a,b)      ( W(a) == W(b) )
  798.  
  799. /****************************************************************
  800.  * Function find_vms_attrs scans ZIP entry extra field if any   *
  801.  * and looks for VMS attribute records. Returns 0 if either no  *
  802.  * attributes found or no fab given.                            *
  803.  ****************************************************************/
  804. int find_vms_attrs(__G)
  805.     __GDEF
  806. {
  807.     uch *scan = G.extra_field;
  808.     struct  EB_header *hdr;
  809.     int len;
  810.     int type=VAT_NONE;
  811.  
  812.     outfab = NULL;
  813.     xabfhc = NULL;
  814.     xabdat = NULL;
  815.     xabrdt = NULL;
  816.     xabpro = NULL;
  817.     first_xab = last_xab = NULL;
  818.  
  819.     if (scan == NULL)
  820.         return VAT_NONE;
  821.     len = G.lrec.extra_field_length;
  822.  
  823. #define LINK(p) {/* Link xaballs and xabkeys into chain */      \
  824.                 if ( first_xab == NULL )                \
  825.                         first_xab = (void *) p;         \
  826.                 if ( last_xab != NULL )                 \
  827.                         last_xab -> xab$l_nxt = (void *) p;     \
  828.                 last_xab = (void *) p;                  \
  829.                 p -> xab$l_nxt = NULL;                  \
  830.         }
  831.     /* End of macro LINK */
  832.  
  833.     while (len > 0)
  834.     {
  835.         hdr = (struct EB_header *) scan;
  836.         if (EQL_W(&hdr->tag, IZ_SIGNATURE))
  837.         {
  838.             /*
  839.              *  INFO-ZIP style extra block decoding
  840.              */
  841.             struct IZ_block *blk;
  842.             uch *block_id;
  843.  
  844.             type = VAT_IZ;
  845.  
  846.             blk = (struct IZ_block *)hdr;
  847.             block_id = (uch *) &blk->bid;
  848.             if (EQL_L(block_id, FABSIG))
  849.             {
  850.                 outfab = (struct FAB *) extract_block(__G__ blk, 0,
  851.                                                 (uch *)&cc$rms_fab, FABL);
  852.             }
  853.             else if (EQL_L(block_id, XALLSIG))
  854.             {
  855.                 xaball = (struct XABALL *) extract_block(__G__ blk, 0,
  856.                                                 (uch *)&cc$rms_xaball, XALLL);
  857.                 LINK(xaball);
  858.             }
  859.             else if (EQL_L(block_id, XKEYSIG))
  860.             {
  861.                 xabkey = (struct XABKEY *) extract_block(__G__ blk, 0,
  862.                                                 (uch *)&cc$rms_xabkey, XKEYL);
  863.                 LINK(xabkey);
  864.             }
  865.             else if (EQL_L(block_id, XFHCSIG))
  866.             {
  867.                 xabfhc = (struct XABFHC *) extract_block(__G__ blk, 0,
  868.                                                 (uch *)&cc$rms_xabfhc, XFHCL);
  869.             }
  870.             else if (EQL_L(block_id, XDATSIG))
  871.             {
  872.                 xabdat = (struct XABDAT *) extract_block(__G__ blk, 0,
  873.                                                 (uch *)&cc$rms_xabdat, XDATL);
  874.             }
  875.             else if (EQL_L(block_id, XRDTSIG))
  876.             {
  877.                 xabrdt = (struct XABRDT *) extract_block(__G__ blk, 0,
  878.                                                 (uch *)&cc$rms_xabrdt, XRDTL);
  879.             }
  880.             else if (EQL_L(block_id, XPROSIG))
  881.             {
  882.                 xabpro = (struct XABPRO *) extract_block(__G__ blk, 0,
  883.                                                 (uch *)&cc$rms_xabpro, XPROL);
  884.             }
  885.             else if (EQL_L(block_id, VERSIG))
  886.             {
  887. #ifdef CHECK_VERSIONS
  888.                 char verbuf[80];
  889.                 int verlen = 0;
  890.                 uch *vers;
  891.                 char *m;
  892.  
  893.                 get_vms_version(verbuf, sizeof(verbuf));
  894.                 vers = extract_block(__G__ blk, &verlen, 0, 0);
  895.                 if ((m = strrchr((char *) vers, '-')) != NULL)
  896.                     *m = '\0';  /* Cut out release number */
  897.                 if (strcmp(verbuf, (char *) vers) && G.qflag < 2)
  898.                 {
  899.                     Info(slide, 0, ((char *)slide,
  900.                          "[ Warning: VMS version mismatch."));
  901.  
  902.                     Info(slide, 0, ((char *)slide,
  903.                          "   This version %s --", verbuf));
  904.                     strncpy(verbuf, (char *) vers, verlen);
  905.                     verbuf[verlen] = '\0';
  906.                     Info(slide, 0, ((char *)slide,
  907.                          " version made by %s ]\n", verbuf));
  908.                 }
  909.                 free(vers);
  910. #endif /* CHECK_VERSIONS */
  911.             }
  912.             else
  913.                 Info(slide, 1, ((char *)slide,
  914.                      "[ Warning: Unknown block signature %s ]\n",
  915.                      block_id));
  916.         }
  917.         else if (hdr->tag == PK_SIGNATURE)
  918.         {
  919.             /*
  920.              *  PKWARE style extra block decoding
  921.              */
  922.             struct  PK_header   *blk;
  923.             register byte   *scn;
  924.             register int    len;
  925.  
  926.             type = VAT_PK;
  927.  
  928.             blk = (struct PK_header *)hdr;
  929.             len = blk->size - (PK_HEADER_SIZE - EB_HEADSIZE);
  930.             scn = (byte *)(&blk->data);
  931.             pka_idx = 0;
  932.  
  933.             if (blk->crc32 != crc32(CRCVAL_INITIAL, scn, (extent)len))
  934.             {
  935.                 Info(slide, 1, ((char *)slide,
  936.                      "[ Warning: CRC error, discarting PKware extra field]\n"
  937.                      ));
  938.                 len = 0;
  939.                 type = VAT_NONE;
  940.             }
  941.  
  942.             while (len > PK_FLDHDR_SIZE)
  943.             {
  944.                 register struct  PK_field  *fld;
  945.                 int skip=0;
  946.  
  947.                 fld = (struct PK_field *)scn;
  948.                 switch(fld->tag)
  949.                 {
  950.                     case ATR$C_UCHAR:
  951.                         pka_uchar = L(&fld->value);
  952.                         break;
  953.                     case ATR$C_RECATTR:
  954.                         pka_rattr = *(struct fatdef *)(&fld->value);
  955.                         break;
  956.                     case ATR$C_UIC:
  957.                     case ATR$C_ADDACLENT:
  958.                         skip = !G.X_flag;
  959.                         break;
  960.                 }
  961.  
  962.                 if ( !skip )
  963.                 {
  964.                     pka_atr[pka_idx].atr$w_size = fld->size;
  965.                     pka_atr[pka_idx].atr$w_type = fld->tag;
  966.                     pka_atr[pka_idx].atr$l_addr = &fld->value;
  967.                     ++pka_idx;
  968.                 }
  969.                 len -= fld->size + PK_FLDHDR_SIZE;
  970.                 scn += fld->size + PK_FLDHDR_SIZE;
  971.             }
  972.             pka_atr[pka_idx].atr$w_size = 0;    /* End of list */
  973.             pka_atr[pka_idx].atr$w_type = 0;
  974.             pka_atr[pka_idx].atr$l_addr = 0; /* NULL when DECC VAX gets fixed */
  975.         }
  976.         len -= hdr->size + EB_HEADSIZE;
  977.         scan += hdr->size + EB_HEADSIZE;
  978.     }
  979.  
  980.  
  981.     if ( type == VAT_IZ )
  982.     {
  983.         if (outfab != NULL)
  984.         {   /* Do not link XABPRO,XABRDT now. Leave them for sys$close() */
  985.  
  986.             outfab->fab$l_xab = NULL;
  987.             if (xabfhc != NULL)
  988.             {
  989.                 xabfhc->xab$l_nxt = outfab->fab$l_xab;
  990.                 outfab->fab$l_xab = (void *) xabfhc;
  991.             }
  992.             if (xabdat != NULL)
  993.             {
  994.                 xabdat->xab$l_nxt = outfab->fab$l_xab;
  995.                 outfab->fab$l_xab = (void *) xabdat;
  996.             }
  997.             if (first_xab != NULL)      /* Link xaball,xabkey subchain */
  998.             {
  999.                 last_xab->xab$l_nxt = outfab->fab$l_xab;
  1000.                 outfab->fab$l_xab = (void *) first_xab;
  1001.             }
  1002.         }
  1003.         else
  1004.             type = VAT_NONE;
  1005.     }
  1006.     return type;
  1007. }
  1008.  
  1009.  
  1010.  
  1011. static void free_up()
  1012. {
  1013.                                 /*
  1014.                                  *      Free up all allocated xabs
  1015.                                  */
  1016.     if (xabdat != NULL) free(xabdat);
  1017.     if (xabpro != NULL) free(xabpro);
  1018.     if (xabrdt != NULL) free(xabrdt);
  1019.     if (xabfhc != NULL) free(xabfhc);
  1020.     while (first_xab != NULL)
  1021.     {
  1022.         struct XAB *x;
  1023.  
  1024.         x = (struct XAB *) first_xab->xab$l_nxt;
  1025.         free(first_xab);
  1026.         first_xab = x;
  1027.     }
  1028.     if (outfab != NULL && outfab != &fileblk)
  1029.         free(outfab);
  1030. }
  1031.  
  1032.  
  1033.  
  1034. #ifdef CHECK_VERSIONS
  1035.  
  1036. static int get_vms_version(verbuf, len)
  1037.     char *verbuf;
  1038.     int len;
  1039. {
  1040.     int i = SYI$_VERSION;
  1041.     int verlen = 0;
  1042.     struct dsc$descriptor version;
  1043.     char *m;
  1044.  
  1045.     version.dsc$a_pointer = verbuf;
  1046.     version.dsc$w_length  = len - 1;
  1047.     version.dsc$b_dtype   = DSC$K_DTYPE_B;
  1048.     version.dsc$b_class   = DSC$K_CLASS_S;
  1049.  
  1050.     if (ERR(lib$getsyi(&i, 0, &version, &verlen, 0, 0)) || verlen == 0)
  1051.         return 0;
  1052.  
  1053.     /* Cut out trailing spaces "V5.4-3   " -> "V5.4-3" */
  1054.     for (m = verbuf + verlen, i = verlen - 1; i > 0 && verbuf[i] == ' '; --i)
  1055.         --m;
  1056.     *m = '\0';
  1057.  
  1058.     /* Cut out release number "V5.4-3" -> "V5.4" */
  1059.     if ((m = strrchr(verbuf, '-')) != NULL)
  1060.         *m = '\0';
  1061.     return strlen(verbuf) + 1;  /* Transmit ending '\0' too */
  1062. }
  1063.  
  1064. #endif /* CHECK_VERSIONS */
  1065.  
  1066.  
  1067.  
  1068. /*
  1069.  * Extracts block from p. If resulting length is less then needed, fill
  1070.  * extra space with corresponding bytes from 'init'.
  1071.  * Currently understands 3 formats of block compression:
  1072.  * - Simple storing
  1073.  * - Compression of zero bytes to zero bits
  1074.  * - Deflation (see memextract() in extract.c)
  1075.  */
  1076. static uch *extract_block(__G__ p, retlen, init, needlen)
  1077.     __GDEF
  1078.     struct IZ_block *p;
  1079.     int *retlen;
  1080.     uch *init;
  1081.     int needlen;
  1082. {
  1083.     uch *block;         /* Pointer to block allocated */
  1084.     int cmptype;
  1085.     int usiz, csiz, max;
  1086.  
  1087.     cmptype = p->flags & BC_MASK;
  1088.     csiz = p->size - EXTBSL - RESL;
  1089.     usiz = (cmptype == BC_STORED ? csiz : p->length);
  1090.  
  1091.     if (needlen == 0)
  1092.         needlen = usiz;
  1093.  
  1094.     if (retlen)
  1095.         *retlen = usiz;
  1096.  
  1097. #ifndef MAX
  1098. # define MAX(a,b)       ( (a) > (b) ? (a) : (b) )
  1099. #endif
  1100.  
  1101.     if ((block = (uch *) malloc(MAX(needlen, usiz))) == NULL)
  1102.         return NULL;
  1103.  
  1104.     if (init && (usiz < needlen))
  1105.         memcpy(block, init, needlen);
  1106.  
  1107.     switch (cmptype)
  1108.     {
  1109.         case BC_STORED: /* The simplest case */
  1110.             memcpy(block, &(p->body[0]), usiz);
  1111.             break;
  1112.         case BC_00:
  1113.             decompress_bits(block, usiz, &(p->body[0]));
  1114.             break;
  1115.         case BC_DEFL:
  1116.             memextract(__G__ block, usiz, &(p->body[0]), csiz);
  1117.             break;
  1118.         default:
  1119.             free(block);
  1120.             block = NULL;
  1121.     }
  1122.     return block;
  1123. }
  1124.  
  1125.  
  1126.  
  1127. /*
  1128.  *  Simple uncompression routine. The compression uses bit stream.
  1129.  *  Compression scheme:
  1130.  *
  1131.  *  if (byte!=0)
  1132.  *      putbit(1),putbyte(byte)
  1133.  *  else
  1134.  *      putbit(0)
  1135.  */
  1136. static void decompress_bits(outptr, needlen, bitptr)
  1137.     uch *outptr;        /* Pointer into output block */
  1138.     int needlen;        /* Size of uncompressed block */
  1139.     uch *bitptr;        /* Pointer into compressed data */
  1140. {
  1141.     ulg bitbuf = 0;
  1142.     int bitcnt = 0;
  1143.  
  1144. #define _FILL   if (bitcnt+8 <= 32)                     \
  1145.                 {       bitbuf |= (*bitptr++) << bitcnt;\
  1146.                         bitcnt += 8;                    \
  1147.                 }
  1148.  
  1149.     while (needlen--)
  1150.     {
  1151.         if (bitcnt <= 0)
  1152.             _FILL;
  1153.  
  1154.         if (bitbuf & 1)
  1155.         {
  1156.             bitbuf >>= 1;
  1157.             if ((bitcnt -= 1) < 8)
  1158.                 _FILL;
  1159.             *outptr++ = (uch) bitbuf;
  1160.             bitcnt -= 8;
  1161.             bitbuf >>= 8;
  1162.         }
  1163.         else
  1164.         {
  1165.             *outptr++ = '\0';
  1166.             bitcnt -= 1;
  1167.             bitbuf >>= 1;
  1168.         }
  1169.     }
  1170. }
  1171.  
  1172.  
  1173.  
  1174. /* flush contents of output buffer */
  1175. int flush(__G__ rawbuf, size, unshrink)    /* return PK-type error code */
  1176.     __GDEF
  1177.     uch *rawbuf;
  1178.     ulg size;
  1179.     int unshrink;
  1180. {
  1181.     G.crc32val = crc32(G.crc32val, rawbuf, (extent)size);
  1182.     if (G.tflag)
  1183.         return PK_COOL; /* Do not output. Update CRC only */
  1184.     else
  1185.         return (*_flush_routine)(__G__ rawbuf, size, 0);
  1186. }
  1187.  
  1188.  
  1189.  
  1190. static int _flush_blocks(__G__ rawbuf, size, final_flag)
  1191.                                                 /* Asynchronous version */
  1192.     __GDEF
  1193.     uch *rawbuf;
  1194.     unsigned size;
  1195.     int final_flag;   /* 1 if this is the final flushout */
  1196. {
  1197.     int round;
  1198.     int rest;
  1199.     int off = 0;
  1200.     int status;
  1201.  
  1202.     while (size > 0)
  1203.     {
  1204.         if (curbuf->bufcnt < BUFS512)
  1205.         {
  1206.             int ncpy;
  1207.  
  1208.             ncpy = size > (BUFS512 - curbuf->bufcnt) ?
  1209.                             BUFS512 - curbuf->bufcnt :
  1210.                             size;
  1211.             memcpy(curbuf->buf + curbuf->bufcnt, rawbuf + off, ncpy);
  1212.             size -= ncpy;
  1213.             curbuf->bufcnt += ncpy;
  1214.             off += ncpy;
  1215.         }
  1216.         if (curbuf->bufcnt == BUFS512)
  1217.         {
  1218.             status = WriteBuffer(__G__ curbuf->buf, curbuf->bufcnt);
  1219.             if (status)
  1220.                 return status;
  1221.             curbuf = curbuf->next;
  1222.             curbuf->bufcnt = 0;
  1223.         }
  1224.     }
  1225.  
  1226.     return (final_flag && (curbuf->bufcnt > 0)) ?
  1227.         WriteBuffer(__G__ curbuf->buf, curbuf->bufcnt) :
  1228.         PK_COOL;
  1229. }
  1230.  
  1231.  
  1232.  
  1233. static int _flush_qio(__G__ rawbuf, size, final_flag)
  1234.     __GDEF
  1235.     uch *rawbuf;
  1236.     unsigned size;
  1237.     int final_flag;   /* 1 if this is the final flushout */
  1238. {
  1239.     int status;
  1240.     uch *out_ptr=rawbuf;
  1241.  
  1242.     if ( final_flag )
  1243.     {
  1244.         if ( loccnt > 0 )
  1245.         {
  1246.             status = sys$qiow(0, pka_devchn, IO$_WRITEVBLK,
  1247.                               &pka_io_sb, 0, 0,
  1248.                               locbuf,
  1249.                               (loccnt+1)&(~1), /* Round up to even byte count */
  1250.                               pka_vbn,
  1251.                               0, 0, 0);
  1252.             if (!ERR(status))
  1253.                 status = pka_io_sb.status;
  1254.             if (ERR(status))
  1255.             {
  1256.                 vms_msg(__G__ "[ Write QIO failed ]\n",status);
  1257.                 return PK_DISK;
  1258.             }
  1259.         }
  1260.         return PK_COOL;
  1261.     }
  1262.  
  1263.     if ( loccnt > 0 )
  1264.     {
  1265.         /*
  1266.          *   Fill local buffer upto 512 bytes then put it out
  1267.          */
  1268.         int ncpy;
  1269.  
  1270.         ncpy = 512-loccnt;
  1271.         if ( ncpy > size )
  1272.             ncpy = size;
  1273.  
  1274.         memcpy(locptr,rawbuf,ncpy);
  1275.         locptr += ncpy;
  1276.         loccnt += ncpy;
  1277.         size -= ncpy;
  1278.         out_ptr += ncpy;
  1279.         if ( loccnt == 512 )
  1280.         {
  1281.             status = sys$qiow(0, pka_devchn, IO$_WRITEVBLK,
  1282.                               &pka_io_sb, 0, 0,
  1283.                               locbuf, loccnt, pka_vbn,
  1284.                               0, 0, 0);
  1285.             if (!ERR(status))
  1286.                 status = pka_io_sb.status;
  1287.             if (ERR(status))
  1288.             {
  1289.                 vms_msg(__G__ "[ Write QIO failed ]\n",status);
  1290.                 return PK_DISK;
  1291.             }
  1292.  
  1293.             pka_vbn++;
  1294.             loccnt = 0;
  1295.             locptr = locbuf;
  1296.         }
  1297.     }
  1298.  
  1299.     if ( size >= 512 )
  1300.     {
  1301.         int nblk,put_cnt;
  1302.  
  1303.         /*
  1304.          *   Put rest of buffer as a single VB
  1305.          */
  1306.         put_cnt = (nblk = size>>9)<<9;
  1307.         status = sys$qiow(0, pka_devchn, IO$_WRITEVBLK,
  1308.                           &pka_io_sb, 0, 0,
  1309.                           out_ptr, put_cnt, pka_vbn,
  1310.                           0, 0, 0);
  1311.         if (!ERR(status))
  1312.             status = pka_io_sb.status;
  1313.         if (ERR(status))
  1314.         {
  1315.             vms_msg(__G__ "[ Write QIO failed ]\n",status);
  1316.             return PK_DISK;
  1317.         }
  1318.  
  1319.         pka_vbn += nblk;
  1320.         out_ptr += put_cnt;
  1321.         size -= put_cnt;
  1322.     }
  1323.  
  1324.     if ( size > 0 )
  1325.     {
  1326.         memcpy(locptr,out_ptr,size);
  1327.         loccnt += size;
  1328.         locptr += size;
  1329.     }
  1330.  
  1331.     return PK_COOL;
  1332. }
  1333.  
  1334.  
  1335.  
  1336. static int _flush_varlen(__G__ rawbuf, size, final_flag)
  1337.     __GDEF
  1338.     uch *rawbuf;
  1339.     unsigned size;
  1340.     int final_flag;
  1341. {
  1342.     ush nneed;
  1343.     ush reclen;
  1344.     uch *inptr=rawbuf;
  1345.  
  1346.     /*
  1347.      * Flush local buffer
  1348.      */
  1349.  
  1350.     if ( loccnt > 0 )
  1351.     {
  1352.         reclen = *(ush*)locbuf;
  1353.         if ( (nneed = reclen + 2 - loccnt) > 0 )
  1354.         {
  1355.             if ( nneed > size )
  1356.             {
  1357.                 if ( size+loccnt > BUFS512 )
  1358.                 {
  1359.                     char buf[80];
  1360.                     Info(buf, 1, (buf,
  1361.                          "[ Record too long (%d bytes) ]\n",reclen ));
  1362.                     return PK_DISK;
  1363.                 }
  1364.                 memcpy(locbuf+loccnt,rawbuf,size);
  1365.                 loccnt += size;
  1366.                 size = 0;
  1367.             }
  1368.             else
  1369.             {
  1370.                 memcpy(locbuf+loccnt,rawbuf,nneed);
  1371.                 loccnt += nneed;
  1372.                 size -= nneed;
  1373.                 inptr += nneed;
  1374.                 if ( reclen & 1 )
  1375.                 {
  1376.                     size--;
  1377.                     inptr++;
  1378.                 }
  1379.                 if ( WriteRecord(__G__ locbuf+2,reclen) )
  1380.                     return PK_DISK;
  1381.                 loccnt = 0;
  1382.             }
  1383.         }
  1384.         else
  1385.         {
  1386.             if (WriteRecord(__G__ locbuf+2,reclen))
  1387.                 return PK_DISK;
  1388.             loccnt -= reclen+2;
  1389.         }
  1390.     }
  1391.     /*
  1392.      * Flush incoming records
  1393.      */
  1394.     while (size > 0)
  1395.     {
  1396.         reclen = *(ush*)inptr;
  1397.         if ( reclen+2 <= size )
  1398.         {
  1399.             if (WriteRecord(__G__ inptr+2,reclen))
  1400.                 return PK_DISK;
  1401.             size -= 2+reclen;
  1402.             inptr += 2+reclen;
  1403.             if ( reclen & 1)
  1404.             {
  1405.                 --size;
  1406.                 ++inptr;
  1407.             }
  1408.         }
  1409.         else
  1410.         {
  1411.             memcpy(locbuf,inptr,size);
  1412.             loccnt = size;
  1413.             size = 0;
  1414.         }
  1415.  
  1416.     }
  1417.     /*
  1418.      * Final flush rest of local buffer
  1419.      */
  1420.     if ( final_flag && loccnt > 0 )
  1421.     {
  1422.         char buf[80];
  1423.  
  1424.         Info(buf, 1, (buf,
  1425.             "[ Warning, incomplete record of length %d ]\n",
  1426.             *(ush*)locbuf));
  1427.         if ( WriteRecord(__G__ locbuf+2,loccnt-2) )
  1428.             return PK_DISK;
  1429.     }
  1430.     return PK_COOL;
  1431. }
  1432.  
  1433.  
  1434.  
  1435. /*
  1436. *   Routine _flush_stream breaks decompressed stream into records
  1437. *   depending on format of the stream (fab->rfm, G.pInfo->textmode, etc.)
  1438. *   and puts out these records. It also handles CR LF sequences.
  1439. *   Should be used when extracting *text* files.
  1440. */
  1441.  
  1442. #define VT      0x0B
  1443. #define FF      0x0C
  1444.  
  1445. /* The file is from MSDOS/OS2/NT -> handle CRLF as record end, throw out ^Z */
  1446.  
  1447. /* GRR NOTES:  cannot depend on hostnum!  May have "flip'd" file or re-zipped
  1448.  * a Unix file, etc. */
  1449.  
  1450. #ifdef USE_ORIG_DOS
  1451. # define ORG_DOS   (hostnum==FS_FAT_ || hostnum==FS_HPFS_ || hostnum==FS_NTFS_)
  1452. #else
  1453. # define ORG_DOS    1
  1454. #endif
  1455.  
  1456. /* Record delimiters */
  1457. #ifdef undef
  1458. #define RECORD_END(c,f)                                                 \
  1459. (    ( ORG_DOS || G.pInfo->textmode ) && c==CTRLZ                      \
  1460.   || ( f == FAB$C_STMLF && c==LF )                                      \
  1461.   || ( f == FAB$C_STMCR || ORG_DOS || G.pInfo->textmode ) && c==CR     \
  1462.   || ( f == FAB$C_STM && (c==CR || c==LF || c==FF || c==VT) )           \
  1463. )
  1464. #else
  1465. #   define  RECORD_END(c,f)   ((c) == LF || (c) == (CR))
  1466. #endif
  1467.  
  1468. static int  find_eol(p,n,l)
  1469. /*
  1470.  *  Find first CR,LF,CR-LF or LF-CR in string 'p' of length 'n'.
  1471.  *  Return offset of the sequence found or 'n' if not found.
  1472.  *  If found, return in '*l' length of the sequence (1 or 2) or
  1473.  *  zero if sequence end not seen, i.e. CR or LF is last char
  1474.  *  in the buffer.
  1475.  */
  1476.     uch *p;
  1477.     int n;
  1478.     int *l;
  1479. {
  1480.     int off = n;
  1481.     uch *q;
  1482.  
  1483.     *l = 0;
  1484.  
  1485.     for (q=p ; n > 0 ; --n,++q)
  1486.         if ( RECORD_END(*q,rfm) )
  1487.         {
  1488.             off = q-p;
  1489.             break;
  1490.         }
  1491.  
  1492.     if ( n > 1 )
  1493.     {
  1494.         *l = 1;
  1495.         if ( ( q[0] == CR && q[1] == LF ) || ( q[0] == LF && q[1] == CR ) )
  1496.             *l = 2;
  1497.     }
  1498.  
  1499.     return off;
  1500. }
  1501.  
  1502. /* Record delimiters that must be put out */
  1503. #define PRINT_SPEC(c)   ( (c)==FF || (c)==VT )
  1504.  
  1505.  
  1506.  
  1507. static int _flush_stream(__G__ rawbuf, size, final_flag)
  1508.     __GDEF
  1509.     uch *rawbuf;
  1510.     unsigned size;
  1511.     int final_flag; /* 1 if this is the final flushout */
  1512. {
  1513.     int rest;
  1514.     int end = 0, start = 0;
  1515.     int off = 0;
  1516.  
  1517.     if (size == 0 && loccnt == 0)
  1518.         return PK_COOL;         /* Nothing to do ... */
  1519.  
  1520.     if ( final_flag )
  1521.     {
  1522.         int recsize;
  1523.  
  1524.         /*
  1525.          * This is flush only call. size must be zero now.
  1526.          * Just eject everything we have in locbuf.
  1527.          */
  1528.         recsize = loccnt - (got_eol ? 1:0);
  1529.         /*
  1530.          *  If the last char of file was ^Z ( end-of-file in MSDOS ),
  1531.          *  we will see it now.
  1532.          */
  1533.         if ( recsize==1 && locbuf[0] == CTRLZ )
  1534.             return PK_COOL;
  1535.  
  1536.         return WriteRecord(__G__ locbuf, recsize) ? PK_DISK : PK_COOL;
  1537.     }
  1538.  
  1539.  
  1540.     if ( loccnt > 0 )
  1541.     {
  1542.         /* Find end of record partially saved in locbuf */
  1543.  
  1544.         int recsize;
  1545.         int complete=0;
  1546.  
  1547.         if ( got_eol )
  1548.         {
  1549.             recsize = loccnt - 1;
  1550.             complete = 1;
  1551.  
  1552.             if ( (got_eol == CR && rawbuf[0] == LF) ||
  1553.                  (got_eol == LF && rawbuf[0] == CR) )
  1554.                 end = 1;
  1555.  
  1556.             got_eol = 0;
  1557.         }
  1558.         else
  1559.         {
  1560.             int eol_len;
  1561.             int eol_off;
  1562.  
  1563.             eol_off = find_eol(rawbuf,size,&eol_len);
  1564.  
  1565.             if ( loccnt+eol_off > BUFS512 )
  1566.             {
  1567.                 /*
  1568.                  *  No room in locbuf. Dump it and clear
  1569.                  */
  1570.                 char buf[80];           /* CANNOT use slide for Info() */
  1571.  
  1572.                 recsize = loccnt;
  1573.                 start = 0;
  1574.                 Info(buf, 1, (buf,
  1575.                      "[ Warning: Record too long (%d) ]\n", loccnt+eol_off));
  1576.                 complete = 1;
  1577.                 end = 0;
  1578.             }
  1579.             else
  1580.             {
  1581.                 if ( eol_off >= size )
  1582.                 {
  1583.                     end = size;
  1584.                     complete = 0;
  1585.                 }
  1586.                 else if ( eol_len == 0 )
  1587.                 {
  1588.                     got_eol = rawbuf[eol_off];
  1589.                     end = size;
  1590.                     complete = 0;
  1591.                 }
  1592.                 else
  1593.                 {
  1594.                     memcpy(locptr, rawbuf, eol_off);
  1595.                     recsize = loccnt + eol_off;
  1596.                     locptr += eol_off;
  1597.                     loccnt += eol_off;
  1598.                     end = eol_off + eol_len;
  1599.                     complete = 1;
  1600.                 }
  1601.             }
  1602.         }
  1603.  
  1604.         if ( complete )
  1605.         {
  1606.             if (WriteRecord(__G__ locbuf, recsize))
  1607.                 return PK_DISK;
  1608.             loccnt = 0;
  1609.             locptr = locbuf;
  1610.         }
  1611.     }                           /* end if ( loccnt ) */
  1612.  
  1613.     for (start = end; start < size && end < size; )
  1614.     {
  1615.         int eol_off,eol_len;
  1616.  
  1617.         got_eol = 0;
  1618.  
  1619. #ifdef undef
  1620.         if (G.cflag)
  1621.             /* skip CR's at the beginning of record */
  1622.             while (start < size && rawbuf[start] == CR)
  1623.                 ++start;
  1624. #endif
  1625.  
  1626.         if ( start >= size )
  1627.             continue;
  1628.  
  1629.         /* Find record end */
  1630.         end = start+(eol_off = find_eol(rawbuf+start, size-start, &eol_len));
  1631.  
  1632.         if ( end >= size )
  1633.             continue;
  1634.  
  1635.         if ( eol_len > 0 )
  1636.         {
  1637.             if ( WriteRecord(__G__ rawbuf+start, end-start) )
  1638.                 return PK_DISK;
  1639.             start = end + eol_len;
  1640.         }
  1641.         else
  1642.         {
  1643.             got_eol = rawbuf[end];
  1644.             end = size;
  1645.             continue;
  1646.         }
  1647.     }
  1648.  
  1649.     rest = size - start;
  1650.  
  1651.     if (rest > 0)
  1652.     {
  1653.         if ( rest > BUFS512 )
  1654.         {
  1655.             int recsize;
  1656.             char buf[80];               /* CANNOT use slide for Info() */
  1657.  
  1658.             recsize = rest - (got_eol ? 1:0 );
  1659.             Info(buf, 1, (buf,
  1660.                  "[ Warning: Record too long (%d) ]\n", recsize));
  1661.             got_eol = 0;
  1662.             return WriteRecord(__G__ rawbuf+start,recsize) ? PK_DISK : PK_COOL;
  1663.         }
  1664.         else
  1665.         {
  1666.             memcpy(locptr, rawbuf + start, rest);
  1667.             locptr += rest;
  1668.             loccnt += rest;
  1669.         }
  1670.     }
  1671.     return PK_COOL;
  1672. }
  1673.  
  1674.  
  1675.  
  1676. static int WriteBuffer(__G__ buf, len)
  1677.     __GDEF
  1678.     uch *buf;
  1679.     int len;
  1680. {
  1681.     int status;
  1682.  
  1683.     status = sys$wait(outrab);
  1684.     if (ERR(status))
  1685.     {
  1686.         vms_msg(__G__ "[ WriteBuffer failed ]\n", status);
  1687.         vms_msg(__G__ "", outrab->rab$l_stv);
  1688.     }
  1689.     outrab->rab$w_rsz = len;
  1690.     outrab->rab$l_rbf = (char *) buf;
  1691.  
  1692.     if (ERR(status = sys$write(outrab)))
  1693.     {
  1694.         vms_msg(__G__ "[ WriteBuffer failed ]\n", status);
  1695.         vms_msg(__G__ "", outrab->rab$l_stv);
  1696.         return PK_DISK;
  1697.     }
  1698.     return PK_COOL;
  1699. }
  1700.  
  1701.  
  1702.  
  1703. static int WriteRecord(__G__ rec, len)
  1704.     __GDEF
  1705.     uch *rec;
  1706.     int len;
  1707. {
  1708.     int status;
  1709.  
  1710.     if (G.cflag)
  1711.     {
  1712.         (void)(*G.message)((zvoid *)&G, rec, len, 0);
  1713.         (void)(*G.message)((zvoid *)&G, (uch *) ("\n"), 1, 0);
  1714.     }
  1715.     else
  1716.     {
  1717.         if (ERR(status = sys$wait(outrab)))
  1718.         {
  1719.             vms_msg(__G__ "[ WriteRecord failed ]\n", status);
  1720.             vms_msg(__G__ "", outrab->rab$l_stv);
  1721.         }
  1722.         outrab->rab$w_rsz = len;
  1723.         outrab->rab$l_rbf = (char *) rec;
  1724.  
  1725.         if (ERR(status = sys$put(outrab)))
  1726.         {
  1727.             vms_msg(__G__ "[ WriteRecord failed ]\n", status);
  1728.             vms_msg(__G__ "", outrab->rab$l_stv);
  1729.             return PK_DISK;
  1730.         }
  1731.     }
  1732.     return PK_COOL;
  1733. }
  1734.  
  1735.  
  1736.  
  1737. void close_outfile(__G)
  1738.     __GDEF
  1739. {
  1740.     int status;
  1741.  
  1742.     status = (*_flush_routine)(__G__ NULL, 0, 1);
  1743.     if (status)
  1744.         return /* PK_DISK */;
  1745.     if (G.cflag)
  1746.         return /* PK_COOL */;   /* Don't close stdout */
  1747.     /* return */ (*_close_routine)(__G);
  1748. }
  1749.  
  1750.  
  1751.  
  1752. static int _close_rms(__GPRO)
  1753. {
  1754.     int status;
  1755.     struct XABPRO pro;
  1756.  
  1757.     /* Link XABRDT,XABDAT and optionaly XABPRO */
  1758.     if (xabrdt != NULL)
  1759.     {
  1760.         xabrdt->xab$l_nxt = NULL;
  1761.         outfab->fab$l_xab = (void *) xabrdt;
  1762.     }
  1763.     else
  1764.     {
  1765.         rdt.xab$l_nxt = NULL;
  1766.         outfab->fab$l_xab = (void *) &rdt;
  1767.     }
  1768.     if (xabdat != NULL)
  1769.     {
  1770.         xabdat->xab$l_nxt = outfab->fab$l_xab;
  1771.         outfab->fab$l_xab = (void *)xabdat;
  1772.     }
  1773.  
  1774.     if (xabpro != NULL)
  1775.     {
  1776.         if ( !G.X_flag )
  1777.             xabpro->xab$l_uic = 0;    /* Use default (user's) uic */
  1778.         xabpro->xab$l_nxt = outfab->fab$l_xab;
  1779.         outfab->fab$l_xab = (void *) xabpro;
  1780.     }
  1781.     else
  1782.     {
  1783.         pro = cc$rms_xabpro;
  1784.         pro.xab$w_pro = G.pInfo->file_attr;
  1785.         pro.xab$l_nxt = outfab->fab$l_xab;
  1786.         outfab->fab$l_xab = (void *) &pro;
  1787.     }
  1788.  
  1789.     sys$wait(outrab);
  1790.  
  1791.     status = sys$close(outfab);
  1792. #ifdef DEBUG
  1793.     if (ERR(status))
  1794.     {
  1795.         vms_msg(__G__
  1796.           "\r[ Warning: cannot set owner/protection/time attributes ]\n",
  1797.           status);
  1798.         vms_msg(__G__ "", outfab->fab$l_stv);
  1799.     }
  1800. #endif
  1801.     free_up();
  1802.     return PK_COOL;
  1803. }
  1804.  
  1805.  
  1806.  
  1807. static int _close_qio(__GPRO)
  1808. {
  1809.     int status;
  1810.  
  1811.     pka_fib.FIB$L_ACCTL =
  1812.         FIB$M_WRITE | FIB$M_NOTRUNC ;
  1813.     pka_fib.FIB$W_EXCTL = 0;
  1814.  
  1815.     pka_fib.FIB$W_FID[0] =
  1816.     pka_fib.FIB$W_FID[1] =
  1817.     pka_fib.FIB$W_FID[2] =
  1818.     pka_fib.FIB$W_DID[0] =
  1819.     pka_fib.FIB$W_DID[1] =
  1820.     pka_fib.FIB$W_DID[2] = 0;
  1821.  
  1822.     status = sys$qiow(0, pka_devchn, IO$_DEACCESS, &pka_acp_sb,
  1823.                       0, 0,
  1824.                       &pka_fibdsc, 0, 0, 0,
  1825.                       &pka_atr, 0);
  1826.  
  1827.     sys$dassgn(pka_devchn);
  1828.     if ( !ERR(status) )
  1829.         status = pka_acp_sb.status;
  1830.     if ( ERR(status) )
  1831.     {
  1832.         vms_msg(__G__ "[ Deaccess QIO failed ]\n",status);
  1833.         return PK_DISK;
  1834.     }
  1835.     return PK_COOL;
  1836. }
  1837.  
  1838.  
  1839.  
  1840. #ifdef DEBUG
  1841. #if 0   /* currently not used anywhere ! */
  1842. void dump_rms_block(p)
  1843.     unsigned char *p;
  1844. {
  1845.     unsigned char bid, len;
  1846.     int err;
  1847.     char *type;
  1848.     char buf[132];
  1849.     int i;
  1850.  
  1851.     err = 0;
  1852.     bid = p[0];
  1853.     len = p[1];
  1854.     switch (bid)
  1855.     {
  1856.         case FAB$C_BID:
  1857.             type = "FAB";
  1858.             break;
  1859.         case XAB$C_ALL:
  1860.             type = "xabALL";
  1861.             break;
  1862.         case XAB$C_KEY:
  1863.             type = "xabKEY";
  1864.             break;
  1865.         case XAB$C_DAT:
  1866.             type = "xabDAT";
  1867.             break;
  1868.         case XAB$C_RDT:
  1869.             type = "xabRDT";
  1870.             break;
  1871.         case XAB$C_FHC:
  1872.             type = "xabFHC";
  1873.             break;
  1874.         case XAB$C_PRO:
  1875.             type = "xabPRO";
  1876.             break;
  1877.         default:
  1878.             type = "Unknown";
  1879.             err = 1;
  1880.             break;
  1881.     }
  1882.     printf("Block @%08X of type %s (%d).", p, type, bid);
  1883.     if (err)
  1884.     {
  1885.         printf("\n");
  1886.         return;
  1887.     }
  1888.     printf(" Size = %d\n", len);
  1889.     printf(" Offset - Hex - Dec\n");
  1890.     for (i = 0; i < len; i += 8)
  1891.     {
  1892.         int j;
  1893.  
  1894.         printf("%3d - ", i);
  1895.         for (j = 0; j < 8; j++)
  1896.             if (i + j < len)
  1897.                 printf("%02X ", p[i + j]);
  1898.             else
  1899.                 printf("   ");
  1900.         printf(" - ");
  1901.         for (j = 0; j < 8; j++)
  1902.             if (i + j < len)
  1903.                 printf("%03d ", p[i + j]);
  1904.             else
  1905.                 printf("    ");
  1906.         printf("\n");
  1907.     }
  1908. }
  1909.  
  1910. #endif                          /* never */
  1911. #endif                          /* DEBUG */
  1912.  
  1913.  
  1914.  
  1915. static void vms_msg(__GPRO__ char *string, int status)
  1916. {
  1917.     static char msgbuf[256];
  1918.  
  1919.     $DESCRIPTOR(msgd, msgbuf);
  1920.     int msglen = 0;
  1921.  
  1922.     if (ERR(lib$sys_getmsg(&status, &msglen, &msgd, 0, 0)))
  1923.         Info(slide, 1, ((char *)slide,
  1924.              "%s[ VMS status = %d ]\n", string, status));
  1925.     else
  1926.     {
  1927.         msgbuf[msglen] = '\0';
  1928.         Info(slide, 1, ((char *)slide, "%s[ %s ]\n", string, msgbuf));
  1929.     }
  1930. }
  1931.  
  1932.  
  1933.  
  1934. #ifndef SFX
  1935.  
  1936. char *do_wild( __G__ wld )
  1937.     __GDEF
  1938.     char *wld;
  1939. {
  1940.     int status;
  1941.  
  1942.     static char filenam[256];
  1943.     static char efn[256];
  1944.     static char last_wild[256];
  1945.     static struct FAB fab;
  1946.     static struct NAM nam;
  1947.     static int first_call=1;
  1948.     static const char deflt[] = "[]*.zip";
  1949.  
  1950.     if ( first_call || strcmp(wld, last_wild) )
  1951.     {   /* (Re)Initialize everything */
  1952.  
  1953.         strcpy( last_wild, wld );
  1954.         first_call = 1;            /* New wild spec */
  1955.  
  1956.         fab = cc$rms_fab;
  1957.         fab.fab$l_fna = last_wild;
  1958.         fab.fab$b_fns = strlen(last_wild);
  1959.         fab.fab$l_dna = (char *) deflt;
  1960.         fab.fab$b_dns = sizeof(deflt)-1;
  1961.         fab.fab$l_nam = &nam;
  1962.         nam = cc$rms_nam;
  1963.         nam.nam$l_esa = efn;
  1964.         nam.nam$b_ess = sizeof(efn)-1;
  1965.         nam.nam$l_rsa = filenam;
  1966.         nam.nam$b_rss = sizeof(filenam)-1;
  1967.  
  1968.         if ( !OK(sys$parse(&fab)) )
  1969.             return (char *)NULL;     /* Initialization failed */
  1970.         first_call = 0;
  1971.         if ( !OK(sys$search(&fab)) )
  1972.         {
  1973.             strcpy( filenam, wld );
  1974.             return filenam;
  1975.         }
  1976.     }
  1977.     else
  1978.     {
  1979.         if ( !OK(sys$search(&fab)) )
  1980.         {
  1981.             first_call = 1;        /* Reinitialize next time */
  1982.             return (char *)NULL;
  1983.         }
  1984.     }
  1985.     filenam[nam.nam$b_rsl] = 0;
  1986.     return filenam;
  1987.  
  1988. } /* end function do_wild() */
  1989.  
  1990. #endif /* !SFX */
  1991.  
  1992.  
  1993.  
  1994. static ulg unix_to_vms[8]={ /* Map from UNIX rwx to VMS rwed */
  1995.                             /* Note that unix w bit is mapped to VMS wd bits */
  1996.                                                               /* no access */
  1997.     XAB$M_NOREAD | XAB$M_NOWRITE | XAB$M_NODEL | XAB$M_NOEXE,    /* --- */
  1998.     XAB$M_NOREAD | XAB$M_NOWRITE | XAB$M_NODEL,                  /* --x */
  1999.     XAB$M_NOREAD |                               XAB$M_NOEXE,    /* -w- */
  2000.     XAB$M_NOREAD,                                                /* -wx */
  2001.                    XAB$M_NOWRITE | XAB$M_NODEL | XAB$M_NOEXE,    /* r-- */
  2002.                    XAB$M_NOWRITE | XAB$M_NODEL,                  /* r-x */
  2003.                                                  XAB$M_NOEXE,    /* rw- */
  2004.     0                                                            /* rwx */
  2005.                                                               /* full access */
  2006. };
  2007.  
  2008. #define SETDFPROT   /* We are using undocumented VMS System Service     */
  2009.                     /* SYS$SETDFPROT here. If your version of VMS does  */
  2010.                     /* not have that service, undef SETDFPROT.          */
  2011.                     /* IM: Maybe it's better to put this to Makefile    */
  2012.                     /* and DESCRIP.MMS */
  2013.  
  2014. #ifdef SETDFPROT
  2015. extern int SYS$SETDFPROT();
  2016. #endif
  2017.  
  2018.  
  2019. int mapattr(__G)
  2020.     __GDEF
  2021. {
  2022.     ulg  tmp=G.crec.external_file_attributes, theprot;
  2023.     static ulg  defprot = -1L,
  2024.                 sysdef,owndef,grpdef,wlddef;  /* Default protection fields */
  2025.  
  2026.  
  2027.     /* IM: The only field of XABPRO we need to set here is */
  2028.     /*     file protection, so we need not to change type */
  2029.     /*     of G.pInfo->file_attr. WORD is quite enough. */
  2030.  
  2031.     if ( defprot == -1L )
  2032.     {
  2033.         /*
  2034.          * First time here -- Get user default settings
  2035.          */
  2036.  
  2037. #ifdef SETDFPROT    /* Undef this if linker cat't resolve SYS$SETDFPROT */
  2038.         defprot = 0L;
  2039.         if ( !ERR(SYS$SETDFPROT(0,&defprot)) )
  2040.         {
  2041.             sysdef = defprot & ( (1L<<XAB$S_SYS)-1 ) << XAB$V_SYS;
  2042.             owndef = defprot & ( (1L<<XAB$S_OWN)-1 ) << XAB$V_OWN;
  2043.             grpdef = defprot & ( (1L<<XAB$S_GRP)-1 ) << XAB$V_GRP;
  2044.             wlddef = defprot & ( (1L<<XAB$S_WLD)-1 ) << XAB$V_WLD;
  2045.         }
  2046.         else
  2047.         {
  2048. #endif /* ?SETDFPROT */
  2049.             umask(defprot = umask(0));
  2050.             defprot = ~defprot;
  2051.             wlddef = unix_to_vms[defprot & 07] << XAB$V_WLD;
  2052.             grpdef = unix_to_vms[(defprot>>3) & 07] << XAB$V_GRP;
  2053.             owndef = unix_to_vms[(defprot>>6) & 07] << XAB$V_OWN;
  2054.             sysdef = owndef << (XAB$V_SYS - XAB$V_OWN);
  2055.             defprot = sysdef | owndef | grpdef | wlddef;
  2056. #ifdef SETDFPROT
  2057.         }
  2058. #endif /* ?SETDFPROT */
  2059.     }
  2060.  
  2061.     switch (G.pInfo->hostnum) {
  2062.         case UNIX_:
  2063.         case VMS_:  /*IM: ??? Does VMS Zip store protection in UNIX format ?*/
  2064.                     /* GRR:  Yup.  Bad decision on my part... */
  2065.             tmp = (unsigned)(tmp >> 16);  /* drwxrwxrwx */
  2066.             theprot  = (unix_to_vms[tmp & 07] << XAB$V_WLD)
  2067.                      | (unix_to_vms[(tmp>>3) & 07] << XAB$V_GRP)
  2068.                      | (unix_to_vms[(tmp>>6) & 07] << XAB$V_OWN);
  2069.  
  2070.             if ( tmp & 0x4000 )
  2071.                 /* Directory -- set D bits */
  2072.                 theprot |= (XAB$M_NODEL << XAB$V_SYS)
  2073.                         | (XAB$M_NODEL << XAB$V_OWN)
  2074.                         | (XAB$M_NODEL << XAB$V_GRP)
  2075.                         | (XAB$M_NODEL << XAB$V_WLD);
  2076.             G.pInfo->file_attr = theprot;
  2077.             break;
  2078.  
  2079.         case AMIGA_:
  2080.             tmp = (unsigned)(tmp>>16 & 0x0f);   /* Amiga RWED bits */
  2081.             G.pInfo->file_attr =  (tmp << XAB$V_OWN) |
  2082.                                    grpdef | sysdef | wlddef;
  2083.             break;
  2084.  
  2085.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  2086.         case FS_FAT_:
  2087.         case FS_HPFS_:
  2088.         case FS_NTFS_:
  2089.         case MAC_:
  2090.         case ATARI_:             /* (used to set = 0666) */
  2091.         case TOPS20_:
  2092.         default:
  2093.             theprot = defprot;
  2094.             if ( tmp & 1 )   /* Test read-only bit */
  2095.             {   /* Bit is set -- set bits in all fields */
  2096.                 tmp = XAB$M_NOWRITE | XAB$M_NODEL;
  2097.                 theprot |= (tmp << XAB$V_SYS) | (tmp << XAB$V_OWN) |
  2098.                            (tmp << XAB$V_GRP) | (tmp << XAB$V_WLD);
  2099.             }
  2100.             G.pInfo->file_attr = theprot;
  2101.             break;
  2102.     } /* end switch (host-OS-created-by) */
  2103.  
  2104.     return 0;
  2105.  
  2106. } /* end function mapattr() */
  2107.  
  2108.  
  2109.  
  2110. #ifndef EEXIST
  2111. #  include <errno.h>    /* For mkdir() status codes */
  2112. #endif
  2113.  
  2114. #include <fscndef.h> /* for filescan */
  2115.  
  2116. #   define FN_MASK   7
  2117. #   define USE_DEFAULT  (FN_MASK+1)
  2118.  
  2119. /*
  2120.  * Checkdir function codes:
  2121.  *      ROOT        -   set root path from unzip qq d:[dir]
  2122.  *      INIT        -   get ready for "filename"
  2123.  *      APPEND_DIR  -   append pathcomp
  2124.  *      APPEND_NAME -   append filename
  2125.  *      APPEND_NAME | USE_DEFAULT   -    expand filename using collected path
  2126.  *      GETPATH     -   return resulting filespec
  2127.  *      END         -   free dynamically allocated space prior to program exit
  2128.  */
  2129.  
  2130. static  int created_dir;
  2131.  
  2132. int mapname(__G__ renamed)
  2133.             /* returns: */
  2134.             /* 0 (PK_COOL) if no error, */
  2135.             /* 1 (PK_WARN) if caution (filename trunc), */
  2136.             /* 2 (PK_ERR)  if warning (skip file because dir doesn't exist), */
  2137.             /* 3 (PK_BADERR) if error (skip file), */
  2138.             /* 77 (IZ_CREATED_DIR) if has created directory, */
  2139.             /* 10 if no memory (skip file) */
  2140.     __GDEF
  2141.     int renamed;
  2142. {
  2143.     char pathcomp[FILNAMSIZ];   /* path-component buffer */
  2144.     char *pp, *cp=NULL;         /* character pointers */
  2145.     char *lastsemi = NULL;      /* pointer to last semi-colon in pathcomp */
  2146.     char *last_dot = NULL;      /* last dot not converted to underscore */
  2147.     int quote = FALSE;          /* flag:  next char is literal */
  2148.     int dotname = FALSE;        /* flag:  path component begins with dot */
  2149.     int error = 0;
  2150.     register unsigned workch;   /* hold the character being tested */
  2151.  
  2152.     if ( renamed )
  2153.     {
  2154.         if ( !(error = checkdir(__G__ pathcomp, APPEND_NAME | USE_DEFAULT)) )
  2155.             strcpy(G.filename, pathcomp);
  2156.         return error;
  2157.     }
  2158.  
  2159. /*---------------------------------------------------------------------------
  2160.     Initialize various pointers and counters and stuff.
  2161.   ---------------------------------------------------------------------------*/
  2162.  
  2163.     /* can create path as long as not just freshening, or if user told us */
  2164.     G.create_dirs = !G.fflag;
  2165.  
  2166.     created_dir = FALSE;        /* not yet */
  2167.  
  2168. /* GRR:  for VMS, convert to internal format now or later? or never? */
  2169.     if (checkdir(__G__ pathcomp, INIT) == 10)
  2170.         return 10;              /* initialize path buffer, unless no memory */
  2171.  
  2172.     *pathcomp = '\0';           /* initialize translation buffer */
  2173.     pp = pathcomp;              /* point to translation buffer */
  2174.     if (G.jflag)                /* junking directories */
  2175. /* GRR:  watch out for VMS version... */
  2176.         cp = (char *)strrchr(G.filename, '/');
  2177.     if (cp == NULL)             /* no '/' or not junking dirs */
  2178.         cp = G.filename;        /* point to internal zipfile-member pathname */
  2179.     else
  2180.         ++cp;                   /* point to start of last component of path */
  2181.  
  2182. /*---------------------------------------------------------------------------
  2183.     Begin main loop through characters in G.filename.
  2184.   ---------------------------------------------------------------------------*/
  2185.  
  2186.     while ((workch = (uch)*cp++) != 0) {
  2187.  
  2188.         if (quote) {              /* if character quoted, */
  2189.             *pp++ = (char)workch; /*  include it literally */
  2190.             quote = FALSE;
  2191.         } else
  2192.             switch (workch) {
  2193.             case '/':             /* can assume -j flag not given */
  2194.                 *pp = '\0';
  2195.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  2196.                     return error;
  2197.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  2198.                 last_dot = NULL;  /* directory names must not contain dots */
  2199.                 lastsemi = NULL;  /* leave directory semi-colons alone */
  2200.                 break;
  2201.  
  2202.             case ':':
  2203.                 *pp++ = '_';      /* drive names not stored in zipfile, */
  2204.                 break;            /*  so no colons allowed */
  2205.  
  2206.             case '.':
  2207.                 if (pp == pathcomp) {     /* nothing appended yet... */
  2208.                     if (*cp == '/') {     /* don't bother appending a "./" */
  2209.                         ++cp;             /*  component to the path:  skip */
  2210.                                           /*  to next char after the '/' */
  2211.                     } else if (*cp == '.' && cp[1] == '/') {   /* "../" */
  2212.                         *pp++ = '.';      /* add first dot, unchanged... */
  2213.                         *pp++ = '.';      /* add second dot, unchanged... */
  2214.                         ++cp;             /* skip second dot */
  2215.                     }                     /* next char is  the '/' */
  2216.                     break;
  2217.                 }
  2218.                 last_dot = pp;    /* point at last dot so far... */
  2219.                 *pp++ = '_';      /* convert dot to underscore for now */
  2220.                 break;
  2221.  
  2222.             case ';':             /* start of VMS version? */
  2223.                 if (lastsemi)
  2224.                     *lastsemi = '_';   /* convert previous one to underscore */
  2225.                 lastsemi = pp;
  2226.                 *pp++ = ';';      /* keep for now; remove VMS vers. later */
  2227.                 break;
  2228.  
  2229.             case ' ':
  2230.                 *pp++ = '_';
  2231.                 break;
  2232.  
  2233.             default:
  2234.                 if ( isalpha(workch) || isdigit(workch) ||
  2235.                     workch=='$' || workch=='-' )
  2236.                     *pp++ = (char)workch;
  2237.                 else
  2238.                     *pp++ = '_';  /* convert everything else to underscore */
  2239.                 break;
  2240.             } /* end switch */
  2241.  
  2242.     } /* end while loop */
  2243.  
  2244.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  2245.  
  2246.     /* if not saving them, remove VMS version numbers (appended "###") */
  2247.     if (lastsemi) {
  2248.         pp = lastsemi + 1;        /* expect all digits after semi-colon */
  2249.         while (isdigit((uch)(*pp)))
  2250.             ++pp;
  2251.         if (*pp)                  /* not version number:  convert ';' to '_' */
  2252.             *lastsemi = '_';
  2253.         else if (!G.V_flag)       /* only digits between ';' and end:  nuke */
  2254.             *lastsemi = '\0';
  2255.         /* else only digits and we're saving version number:  do nothing */
  2256.     }
  2257.  
  2258.     if (last_dot != NULL)         /* one dot is OK:  put it back in */
  2259.         *last_dot = '.';
  2260.  
  2261. /*---------------------------------------------------------------------------
  2262.     Report if directory was created (and no file to create:  filename ended
  2263.     in '/'), check name to be sure it exists, and combine path and name be-
  2264.     fore exiting.
  2265.   ---------------------------------------------------------------------------*/
  2266.  
  2267.     if (G.filename[strlen(G.filename) - 1] == '/') {
  2268.         checkdir(__G__ "", APPEND_NAME);   /* create directory, if not found */
  2269.         checkdir(__G__ G.filename, GETPATH);
  2270.         if (created_dir && QCOND2) {
  2271.             Info(slide, 0, ((char *)slide, "   creating: %s\n", G.filename));
  2272.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  2273.         }
  2274.         return 2;   /* dir existed already; don't look for data to extract */
  2275.     }
  2276.  
  2277.     if (*pathcomp == '\0') {
  2278.         Info(slide, 1, ((char *)slide,
  2279.              "mapname:  conversion of %s failed\n", G.filename));
  2280.         return 3;
  2281.     }
  2282.  
  2283.     checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated:  care? */
  2284.     checkdir(__G__ G.filename, GETPATH);
  2285.  
  2286.     return error;
  2287.  
  2288. } /* end function mapname() */
  2289.  
  2290.  
  2291.  
  2292. int checkdir(__G__ pathcomp, fcn)
  2293. /*
  2294.  * returns:  1 - (on APPEND_NAME) truncated filename
  2295.  *           2 - path doesn't exist, not allowed to create
  2296.  *           3 - path doesn't exist, tried to create and failed; or
  2297.  *               path exists and is not a directory, but is supposed to be
  2298.  *           4 - path is too long
  2299.  *          10 - can't allocate memory for filename buffers
  2300.  */
  2301.     __GDEF
  2302.     char *pathcomp;
  2303.     int fcn;
  2304. {
  2305.     int function=fcn & FN_MASK;
  2306.     static char pathbuf[FILNAMSIZ];
  2307.     static char lastdir[FILNAMSIZ]="\t"; /* directory created last time */
  2308.                                          /* initially - impossible dir. spec. */
  2309.     static char *pathptr=pathbuf;        /* For debugger */
  2310.     static char *devptr, *dirptr, *namptr;
  2311.     static int  devlen, dirlen, namlen;
  2312.     static int  root_dirlen;
  2313.     static char *end;
  2314.     static int  first_comp,root_has_dir;
  2315.     static int  rootlen=0;
  2316.     static char *rootend;
  2317.     static int  mkdir_failed=0;
  2318.     int status;
  2319.  
  2320. /************
  2321.  *** ROOT ***
  2322.  ************/
  2323.  
  2324. #if (!defined(SFX) || defined(SFX_EXDIR))
  2325.     if (function==ROOT)
  2326.     {        /*  Assume VMS root spec */
  2327.         char  *p = pathcomp;
  2328.         char  *q;
  2329.  
  2330.         struct
  2331.         {
  2332.             short  len;
  2333.             short  code;
  2334.             char   *addr;
  2335.         } itl [4] =
  2336.         {
  2337.             {  0,  FSCN$_DEVICE,    NULL  },
  2338.             {  0,  FSCN$_ROOT,      NULL  },
  2339.             {  0,  FSCN$_DIRECTORY, NULL  },
  2340.             {  0,  0,               NULL  }   /* End of itemlist */
  2341.         };
  2342.         int fields = 0;
  2343.         struct dsc$descriptor  pthcmp;
  2344.  
  2345.         /*
  2346.          *  Initialize everything
  2347.          */
  2348.         end = devptr = dirptr = rootend = pathbuf;
  2349.         devlen = dirlen = rootlen = 0;
  2350.  
  2351.         pthcmp.dsc$a_pointer = pathcomp;
  2352.         if ( (pthcmp.dsc$w_length = strlen(pathcomp)) > 255 )
  2353.             return 4;
  2354.  
  2355.         status = sys$filescan(&pthcmp, itl, &fields);
  2356.         if ( !OK(status) )
  2357.             return 3;
  2358.  
  2359.         if ( fields & FSCN$M_DEVICE )
  2360.         {
  2361.             strncpy(devptr = end, itl[0].addr, itl[0].len);
  2362.             dirptr = (end += (devlen = itl[0].len));
  2363.         }
  2364.  
  2365.         root_has_dir = 0;
  2366.  
  2367.         if ( fields & FSCN$M_ROOT )
  2368.         {
  2369.             int   len;
  2370.  
  2371.             strncpy(dirptr = end, itl[1].addr,
  2372.                 len = itl[1].len - 1);        /* Cut out trailing ']' */
  2373.             end += len;
  2374.             root_has_dir = 1;
  2375.         }
  2376.  
  2377.         if ( fields & FSCN$M_DIRECTORY )
  2378.         {
  2379.             char  *ptr;
  2380.             int   len;
  2381.  
  2382.             len = itl[2].len-1;
  2383.             ptr = itl[2].addr;
  2384.  
  2385.             if ( root_has_dir /* i.e. root specified */ )
  2386.             {
  2387.                 --len;                            /* Cut out leading dot */
  2388.                 ++ptr;                            /* ??? [a.b.c.][.d.e] */
  2389.             }
  2390.  
  2391.             strncpy(dirptr=end, ptr, len);  /* Replace trailing ']' */
  2392.             *(end+=len) = '.';                    /* ... with dot */
  2393.             ++end;
  2394.             root_has_dir = 1;
  2395.         }
  2396.  
  2397.         /* When user specified "[a.b.c.]" or "[qq...]", we have too many
  2398.         *  trailing dots. Let's cut them out. Now we surely have at least
  2399.         *  one trailing dot and "end" points just behind it. */
  2400.  
  2401.         dirlen = end - dirptr;
  2402.         while ( dirlen > 1 && end[-2] == '.' )
  2403.             --dirlen,--end;
  2404.  
  2405.         first_comp = !root_has_dir;
  2406.         root_dirlen = end - dirptr;
  2407.         *(rootend = end) = '\0';
  2408.         rootlen = rootend - devptr;
  2409.         return 0;
  2410.     }
  2411. #endif /* !SFX || SFX_EXDIR */
  2412.  
  2413.  
  2414. /************
  2415.  *** INIT ***
  2416.  ************/
  2417.  
  2418.     if ( function == INIT )
  2419.     {
  2420.         if ( strlen(G.filename) + rootlen + 13 > 255 )
  2421.             return 4;
  2422.  
  2423.         if ( rootlen == 0 )     /* No root given, reset everything. */
  2424.         {
  2425.             devptr = dirptr = rootend = pathbuf;
  2426.             devlen = dirlen = 0;
  2427.         }
  2428.         end = rootend;
  2429.         first_comp = !root_has_dir;
  2430.         if ( dirlen = root_dirlen )
  2431.             end[-1] = '.';
  2432.         *end = '\0';
  2433.         return 0;
  2434.     }
  2435.  
  2436.  
  2437. /******************
  2438.  *** APPEND_DIR ***
  2439.  ******************/
  2440.     if ( function == APPEND_DIR )
  2441.     {
  2442.         int cmplen;
  2443.  
  2444.         cmplen = strlen(pathcomp);
  2445.  
  2446.         if ( first_comp )
  2447.         {
  2448.             *end++ = '[';
  2449.             if ( cmplen )
  2450.                 *end++ = '.';   /*       "dir/..." --> "[.dir...]"    */
  2451.             /*                     else  "/dir..." --> "[dir...]"     */
  2452.             first_comp = 0;
  2453.         }
  2454.  
  2455.         if ( cmplen == 1 && *pathcomp == '.' )
  2456.             ; /* "..././..." -- ignore */
  2457.  
  2458.         else if ( cmplen == 2 && pathcomp[0] == '.' && pathcomp[1] == '.' )
  2459.         {   /* ".../../..." -- convert to "...-..." */
  2460.             *end++ = '-';
  2461.             *end++ = '.';
  2462.         }
  2463.  
  2464.         else if ( cmplen + (end-pathptr) > 255 )
  2465.             return 4;
  2466.  
  2467.         else
  2468.         {
  2469.             strcpy(end, pathcomp);
  2470.             *(end+=cmplen) = '.';
  2471.             ++end;
  2472.         }
  2473.         dirlen = end - dirptr;
  2474.         *end = '\0';
  2475.         return 0;
  2476.     }
  2477.  
  2478.  
  2479. /*******************
  2480.  *** APPEND_NAME ***
  2481.  *******************/
  2482.     if ( function == APPEND_NAME )
  2483.     {
  2484.         if ( fcn & USE_DEFAULT )
  2485.         {   /* Expand renamed filename using collected path, return
  2486.              *  at pathcomp */
  2487.             struct        FAB fab;
  2488.             struct        NAM nam;
  2489.  
  2490.             fab = cc$rms_fab;
  2491.             fab.fab$l_fna = G.filename;
  2492.             fab.fab$b_fns = strlen(G.filename);
  2493.             fab.fab$l_dna = pathptr;
  2494.             fab.fab$b_dns = end-pathptr;
  2495.  
  2496.             fab.fab$l_nam = &nam;
  2497.             nam = cc$rms_nam;
  2498.             nam.nam$l_esa = pathcomp;
  2499.             nam.nam$b_ess = 255;            /* Assume large enaugh */
  2500.  
  2501.             if (!OK(status = sys$parse(&fab)) && status == RMS$_DNF )
  2502.                                          /* Directory not found: */
  2503.             {                            /* ... try to create it */
  2504.                 char    save;
  2505.                 char    *dirend;
  2506.                 int     mkdir_failed;
  2507.  
  2508.                 dirend = (char*)nam.nam$l_dir + nam.nam$b_dir;
  2509.                 save = *dirend;
  2510.                 *dirend = '\0';
  2511.                 if ( (mkdir_failed = mkdir(nam.nam$l_dev, 0)) &&
  2512.                      errno == EEXIST )
  2513.                     mkdir_failed = 0;
  2514.                 *dirend = save;
  2515.                 if ( mkdir_failed )
  2516.                     return 3;
  2517.                 created_dir = TRUE;
  2518.             }                                /* if (sys$parse... */
  2519.             pathcomp[nam.nam$b_esl] = '\0';
  2520.             return 0;
  2521.         }                                /* if (USE_DEFAULT) */
  2522.         else
  2523.         {
  2524.             *end = '\0';
  2525.             if ( dirlen )
  2526.             {
  2527.                 dirptr[dirlen-1] = ']'; /* Close directory */
  2528.  
  2529.                 /*
  2530.                  *  Try to create the target directory.
  2531.                  *  Don't waste time creating directory that was created
  2532.                  *  last time.
  2533.                  */
  2534.                 if ( STRICMP(lastdir,pathbuf) )
  2535.                 {
  2536.                     mkdir_failed = 0;
  2537.                     if ( mkdir(pathbuf,0) )
  2538.                     {
  2539.                         if ( errno != EEXIST )
  2540.                             mkdir_failed = 1;   /* Mine for GETPATH */
  2541.                     }
  2542.                     else
  2543.                         created_dir = TRUE;
  2544.                     strcpy(lastdir,pathbuf);
  2545.                 }
  2546.             }
  2547.             else
  2548.             {   /*
  2549.                  * Target directory unspecified.
  2550.                  * Try to create "sys$disk:[]"
  2551.                  */
  2552.                 if ( strcmp(lastdir,"sys$disk:[]") )
  2553.                 {
  2554.                     strcpy(lastdir,"sys$disk:[]");
  2555.                     mkdir_failed = 0;
  2556.                     if ( mkdir(lastdir,0) && errno != EEXIST )
  2557.                         mkdir_failed = 1;   /* Mine for GETPATH */
  2558.                 }
  2559.             }
  2560.             if ( strlen(pathcomp) + (end-pathbuf) > 255 )
  2561.                 return 1;
  2562.             strcpy(end, pathcomp);
  2563.             end += strlen(pathcomp);
  2564.             return 0;
  2565.         }
  2566.     }
  2567.  
  2568.  
  2569. /***************
  2570.  *** GETPATH ***
  2571.  ***************/
  2572.     if ( function == GETPATH )
  2573.     {
  2574.         if ( mkdir_failed )
  2575.             return 3;
  2576.         *end = '\0';                    /* To be safe */
  2577.         strcpy( pathcomp, pathbuf );
  2578.         return 0;
  2579.     }
  2580.  
  2581.  
  2582. /***********
  2583.  *** END ***
  2584.  ***********/
  2585.     if ( function == END )
  2586.     {
  2587.         Trace((stderr, "checkdir(): nothing to free...\n"));
  2588.         return 0;
  2589.     }
  2590.  
  2591.     return 99;  /* should never reach */
  2592.  
  2593. }
  2594.  
  2595.  
  2596.  
  2597. int check_for_newer(__G__ filenam)   /* return 1 if existing file newer or */
  2598.     __GDEF                           /*  equal; 0 if older; -1 if doesn't */
  2599.     char *filenam;                   /*  exist yet */
  2600. {
  2601. #ifdef USE_EF_UX_TIME
  2602.     ztimbuf z_utime;
  2603. #endif
  2604.     unsigned short timbuf[7];
  2605.     unsigned dy, mo, yr, hh, mm, ss, dy2, mo2, yr2, hh2, mm2, ss2;
  2606.     struct FAB fab;
  2607.     struct XABDAT xdat;
  2608.  
  2609.  
  2610.     if (stat(filenam, &G.statbuf))
  2611.         return DOES_NOT_EXIST;
  2612.  
  2613.     fab  = cc$rms_fab;
  2614.     xdat = cc$rms_xabdat;
  2615.  
  2616.     fab.fab$l_xab = (char *) &xdat;
  2617.     fab.fab$l_fna = filenam;
  2618.     fab.fab$b_fns = strlen(filenam);
  2619.     fab.fab$l_fop = FAB$M_GET | FAB$M_UFO;
  2620.  
  2621.     if ((sys$open(&fab) & 1) == 0)       /* open failure:  report exists and */
  2622.         return EXISTS_AND_OLDER;         /*  older so new copy will be made  */
  2623.     sys$numtim(&timbuf,&xdat.xab$q_cdt);
  2624.     fab.fab$l_xab = NULL;
  2625.  
  2626.     sys$dassgn(fab.fab$l_stv);
  2627.     sys$close(&fab);   /* be sure file is closed and RMS knows about it */
  2628.  
  2629. #ifdef USE_EF_UX_TIME
  2630.     if (G.extra_field &&
  2631.         ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length,
  2632.                          &z_utime, NULL) > 0) {
  2633.         struct tm *t = localtime(&(z_utime.modtime));
  2634.  
  2635.         yr2 = (unsigned)(t->tm_year) + 1900;
  2636.         mo2 = (unsigned)(t->tm_mon) + 1;
  2637.         dy2 = (unsigned)(t->tm_mday);
  2638.         hh2 = (unsigned)(t->tm_hour);
  2639.         mm2 = (unsigned)(t->tm_min);
  2640.         ss2 = (unsigned)(t->tm_sec);
  2641.  
  2642.         /* round to nearest sec--may become 60,
  2643.            but doesn't matter for compare */
  2644.         ss = (unsigned)((float)timbuf[5] + (float)timbuf[6]*.01 + 0.5);
  2645.         TTrace((stderr, "check_for_newer:  using Unix extra field mtime\n"));
  2646.     } else {
  2647.         yr2 = ((G.lrec.last_mod_file_date >> 9) & 0x7f) + 1980;
  2648.         mo2 = ((G.lrec.last_mod_file_date >> 5) & 0x0f);
  2649.         dy2 = (G.lrec.last_mod_file_date & 0x1f);
  2650.         hh2 = (G.lrec.last_mod_file_time >> 11) & 0x1f;
  2651.         mm2 = (G.lrec.last_mod_file_time >> 5) & 0x3f;
  2652.         ss2 = (G.lrec.last_mod_file_time & 0x1f) * 2;
  2653.  
  2654.         /* round to nearest 2 secs--may become 60,
  2655.            but doesn't matter for compare */
  2656.         ss = (unsigned)((float)timbuf[5] + (float)timbuf[6]*.01 + 1.) & (~1);
  2657.     }
  2658. #else /* !USE_EF_UX_TIME */
  2659.     yr2 = ((G.lrec.last_mod_file_date >> 9) & 0x7f) + 1980;
  2660.     mo2 = ((G.lrec.last_mod_file_date >> 5) & 0x0f);
  2661.     dy2 = (G.lrec.last_mod_file_date & 0x1f);
  2662.     hh2 = (G.lrec.last_mod_file_time >> 11) & 0x1f;
  2663.     mm2 = (G.lrec.last_mod_file_time >> 5) & 0x3f;
  2664.     ss2 = (G.lrec.last_mod_file_time & 0x1f) * 2;
  2665.  
  2666.     /* round to nearest 2 secs--may become 60, but doesn't matter for compare */
  2667.     ss = (unsigned)((float)timbuf[5] + (float)timbuf[6]*.01 + 1.) & (~1);
  2668. #endif /* ?USE_EF_UX_TIME */
  2669.     yr = timbuf[0];
  2670.     mo = timbuf[1];
  2671.     dy = timbuf[2];
  2672.     hh = timbuf[3];
  2673.     mm = timbuf[4];
  2674.  
  2675.     if (yr > yr2)
  2676.         return EXISTS_AND_NEWER;
  2677.     else if (yr < yr2)
  2678.         return EXISTS_AND_OLDER;
  2679.  
  2680.     if (mo > mo2)
  2681.         return EXISTS_AND_NEWER;
  2682.     else if (mo < mo2)
  2683.         return EXISTS_AND_OLDER;
  2684.  
  2685.     if (dy > dy2)
  2686.         return EXISTS_AND_NEWER;
  2687.     else if (dy < dy2)
  2688.         return EXISTS_AND_OLDER;
  2689.  
  2690.     if (hh > hh2)
  2691.         return EXISTS_AND_NEWER;
  2692.     else if (hh < hh2)
  2693.         return EXISTS_AND_OLDER;
  2694.  
  2695.     if (mm > mm2)
  2696.         return EXISTS_AND_NEWER;
  2697.     else if (mm < mm2)
  2698.         return EXISTS_AND_OLDER;
  2699.  
  2700.     if (ss >= ss2)
  2701.         return EXISTS_AND_NEWER;
  2702.  
  2703.     return EXISTS_AND_OLDER;
  2704. }
  2705.  
  2706.  
  2707.  
  2708. #ifdef RETURN_CODES
  2709. void return_VMS(__G__ ziperr)
  2710.     __GDEF
  2711. #else
  2712. void return_VMS(ziperr)
  2713. #endif
  2714.     int ziperr;
  2715. {
  2716.     int severity = (ziperr == 2 || (ziperr >= 9 && ziperr <= 11))? 2 : 4;
  2717.  
  2718. #ifdef RETURN_CODES
  2719. /*---------------------------------------------------------------------------
  2720.     Do our own, explicit processing of error codes and print message, since
  2721.     VMS misinterprets return codes as rather obnoxious system errors ("access
  2722.     violation," for example).
  2723.   ---------------------------------------------------------------------------*/
  2724.  
  2725.     switch (ziperr) {
  2726.         case PK_COOL:
  2727.             break;   /* life is fine... */
  2728.         case PK_WARN:
  2729.             Info(slide, 1, ((char *)slide, "\n\
  2730. [return-code %d:  warning error \
  2731. (e.g., failed CRC or unknown compression method)]\n", ziperr));
  2732.             break;
  2733.         case PK_ERR:
  2734.         case PK_BADERR:
  2735.             Info(slide, 1, ((char *)slide, "\n\
  2736. [return-code %d:  error in zipfile \
  2737. (e.g., can't find local file header sig)]\n", ziperr));
  2738.             break;
  2739.         case PK_MEM:
  2740.         case PK_MEM2:
  2741.         case PK_MEM3:
  2742.         case PK_MEM4:
  2743.         case PK_MEM5:
  2744.             Info(slide, 1, ((char *)slide,
  2745.               "\n[return-code %d:  insufficient memory]\n", ziperr));
  2746.             break;
  2747.         case PK_NOZIP:
  2748.             Info(slide, 1, ((char *)slide,
  2749.               "\n[return-code %d:  zipfile not found]\n", ziperr));
  2750.             break;
  2751.         case PK_PARAM:   /* exit(PK_PARAM); gives "access violation" */
  2752.             Info(slide, 1, ((char *)slide, "\n\
  2753. [return-code %d:  bad or illegal parameters specified on command line]\n",
  2754.               ziperr));
  2755.             break;
  2756.         case PK_FIND:
  2757.             Info(slide, 1, ((char *)slide,
  2758.               "\n[return-code %d:  no files found to extract/view/etc.]\n",
  2759.               ziperr));
  2760.             break;
  2761.         case PK_DISK:
  2762.             Info(slide, 1, ((char *)slide,
  2763.               "\n[return-code %d:  disk full or other I/O error]\n", ziperr));
  2764.             break;
  2765.         case PK_EOF:
  2766.             Info(slide, 1, ((char *)slide, "\n\
  2767. [return-code %d:  unexpected EOF in zipfile (i.e., truncated)]\n", ziperr));
  2768.             break;
  2769.         default:
  2770.             Info(slide, 1, ((char *)slide,
  2771.               "\n[return-code %d:  unknown return-code (screw-up)]\n", ziperr));
  2772.             break;
  2773.     }
  2774. #endif /* RETURN_CODES */
  2775.  
  2776. /*---------------------------------------------------------------------------
  2777.     Return an intelligent status/severity level:
  2778.  
  2779.         $STATUS          $SEVERITY = $STATUS & 7
  2780.         31 .. 16 15 .. 3   2 1 0
  2781.                            -----
  2782.         VMS                0 0 0  0    Warning
  2783.         FACILITY           0 0 1  1    Success
  2784.         Number             0 1 0  2    Error
  2785.                  MESSAGE   0 1 1  3    Information
  2786.                  Number    1 0 0  4    Severe (fatal) error
  2787.  
  2788.     0x7FFF0000 was chosen (by experimentation) to be outside the range of
  2789.     VMS FACILITYs that have dedicated message numbers.  Hopefully this will
  2790.     always result in silent exits--it does on VMS 5.4.  Note that the C li-
  2791.     brary translates exit arguments of zero to a $STATUS value of 1 (i.e.,
  2792.     exit is both silent and has a $SEVERITY of "success").
  2793.   ---------------------------------------------------------------------------*/
  2794.  
  2795.     exit(                                          /* $SEVERITY:        */
  2796.          (ziperr == PK_COOL) ? 1 :                 /*   success         */
  2797.          (ziperr == PK_WARN) ? 0x7FFF0000 :        /*   warning         */
  2798.          (0x7FFF0000 | (ziperr << 4) | severity)   /*   error or fatal  */
  2799.         );
  2800.  
  2801. } /* end function return_VMS() */
  2802.  
  2803.  
  2804. #ifdef MORE
  2805. int screenlines(void)
  2806. {
  2807.     /*
  2808.      * For VMS v5.x:
  2809.      *   IO$_SENSEMODE/SETMODE info:  Programming, Vol. 7A, System Programming,
  2810.      *     I/O User's: Part I, sec. 8.4.1.1, 8.4.3, 8.4.5, 8.6
  2811.      *   sys$assign(), sys$qio() info:  Programming, Vol. 4B, System Services,
  2812.      *     System Services Reference Manual, pp. sys-23, sys-379
  2813.      *   fixed-length descriptor info:  Programming, Vol. 3, System Services,
  2814.      *     Intro to System Routines, sec. 2.9.2
  2815.      * GRR, 15 Aug 91 / SPC, 07 Aug 1995
  2816.      */
  2817.  
  2818. #ifndef OUTDEVICE_NAME
  2819. #define OUTDEVICE_NAME  "SYS$OUTPUT"
  2820. #endif
  2821.  
  2822.     static int scrnlines = -1;
  2823.  
  2824.     static const struct dsc$descriptor_s OutDevDesc =
  2825.         {(sizeof(OUTDEVICE_NAME) - 1), DSC$K_DTYPE_T, DSC$K_CLASS_S,
  2826.          OUTDEVICE_NAME};
  2827.      /* {dsc$w_length, dsc$b_dtype, dsc$b_class, dsc$a_pointer}; */
  2828.  
  2829.     short  OutDevChan, iosb[4];
  2830.     long   status;
  2831.     struct tt_characts
  2832.     {
  2833.         uch class, type;
  2834.         ush pagewidth;
  2835.         uch ttcharsbits[3];
  2836.         uch pagelength;
  2837.     }      ttmode;              /* total length = 8 bytes */
  2838.  
  2839.  
  2840.     if (scrnlines < 0)
  2841.     {
  2842.         /* assign a channel to standard output */
  2843.         status = sys$assign(&OutDevDesc, &OutDevChan, 0, 0);
  2844.         if (status & 1)
  2845.         {
  2846.             /* use sys$qiow and the IO$_SENSEMODE function to determine
  2847.              * the current tty status.
  2848.              */
  2849.             status = sys$qiow(0, OutDevChan, IO$_SENSEMODE, &iosb, 0, 0,
  2850.                               &ttmode, sizeof(ttmode), 0, 0, 0, 0);
  2851.             /* deassign the output channel by way of clean-up */
  2852.             (void) sys$dassgn(OutDevChan);
  2853.         }
  2854.  
  2855.         scrnlines = ( ( (status & 1) &&
  2856.                         ((status = iosb[0]) & 1) &&
  2857.                         (ttmode.pagelength >= 5)
  2858.                       )
  2859.                      ? (int) (ttmode.pagelength)        /* TT device value */
  2860.                      : (24) );                          /* VT 100 default  */
  2861.     }
  2862.  
  2863.     return (scrnlines);
  2864. }
  2865. #endif /* MORE */
  2866.  
  2867.  
  2868. #ifndef SFX
  2869.  
  2870. /************************/
  2871. /*  Function version()  */
  2872. /************************/
  2873.  
  2874. void version(__G)
  2875.     __GDEF
  2876. {
  2877.     int len;
  2878. #ifdef VMS_VERSION
  2879.     char buf[40];
  2880. #endif
  2881. #ifdef __DECC_VER
  2882.     char buf2[40];
  2883.     int  vtyp;
  2884. #endif
  2885.  
  2886. /*  DEC C in ANSI mode does not like "#ifdef MACRO" inside another
  2887.     macro when MACRO is equated to a value (by "#define MACRO 1").   */
  2888.  
  2889.     len = sprintf((char *)slide, LoadFarString(CompiledWith),
  2890.  
  2891. #ifdef __GNUC__
  2892.       "gcc ", __VERSION__,
  2893. #else
  2894. #  if defined(DECC) || defined(__DECC) || defined (__DECC__)
  2895.       "DEC C",
  2896. #    ifdef __DECC_VER
  2897.       (sprintf(buf2, " %c%d.%d-%03d",
  2898.                ((vtyp = (__DECC_VER / 10000) % 10) == 6 ? 'T' :
  2899.                 (vtyp == 8 ? 'S' : 'V')),
  2900.                __DECC_VER / 10000000,
  2901.                (__DECC_VER % 10000000) / 100000, __DECC_VER % 1000), buf2),
  2902. #    else
  2903.       "",
  2904. #    endif
  2905. #  else
  2906. #  ifdef VAXC
  2907.       "VAX C", "",
  2908. #  else
  2909.       "unknown compiler", "",
  2910. #  endif
  2911. #  endif
  2912. #endif
  2913.  
  2914. #ifdef VMS_VERSION
  2915. #  if defined(__alpha)
  2916.       "OpenVMS",   /* version has trailing spaces ("V6.1   "), so truncate: */
  2917.       (sprintf(buf, " (%.4s for Alpha)", VMS_VERSION), buf),
  2918. #  else /* VAX */
  2919.       (VMS_VERSION[1] >= '6')? "OpenVMS" : "VMS",
  2920.       (sprintf(buf, " (%.4s for VAX)", VMS_VERSION), buf),
  2921. #  endif
  2922. #else
  2923.       "VMS",
  2924.       "",
  2925. #endif /* ?VMS_VERSION */
  2926.  
  2927. #ifdef __DATE__
  2928.       " on ", __DATE__
  2929. #else
  2930.       "", ""
  2931. #endif
  2932.     );
  2933.  
  2934.     (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  2935.  
  2936. } /* end function version() */
  2937.  
  2938. #endif /* !SFX */
  2939. #endif /* VMS */
  2940.