home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip511.zip / vms / vms.c < prev    next >
C/C++ Source or Header  |  1994-07-19  |  66KB  |  2,619 lines

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