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