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