home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp4 / ckvfio.c < prev    next >
C/C++ Source or Header  |  2009-10-16  |  202KB  |  6,163 lines

  1. #ifndef VMS
  2.       ERROR -- CKVFIO.C is used only on the OpenVMS(tm) Operating System
  3. #endif /* VMS */
  4.  
  5. #if defined(__ia64) || defined(__ia64__)
  6. # define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) IA64"
  7.  
  8. #else
  9. # if defined(__ALPHA) || defined(__alpha)
  10. #  define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) Alpha(tm)"
  11.          /* do nothing */
  12. #else
  13. # ifdef VAX
  14. #  define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) VAX(tm)"
  15. # else
  16. #  ifdef __GNUC__
  17. #     define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) VAX(tm) (GCC)"
  18. #  else
  19. #     ERROR -- CKVFIO.C unknown architecture - Not VAX ALPHA or IA64
  20. #  endif /* __GNUC__ */
  21. # endif /* VAX */
  22. #endif /* __ALPHA */
  23. #endif /* __IA64 */
  24.  
  25. char *ckzv = "File support, 9.0.181,  16 Oct 2009";
  26. char *ckzsys = CKVFIO_OS_ARCH_STRING;
  27.  
  28. /* C K V F I O  --  Kermit file system support for VAX/VMS.  */
  29.  
  30. /*
  31.   Author: Frank da Cruz <fdc@columbia.edu>
  32.   Columbia University Academic Information Systems, New York City.
  33.  
  34.   Copyright (C) 1985, 2009,
  35.     Trustees of Columbia University in the City of New York.
  36.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  37.     copyright text in the ckcmai.c module for disclaimer and permissions.
  38. */
  39.  
  40. /*
  41.   Originally adapted to VMS by:
  42.   Stew Rubenstein, Harvard University Chemical Labs, 1985,
  43.   Contributors:
  44.   Frank da Cruz (fdc), Columbia University, New York City (1985-present)
  45.   Terry Kennedy (TMK), St. Peter's College, Jersey City, NJ (1990-present)
  46.   William Bader (WB), Lehigh University, Bethlehem, PA (1990-present)
  47.   Mark Berryman (mb), SAIC (1994-present)
  48.   Lucas Hart (LH), Oregon State U, (1998-present)
  49.   Stew Rubenstein, Harvard University Chemical Labs, Cambridge, MA (1985)
  50.   Martin Minow (MM), Digital Equipment Corporation, Maynard MA (1985) (d.2000)
  51.   Dan Schullman (DS), Digital Equipment Corporation, Maynard MA (1985)
  52.   Mark Buda (MAB), Digital Equipment Corporation, Nashua, NH (1989-90)
  53.   Gary Mussar (GM), Bell-Northern Research, Ottawa, Canada (1991)
  54.   James Sturdevant (JS) (1992)
  55.   Tarjei T. Jensen (ttj), Norwegian Hydrographic Service (1993-94)
  56.   Martin PJ Zinzer (mpjz), Gesellschaft fuer Schwerionenforschung GSI Darmstadt
  57. */
  58. /* Edit history
  59.  * 003 20-Mar-85 MM  fixed fprintf bug in zsout.c
  60.  * 004 21-Mar-84 MM  create text files in variable-stream.
  61.  * 005  8-May-85 MM  filled in zkself (not tested), fixed other minor bugs
  62.  * 006  5-Jul-85 DS  handle version number in zltor, zrtol
  63.  * 007 11-Jul-85 fdc fix zclose() to give return codes
  64.  * 008 19-Mar-86 fdc Fix system() for "!", zopeni() for REMOTE commands.
  65.  * 008 17-Sep-87 fdc Define PWDCMD.
  66.  * 009 (???)
  67.  * 010 24-Jan-88 fdc Add zgtdir() function, even tho it doesn't work...
  68.  * 011 14-Feb-89 mab Make zgtdir() work in V2/V3 C envirements,
  69.  *             Make zkself work using delprc() using Will Wood's changes.
  70.  * 012 26-Feb-89 mab Add function that searches for kermit.ini file in various
  71.  *                   ways
  72.  * 013 05-Mar-89 mab Add Barry Archers enhancements/fixes.
  73.  * 014 15-Mar-89 mab Check for non-null data, not array of pointers in
  74.  *                   zkermini
  75.  * 015 04-Apr-89 mab Add latent support for attribute packet.  Clean up
  76.  *             file name translation code.
  77.  * 016 05-Apr-89 mab Add PWP code to optimize packetizing.
  78.  * 017 16-Apr-89 mab PWP changes broke REMOTE command.  Fixed.
  79.  * 018 18-Apr-89 mab #ifdef chkfn.  This removes a lot of overhead.
  80.  *             Add code to gtdir() for V4.x.
  81.  * 019 12-Jun-89 mab Add PWP's encode logic
  82.  * 020 09-Jul-89 mab Add logic to check for system() availability
  83.  * 021 10-Jul-89 mab Fix SHOW USER USERNAME.  Added space after 'SHOW USER'.
  84.  * 022 27-Sep-89 mab Added zmail/zprint, plus added changes from CKUFIO.C
  85.  * 023 01-Dec-89 mab Add RMS file support
  86.  * 024 20-Jul-90 wb  Add support for old VAX C & VMS versions + zstrip & rename
  87.  * 025 29-Jul-90 tmk Change space command to show avail, not used (match spec)
  88.  * 026 29-Jul-90 tmk Hack out the RMS stuff - it can come back when it works
  89.  * 027 29-Jul-90 tmk Likewise the VMS V3 stuff - ancient history
  90.  * 028 29-Jul-90 tmk Replace the attribute stuff. It now works.
  91.  * 029 31-Jul-90 tmk Fix CWD command (via hack)
  92.  * 030 31-Jul-90 tmk Fix assorted bugs preventing remote commands from working
  93.  * 031 31-Jul-90 tmk Correctly handle interrupted remote commands
  94.  * 032 04-Aug-90 tmk Start work on full RMS support for input files
  95.  * 033 04-Aug-90 tmk Tack LF on end of subprocess output lines
  96.  * 034 04-Aug-90 tmk Complete work on full RMS support for input files
  97.  * 035 04-Aug-90 tmk Add support for Fortran CC, fill in recfm data
  98.  * 036 05-Aug-90 tmk Add trailing CRLF on print format files
  99.  * 037 12-Aug-90 tmk Start work on full RMS support for output files
  100.  * 038 12-Aug-90 tmk Honor first free byte (FFB) on SENDs
  101.  * 039 13-Aug-90 tmk Finished first cut of full RMS support for output files
  102.  * 040 29-Sep-90 tmk Add iswild() from FDC for C-Kermit 5A edit 157
  103.  * 041 06-Oct-90 tmk Add filetype IMAGE support for outbound transfers. Note
  104.  *             that this doesn't currently work as the receiver overrides
  105.  *             it (must talk to fdc).
  106.  * 042 06-Oct-90 tmk Make logfiles MRS=80. Being able to edit them outweighs
  107.  *             any use for un-split lines.
  108.  * 043 17-Oct-90 wb  Make zclosf() remove delete mailboxes & deassign channels
  109.  *                   used to talk to the subprocess, so quotas are not used
  110.  *                   up after repeated mailbox use (installed by fdc).
  111.  * 044 19-Oct-90 fdc Changed zxcmd() to use the fp[] arrays in the normal way,
  112.  *                   and zsyscmd to call zxcmd(ZIFILE) rather than
  113.  *                   zxcmd(ZSYSFN).  Got rid of all calls to system(), used
  114.  *                   zsyscmd() instead, so commands like DIR could be
  115.  *                   interrupted.  Made zoutdump() return(-1) rather than
  116.  *                   exit() when "line too long for buffer", and increased
  117.  *                   line output buffer from 1K to 4K.
  118.  * 045 01-Nov-90 tmk Corrected behavior of error check on $create call so a
  119.  *             file supersede would work properly.
  120.  * 046 01-Nov-90 tmk Clone binary flag to ofile_bmode so we have a consistent
  121.  *             view of this flag during file operations - the binary flag
  122.  *             tends to toggle when we don't want/expect it to.
  123.  * 047 01-Nov-90 tmk Make IMAGE mode work. Note that image mode is only used
  124.  *             when VMS is sending a file, and includes all record
  125.  *             control characters not normally sent. Only useful in
  126.  *             unusual circumstances.
  127.  * 048 01-Nov-90 tmk Remove spurious \n from zsoutl() which caused debug logs
  128.  *             to have spurious <CR>'s when viewed with editors.
  129.  * 049 02-Nov-90 fdc Adapt to dynamic allocation of file i/o buffers.  Changes
  130.  *                   are within #ifdef DYNAMIC..#else..#endif brackets.
  131.  * 050 02-Nov-90 fdc Make zsyscmd() close inferior process.
  132.  * 051 ??-???-?? ??? Add ckermit_init logical, return 0 on wildcard operations.
  133.  * 052 24-Dec-90 tmk Fix performance problems after 32Kb w/ ASCII receives, fix
  134.              2-nulls-per-32Kb in binary mode bug (actually in ckcker.h,
  135.              this is a placeholder).
  136.  * 053 13-Jan-91 tmk Add support for SET FILE RECORD-LENGTH.
  137.  * 054 14-Jan-91 tmk Fix cases of /x/CR/LF/y/ and /x/CR/LF/y/CR/LF/ in ASCII
  138.  *             file receives.
  139.  * 055 16-Jan-91 tmk Log requested file type to debug log when receiving.
  140.  * 056 16-Jan-91 tmk Add support for all zstime() functions.
  141.  * 057 17-Jan-91 tmk Add support for zchkspa() function.
  142.  * 058 17-Jan-91 tmk Move debug() call into if clause in zxpand, per fdc.
  143.  * 059 18-Jan-91 tmk Support remote (DECnet) file accesses.
  144.  * 060 18-Jan-91 tmk Fix READ command.
  145.  * 061 30-Jan-91 tmk Support creation of UNDEFINED file types for brain-dead
  146.  *             BASIC implementation.
  147.  * 062 30-Jan-91 tmk Fix REMOTE commands when VERIFY is set.
  148.  * 063 29-Mar-91 tmk Add padding factor for received text files to accomodate
  149.  *             space taken up by record delimiters (per fdc).
  150.  * 064 29-Mar-91 gm  Remove unnecessary mem-mem moves during ASCII receives.
  151.  *             (Installed by tmk. To back out, #define OLD_WAY).
  152.  * 065 30-Mar-91 tmk First pass at implementing LABELED. Send only, dummy
  153.  *             data records.
  154.  * 066 02-Apr-91 tmk Finish first pass at LABELED. Send VMS filename, attri-
  155.  *             butes. Still need ACL's, "hidden" char. longword, recep-
  156.  *             tion.
  157.  * 067 09-Apr-91 tmk LABELED bugfixes - VMSFILE is 70 bytes, not 74, use the
  158.  *             xab$w_lrl field instead of rab$w_rsz, fab$w_deq instead
  159.  *             of xab$w_rsz, fab$b_bks instead of xab$b_bkz, always pro-
  160.  *             cess an even multiple of 512 bytes when LABELED.
  161.  * 068 14-Apr-91 tmk Don't use C definition of fab$b_journal as it doesn't ex-
  162.  *             ist before C V3.1. Compute it ourselves instead.
  163.  * 069 15-Apr-91 tmk Initial work on retrieving ACL information for LABELED.
  164.  * 070 16-Apr-91 tmk Make edits 066-069 compatible with DECnet.
  165.  * 071 21-May-91 tmk Address R. Weiner QAR item 2 (filesize).
  166.  * 072 21-Jun-91 tmk Check (and prohibit) spawns from captive accounts.
  167.  * 073 21-Jun-91 tmk Fix session logging (for Charlie Luce/DECUServe).
  168.  * 074 21-Jun-91 tmk Rework 071 to only apply to SPAWN/PUSH and not to the
  169.  *             pseudo-builtins like DEL, SPACE, WHO, PWD, etc.
  170.  * 075 21-Jun-91 tmk Fix possible endless loop when flushing output file in
  171.  *             zclosf() after zoutdump() error.
  172.  * 076 21-Jun-91 tmk First pass on handling inbound LABELED files.
  173.  * 077 14-Nov-91 tmk Fix zprint(), zmail() (need to use system() for these).
  174.  *                   This is a partial backout of 044.
  175.  * 078 14-Nov-91 tmk Various cleanups.  Delete files after successful mailing
  176.  *             or printing, remove dead code inside #ifdef COMMENT and
  177.  *                   #ifdef OLD_WAY, fix typo in spawning message, make sure
  178.  *                   all source lines < 80 chars.
  179.  * 079 22-Nov-91 fdc Change zmail(), zprint() error return values to improve
  180.  *             error reporting.
  181.  * 080 18-Jan-92 tmk Fix REMOTE so output from a remote command correctly dis-
  182.  *             plays on terminal. This has been broken since 040 or so.
  183.  * 081 10-Jun-92 tmk Add William Bader's fix for fixed-length files which have
  184.  *                   record attributes.
  185.  * 082 03-Jul-92 tmk Fix really bad bug introduced in 081 (which made *all*
  186.  *                   fixed-format files be sent as text).
  187.  * 083 15-Jul-92 jah Fix fencepost error in zoutdump when line breaks at 32K.
  188.  * 084 03-Aug-92 fdc Remove current directory from init file search.
  189.  * 085 26-Aug-92 tmk Add Bernd Onasch's fix for fgen().
  190.  * 086 28-Aug-92 tmk Fix bug reported by Bill Hoelzer where C-K would execute
  191.  *             a file named "." as a C-K initialization file.
  192.  * 087 04-Sep-92 tmk Fix bug reported by Chuck McMichael where C-K would not
  193.  *             set the FFB properly when receiving a labeled file which
  194.  *             did not have the FFB on a record boundary.
  195.  * 088 09-Sep-92 tmk Fix Hunter Goatley's problem with SPAWN command ignoring
  196.  *             Ctrl-C.
  197.  * 089 11-Sep-92  js Fixed malloc() in zmail().
  198.  * 090 28-Oct-92 tmk Fix null-byte error introduced by 087. Gee, this looked
  199.  *             so simple when I designed it.
  200.  * 091 02-Nov-92 tmk Start work on fixing spawn/push/remote commands, due to
  201.  *             popular whining.
  202.  * 092 03-Nov-92 tmk Finish up initial 091 work. Vote for Kermit!
  203.  * 093 03-Nov-92 fdc Change zkermini() to work with "-y" command-line option.
  204.  * 094 04-Nov-92 tmk Make zxpand() not return all files if given null string.
  205.  * 095 05-Nov-92 fdc Make zxcmd(), zclose(), etc, handle ZRFILE (OPEN !READ).
  206.  * 096 17-Feb-93 fdc prevent zopeno from calling zstime if date struct is NULL,
  207.  *                   and add support for ZMFILE (misc output file).
  208.  * 097 08-Apr-93 tmk Correctly handle "international VMS" which uses <> instead
  209.  *             of [] for directory delimiters.
  210.  * 098 16-May-93 fdc ANSIfication for GNU CC, from James Sturdevant, plus
  211.  *                   add FAB$M_PRN to list of text-file types, for VMS batch
  212.  *                   logs.
  213.  * 099 07-Jun-93 fdc Fix calculation of file size in zchki(), fix declaration
  214.  *                   of mbxnam[] (add one to size) to prevent overflow, which
  215.  *                   would result in failure of server to respond to REMOTE
  216.  *                   directory, etc.  Both fixes from Bill Glass.
  217.  * 100 21-Jun-93 fdc file_date[] and attr_date[] declarations in zstime()
  218.  *                   changed from long to unsigned long to prevent signed date
  219.  *                   comparisons, which could prevent SET FILE COLLISION
  220.  *                   UPDATE from working.  From James Sturdevant.
  221.  * 101  8-Aug-93 fdc Add types to all function declarations.
  222.  * 102 18-Aug-93 ttj Minor updates in zsattr() and do_label_recv() mainly to
  223.  *                   quieten the compiler (which had every reason to complain).
  224.  * 103  5-Nov-93 wb   Add isdir() function.
  225.  * 104  8-Nov-93 wb   Add zfcdate() function.
  226.  * 105 25-Nov-93 fdc  Correct record format for session log; change name of
  227.  *                    password structure member of zattr struct.
  228.  * 106 22-Dec-93 tmk  Correct fd "leakage" in OPEN READ/CLOSE READ pairs.
  229.  * 107 26-Feb-94 mb   Addition of zmkdir() routine.
  230.  * 108 27-Mar-94 tmk  Increase max record size for logs from 80 to 254.
  231.  *                    Add support for file append operations.
  232.  *                    Make zkself() retry a few times to avoid zombies on BYE.
  233.  * 109  5-Apr-94 tmk  Fix xx->lengthk not being set in zsattr().
  234.  * 110  8-Jun-94 tmk  Use private fab in zchki (fixes OPEN READ bug).
  235.  * 111 17-Jun-94 tmk  Let zsattr() work even if there is no rights database.
  236.  * 112  7-Jul-94 js   A couple small ANSIfications for gcc.
  237.  * 113  7-Aug-94 fdc  Make zshcmd()/zsyscmd() return proper return code, with
  238.  *                    help from Larry Henry at TWG.  Still not quite right...
  239.  * 114 29-Sep-94 fdc  Increase max wildcard matches from 500 to 4096.
  240.  * 115  4-Oct-94 mb   Add support for RESEND.
  241.  * 116 26-Oct-94 mb   Minor fix to 115.
  242.  * 117  1-Nov-94 wb   A couple #ifdefs added to allow compilation in VMS v4.
  243.  * 118 25-Feb-95 mpjz Fix for DECC on VAX.
  244.  * 119 11-May-96 fdc  Change znewn() to change version to x+1, or add ;0.
  245.  * 120 31-May-96 fdc  Fix zfnqfp() to remove trailing .; if directory name.
  246.  * 121 12-Jun-96 fdc  Prevent dereferencing null pointer in do_label_recv().
  247.  * 122 23-Jun-96 fdc  Fix a bug in do_label_recv that I introduced in edit 121.
  248.  * 123 21-Aug-96 fdc  Separate ZRFILE from ZIFILE so now we can have both.
  249.  * 124 21-Aug-96 fdc  Fix includes for getenv & strcpy for DECC vs VAXC.
  250.  * 125 25-Aug-96 mpjz More DECC/VAX fixes.
  251.  * 126 05-Sep-96 fdc  Remove #module, remove refs to xaballdef$$_fill_7.
  252.  * 127 06-Sep-96 fdc  Recover from interrupted labeled-mode receives.
  253.  * 128 06-Sep-96 fdc  Recover from the previous edit.
  254.  * 129 06-Sep-96 fdc  Fix zchki() to return -2 if blah.DIR;n is a directory.
  255.  * 130 06-Sep-96 fdc  Fixes to isdir(), zstrip(), zchdir().
  256.  * 131 17-Feb-97 fdc  Another fix to isdir() (free() was called too early).
  257.  * 132 14-Jul-97 fdc  fgen() malloc'd but never freed filenames; isdir()
  258.  *                    now handles directory files too (like [FOO]BAR.DIR;1).
  259.  * 133 14-Jul-97 mb   New isdir() function uses sys$parse().
  260.  * 134 15-Jul-97 fdc  Added cvtdir(); rewrote zchdir() to use it & new isdir().
  261.  * 135 14-Aug-97 fdc  zopeni: Change mainline binary variable when switching
  262.  *                    automatically from text to binary mode (cosmetic only).
  263.  * 136 24-Aug-97 fdc  zhome: Return SYS$LOGIN value rather than HOME value.
  264.  * 137 04-Sep-97 fdc  zchdir: use sys$setddir() rather than chdir() because
  265.  *                    VMS 6.2 CRTL chdir() has bugs.
  266.  * 138 06-Sep-97 fdc  zchdir again -- try one, then the other.  Add startupdir.
  267.  * 139 04-Nov-97 fdc  Handle absolute/relative/standard/literal dir names.
  268.  * 140 12-Dec-97 fdc  Fix fgen() to report directories if not told not to.
  269.  * 141  1-Jan-98 fdc  Send and set file protections: zsattr(), zstime().
  270.  * 142 15-Jan-98 fdc  Raised max number of files (MAXWLD) from 4K to 1024K.
  271.  *                    and made the mtchs[] array dynamic.
  272.  * 143 29-Jan-98 fdc  Make zshcmd() (RUN command) return proper status,
  273.  *                    ditto for zxcmd(), and make both set pexitstat.
  274.  *                    Also, added ckvmserrstr().
  275.  * 144  8-Feb-98 lh   Add diagnostic message when SPAWN fails; handle files
  276.  *                    with odd fixed record length; isdir() improvements.
  277.  * 145  8-Feb-98 fdc  Slightly better handling of failures in zrmdir().
  278.  * 146  3-May-98 fdc  Fix out-of-bounds cdate[] array reference.
  279.  * 147  7-May-98 lh   another isdir() update; expanded mb (Apr-98) version.
  280.  *               lh   Add REL_DIR item for do_label transfers.
  281.  *               lh   FTN CC to handle zero-length records and overprinting.
  282.  *               lh   Fix zltor for fspath and no match.
  283.  *               fdc  Fix zmkdir to return 1 if >0 directories were created.
  284.  * 148 28-Jun-98 fdc  Fix to odd-record-length code from edit 144, which broke
  285.  *                    RESEND/REGET.  Added zgetfs().
  286.  * 149  5-Sep-98 fdc  cdate[] array declaration needed to be 24, not 23.
  287.  * 150 21-Dec-98 fdc  Added zrelname(), zgperm(), and ziperm().
  288.  * 151 26-Jan-99 fdc  nzltor(): convert path format if other Kermit not VMS.
  289.  * 152  8-Feb-99 lh   Fix constructions like "cd sys$manager" in isdir().
  290.  * 153  8-Feb-99 fdc  Add zxrewind(), add support for recursion in fgen(),
  291.  *                    make zgetfs call stat() to get size.
  292.  *                    Fix chkfn() for connection log.
  293.  * 154 17-Mar-99 lh   Add support for sending PRN files.
  294.  * 155 15-Apr-99 fdc  Fix zmkdir() to tolerate long filenames.
  295.  * 156 16-Apr-99 fdc  Add (nonworking) timeout support to get_subprc_line().
  296.  *                    Somebody please look at this routine & fix it.  It's
  297.  *                    supposed to time out and return after 1 second if nothing
  298.  *                    comes from the subprocess, but instead it seems wait
  299.  *                    forever.  To test, put VMS C-K in server mode and then
  300.  *                    tell the client "remote host wait 00:00:30".  VMS C-K
  301.  *                    is supposed to send back an empty data packet every
  302.  *                    second but it doesn't.
  303.  * 157 16-Apr-99 lh   Fix 156.
  304.  * 158 20-Jun-99 fdc  Fix rinfill() to also work with mailboxes.  I don't see
  305.  *                    how OPEN !READ/ READ/ CLOSE READ could ever have worked.
  306.  * 159 22-Jul-99 lh   Add zcopy().
  307.  * 160 23-Jul-99 fdc  Add lots of signed-vs-unsigned char casts.
  308.  * 161 27-Jul-99 lh   Fix sys$synch() call in get_subprc_line().
  309.  * 162 29-Jul-99 lh   Fixed get_subprc_line() keepalive performance.
  310.  * 163 24-Aug-99 fdc  Fix do_label_send() broken by edit 160 (diagnosis by lh).
  311.  * 164 02-Sep-99 fdc  Add nopush checks to zxcmd(), zshcmd(), and zsyscmd().
  312.  * 165 19-Sep-99 lh   Fix zfnqfp() error return (NULL, not -1).
  313.  *                    Subtly alter fgen()'s expansion of wildcards (*.* -> .*).
  314.  * 166 06-Oct-99 lh   Fill in zchko.  Change log file error handling, zopen
  315.  *                    and zclose.  Use parse result to handle <> and rel names
  316.  *                    in zmkdir; cleanup.  Extend nzrtol to handle device spec
  317.  *                    in absolute to relative; more <> dir syntax; no longer
  318.  *                    need absolute for zmkdir.  Convert non-file oriented
  319.  *                    device name to upper case in zfnqfp instead of appending
  320.  *                    cwd; replace multiple strncat calls.  Fix fgen edit 165.
  321.  *                    Fix typo in zrmdir.
  322.  * 167 11-Oct-99 fdc  Replace all strncpy() and most strcpy() by ckstrncpy()
  323.  *                    from ckclib.c.
  324.  * 168 19-Nov-99 fdc  Fix a couple from the previous edit that were wrong.
  325.  * 169 29-Aug-A0 fdc  Fix a couple signed/unsigned conflicts.
  326.  * 170 01-Feb-A2 fdc  Make zkermini() return 1 on success and 0 on failure.
  327.  * 171 27-Apr-A2 fdc  Fix zgetfs() to fail when file doesn't exist.
  328.  * 172 30-Jul-A2 mb   Create a proper zchkpath().
  329.  * 173 24-Oct-A2 fdc  Don't #include <conv$routines.h> ifdef NOCONVROUTINES,
  330.  *                    needed in VMS 6.1 with UCX 4.2.
  331.  * 174 24-Oct-A2 lh   define nam$w_did_num/_seq for VAX C 2.x.
  332.  * 175 28-Oct-A2 fdc  Separate text and binary mode open for session log.
  333.  * 176  3-May-A2 jea  ON_CD support.
  334.  * 177  2-Jul-A2 fdc  #include <ckucmd.h> for prototypes needed by ON_CD.
  335.  * 178 05-Apr-A2 fdc  Add __ia64 arch_string.
  336.  * 179 23-Aug-A5 fdc  Fix nzrtol() for filenames like DEV:FOO.BAR.
  337.  * 180 29-Dec-A5 fdc  Change file-size/offset variables from long to CK_OFF_T.
  338.  * 181 15-Feb-A7 sms  Add zxin() routine.
  339.  */
  340.  
  341. /* Definitions of some VMS system commands */
  342.  
  343. char *DIRCMD = "directory ";        /* For directory listing */
  344. char *DIRCM2 = "directory ";        /* For directory listing, no args */
  345. char *DELCMD = "delete ";        /* For file deletion */
  346. char *TYPCMD = "type ";            /* For typing a file */
  347. char *SPACMD = "show quota ";         /* Space/quota of current directory */
  348. char *SPACM2 = "show quota ";         /* Space/quota of specified dir */
  349. char *WHOCMD = "show users ";        /* For seeing who's logged in */
  350. char *PWDCMD = "show default ";        /* For seeing current directory */
  351.  
  352. /*
  353.   Functions (n is one of the predefined file numbers from ckermi.h):
  354.  
  355.    zopeni(n,name)   -- Opens an existing file for input.
  356.    zopeno(n,name)   -- Opens a new file for output.
  357.    zclose(n)        -- Closes a file.
  358.    zchin(n)         -- Gets the next character from an input file.
  359.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  360.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  361.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  362.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  363.    zchki(name)      -- Check if named file exists and is readable, return size.
  364.    zchko(name)      -- Check if named file can be created.
  365.    zchkspa(name,n)  -- Check if n bytes available to create new file, name.
  366.    znewn(name,s)    -- Make a new unique file name based on the given name.
  367.    zdelet(name)     -- Delete the named file.
  368.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  369.    znext(string)    -- Returns the next file from the list in "string".
  370.    zxcmd(n,cmd)     -- Execute the command in a lower fork on file number n.
  371.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  372.    zrtol(n1,n2)     -- Convert remote filename into local form.
  373.    zltor(n1,n2)     -- Convert local filename into remote form.
  374.    zchdir(dirnam)   -- Change working directory.
  375.    zhome()          -- Return pointer to home directory name string.
  376.    zkself()         -- Log self out
  377.    zsattr(struc zattr *) -- Return attributes for file which is being sent.
  378.    zkermini(n1,n2)  -- Find kermit.ini using default scanning process
  379.  */
  380.  
  381. /* Includes */
  382.  
  383. #define CKVFIO_C
  384. #include "ckcdeb.h"
  385. #include "ckcasc.h"
  386. #include "ckcker.h"
  387. #ifndef NOSPL
  388. #include "ckuusr.h"                     /* ON_CD: mlook, parser, dodo */
  389. #endif /* NOSPL */
  390. #include "ckvvms.h"
  391. #include <stdio.h>
  392. #include <types.h>
  393. #include <stat.h>
  394. #include <ctype.h>
  395. #include <time.h>
  396. #include <errno.h>
  397. #include <rms.h>
  398. #include <ssdef.h>
  399. #include <descrip.h>
  400. #include <dvidef.h>
  401. #include <dcdef.h>
  402. #include <iodef.h>
  403. #include <jpidef.h>
  404. #include <signal.h>
  405. #include <string.h>
  406. #include <syidef.h>
  407. #ifndef OLD_VMS
  408. #include <uaidef.h>
  409. #endif  /* OLD_VMS */
  410.  
  411. /* rms.h above includes nam, fab, xab and rmsdef */
  412. /* vms v4 headers do not check against multiple inclusion */
  413. /* ifndefs below prevent multiple declaration of FAB and NAM structs */
  414.  
  415. #ifndef FAB$C_BID
  416. #include <fab.h>            /* These are needed for isdir() */
  417. #endif /* FAB$C_BID */
  418.  
  419. #ifndef NAM$C_BID
  420. #include <nam.h>
  421. #endif /* NAM$C_BID */
  422.  
  423. #ifdef VAXCV2         /* VAX C v2.x nam.h does not define the unions */
  424. #define nam$w_did_num  nam$w_did[0]
  425. #define nam$w_did_seq  nam$w_did[1]
  426. #endif
  427.  
  428. #include <lnmdef.h>
  429. #include <rmsdef.h>
  430.  
  431. #ifndef MAXWLD
  432. #define MAXWLD 102400            /* Maximum wildcard filenames */
  433. #endif /* MAXWLD */
  434.  
  435. /* external def. of things used in buffered file input and output */
  436.  
  437. #ifdef DYNAMIC
  438. extern CHAR *zinbuffer, *zoutbuffer;
  439. #else
  440. extern CHAR zinbuffer[], zoutbuffer[];
  441. #endif /* DYNAMIC */
  442.  
  443. static CHAR rinbuffer[INBUFSIZE], *rinptr;
  444. static int rincnt;
  445. static int lastcount = 0;
  446. static int mtchsinit = 0;
  447.  
  448. extern CHAR *zinptr, *zoutptr;
  449. extern int zincnt, zoutcnt, binary, nopush, rcflag, frecl;
  450. extern long vernum;
  451.  
  452. /* Declarations */
  453.  
  454. FILE *fp[ZNFILS] = {             /* File pointers */
  455.     NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
  456. };
  457.  
  458. #define VMSVERSIONS
  459.  
  460. int vmssversions = 0;            /* Include version number w/filename */
  461. int vmsrversions = 0;            /* Send and Receive.... */
  462.  
  463. /* Flags for each file indicating whether it is a mailbox */
  464. int ispipe[ZNFILS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  465.  
  466. static CK_OFF_T iflen = (CK_OFF_T)-1;    /* Input file length */
  467. static long rflen = -1;
  468. static long oflen = -1;            /* Output file length */
  469. static int fcount = 0;            /* Number of files in wild group */
  470. static int nxpand = 0;            /* Copy of fcount */
  471. static char nambuf[CKMAXPATH];        /* maximum size of a file spec */
  472. static char cwdbuf[NAM$C_MAXRSS];
  473. static struct iosb_struct tmpiosb;    /* For QIOW */
  474.  
  475. extern unsigned long vms_status;    /* Used by CHECK_ERR */
  476. extern unsigned long vms_lasterr;
  477. static int cflag;            /* Flag indicating console in use */
  478.  
  479. int check_spawn(void);
  480. int do_label_recv(void);
  481. int do_label_send(char *name);
  482. int fgen(char *pat, char *resarry[], int len);
  483. static int rinfill(void);
  484.  
  485. #ifdef COMMENT
  486. #ifdef __DECC
  487. #include <stdlib.h>
  488. #else
  489. char *getenv(), *strcpy();
  490. #endif /* __DEC */
  491. #else
  492. /* Recommended by Lee Tibbert */
  493. #ifdef __DECC
  494. #include <stdlib.h>
  495. #include <string.h>
  496. #else
  497. char *getenv(), *strcpy();
  498. #endif /* __DECC */
  499. #endif /* COMMENT */
  500.  
  501. #ifdef __DECC
  502. #include <lib$routines.h>
  503. #include <starlet.h>
  504. #endif /* __DECC */
  505.  
  506. int pexitstat = -2;            /* Process exit status */
  507. unsigned long pexitlong = -2L;        /* ULONG version of same */
  508.  
  509. #ifdef COMMENT
  510. /* static */                /* Not static any more! */
  511. char *mtchs[MAXWLD],            /* Matches found for filename */
  512.   **mtchptr;                /* Pointer to current match */
  513. #else
  514. char **mtchs = NULL, **mtchptr = NULL;
  515. #endif /* COMMENT */
  516. static unsigned short mbx_chan;        /* Mailbox chan for REMOTE commands */
  517. static int subprocess_input = 0, sub_count;
  518. static char *sub_ptr, sub_buf[SUB_BUF_SIZE];
  519.  
  520. #define PIPETIMEOUT            /* For timing out subprocess reads */
  521. #define    SUPERSAFE            /* For safe subprocesses */
  522.  
  523. static unsigned long sub_pid;
  524.  
  525. /*
  526.  * Structures for input (SEND) file
  527.  */
  528.  
  529. static    struct FAB fab_ifile;        /* For SEND file */
  530. static    struct RAB rab_ifile;
  531. static    struct XABDAT xabdat_ifile;
  532. static    struct XABFHC xabfhc_ifile;
  533. static    struct XABPRO xabpro_ifile;
  534. static    struct XABALL xaball_ifile;
  535.  
  536. static    int ifile_bmode;        /* For SEND file */
  537. static    int ifile_bcount;
  538. static    char aclbuf[512];
  539. static    unsigned long xuchar = 0;
  540.  
  541. static    struct FAB fab_rfile;        /* For OPEN READ file */
  542. static    struct RAB rab_rfile;
  543.  
  544. static    struct XABDAT xabdat_rfile;
  545. static    struct XABFHC xabfhc_rfile;
  546. static    struct XABPRO xabpro_rfile;
  547. static    struct XABALL xaball_rfile;
  548.  
  549. static    int rfile_bmode;        /* For READ file */
  550. static    int rfile_bcount;
  551. static    char raclbuf[512];
  552.  
  553. /*
  554.  * Structures for output (RECEIVE) file
  555.  */
  556.  
  557. static    struct FAB fab_ofile;
  558. static    struct RAB rab_ofile;
  559. static    struct XABDAT xabdat_ofile;
  560. static    struct XABFHC xabfhc_ofile;
  561. static    struct XABPRO xabpro_ofile;
  562. static    struct XABALL xaball_ofile;
  563. static    struct XABRDT xabrdt_ofile;
  564. static    int ofile_dump;
  565. static    int ofile_bmode;
  566. static    int ofile_lblopts;
  567. static    int ofile_lblproc = 0;
  568. static    char revdat[8];
  569. static    unsigned short revnum;
  570.  
  571. static    char ofile_vmsname[CKMAXPATH+1];
  572. static    char ofile_vmsacl[512];
  573. static    int ofile_acllen;
  574. static    short ofile_ffb;
  575.  
  576. char startupdir[NAM$C_MAXRSS+1];
  577.  
  578. /*
  579.  * Common RMS items
  580.  */
  581. static unsigned long int rms_sts;
  582.  
  583. /*  I S W I L D  --  Tells whether filespec "str" is wild  */
  584. /*  Returns 0 if not wild, 1 if wild */
  585.  
  586. int
  587. iswild(str) char *str; {
  588.     char c;
  589.     if (!str) return(0);
  590.     /* Should also check for [dir...] */
  591.     while ((c = *str++) != '\0')
  592.       if (c == '*' || c == '%') return(1);
  593.     return(0);
  594. }
  595.  
  596. #ifdef CK_TMPDIR
  597.  
  598. /*  I S D I R  --  Tells if string pointer s is the name of a directory. */
  599. /*
  600.    Returns:
  601.     0 if s is not a directory or doesn't exist
  602.     Nonzero if it is a directory:
  603.       1 if the string is a directory specification, e.g., [FOO.BAR]
  604.       2 if it is the name of a directory file, like [FOO]BAR.DIR;1
  605. */
  606. int
  607. isdir(s) char *s; {
  608. #ifdef COMMENT                /* Frank's version */
  609.     int x;
  610.     int i, s_len;
  611.     char *full_name = NULL;
  612.     char *p = NULL;
  613.  
  614.     static char dot_dir[] = ".dir";
  615.     static char zero_dir[] = "[000000]";
  616.  
  617.     char name_buf[255];            /* Was 512 but that's too big */
  618.     char tmpbuf[255];            /* for unsigned char... */
  619.  
  620.     struct FAB fab;
  621.     struct NAM nam;
  622.  
  623.     struct dsc$descriptor_s indesc;
  624.     $DESCRIPTOR(lnmtable, "LNM$FILE_DEV");
  625.     int new_len;
  626.     int serial_num;
  627.     struct {
  628.     short length;
  629.     short code;
  630.     char *address;
  631.     int *len;
  632.     int term;
  633.     } itemlist;
  634.  
  635.     if (!s) return(0);
  636.     if (!*s) return(0);
  637.  
  638.     /* Determine if this is something we can SET DEFAULT to...  */
  639.  
  640.     s_len = strlen(s);
  641.     debug(F111,"isdir",s,s_len);
  642.  
  643.     /* Does it look like a directory name? */
  644.  
  645.     if (s[s_len-1] != ':' && s[s_len-1] != ']' && s[s_len-1] != '>') {
  646. #ifdef COMMENT
  647. /*
  648.   Bad idea.  No, the user really has to include the colon, otherwise the
  649.   intention is ambiguous.
  650. */
  651.     p = tmpbuf;            /* No, maybe it's a logical name */
  652.     ckstrncpy(tmpbuf,getenv(s),255);
  653.     if (*p) {
  654.         s = p;
  655.         s_len = strlen(s);
  656.         if (s < 1)            /* No definition */
  657.           return(0);
  658.  
  659.         /* It is a logical name, but does it look like a dir name? */
  660.         if (s[s_len-1] != ':' && s[s_len-1] != ']' && s[s_len-1] != '>')
  661.           return(0);        /* No, it doesn't */
  662.     }
  663. #else
  664.  
  665. /*  Check for "DEV:[FOO]BLAH.DIR;1"... */
  666.  
  667.     if (zchki(s) == -2) {        /* It's a directory */
  668.         int i;
  669.         char * p = NULL;        /* Pointer to period */
  670.         debug(F111,"isdir zchki",s,-2);
  671.         /* Note: This does not handle "FOO.DIR.1"... */
  672.         for (i = s_len; i > 0; i--) {
  673.         if (s[i] == '.')
  674.           p = s+i;
  675.         else if (s[i] == ']' || s[i] == '>')
  676.           break;
  677.         }
  678.         debug(F110,"isdir p",p,0);
  679.         if (p) {
  680.         if ((*(p+1) == 'D' || *(p+1) == 'd') &&
  681.             (*(p+2) == 'I' || *(p+2) == 'i') &&
  682.             (*(p+3) == 'R' || *(p+3) == 'r') &&
  683.             (*(p+4) == ';' || *(p+4) == NUL || *(p+4) == SP))
  684.           return(2);
  685.         }
  686.     }
  687.     return(0);
  688. #endif /*  */
  689.     }
  690.     /* Check that the directory part is valid... */
  691.  
  692.     if (s[s_len-1] == ']' || s[s_len-1] == '>') {
  693.         fab = cc$rms_fab;
  694.         nam = cc$rms_nam;
  695.  
  696.         fab.fab$l_dna = 0;
  697.         fab.fab$b_dns = 0;
  698.  
  699.         fab.fab$l_fna = s;
  700.         fab.fab$b_fns = s_len;
  701.  
  702.         fab.fab$l_fop = 0;
  703.         fab.fab$w_ifi = 0;
  704.         fab.fab$l_nam = &nam;
  705.  
  706.         nam.nam$l_esa = name_buf;
  707.         nam.nam$b_ess = sizeof(name_buf);
  708.         nam.nam$b_nop = 0;
  709.         nam.nam$l_rlf = 0;
  710.         nam.nam$l_rsa = 0;
  711.         nam.nam$b_rsl = 0;
  712.         nam.nam$l_fnb = 0;
  713.  
  714.         i = sys$parse(&fab, 0, 0);
  715.     if (!(i & 1)) vms_lasterr = i;
  716. #ifdef COMMENT
  717.     printf("parse returned %d 0x%x, nam fnb is %d 0x%x\n",
  718.            i, i, nam.nam$l_fnb, nam.nam$l_fnb);
  719. #endif /* COMMENT */
  720.         if ((i & 1) == 0) return(0);
  721.     }
  722.  
  723.     /* Check that the logical name is valid */
  724.  
  725.     i = s_len - 1;
  726.     while (i >= 0 && s[i] != ':') i--;
  727.  
  728.     if (i >= 0 && s[i] == ':') {
  729.         if (i == 0) return(0);        /* Single colon (:) */
  730.         if (s[i-1] == ':') {
  731.             if (i > 1) return(1);    /* DECnet node name (blah::) */
  732.             else return(0);        /* or :: alone. */
  733.         }
  734.     s_len = i;
  735.     full_name = malloc(s_len + 1);
  736.     if (!full_name) return(0);
  737.     /* Logicals must be upper case */
  738.     for (i = 0; i < s_len; i++) {
  739.         full_name[i] = s[i];
  740.         if (full_name[i] >= 'a' && full_name[i] <= 'z')
  741.           full_name[i] -= ('a' - 'A');
  742.     }
  743.     indesc.dsc$w_length = s_len;
  744.     indesc.dsc$a_pointer = full_name;
  745.     indesc.dsc$b_class = DSC$K_CLASS_S;
  746.     indesc.dsc$b_dtype = DSC$K_DTYPE_T;
  747.  
  748.     itemlist.length = new_len = sizeof(name_buf);
  749.     itemlist.code = LNM$_STRING;
  750.     itemlist.address = name_buf;
  751.     itemlist.len = &new_len;
  752.     itemlist.term = 0;
  753.  
  754.     i = sys$trnlnm(0, &lnmtable, &indesc, 0, &itemlist);
  755.     if (!(i & 1)) vms_lasterr = i;
  756.     if (i != SS$_NORMAL || new_len < 0) new_len = 0;
  757.     if (new_len >= sizeof(name_buf)) new_len = sizeof(name_buf) - 1;
  758.     name_buf[new_len] = '\0';
  759.  
  760. #ifdef COMMENT
  761.     printf("trnlnm result %d 0x%x, '%.*s'\n", i, i, new_len, name_buf);
  762. #endif /* COMMENT */
  763.  
  764.     if (new_len == 0) {
  765.         /* Could still be a device name. */
  766.         /* Only disks have serial numbers... */
  767.         serial_num = 0;
  768.         itemlist.length = new_len = sizeof(serial_num);
  769.         itemlist.code = DVI$_SERIALNUM;
  770.         itemlist.address = (char *) &serial_num;
  771.         itemlist.len = &new_len;
  772.         itemlist.term = 0;
  773.         i = sys$getdvi(0, 0, &indesc, &itemlist, 0, 0, 0, 0);
  774.         if (!(i & 1)) vms_lasterr = i;
  775. #ifdef COMMENT
  776.         printf("getdvi ret %d 0x%x, serial %d len %d\n",
  777.            i, i, serial_num, new_len);
  778. #endif /* COMMENT */
  779.         free(full_name);
  780.         return(((i & 1) == 1 && new_len > 0) ? 1: 0);
  781.  
  782.     } else if (name_buf[new_len-1] == ':' ||
  783.             name_buf[new_len-1] == ']' ||
  784.             name_buf[new_len-1] == '>') {
  785.         /* Check returned value */
  786.         if (new_len > 2 &&
  787.             (name_buf[new_len-1] == ']' || name_buf[new_len-1] == '>') &&
  788.             name_buf[new_len-2] == '.') {
  789.             /* Remove trailing dot in directory of logical name */
  790.             name_buf[new_len-2] = name_buf[new_len-1];
  791.             name_buf[new_len-1] = '\0';
  792.         }
  793.         free(full_name);
  794.         return( isdir(name_buf) );
  795.     } else {
  796.         /* Logical name is just a random string signifying nothing */
  797.         free(full_name);
  798.         return(0);
  799.     }
  800.     }
  801.     return(1);
  802.  
  803. #else  /* Mark Berryman's version... */
  804.  
  805. /*  Searching for a file ".;" in the specified directory handles both local
  806.  *  and DECNET references and determines whether a ".DIR;1" file is indeed
  807.  *  a directory. No test is performed for multiple directories matching a
  808.  *  wildcard expansion; cmifi2 does that after a isdir call.
  809.  */
  810.     unsigned int retval, status, srch_lst=0;
  811.     struct FAB path_fab;
  812.     struct NAM path_nam;
  813.     const char *directory_type = ".DIR";
  814.     char ch, *path =s;
  815.  
  816.     char expanded_name[NAM$C_MAXRSS];
  817.     char resultant_name[NAM$C_MAXRSS];
  818.  
  819.     path_fab = cc$rms_fab;
  820.     path_fab.fab$l_fna = path;
  821.     path_fab.fab$b_fns = strlen(path);
  822.     path_fab.fab$l_nam = &path_nam;
  823.  
  824.     path_nam = cc$rms_nam;
  825.     path_nam.nam$l_esa = (char *) &expanded_name;
  826.     path_nam.nam$b_ess = sizeof(expanded_name);
  827.     path_nam.nam$l_rsa = (char *) &resultant_name;
  828.     path_nam.nam$b_rss = sizeof(resultant_name);
  829.  
  830.     path_nam.nam$b_nop = NAM$M_SRCHXABS | NAM$M_NOCONCEAL;
  831.                                          /* remote node DISPLAY w/o OPEN */
  832.                                           /* show physical device & root */
  833.     retval = 1;                             /* default type return value */
  834.  
  835. /* PARSE the string */
  836.     status = sys$parse(&path_fab,0,0);
  837.     if (!(status & 1)) {          /* any $PARSE errors are fatal */
  838.         debug(F111,"isdir $PARSE", path, status);
  839.         vms_lasterr = status;
  840.         return (0);
  841.     }
  842.  
  843. /* a directory may be specified as "lnm:foo.dir". Do a $SEARCH to traverse
  844.  * a search list logical and locate the file;  pass on the resultant name
  845.  * to determine whether the file is a directory file.
  846.  */
  847.     srch_lst = path_nam.nam$l_fnb & NAM$M_SEARCH_LIST;
  848.     if (srch_lst && ((path_nam.nam$b_name + path_nam.nam$b_type) != 1)) {
  849.         status = sys$search(&path_fab,0,0);
  850.         if (!(status & 1)) {
  851.             vms_lasterr = status;
  852.             debug(F101,"isdir srch_lst status", "", status);
  853.             return(0);
  854.         }
  855.         *(path_nam.nam$l_ver + path_nam.nam$b_ver) = '\0';
  856.         debug(F110, "isdir srch_lst result", path_nam.nam$l_node,0);
  857.     }
  858.  
  859. /* Was a file name given ? */
  860.  
  861.     if (path_nam.nam$b_name == 0) {
  862.         if (path_nam.nam$b_type != 1)
  863.       return(0);
  864.  
  865. /* Parse returns a file name of ".;" if no filename is given.  However,
  866.  * that is a valid filename.  Testing gets complicated; the following
  867.  * will not catch the case of such a file name passed as a logical,
  868.  * but will otherwise return(0) when given a file name as "." or ";"
  869.  */
  870.         if (*path_nam.nam$l_dir == '[' )
  871.       ch = (int)']';
  872.         else
  873.       ch = (int)'>';
  874.         if (strchr(path,ch)) {        /* directory in path */
  875.             if (*(strchr(path,ch)+1) == '.' ||
  876.                 *(strchr(path,ch)+1) == ';'    ) {
  877.                 debug(F111,"isdir file w/ zero length name" ,path, 0);
  878.                 return(0);
  879.             }
  880.         } else {                        /* no directory specification */
  881.             if (strchr(path,(int)'.') || strchr(path,(int)';') )
  882.               return(0);
  883.         }
  884.         if (srch_lst) return(retval);  /* already did the SEARCH for ".;" */
  885.     } else {
  886.  
  887. /* If a filename was in the path, a directory will only be something.dir */
  888. /* Check to see if a directory was specified as a filename.
  889.    This is done as follows:
  890.     a) The result of the parse has been returned in a string pointed
  891.        to by path_nam.nam$l_esa in the form of device:[dir.dir]name.type;
  892.        nam$l_dev ==> start of device
  893.        nam$l_dir ==> first [
  894.        nam$l_name ==> first char after closing ]
  895.        nam$l_type ==> period between name and type
  896.        nam$l_ver ==> the semicolon
  897.     b) Zero-terminate the TYPE by replacing the ; with a zero
  898.     c) Make sure TYPE is .DIR
  899.     d) replace the closing ] with a .
  900.     e) replace the . that starts the file TYPE with a ]
  901.     f) reparse
  902. */
  903.         *path_nam.nam$l_ver = '\0';    /* zero terminate file TYPE */
  904.     if (!strcmp(path_nam.nam$l_type, directory_type)) {
  905.         *path_nam.nam$l_type = *(path_nam.nam$l_name-1); /* copy the ] */
  906.         *(path_nam.nam$l_name-1) = '.';        /* then change to . */
  907.         path_fab.fab$l_fna = path_nam.nam$l_node;  /* point FAB to result*/
  908.         path_fab.fab$b_fns = path_nam.nam$l_type+1-path_nam.nam$l_node;
  909.         status = sys$parse(&path_fab,0,0);        /* parse new string */
  910.             if (!(status & 1)) {
  911.                 debug(F111,"isdir second $PARSE", path, status);
  912.                 vms_lasterr = status;
  913.                 return (0);
  914.             }
  915.         retval = 2;
  916.         } else
  917.       return(0);            /* non-directory filename specified */
  918.     }
  919.  
  920. /* Access the directory */
  921.  
  922.     status = sys$search(&path_fab,0,0);
  923.     if (!((status == RMS$_FNF) || (status & 1))) {
  924.     switch (status) {
  925.       case RMS$_DNF:
  926.         debug(F100,"isdir SEARCH RMS$_DNF", "", 0);
  927.         break;
  928.       case RMS$_FND:
  929.         if (path_fab.fab$l_stv == SS$_BADIRECTORY) {
  930.         debug(F100,"isdir .DIR file not a directory", "" ,0);
  931.         break;
  932.         } else {
  933.         debug(F101,"isdir directory access RMS$_FND stv","",
  934.               path_fab.fab$l_stv);
  935.         vms_lasterr = status;
  936.         break;
  937.         }
  938.       default:
  939.         retval = 0;
  940.         debug(F101,"isdir $SEARCH status","", status);
  941.         vms_lasterr = status;
  942.         }
  943.         return(0);
  944.     }
  945.     *(path_nam.nam$l_ver + path_nam.nam$b_ver) = '\0';
  946.     debug(F110,"isdir final string", path_nam.nam$l_node,0);
  947.     debug(F101,"isdir directory code","",retval);
  948.     return(retval);
  949.  
  950. #endif /* COMMENT */
  951. }
  952. #endif /* CK_TMPDIR */
  953.  
  954. /*  Z K S E L F  --  Log self out.  */
  955.  
  956. int
  957. zkself() {
  958.     int i;
  959.     unsigned long int rms_s;
  960. /*
  961.   We need a better way.  If C-Kermit was spawned, this does not log out the
  962.   whole job.    It also does not hang up LAT terminal sessions.
  963. */
  964.     for (i = 0; i < 10; i++) {
  965.     rms_s = sys$delprc(0,0);    /* Maybe some output is still */
  966.     if (!(rms_s & 1)) vms_lasterr = rms_s;
  967.     debug(F101,"zkself rms_s","",rms_s);
  968.     if (rms_s == SS$_NORMAL)    /* queued; try a few times... */
  969.       exit(1);
  970.     sleep(1);
  971.     }
  972.     exit(rms_s == SS$_NORMAL);
  973.     return(0); /* dummy - required as this is called in a non-void context */
  974. }
  975.  
  976. /*  Z O P E N I  --  Open an existing file for input. */
  977.  
  978. int
  979. zopeni(n,name) int n; char *name; {
  980.     debug(F111,"zopeni",name,n);
  981.     debug(F101,"zopeni fp","",(int) fp[n]);
  982.     if (chkfn(n)) return(0);
  983.     ispipe[n] = 0;
  984.     if (n == ZSYSFN) {            /* Input from a system function? */
  985.     debug(F110,"zopeni called with ZSYSFN, failing!",name,0);
  986.     *nambuf = '\0';            /* No filename. */
  987.     return(0);            /* fail. */
  988.     }
  989.     zincnt = 0;                /* Initializing these couldn't hurt */
  990.     zinptr = zinbuffer;
  991.     if (n == ZSTDIO) {            /* Standard input? */
  992.     if (isatty(0)) {
  993.         ermsg("?Terminal input not allowed\n");
  994.         debug(F110,"zopeni attempted input from unredirected stdin","",0);
  995.         return(0);
  996.     }
  997.     fp[ZIFILE] = stdin;
  998.     return(1);
  999.     }
  1000. /*
  1001.  * We open the file but waffle on the access mode we're going to use. We then
  1002.  * inspect the file characteristics to see if the organization is fixed or un-
  1003.  * defined. If it is, we convert to block mode operation. This is needed since
  1004.  * VMS maintains a "first free byte" field to tell us how much of the last rec-
  1005.  * ord really contains data, but won't terminate reads at that point. Thus, if
  1006.  * we want to SEND the exact same file we RECEIVEd, we have to honor the FFB
  1007.  * internally.
  1008.  */
  1009.     if (n == ZIFILE) {
  1010.     ifile_bmode = 0;        /* 0 = not binary */
  1011.     ifile_bcount = 0;
  1012.     fab_ifile = cc$rms_fab;
  1013.     fab_ifile.fab$b_fac = FAB$M_BRO | FAB$M_GET;
  1014. /*
  1015.  * Some non-VMS DECnet implementations don't allow switching modes, so set
  1016.  * block-I/O only mode if the user said SET FILE TYPE IMAGE or LABELED.
  1017.  */
  1018.     if (binary == XYFT_I || binary == XYFT_L)
  1019.       fab_ifile.fab$b_fac = FAB$M_BIO | FAB$M_GET;
  1020.     fab_ifile.fab$l_fna = name;
  1021.     fab_ifile.fab$b_fns = strlen(name);
  1022.     fab_ifile.fab$l_xab = (char *)&xabdat_ifile;
  1023.     rab_ifile = cc$rms_rab;
  1024.     rab_ifile.rab$l_fab = &fab_ifile;
  1025.     xabdat_ifile = cc$rms_xabdat;
  1026.     xabdat_ifile.xab$l_nxt = (char *)&xabfhc_ifile;
  1027.     xabfhc_ifile = cc$rms_xabfhc;
  1028.     xabfhc_ifile.xab$l_nxt = (char *)&xabpro_ifile;
  1029.     xabpro_ifile = cc$rms_xabpro;
  1030.     memset(&aclbuf, 0, sizeof(aclbuf));
  1031.     xabpro_ifile.xab$l_aclsts = SS$_NORMAL;        /* Oh Joy! DECnet */
  1032.     xabpro_ifile.xab$l_aclbuf = (char *)&aclbuf;
  1033.     xabpro_ifile.xab$w_aclsiz = sizeof(aclbuf);
  1034.     xabpro_ifile.xab$l_aclctx = 0;
  1035.     xabpro_ifile.xab$l_nxt = (char *)&xaball_ifile;
  1036.     xaball_ifile = cc$rms_xaball;
  1037.  
  1038.     rms_sts = sys$open(&fab_ifile);
  1039.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1040.     if (rms_sts != RMS$_NORMAL) {
  1041.         debug(F101,"zopeni $open failed, status","",rms_sts);
  1042.         debug(F101,"zopeni $open failed, stv","",fab_ifile.fab$l_stv);
  1043.         return(0);
  1044.     }
  1045.     if (!(xabpro_ifile.xab$l_aclsts & 1)) {
  1046.         if (xabpro_ifile.xab$l_aclsts != SS$_ACLEMPTY) {
  1047.         debug(F101,"zopeni $open ACL failed, status","",
  1048.           xabpro_ifile.xab$l_aclsts);
  1049.         return(0);
  1050.         }
  1051.     }
  1052.  
  1053. /* We have the file opened. See if it's fixed or undefined format... */
  1054.  
  1055.     iflen = ((xabfhc_ifile.xab$l_ebk-1)*512)+xabfhc_ifile.xab$w_ffb;
  1056.     if (fab_ifile.fab$b_rfm == FAB$C_UDF) {
  1057.         debug(F100,"zopeni undefined file format - using blk I/O","",0);
  1058.         ifile_bmode = 1;
  1059.     }
  1060.     if (fab_ifile.fab$b_rfm == FAB$C_FIX) {
  1061.         if ((fab_ifile.fab$b_rat & (FAB$M_FTN | FAB$M_CR | FAB$M_PRN))
  1062.         == 0) {
  1063. /* But it may be changed later, for BINARY mode and odd record length */
  1064.         debug(F100,"zopeni fixed file format - using blk I/O","",0);
  1065.         ifile_bmode = 1;
  1066.         }
  1067.       }
  1068.     debug(F101,"zopeni binary flag at open","",binary);
  1069.     if (binary == XYFT_I) {
  1070.         debug(F100,"zopeni using IMAGE mode by user request","",0);
  1071.         ifile_bmode = 1;
  1072.     }
  1073.     if (binary == XYFT_L) {
  1074.         debug(F100,"zopeni using LABELED mode by user request","",0);
  1075.         ifile_bmode = 2;
  1076.     }
  1077.     debug(F101,"zopeni ifile_bmode","",ifile_bmode);
  1078.     debug(F101,"zopeni binary","",binary);
  1079.     if (ifile_bmode == 1 && binary == XYFT_T) {
  1080.         debug(F100,"zopeni autoswitch from TEXT to BINARY","",0);
  1081.         binary = XYFT_B;
  1082.     } else if (ifile_bmode == 0 && binary == XYFT_B) {
  1083.         debug(F100,"zopeni autoswitch from BINARY to TEXT","",0);
  1084.         binary = XYFT_T;
  1085.     }
  1086. /*
  1087.  * In BINARY mode, for the case of fixed record length format with odd
  1088.  * length records, use record i/o to suppress the RMS NUL pad ala Kermit-32
  1089.  */
  1090.     if ((fab_ifile.fab$b_rfm == FAB$C_FIX) &&
  1091.         (fab_ifile.fab$w_mrs & 1) && /* was "(...mrs | 1)" oops! */
  1092.         (binary == XYFT_B)) {
  1093.         ifile_bmode = 0;
  1094.  
  1095.         /* WARNING: setting ifile_bmode = 0 here can break RESEND */
  1096.         /* (but only for odd-record length fixed-block files) */
  1097.  
  1098.         debug(F100,"zopeni record i/o for BINARY, Odd Fixed RL","",0);
  1099.           }
  1100.     rab_ifile.rab$l_rop = 0;
  1101.     rms_sts = sys$connect(&rab_ifile);
  1102.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1103.     if (rms_sts != RMS$_NORMAL) {
  1104.         debug(F101,"zopeni $connect failed, status","",rms_sts);
  1105.         return(0);
  1106.     }
  1107.     debug(F100,"zopeni RMS operations completed ok","",0);
  1108.     fp[n] = fopen("NLA0:","r");    /* it wants a fp, give it one */
  1109.     zincnt = 0;            /* reset input buffer */
  1110.     if (binary == XYFT_L)
  1111.         do_label_send(name);    /* make a file label */
  1112.     return(1);
  1113.     } else if (n == ZRFILE) {        /* READ file */
  1114.     rfile_bmode = 0;
  1115.     rfile_bcount = 0;
  1116.     fab_rfile = cc$rms_fab;
  1117.         /* Always READ in text mode */
  1118.     fab_rfile.fab$b_fac = FAB$M_BRO | FAB$M_GET;
  1119.     fab_rfile.fab$l_fna = name;
  1120.     fab_rfile.fab$b_fns = strlen(name);
  1121.     fab_rfile.fab$l_xab = (char *)&xabdat_rfile;
  1122.     rab_rfile = cc$rms_rab;
  1123.     rab_rfile.rab$l_fab = &fab_rfile;
  1124.     xabdat_rfile = cc$rms_xabdat;
  1125.     xabdat_rfile.xab$l_nxt = (char *)&xabfhc_rfile;
  1126.     xabfhc_rfile = cc$rms_xabfhc;
  1127.     xabfhc_rfile.xab$l_nxt = (char *)&xabpro_rfile;
  1128.     xabpro_rfile = cc$rms_xabpro;
  1129.     memset(&raclbuf, 0, sizeof(raclbuf)); /* Do we need this for READ? */
  1130.     xabpro_rfile.xab$l_aclsts = SS$_NORMAL; /* Oh Joy! DECnet */
  1131.     xabpro_rfile.xab$l_aclbuf = (char *)&raclbuf;
  1132.     xabpro_rfile.xab$w_aclsiz = sizeof(raclbuf);
  1133.     xabpro_rfile.xab$l_aclctx = 0;
  1134.     xabpro_rfile.xab$l_nxt = (char *)&xaball_rfile;
  1135.     xaball_rfile = cc$rms_xaball;
  1136.  
  1137.     rms_sts = sys$open(&fab_rfile);
  1138.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1139.     if (rms_sts != RMS$_NORMAL) {
  1140.         debug(F101,"zopeni $open ZRFILE failed, status","",rms_sts);
  1141.         debug(F101,"zopeni $open ZRFILE failed, stv","",
  1142.           fab_rfile.fab$l_stv);
  1143.         return(0);
  1144.     }
  1145. #ifdef COMMENT                /* who needs it */
  1146.     if (!(xabpro_rfile.xab$l_aclsts & 1)) {
  1147.         if (xabpro_rfile.xab$l_aclsts != SS$_ACLEMPTY) {
  1148.         debug(F101,"zopeni $open ACL failed, status","",
  1149.           xabpro_rfile.xab$l_aclsts);
  1150.         return(0);
  1151.         }
  1152.     }
  1153. #endif /* COMMENT */
  1154.  
  1155. /* We have the file opened. See if it's fixed or undefined format... */
  1156.  
  1157.     rflen = ((xabfhc_rfile.xab$l_ebk-1)*512)+xabfhc_rfile.xab$w_ffb;
  1158.     if (fab_rfile.fab$b_rfm == FAB$C_UDF) {
  1159.         debug(F100,"zopeni ZRFILE undefined file format - fail","",0);
  1160.         return(0);
  1161.     }
  1162.     if (fab_rfile.fab$b_rfm == FAB$C_FIX) {
  1163.         if ((fab_rfile.fab$b_rat & (FAB$M_FTN | FAB$M_CR | FAB$M_PRN))
  1164.         == 0) {
  1165.         debug(F100,"zopeni ZRFILE fixed file format - fail","",0);
  1166.         return(0);
  1167.         }
  1168.       }
  1169.     rab_rfile.rab$l_rop = 0;
  1170.     rms_sts = sys$connect(&rab_rfile);
  1171.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1172.     if (rms_sts != RMS$_NORMAL) {
  1173.         debug(F101,"zopeni $connect failed, status","",rms_sts);
  1174.         return(0);
  1175.     }
  1176.     debug(F100,"zopeni RFILE RMS operations completed ok","",0);
  1177.     fp[n] = fopen("NLA0:","r");    /* it wants a fp, give it one */
  1178.     rincnt = 0;            /* reset input buffer */
  1179.     return(1);
  1180.     }
  1181.     zincnt = 0;                /* Reset input buffer */
  1182.     fp[n] = fopen(name,"r");        /* Real file. */
  1183.     debug(F111,"zopeni", name, (int) fp[n]);
  1184.     if (fp[n] == NULL) perror("zopeni");
  1185.     return((fp[n] != NULL) ? 1 : 0);
  1186. }
  1187.  
  1188. /*  Z O P E N O  --  Open a new file for output.  */
  1189.  
  1190. char * ckvmserrstr(unsigned long);
  1191.  
  1192. int
  1193. zopeno(n,name,zz,fcb)
  1194.     int n; char *name; struct zattr *zz; struct filinfo *fcb; {
  1195. /*
  1196.   As of Version 5A, the attribute structure and the file information
  1197.   structure are included in the arglist.
  1198. */
  1199.     int x;
  1200.     int fildes;
  1201.     int writeover = 0;
  1202.     extern int rs_len;
  1203.     char p[4];
  1204.  
  1205.     debug(F111,"zopeno",name,n);
  1206.     x = chkfn(n);
  1207.     debug(F101,"zopeno chkfn","",x);
  1208.     if (x != 0)
  1209.       return(0);
  1210.     ispipe[n] = 0;
  1211.     zoutcnt = 0;            /* Reset output buffer */
  1212.     zoutptr = zoutbuffer;
  1213.     cflag = 0;                /* Default to not using console */
  1214.     ofile_lblproc = 0;
  1215.  
  1216.     /* Open terminal or STDIO */
  1217.  
  1218.     if ((n == ZCTERM) || (n == ZSTDIO)) {
  1219.     fp[ZOFILE] = stdout;
  1220.     cflag = 1;            /* Say using console */
  1221.     debug(F101,"zopeno fp[]=stdout", "", (int) fp[n]);
  1222.     return(1);
  1223.     }
  1224. /*
  1225.   Open Debug, Transaction, Packet, Session logfile, or a Write file.
  1226.   The only other possibility at this point is the output file, so we test that.
  1227. */
  1228. #ifdef DEBUG
  1229.     if (deblog) {
  1230.     if (fcb)
  1231.       debug(F101,"zopeno fcb disposition", "", fcb->dsp);
  1232.     if (zz)
  1233.       debug(F111,"zopeno zz disposition",zz->disp.val,zz->disp.len);
  1234.     }
  1235. #endif /* DEBUG */
  1236.     if (n != ZOFILE) {
  1237.     strcpy(p,"w");                /* Assume write/create mode */
  1238.     if (fcb) {                /* If called with an FCB... */
  1239.         if (fcb->dsp == XYFZ_A)        /* Does it say Append? */
  1240.         strcpy(p,"a");            /* Yes. */
  1241.     }
  1242.  
  1243. /* Note: don't add "ctx=rec", "shr=get" here - it slows writes to a crawl */
  1244.  
  1245.     if (n != ZSFILE) {
  1246.         /* was mrs = 80; 254 is max record size for EDT */
  1247.         fp[n] = fopen(name, p, "rat=cr", "rfm=var", "mrs=254");
  1248.     } else {            /* Session Log */
  1249.         extern int sessft;        /* Type */
  1250.         if (sessft == XYFT_T) {    /* Text */
  1251.         fp[n] = fopen(name, p, "ctx=stm", "rat=cr", "rfm=stmlf");
  1252.         } else {            /* Binary */
  1253.         fp[n] = fopen(name, p, "ctx=bin", "rat=none",
  1254.                            "rfm=fix", "mrs=512");
  1255.         }
  1256.     }
  1257.     if (fp[n] == NULL) {        /* Failed */
  1258.             if (errno == EVMSERR) {
  1259.             debug(F111,"zopeno fopen failed vaxc$errno",name,vaxc$errno);
  1260.                 if (vaxc$errno == RMS$_SYN)
  1261.                   printf("?fopen file name syntax error : %s\n", name);
  1262.                 else
  1263.                   printf("?fopen failed %s : %s\n",name,
  1264.                          ckvmserrstr(vaxc$errno));
  1265.             } else {
  1266.             debug(F111,"zopeno fopen failed errno",name,errno);
  1267.                 perror(name);
  1268.             }
  1269.     } else {            /* Didn't fail */
  1270.         debug(F100,"zopeno fopen ok", "", 0);
  1271.     }
  1272.     return((fp[n] != NULL) ? 1 : 0);
  1273.     }
  1274.  
  1275. /* Open a file to store data being RECEIVEd */
  1276.  
  1277.     if (n == ZOFILE) {
  1278. #ifdef DEBUG
  1279.     if (deblog)
  1280.       switch (binary) {
  1281.         case XYFT_T:
  1282.         debug(F100,"zopeno receiving TEXT file","",0);
  1283.         break;
  1284.         case XYFT_B:
  1285.         debug(F100,"zopeno receiving BINARY file","",0);
  1286.         break;
  1287.         case XYFT_I:
  1288.         debug(F100,"zopeno receiving IMAGE file-program bug!","",0);
  1289.         break;
  1290.         case XYFT_L:
  1291.         debug(F100,"zopeno receiving LABELED file","",0);
  1292.         break;
  1293.         case XYFT_U:
  1294.         debug(F100,"zopeno receiving UNDEFINED file","",0);
  1295.         break;
  1296.         default:
  1297.         debug(F101,"zopeno unknown file type","",binary);
  1298.     }
  1299. #endif /* DEBUG */
  1300.     ofile_bmode = binary;
  1301.     ofile_dump = 0;
  1302.     ofile_ffb = -1;
  1303.     fab_ofile = cc$rms_fab;
  1304.     fab_ofile.fab$l_fna = name;
  1305.     fab_ofile.fab$b_fns = strlen(name);
  1306.     fab_ofile.fab$l_fop = FAB$M_MXV;
  1307.     fab_ofile.fab$b_fac = FAB$M_PUT;
  1308. /*
  1309.   Note that we could actually implement a true overwrite (step on existing
  1310.   file version) operation here by testing for a new XYFZ_* type as well in
  1311.   the line below, and then simply not doing a RAB$M_EOF about 25 lines down
  1312.   from here. We might also have to do something to reset the file allocation
  1313.   so we aren't left with a large number of "leftover" blocks if the new file
  1314.   is smaller than the old one.
  1315. */
  1316.     if (fcb) {
  1317.         if (fcb->dsp == XYFZ_A) {
  1318.         fab_ofile.fab$l_fop = FAB$M_CIF;
  1319. /*
  1320.   This is for RESEND.  If the output mode is APPEND and the incoming
  1321.   attributes structure (zz) says "Resend", then we know this file is
  1322.   being resent.
  1323. */
  1324.         if (*(zz->disp.val) == 'R' && ofile_bmode)
  1325.           writeover = 1;
  1326.         debug(F101,"zopeno APPENDing, writeover","",writeover);
  1327.         if (writeover)
  1328.           debug(F101,"zopeno RESEND, rs_len","",rs_len);
  1329. /*
  1330.   So if writeover != 0, we can take rs_len to be the length of the existing
  1331.   file to keep, and to write over the rest, which normally should be the final
  1332.   block.
  1333. */
  1334.         }
  1335.     }
  1336.     if (ofile_bmode) {
  1337.         fab_ofile.fab$b_fac = FAB$M_BIO;
  1338.         debug(F101,"zopeno using record size","",frecl);
  1339.         fab_ofile.fab$w_mrs = frecl;
  1340.         if (ofile_bmode == XYFT_U)
  1341.           fab_ofile.fab$b_rfm = FAB$C_UDF;
  1342.         else
  1343.           fab_ofile.fab$b_rfm = FAB$C_FIX;
  1344.     } else {
  1345.         fab_ofile.fab$b_rat = FAB$M_CR;
  1346.         fab_ofile.fab$b_rfm = FAB$C_VAR;
  1347.     }
  1348.     fab_ofile.fab$b_shr = FAB$M_NIL;
  1349.     fab_ofile.fab$l_xab = (char *)&xabdat_ofile;
  1350.     rab_ofile = cc$rms_rab;
  1351.     rab_ofile.rab$l_fab = &fab_ofile;
  1352.     if (fcb)
  1353.         if ((fcb->dsp == XYFZ_A) && (writeover == 0))
  1354.         rab_ofile.rab$l_rop = RAB$M_EOF;
  1355.     xabdat_ofile = cc$rms_xabdat;
  1356.     xabdat_ofile.xab$l_nxt = (char *)&xabfhc_ofile;
  1357.     xabfhc_ofile = cc$rms_xabfhc;
  1358. #ifndef THIS_IS_WRONG
  1359.     xabfhc_ofile.xab$l_nxt = (char *)&xabpro_ofile;
  1360.     xabpro_ofile = cc$rms_xabpro;
  1361. #endif /* THIS_IS_WRONG */
  1362.     if (zz)
  1363.       zstime(name, zz, 2);        /* Set creation date from A packet */
  1364.     if (ofile_bmode == XYFT_L) {    /* DEFER OPEN IF LABELED <-- NOTE  */
  1365.         ofile_lblproc = 0;        /* (Haven't processed labels yet.) */
  1366.         ofile_lblopts = fcb->lblopts;
  1367.         debug(F101,"zopeno lblopts","",ofile_lblopts);
  1368.         debug(F100,"zopeno RMS operations deferred","",0);
  1369.     } else {
  1370.         rms_sts = sys$create(&fab_ofile);
  1371.         if (!(rms_sts & 1)) {
  1372.         vms_lasterr = rms_sts;
  1373.         debug(F101,"zopeno $create failed, status","",rms_sts);
  1374.         return(0);
  1375.         }
  1376.         rms_sts = sys$connect(&rab_ofile);
  1377.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1378.         if (rms_sts != RMS$_NORMAL) {
  1379.         debug(F101,"zopeno $connect failed, status","",rms_sts);
  1380.         return(0);
  1381.         }
  1382.         if (writeover == 1) {        /* if resend ... */
  1383.         rab_ofile.rab$l_bkt = (unsigned long) rs_len >> 9;
  1384.         rms_sts = sys$space(&rab_ofile);/* space forward to last */
  1385.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1386.         rab_ofile.rab$l_bkt = 0;    /* complete block */
  1387.         if (rms_sts != RMS$_NORMAL) {
  1388.             debug(F101,"zopeno $space failed, status","",rms_sts);
  1389.             return(0);
  1390.         }
  1391.         }
  1392.         debug(F100,"zopeno RMS operations completed ok","",0);
  1393.     }
  1394.     fp[n] = fopen("NLA0:","r");    /* CK wants a fp, give it one */
  1395.     return(1);
  1396.     }
  1397. }
  1398.  
  1399. /*  Z C L O S E  --  Close the given file.  */
  1400.  
  1401. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  1402.  
  1403. int
  1404. zclose(n) int n; {
  1405.     int x = 0;
  1406.  
  1407.     debug(F101,"zclose n","",n);
  1408.     if (chkfn(n) < 1)
  1409.       return(0);
  1410.  
  1411. /* If this is the subprocess file, close it to flush output */
  1412.  
  1413.     if ((n == ZIFILE || (n == ZRFILE)) && (subprocess_input != 0)) {
  1414.         debug(F100, "zclose calling zclosf", "", 0);
  1415.     return (zclosf(n));
  1416.     }
  1417.     if (n == ZIFILE) {            /* Input (e.g. SEND) file */
  1418.     rms_sts = sys$close(&fab_ifile);
  1419.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1420.     if (rms_sts != RMS$_NORMAL) {
  1421.         debug(F101,"zclose ZIFILE $close failed, status","",rms_sts);
  1422.         return(-1);
  1423.     }
  1424.     debug(F100,"zclose ZIFILE RMS operations completed ok","",0);
  1425.     x = fclose(fp[n]);        /* Close the dummy C library file */
  1426.     fp[n] = NULL;            /* Mark it closed */
  1427.     iflen = -1;            /* Invalidate length */
  1428.     return(1);
  1429.     } else if (n == ZRFILE) {        /* READ file */
  1430.     rms_sts = sys$close(&fab_rfile); /* Close it */
  1431.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1432.     if (rms_sts != RMS$_NORMAL) {    /* Check status */
  1433.         debug(F101,"zclose ZRFILE $close failed, status","",rms_sts);
  1434.         return(-1);
  1435.     }
  1436.     debug(F100,"zclose ZRFILE RMS operations completed ok","",0);
  1437.     x = fclose(fp[n]);        /* Close the dummy C-Library file */
  1438.     fp[n] = NULL;            /* and mark it closed */
  1439.     rflen = -1;            /* invalidate its length */
  1440.     return(1);
  1441.     }
  1442.     if (n == ZOFILE) {            /* Output (e.g. RECEIVE) file */
  1443.     ofile_dump = 1;            /* Force complete dump */
  1444.     while (zoutcnt != 0) {
  1445.         rms_sts = zoutdump();    /* Flush buffers to disk */
  1446.         if (rms_sts != 0) {
  1447.         x = fclose(fp[n]);    /* Close the associated dummy file */
  1448.         fp[n] = NULL;        /* Mark it closed */
  1449.         iflen = -1;        /* Invalidate length */
  1450.         return(-1);
  1451.         }
  1452.     }
  1453.     if (ofile_bmode == XYFT_L) {    /* Update revisions if labeled */
  1454. #ifdef BUGFILL7                /* We should use a separate symbol.. */
  1455.         int i;
  1456.         char * s1, * s2;
  1457.         s1 = (char *)&xabrdt_ofile.xab$q_rdt;
  1458.         s2 = (char *)revdat;
  1459.         for (i = 0; i < 8; i++)
  1460.           *s1++ = *s2++;
  1461.         s1 = (char *)&xabrdt_ofile.xab$w_rvn;
  1462.         s2 = (char *)revnum;
  1463.         for (i = 0; i < 2; i++)
  1464.           *s1++ = *s2++;
  1465. #else
  1466.         memmove(&xabrdt_ofile.xab$q_rdt, revdat, 8);
  1467.         memmove(&xabrdt_ofile.xab$w_rvn, &revnum, 2);
  1468. #endif /* BUGFILL7 */
  1469.         debug(F100,"zclose ZOFILE updated labeled revision count","",0);
  1470.     }
  1471.     if (cflag != 1) {        /* Not console */
  1472.         rms_sts = sys$close(&fab_ofile); /* So close */
  1473.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  1474.         if (rms_sts != RMS$_NORMAL) {
  1475.         debug(F101,"zclose ZOFILE $close failed, status","",rms_sts);
  1476.         return(-1);
  1477.         }
  1478.         x = fclose(fp[n]);        /* Close the associated dummy file */
  1479.     } else                /* Console */
  1480.       cflag = 0;            /* So not console any more */
  1481.  
  1482.     ofile_lblproc = 0;        /* Done with this file's labels */
  1483.     debug(F100,"zclose ZOFILE RMS operations completed ok","",0);
  1484.     fp[n] = NULL;            /* Mark it closed */
  1485.     iflen = -1;            /* Invalidate length */
  1486.     return(1);
  1487.     }
  1488.     if ((fp[n] != stdout) && (fp[n] != stdin)) { /* Other kind of file */
  1489.         x = fclose(fp[n]);        /* C-Library close */
  1490.         if (x == EOF) {            /* If we got a close error */
  1491.             debug(F101,"zclose failed errno","",errno);
  1492.             perror("zclose");
  1493.         }
  1494.     }
  1495.     debug(F101,"zclose OTHER ","",fp[n]);
  1496.     fp[n] = NULL;            /* Mark it closed */
  1497.     iflen = -1;             /* Invalidate file length */
  1498.     if (x == EOF)             /* If we got a close error */
  1499.       return (-1);            /* fail */
  1500.     else                 /* otherwise */
  1501.       return (1);            /* succeed */
  1502. }
  1503.  
  1504. int
  1505. get_subprc_line() {
  1506.     struct iosb_struct subiosb;
  1507.     int timedout = 0;
  1508. #ifdef PIPETIMEOUT
  1509.     extern int kactive, srvping;
  1510. #endif /* PIPETIMEOUT */
  1511.  
  1512.     unsigned int sts;
  1513. /*
  1514.  * Someone complained that subprocess deletion would hang the Kermit server.
  1515.  * This can be triggered by sending something silly like REMOTE HOST STOP/ID=0.
  1516.  * If SUPERSAFE is defined we will check to make sure the subprocess still
  1517.  * exists before every read from the mailbox. This will slow things down a bit,
  1518.  * but should stop the "C-Kermit just dies" reports.
  1519.  */
  1520. #ifdef    SUPERSAFE
  1521.     unsigned short pid;
  1522.  
  1523.     struct itmlstdef {
  1524.     short int buflen;
  1525.     short int itmcod;
  1526.     char *bufaddr;
  1527.     long int *retlen;
  1528.     };
  1529.     struct itmlstdef itmlst[] = {
  1530.     4, JPI$_PID, NULL, 0,
  1531.     0, 0, 0, 0
  1532.     };
  1533.     itmlst[0].bufaddr = (char *)&pid;
  1534.  
  1535.     sts = sys$getjpiw(0, &sub_pid, 0, &itmlst, 0, 0, 0);
  1536.     if (!(sts & 1)) vms_lasterr = sts;
  1537.  
  1538.     debug(F101,"get_subprc_line sys$getjpiw status", "", sts);
  1539.     if (sts == SS$_NONEXPR)
  1540.       return(-1);
  1541. #endif    /* SUPERSAFE */
  1542.  
  1543. #ifndef PIPETIMEOUT
  1544.     sts = sys$qiow(QIOW_EFN, mbx_chan, IO$_READVBLK, &subiosb, 0, 0, sub_buf,
  1545.            sizeof(sub_buf), 0, 0, 0, 0);
  1546. #else
  1547.     if (kactive && srvping)  {
  1548.     unsigned long wait_sts, reqidt = 0L;
  1549.     unsigned long int mask = (1 << QIOW_EFN) | (1 << TIM_EFN);
  1550.     struct { int hi, lo; } qtime;
  1551.     qtime.hi = -10*1000*1000;    /* One second delta-time */
  1552.     qtime.lo = -1;
  1553.  
  1554.     sts = sys$setimr(TIM_EFN, &qtime, 0, reqidt, 0);
  1555.     if (sts != SS$_NORMAL) {
  1556.         debug(F101,"get_subprc_line sys$setimr", "", sts);
  1557.         vms_lasterr = sts;
  1558.         return(-1);
  1559.     }
  1560.     sts = sys$qio(QIOW_EFN, mbx_chan, IO$_READVBLK,
  1561.               &subiosb, 0, 0, sub_buf, sizeof(sub_buf), 0, 0, 0, 0);
  1562.  
  1563.     wait_sts = sys$wflor(TIM_EFN,mask);
  1564.     if (wait_sts != SS$_NORMAL) {
  1565.         debug(F101,"get_subprc_line sys$wflor status", "", wait_sts);
  1566.         vms_lasterr = wait_sts;
  1567.         return(-1);
  1568.     }
  1569.     wait_sts = sys$readef(TIM_EFN, &mask);
  1570.     if (wait_sts != SS$_WASCLR && wait_sts != SS$_WASSET) {
  1571.         vms_lasterr = wait_sts;
  1572.         return(-1);
  1573.     }
  1574.     if (wait_sts == SS$_WASSET) {
  1575.         sys$clref(TIM_EFN);
  1576.         debug(F100,"get_subprc_line TIM_EFN timedout", "",0);
  1577.         timedout = 1;
  1578.     } else {
  1579.         wait_sts = sys$cantim(reqidt,0);
  1580.         debug(F100,"get_subprc_line TIM_EFN was clear", "",0);
  1581.         sys$synch(QIOW_EFN,&subiosb); /* Check QIO completion */
  1582.         debug(F100,"get_subprc_line $synch(QIO) complete","",0);
  1583.     }
  1584.     } else
  1585.       sts = sys$qiow(QIOW_EFN, mbx_chan, IO$_READVBLK,
  1586.              &subiosb, 0, 0, sub_buf, sizeof(sub_buf), 0, 0, 0, 0);
  1587. #endif /* PIPETIMEOUT */
  1588.  
  1589.     debug(F101,"get_subprc_line sys$qio{w} status", "", sts);
  1590.     if (sts != SS$_NORMAL) {
  1591.         vms_lasterr = sts;
  1592.         return(-1);
  1593.     }
  1594.     debug(F101,"get_subprc_line sys$qiow subiosb.status", "", subiosb.status);
  1595.     if (subiosb.status != SS$_NORMAL && !timedout) {
  1596.         vms_lasterr = subiosb.status;
  1597.         return(-1);
  1598.     }
  1599.     sub_count = subiosb.size;
  1600.     debug(F111,"get_subprc_line size", sub_buf, sub_count);
  1601.  
  1602.     if (timedout && (sub_count == 0))
  1603.       return(-3);
  1604.  
  1605.     sub_buf[sub_count] = '\r';
  1606.     sub_buf[sub_count + 1] = '\n';
  1607.     sub_buf[sub_count + 2] = '\0';
  1608.     sub_count += 2;
  1609.     debug(F111,"get_subprc_line size 2", sub_buf, sub_count);
  1610.     sub_ptr = sub_buf;
  1611.  
  1612.     return(0);
  1613. }
  1614.  
  1615. /*  Z C H I N  --  Get a character from the input file.  */
  1616.  
  1617. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  1618.  
  1619. int
  1620. zchin(n,c) int n, *c; {
  1621.     int a = -1;
  1622.  
  1623. #ifdef DEBUG
  1624.     if (chkfn(n) < 1) return(-1);
  1625. #endif /* DEBUG */
  1626.  
  1627.     if (n == ZIFILE) {
  1628.     if (subprocess_input) {
  1629.         if (--sub_count < 0)
  1630.           if (get_subprc_line()) return(-1);
  1631.         a = *sub_ptr++;
  1632.     } else {
  1633.         a = zminchar();
  1634.     }
  1635.     } else if (n == ZRFILE) {
  1636.     a = ((--rincnt)>=0) ? ((int)(*rinptr++) & 0377) : rinfill();
  1637.     /* debug(F101,"zchin a","",a); */
  1638.     }
  1639.     if (a == -1)
  1640.       return(-1);
  1641.     *c = (unsigned char)a;
  1642.     return(0);
  1643. }
  1644.  
  1645. /*  Z S I N L  --  Read a line from a file.  */
  1646.  
  1647. /*
  1648.   Writes the line into the address provided by the caller.
  1649.   n is the Kermit "channel number".
  1650.   Writing terminates when newline is encountered, newline is not copied.
  1651.   Writing also terminates upon EOF or if length x is exhausted.
  1652.   Returns 0 on success, -1 on EOF or error.
  1653. */
  1654.  
  1655. int
  1656. zsinl(n,s,x) int n, x; char *s; {
  1657.     int a, z = 0, count = 0;
  1658.     int old;
  1659.  
  1660.     if (!s || chkfn(n) < 1)        /* Make sure file is open etc */
  1661.       return(-1);
  1662.     s[0] = '\0';            /* Clear buffer */
  1663.     a = -1;
  1664.     while (x--) {
  1665.     old = a;            /* Previous character */
  1666.     z = zchin(n,&a);
  1667.     /* debug(F101,"zsinl z","",z); */
  1668.     if (z < 0) {            /* Read a character from the file */
  1669.         if (count == 0)
  1670.           return(-1);        /* Signal EOF if problem */
  1671.         else            /* Or if we have something */
  1672.           return(0);        /* save failure till next time */
  1673.     } else
  1674.       count++;
  1675.     a = a & 0377;
  1676. #ifdef NLCHAR
  1677.     if (a == (char) NLCHAR) break;    /* Single-character line terminator */
  1678. #else
  1679.     if (a == '\r') {
  1680.         continue;
  1681.     }
  1682.     if (old == '\r') {
  1683.         if (a == '\n') break;
  1684.         else *s++ = '\r';
  1685.     }
  1686. #endif /* NLCHAR */
  1687.     *s = a;
  1688.     s++;
  1689.     }
  1690.     *s = '\0';
  1691.     return(z);
  1692. }
  1693.  
  1694. /*  Z X I N  --  Read x bytes from a file  */
  1695.  
  1696. /*
  1697.   Reads x bytes (or less) from channel n and writes them
  1698.   to the address provided by the caller.
  1699.   Returns number of bytes read on success, 0 on EOF or error.
  1700. */
  1701. int
  1702. zxin(n,s,x) int n, x; char *s; {
  1703. #ifdef IKSD
  1704.     if (inserver && !local && (n == ZCTERM || n == ZSTDIO)) {
  1705.         int a, i;
  1706.         a = ttchk();
  1707.         if (a < 1) return(0);
  1708.         for (i = 0; i < a && i < x; i++)
  1709.           s[i] = coninc(0);
  1710.         return(i);
  1711.     }
  1712. #endif /* IKSD */
  1713.     return(fread(s, sizeof (char), x, fp[n]));
  1714. }
  1715.  
  1716.  
  1717. /*  Z I N F I L L  --  Read a line from a file.  */
  1718.  
  1719. /*
  1720.  * (re)fill the buffered file input buffer with data.  All file input
  1721.  * should go through this routine, usually by calling the zminchar()
  1722.  * macro (defined in ckcker.h).
  1723.  */
  1724.  
  1725. int
  1726. zinfill() {
  1727.     char cchar;
  1728.     int x, linelen;
  1729.     unsigned char vfcbuf[255];    /* 2 is default for PRN, but 255 is max */
  1730.  
  1731.     debug(F101,"zinfill rab_ifile.rab$l_bkt 1","",rab_ifile.rab$l_bkt);
  1732.     debug(F101,"zinfill ifile_bmode","",ifile_bmode);
  1733.  
  1734.     if (subprocess_input) {
  1735.     x = get_subprc_line();
  1736.     debug(F101,"zinfill get_subprc_line","",x);
  1737.     if (x == -3)
  1738.       return(-3);
  1739.     if (x != 0)
  1740.       return(-1);
  1741. /*
  1742.  * The size problem should never happen.  sub_buf of a size greater than
  1743.  * 1k is highly unlikely to be needed.
  1744.  */
  1745.     if (INBUFSIZE < SUB_BUF_SIZE) {
  1746.         fprintf(stderr,"zinfill: sub_buf too large for zinbuffer");
  1747.         exit(BAD_EXIT);
  1748.     }
  1749.     zinptr = (CHAR *)sub_buf;
  1750.     zincnt = sub_count;
  1751.     } else {
  1752.     if (ifile_bmode != 0) {
  1753.         rab_ifile.rab$l_rop = RAB$M_BIO;    /* block mode I/O */
  1754. #ifdef DYNAMIC
  1755.         rab_ifile.rab$l_ubf = (char *)zinbuffer;
  1756. #else
  1757. /* NOTE: Might need "(char *)&zinbuffer" here */
  1758.         rab_ifile.rab$l_ubf = &zinbuffer;
  1759. #endif /* DYNAMIC */
  1760. /*
  1761.   There is a serious flaw here, namely that reading blocks rather than
  1762.   records *includes* the record pad byte (NUL) when the record has an
  1763.   odd length.  All RMS records are stored on even-numbered byte boundaries.
  1764.   Kermit-32 did it right, sigh.  So we need a total rewrite to allow for
  1765.   odd-length records.  Hmmm.. I wonder what the impact on RESEND is...
  1766. */
  1767.         rab_ifile.rab$w_usz = 512;
  1768.         rms_sts = sys$read(&rab_ifile);
  1769.         if (!(rms_sts & 1)) {
  1770.         debug(F101,"zinfill sys$read failed, status","",rms_sts);
  1771.         vms_lasterr = rms_sts;
  1772.         }
  1773.         if (rms_sts == RMS$_EOF)
  1774.           return(-1);        /* End of file */
  1775.         if (rms_sts != RMS$_NORMAL)
  1776.           return(-1);        /* Fatal */
  1777.  
  1778.         ifile_bcount++;        /* No error, say another block read */
  1779.         debug(F101,"zinfill sys$read ok, ifile_bcount","",ifile_bcount);
  1780.         zincnt = 512;
  1781.         zinptr = zinbuffer;
  1782.         if (rab_ifile.rab$l_bkt != 0) { /* If just a file position... */
  1783.         debug(F101,"zinfill updating block counter","",0);
  1784.         ifile_bcount = rab_ifile.rab$l_bkt; /* update block counter */
  1785.         }
  1786.         if (ifile_bcount == xabfhc_ifile.xab$l_ebk) {
  1787.         if (ifile_bmode == 1)    /* BINARY but not LABELED */
  1788.             zincnt = xabfhc_ifile.xab$w_ffb;
  1789.         }
  1790.         debug(F101,"zinfill rab_ifile.rab$l_bkt 2","",rab_ifile.rab$l_bkt);
  1791.         if (rab_ifile.rab$l_bkt != 0) { /* If just a file position... */
  1792.         return(0);            /*...then done                */
  1793.         }
  1794.         zincnt--;            /* one less char in buffer */
  1795.         return((int)(*zinptr++) & 0377); /* because we return the first */
  1796.     }
  1797.  
  1798. /* if we reached this point, we want to do record IO; first initialize RAB */
  1799.  
  1800.     if (fab_ifile.fab$b_rat & FAB$M_FTN) {
  1801. #ifdef DYNAMIC
  1802.         rab_ifile.rab$l_ubf = (char *)zinbuffer+2;
  1803. #else
  1804. /* NOTE: Might need "(char *)&zinbuffer+2" here */
  1805.         rab_ifile.rab$l_ubf = &zinbuffer+2;
  1806. #endif /* DYNAMIC */
  1807.         rab_ifile.rab$w_usz = INBUFSIZE-4; /* Space for carriage control */
  1808.     } else {
  1809. #ifdef DYNAMIC
  1810.         rab_ifile.rab$l_ubf = (char *)zinbuffer;
  1811. #else
  1812. /* NOTE: Might need "(char *)&zinbuffer" here */
  1813.         rab_ifile.rab$l_ubf = &zinbuffer;
  1814. #endif /* DYNAMIC */
  1815.         rab_ifile.rab$w_usz = INBUFSIZE-2; /* Space for possible CR/LF */
  1816.     }
  1817.     if (fab_ifile.fab$b_rat & FAB$M_PRN) { /* A fixed control area */
  1818.         rab_ifile.rab$l_rhb = (char *)&vfcbuf;
  1819.         rab_ifile.rab$w_usz = INBUFSIZE-255; /* Max fixed size*/
  1820.         }
  1821.     rab_ifile.rab$l_rop = 0;    /* Now do the record I/O */
  1822.     rms_sts = sys$get(&rab_ifile);
  1823.     if (!(rms_sts & 1)) {
  1824.         debug(F101,"zinfill sys$get failed, status","",rms_sts);
  1825.         vms_lasterr = rms_sts;
  1826.     }
  1827.     if (rms_sts == RMS$_EOF)
  1828.       return(-1);            /* End of file */
  1829.     if (rms_sts != RMS$_NORMAL)
  1830.       return(-1);            /* Fatal */
  1831. /*
  1832.  * Do assorted contortions with Fortran carriage control to make it formatted
  1833.  * ASCII instead, since many systems don't know about Fortran format in files.
  1834.  */
  1835.     if (fab_ifile.fab$b_rat & FAB$M_FTN) {
  1836.             linelen = rab_ifile.rab$w_rsz;
  1837.             if (linelen > 0) {
  1838.             linelen --;        /* sans control code */
  1839.             cchar = zinbuffer[2];    /* control code */
  1840.             } else {            /* zero length record */
  1841.                 cchar = ' ';        /* space to give <LF><CR> */
  1842.             }
  1843.         switch (cchar) {
  1844.           case '\0':        /* data */
  1845.         zinptr = zinbuffer+3;
  1846.         zincnt = linelen;
  1847.         break;
  1848.           case '+':                /* data<CR> */
  1849.         zinbuffer[linelen+3] = '\r';    /* insert return */
  1850.         zinptr = zinbuffer+3;
  1851.         zincnt = linelen+1;        /* count it */
  1852.         break;
  1853.           case '$':                /* <LF>data */
  1854.         zinbuffer[2] = '\n';        /*  insert newline */
  1855.         zinptr = zinbuffer+2;
  1856.         zincnt = linelen+1;        /* count it */
  1857.         break;
  1858.           case ' ':                /* <LF>data<CR> */
  1859.         zinbuffer[2] = '\n';        /*  insert newline */
  1860.         zinbuffer[linelen+3] = '\r';    /*  insert return */
  1861.         zinptr = zinbuffer+2;
  1862.         zincnt = linelen+2;        /*  count 'em */
  1863.         break;
  1864.           case '0':                /* <LF><CR><LF>data<CR> */
  1865.         zinbuffer[0] = '\n';        /*  insert 1st newline */
  1866.         zinbuffer[1] = '\r';        /*  insert 1st return */
  1867.         zinbuffer[2] = '\n';        /*  insert 2nd newline */
  1868.         zinbuffer[linelen+3] = '\r';    /*  insert 2nd return */
  1869.         zinptr = zinbuffer;
  1870.         zincnt = linelen+4;        /*  count 'em */
  1871.         break;
  1872.           case '1':                /* <FF>data<CR> */
  1873.         zinbuffer[2] = '\f';        /*  insert formfeed */
  1874.         zinbuffer[linelen+3] = '\r';    /*  insert return */
  1875.         zinptr = zinbuffer+2;
  1876.         zincnt = linelen+2;        /*  count 'em */
  1877.         break;
  1878.           default:                /* <LF>data<CR> */
  1879.         zinbuffer[2] = '\n';        /*  insert newline */
  1880.         zinbuffer[linelen+3] = '\r';    /*  insert return */
  1881.         zinptr = zinbuffer+2;
  1882.         zincnt = linelen+2;        /*  count 'em */
  1883.         break;
  1884.         }
  1885.     } else {
  1886.         zincnt = rab_ifile.rab$w_rsz;
  1887.         zinptr = zinbuffer;            /* reset pointer */
  1888.     }
  1889. /*
  1890.  * Here we see if we need to insert CR/LF pairs at the record boundary. For
  1891.  * the moment, we will add them if the file has "carriage return carriage
  1892.  * control" when looked at by a DIRECTORY command. As of edit 036 we also do
  1893.  * this for "print file carriage control" files. I'm open to comments de-
  1894.  * scribing cases where this doesn't work...
  1895.  */
  1896.     if (fab_ifile.fab$b_rat & FAB$M_CR) {
  1897.         zinbuffer[zincnt] = '\r';
  1898.         zinbuffer[zincnt + 1] = '\n';
  1899.         zincnt += 2;
  1900.     }
  1901.  
  1902. /* that worked for the default PRN format of batch log files etc., but
  1903.  * ignored any user format specification (v. RMS FAB$B_RAT docs).
  1904.  */
  1905.     if (fab_ifile.fab$b_rat & FAB$M_PRN) {
  1906.  
  1907.             int i, ifcv, ipcnt, iscnt;
  1908.             unsigned char ipchr, ischr;    /* Eight bit ASCII control code */
  1909.  
  1910.         if (vfcbuf[0] == 0x01 && vfcbuf[1] == 0x8D) {
  1911.             zinbuffer[zincnt] = '\r'; /* Do batch log files the old way */
  1912.             zinbuffer[zincnt + 1] = '\n';
  1913.             zincnt += 2;
  1914.         } else {            /* Decode the control area */
  1915.         ipcnt = 0;        /* First, the prefix byte */
  1916.         ipchr = '\0';
  1917.                 if (vfcbuf[0] != '\0') {
  1918.                     ifcv =  (vfcbuf[0] & 0xe0) >> 5; /* The control bits */
  1919.                     switch (ifcv) {
  1920.               case 0: ipcnt = (int) vfcbuf[0];
  1921.             break;
  1922.               case 4: ipchr = vfcbuf[0] & 0x1f;
  1923.             break;
  1924.               case 6: ipchr = (vfcbuf[0] & 0x1f) + 128;
  1925.             break;
  1926.               case 7:
  1927.             debug(F101,
  1928.                   "zinfill illegal PRN code, byte 0 :",
  1929.                   "",
  1930.                   vfcbuf[0]
  1931.                   );
  1932.             ipcnt = 1;    /* Ignore code, apply CRLF */
  1933.             break;
  1934.               default:
  1935.             debug(F101,
  1936.                   "zinfill unknown PRN code, byte 0 :",
  1937.                   "",
  1938.                   vfcbuf[0]
  1939.                   );
  1940.             ipcnt = 1;
  1941.             break;
  1942.             }
  1943.         }
  1944.         iscnt = 0;        /* Now, the suffix byte */
  1945.         ischr = '\0';
  1946.                 if (vfcbuf[1] != '\0') {
  1947.                     ifcv =  (vfcbuf[1] & 0xe0) >> 5; /* The control bits */
  1948.                     switch (ifcv) {
  1949.               case 0: iscnt = (int) vfcbuf[1];
  1950.             break;
  1951.               case 4: ischr = vfcbuf[1] & 0x1f;
  1952.             break;
  1953.               case 6: ischr = (vfcbuf[1] & 0x1f) + 128;
  1954.             break;
  1955.               case 7:
  1956.             debug(F101,
  1957.                   "zinfill illegal PRN code, byte 1 :",
  1958.                   "",
  1959.                   vfcbuf[1]
  1960.                   );
  1961.             iscnt = 1;    /* Ignore code,  apply CRLF */
  1962.             break;
  1963.               default :
  1964.             debug(F101,
  1965.                   "zinfill unknown PRN code, byte 1 :",
  1966.                   "",
  1967.                   vfcbuf[1]
  1968.                   );
  1969.             iscnt = 1;
  1970.             break;
  1971.             }
  1972.         }
  1973.         if (ipcnt != 0 || ipchr != '\0') { /* Insert */
  1974.             if (ipcnt > 0) {           /* CR & n LF's */
  1975.             memmove(zinbuffer+ipcnt+1, zinbuffer, zincnt);
  1976.             zincnt += ipcnt + 1;
  1977.             zinbuffer[0] = '\r';
  1978.             for (i = 1; i <= ipcnt; i++)
  1979.               zinbuffer[i] = '\n';
  1980.             } else {        /* Or an ASCII control code */
  1981.             memmove(zinbuffer+1, zinbuffer, zincnt);
  1982.             zincnt ++;
  1983.             zinbuffer[0] = ipchr;
  1984.             }
  1985.         }
  1986.         if (iscnt != 0 || ischr != '\0') { /* Append */
  1987.             if (iscnt > 0) {
  1988.             zinbuffer[zincnt++]='\r';
  1989.             for (i = 1; i <= iscnt; i++)
  1990.               zinbuffer[zincnt++]='\n';
  1991.             } else {
  1992.             zinbuffer[zincnt++] = ischr;
  1993.             }
  1994.         }
  1995.         }
  1996.         }
  1997.     }
  1998. /*
  1999.  * And finally return the record
  2000.  */
  2001.     zincnt--;                /* one less char in buffer */
  2002.     return((int)(*zinptr++) & 0377);    /* because we return the first */
  2003. }
  2004.  
  2005. static int
  2006. rinfill() {
  2007.     char cchar;
  2008.     int linelen;
  2009.  
  2010.     if (ispipe[ZRFILE]) {        /* OPEN !READ file */
  2011.     int x;
  2012.     x = get_subprc_line();
  2013.     debug(F101,"rinfill get_subprc_line","",x);
  2014.     if (x < 0) return(x);
  2015.     rinptr = (CHAR *)sub_buf;
  2016.     rincnt = sub_count - 1;
  2017.     debug(F101,"rinfill rincount","",rincnt);
  2018.     return((int)(*rinptr++) & 0377);
  2019.     }
  2020.     /* Regular file... */
  2021.  
  2022.     debug(F101,"rinfill rfile_bmode","",rfile_bmode);
  2023.     if (rfile_bmode != 0) {
  2024.     rab_rfile.rab$l_rop = RAB$M_BIO; /* Block mode I/O */
  2025. #ifdef DYNAMIC
  2026.     rab_rfile.rab$l_ubf = (char *)rinbuffer;
  2027. #else
  2028. /* NOTE: Might need "(char *)&rinbuffer" here */
  2029.     rab_rfile.rab$l_ubf = &rinbuffer;
  2030. #endif /* DYNAMIC */
  2031.     rab_rfile.rab$w_usz = 512;
  2032.     rms_sts = sys$read(&rab_rfile);
  2033.     if (!(rms_sts & 1)) {
  2034.         debug(F101,"rinfill sys$read failed, status","",rms_sts);
  2035.         vms_lasterr = rms_sts;
  2036.     }
  2037.     if (rms_sts == RMS$_EOF)
  2038.       return(-1);            /* End of file */
  2039.     if (rms_sts != RMS$_NORMAL)
  2040.       return(-1);            /* Fatal */
  2041.     rfile_bcount++;            /* Say another block read */
  2042.     debug(F101,"rinfill sys$read ok, rfile_bcount","",rfile_bcount);
  2043.     rincnt = 512;
  2044.     rinptr = rinbuffer;
  2045.  
  2046.     if (rab_rfile.rab$l_bkt != 0) { /* If just a file position... */
  2047.         rfile_bcount = rab_rfile.rab$l_bkt; /* update block counter */
  2048.     }
  2049.     if (rfile_bcount == xabfhc_rfile.xab$l_ebk) {
  2050.         if (rfile_bmode == 1)    /* BINARY but not LABELED */
  2051.           rincnt = xabfhc_rfile.xab$w_ffb;
  2052.     }
  2053.     if (rab_rfile.rab$l_bkt != 0) { /* If just a file position... */
  2054.         return(0);            /*...then done                */
  2055.     }
  2056.     rincnt--;            /* one less char in buffer */
  2057.     return((int)(*rinptr++) & 0377); /* because we return the first */
  2058.     }
  2059.     if (fab_rfile.fab$b_rat & FAB$M_FTN) {
  2060.     debug(F100,"rinfill FAB$M_FTN","",0);
  2061. #ifdef DYNAMIC
  2062.     rab_rfile.rab$l_ubf = (char *)rinbuffer+2;
  2063. #else
  2064. /* NOTE: Might need "(char *)&rinbuffer" here */
  2065.     rab_rfile.rab$l_ubf = &rinbuffer+2;
  2066. #endif /* DYNAMIC */
  2067.     rab_rfile.rab$w_usz = INBUFSIZE-4; /* space for carriage ctl */
  2068.     } else {
  2069.     debug(F100,"rinfill not FAB$M_FTN","",0);
  2070. #ifdef DYNAMIC
  2071.     rab_rfile.rab$l_ubf = (char *)rinbuffer;
  2072. #else
  2073. /* NOTE: Might need "(char *)&rinbuffer" here */
  2074.     rab_rfile.rab$l_ubf = &rinbuffer;
  2075. #endif /* DYNAMIC */
  2076.     rab_rfile.rab$w_usz = INBUFSIZE-2; /* space for possible CR/LF */
  2077.     }
  2078.     rab_rfile.rab$l_rop = 0;        /* doing record I/O */
  2079.     rms_sts = sys$get(&rab_rfile);
  2080.     if (!(rms_sts & 1)) {
  2081.     debug(F101,"rinfill sys$get failed, status","",rms_sts);
  2082.     vms_lasterr = rms_sts;
  2083.     }
  2084.     if (rms_sts == RMS$_EOF)
  2085.       return(-1);            /* End of file */
  2086.     if (rms_sts != RMS$_NORMAL)
  2087.       return(-1);            /* Fatal */
  2088. /*
  2089.  * Do assorted contortions with Fortran carriage control to make it formatted
  2090.  * ASCII instead, since many systems don't know about Fortran format in files.
  2091.  */
  2092.     if (fab_rfile.fab$b_rat & FAB$M_FTN) {
  2093.     linelen = rab_rfile.rab$w_rsz-1; /* sans control code */
  2094.     cchar = rinbuffer[2];        /* control code */
  2095.     switch (cchar) {
  2096.       case '\0':            /* data<CR> */
  2097.       case '+':
  2098.         rinbuffer[linelen+3] = '\r';/*  insert return */
  2099.         rinptr = rinbuffer+3;
  2100.         rincnt = linelen+1;        /* count it */
  2101.         break;
  2102.       case '$':            /* <LF>data<CR> */
  2103.       case ' ':
  2104.         rinbuffer[2] = '\n';    /*  insert newline */
  2105.         rinbuffer[linelen+3] = '\r';/*  insert return */
  2106.         rinptr = rinbuffer+2;
  2107.         rincnt = linelen+2;        /*  count 'em */
  2108.         break;
  2109.       case '0':            /* <LF><CR><LF>data<CR> */
  2110.         rinbuffer[0] = '\n';    /*  insert 1st newline */
  2111.         rinbuffer[1] = '\r';    /*  insert 1st return */
  2112.         rinbuffer[2] = '\n';    /*  insert 2nd newline */
  2113.         rinbuffer[linelen+3] = '\r';/*  insert 2nd return */
  2114.         rinptr = rinbuffer;
  2115.         rincnt = linelen+4;        /*  count 'em */
  2116.         break;
  2117.       case '1':            /* <FF>data<CR> */
  2118.         rinbuffer[2] = '\f';    /*  insert formfeed */
  2119.         rinbuffer[linelen+3] = '\r';/*  insert return */
  2120.         rinptr = rinbuffer+2;
  2121.         rincnt = linelen+2;        /*  count 'em */
  2122.         break;
  2123.       default:            /* <LF>data<CR> */
  2124.         rinbuffer[2] = '\n';    /*  insert newline */
  2125.         rinbuffer[linelen+3] = '\r';/*  insert return */
  2126.         rinptr = rinbuffer+2;
  2127.         rincnt = linelen+2;        /*  count 'em */
  2128.         break;
  2129.     }
  2130.     } else {
  2131.     rincnt = rab_rfile.rab$w_rsz;
  2132.     rinptr = rinbuffer;        /* reset pointer */
  2133.     }
  2134. /*
  2135.  * Here we see if we need to insert CR/LF pairs at the record boundary. For
  2136.  * the moment, we will add them if the file has "carriage return carriage
  2137.  * control" when looked at by a DIRECTORY command. As of edit 036 we also do
  2138.  * this for "print file carriage control" files. I'm open to comments de-
  2139.  * scribing cases where this doesn't work...
  2140.  */
  2141.     if (fab_rfile.fab$b_rat & (FAB$M_CR | FAB$M_PRN)) {
  2142.     rinbuffer[rincnt] = '\r';
  2143.     rinbuffer[rincnt + 1] = '\n';
  2144.     rincnt += 2;
  2145.     }
  2146.     rincnt--;            /* one less char in buffer */
  2147.     return((int)(*rinptr++) & 0377); /* because we return the first */
  2148. }
  2149.  
  2150. /*  Z F S E E K -- Seek to a given position with an input file */
  2151. /*                 Assumes block-mode I/O being used           */
  2152.  
  2153. int
  2154. zfseek(CK_OFF_T pos) {
  2155.     CK_OFF_T offset;
  2156.     unsigned long errtmp;
  2157.     int x;
  2158.  
  2159.     debug(F101,"zfseek pos","",pos);
  2160.     rab_ifile.rab$l_bkt = (unsigned long) pos >> 9; /* Get block number */
  2161.     debug(F101,"zfseek rab_ifile.rab$l_bkt","",rab_ifile.rab$l_bkt);
  2162.     rab_ifile.rab$l_bkt++;        /* VBN's are 1-based */
  2163.     offset = (unsigned long) pos & 511;    /* Get offset with block */
  2164.     debug(F101,"zfseek offset","",offset);
  2165.     errtmp = vms_lasterr;        /* Make sure we get the real error */
  2166.     vms_lasterr = 0;            /* ... */
  2167.     x = zinfill();            /* Read in the block */
  2168.     debug(F101,"zfseek zinfill","",x);
  2169.     if (x != 0) {            /* Handle any errors */
  2170.     rab_ifile.rab$l_bkt = 0;    /* Sequentially from now on */
  2171.     debug(F101,"zfseek failed","",vms_lasterr);
  2172.     return(-1);
  2173.     }
  2174.     vms_lasterr = errtmp;
  2175.     rab_ifile.rab$l_bkt = 0;        /* Sequentially from now on */
  2176.     if (offset != 0) {            /* if not block boundary... */
  2177.     zincnt = zincnt - offset;    /* ...adjust count and pointer */
  2178.     zinptr = zinptr + offset;
  2179.     }
  2180.     return(0);
  2181. }
  2182.  
  2183. /*  Z S O U T  --  Write a string to the given file, buffered.  */
  2184.  
  2185. int
  2186. zsout(n,s) int n; char *s; {
  2187. #ifdef DEBUG
  2188.     if (chkfn(n) < 1) return(-1);
  2189. #endif
  2190.     fputs(s, fp[n]);            /* Don't use fprintf here MM */
  2191.     return(0);
  2192. }
  2193.  
  2194.  
  2195. /*  Z S O U T L  --  Write string to file, with line terminator, buffered.  */
  2196.  
  2197. int
  2198. zsoutl(n,s) int n; char *s; {
  2199. #ifdef DEBUG
  2200.     if (chkfn(n) < 1) return(-1);
  2201. #endif /* DEBUG */
  2202.     if (fputs(s, fp[n]) == EOF)
  2203.       return(-1);
  2204.     putc('\n', fp[n]);
  2205.     return(0);
  2206. }
  2207.  
  2208.  
  2209. /*  Z S O U T X  --  Write x characters to file, unbuffered.  */
  2210.  
  2211. int
  2212. zsoutx(n,s,x) int n, x; char *s; {
  2213. #ifdef DEBUG
  2214.     if (chkfn(n) < 1) return(-1);
  2215. #endif
  2216.     return(write(fileno(fp[n]),s,x));
  2217. }
  2218.  
  2219.  
  2220. /*  Z C H O U T  --  Add a character to the given file.  */
  2221.  
  2222. int
  2223. #ifdef CK_ANSIC
  2224. zchout(register int n, char c)
  2225. #else
  2226. zchout(n,c) register int n; char c;
  2227. #endif /* CK_ANSIC */
  2228. /* zchout */ {
  2229. #ifdef DEBUG
  2230.     if (chkfn(n) < 1) return(-1);
  2231. #endif
  2232.     if (n == ZSFILE) {
  2233.         return(write(fileno(fp[n]),&c,1)); /* Use unbuffered for session log */
  2234.     } else {
  2235.     if (putc(c,fp[n]) == EOF)    /* If true, maybe there was an error */
  2236.       return(ferror(fp[n]) ? -1 : 0); /* Check to make sure */
  2237.     else                /* Otherwise... */
  2238.       return(0);            /* There was no error. */
  2239.     }
  2240. }
  2241.  
  2242. /*  Z O U T D U M P  --  dump buffered output characters to file.  */
  2243.  
  2244. /* Buffered file output, buffer dump */
  2245.  
  2246. /*
  2247.   No, this isn't an entry in the 199x Obfuscated C programming contest, nor
  2248.   did we get it at an all-night convenience store.  VMS requires that stream
  2249.   format files be written as records, so we have to do _lots_ of contortions
  2250.   to make sure we write whole lines as records.  Not pretty.  - TMK
  2251. */
  2252. int
  2253. zoutdump() {
  2254.     int ocnt;
  2255.     int wrote_one_line = 0;
  2256.     CHAR *optr, *srcptr, *endptr;
  2257.     char csave;
  2258.  
  2259.     debug(F101,"zoutdump zoutcnt","",zoutcnt);
  2260.     debug(F101,"zoutdump ofile_bmode","",ofile_bmode);
  2261. /*
  2262.   Well, this could be to the console. If it is, chop it into itty-bitty parts
  2263.   (the VMS CRTL can't handle a %s spec bigger than 512 bytes) and print it.
  2264. */
  2265.     if (cflag == 1) {            /* If we're dumping to console */
  2266.     endptr = zoutbuffer + zoutcnt;
  2267.     for (optr = zoutbuffer; optr < endptr; optr += 511) {
  2268.         if (optr+511 < endptr) {    /* More than 511, break up */
  2269.         csave = *(optr+511);
  2270.         *(optr+511) = '\0';
  2271.         printf("%s", optr);
  2272.         *(optr+511) = csave;
  2273.         } else {
  2274.         *endptr = '\0';        /* Make sure null-terminated */
  2275.         printf("%s", optr);
  2276.         }
  2277.     }
  2278.     zoutcnt = 0;
  2279.     zoutptr = zoutbuffer;
  2280.     return(0);
  2281.     }
  2282.  
  2283. /* Do we need to processed TYPE LABELED contortions? */
  2284.  
  2285.     if (ofile_bmode == XYFT_L) {    /* Is it labeled? */
  2286.     int x;
  2287.     if (ofile_lblproc == 0)    {    /* I've never gone this way before? */
  2288.         x = do_label_recv();    /* Beyond revolving rainbow door... */
  2289.         if (x == -1)
  2290.           return(-1);        /* Got a hard error in label proc. */
  2291.         if (x == 1 && ofile_dump != 1)
  2292.           return(0);        /* Exit so we can fill up the buffer */
  2293.     }
  2294.     }
  2295. /*
  2296.  * Well, we could be lucky...
  2297.  */
  2298.     if (zoutcnt == 0)
  2299.     return(0);
  2300. /*
  2301.  * Oh well. See if doing binary - that's easy...
  2302.  */
  2303.     if (ofile_bmode) {
  2304.     if (zoutcnt == OBUFSIZE) {
  2305. #ifdef DYNAMIC
  2306.         rab_ofile.rab$l_rbf = (char *)zoutbuffer;
  2307. #else
  2308. /* NOTE: Might need "(char *)&zoutbuffer" here */
  2309.         rab_ofile.rab$l_rbf = &zoutbuffer;
  2310. #endif /* DYNAMIC */
  2311.         rab_ofile.rab$w_rsz = OBUFSIZE;
  2312.         if (ofile_ffb != -1 && ofile_dump == 1) {
  2313.         /*
  2314.          * Only do this when doing _last_ file segment.
  2315.          */
  2316.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  2317.         if (ofile_ffb)
  2318.             rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  2319.         debug(F101,"zoutdump ofile_ffb","",(int)ofile_ffb);
  2320.         debug(F101,"zoutdump rab$w_rsz","",rab_ofile.rab$w_rsz);
  2321.         }
  2322.         rms_sts = sys$write(&rab_ofile);
  2323.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2324.         if (rms_sts != RMS$_NORMAL) {
  2325.         debug(F101,"zoutdump $write failed, status","",rms_sts);
  2326.         return(-1);
  2327.         }
  2328.     } else {
  2329. #ifdef DYNAMIC
  2330.         rab_ofile.rab$l_rbf = (char *)zoutbuffer;
  2331. #else
  2332. /* NOTE: Might need "(char *)&zoutbuffer" here */
  2333.         rab_ofile.rab$l_rbf = &zoutbuffer;
  2334. #endif
  2335.         rab_ofile.rab$w_rsz = zoutcnt;
  2336.         xabfhc_ofile.xab$w_ffb = (zoutcnt & 511)+1;
  2337.         if (ofile_ffb != -1) {
  2338.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  2339.         if (ofile_ffb)
  2340.             rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  2341.         debug(F101,"zoutdump ofile_ffb","",(int)ofile_ffb);
  2342.         debug(F101,"zoutdump rab$w_rsz","",rab_ofile.rab$w_rsz);
  2343.         }
  2344.         rms_sts = sys$write(&rab_ofile);
  2345.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2346.         if (rms_sts != RMS$_NORMAL) {
  2347.         debug(F101,"zoutdump $write failed, status","",rms_sts);
  2348.         return(-1);
  2349.         }
  2350.     }
  2351.     debug(F100,"zoutdump RMS operations completed ok","",0);
  2352.     zoutcnt = 0;
  2353.     zoutptr = zoutbuffer;
  2354.     return(0);
  2355.     }
  2356.  
  2357. /*
  2358.  * Must be ASCII. This is harder, and weirder... It's actually easier than
  2359.  * it looks, but there's (unfortunately) no really easy way to _implement_
  2360.  * it. (sigh, whimper, groan)
  2361.  */
  2362.  
  2363.     srcptr = zoutbuffer;            /* Points to first line in buffer */
  2364.     endptr = zoutbuffer + zoutcnt;  /* Points to location after last char */
  2365. zoutdump_ascii:
  2366.     /* Scan through buffer until we find a CR or we run out of chars */
  2367.     for (optr = srcptr; optr < endptr; optr++) if (*optr == CR) break;
  2368.  
  2369.     /* If there are at least 2 chars left in the buffer when we stop   */
  2370.     /* scanning, then it is assumed the above loop terminated because  */
  2371.     /* it found the CR and that both the CR and LF are present in the  */
  2372.     /* buffer (situation normal.                                       */
  2373.     /* If there are not 2 chars left in the buffer, we have one of two */
  2374.     /* cases which we treat identically:                               */
  2375.     /*  1) If there are 0 chars left in the buffer, then the line's    */
  2376.     /*     terminating CR LF are yet to come. So... we copy the data   */
  2377.     /*     to the front of the buffer and exit (next time it should be */
  2378.     /*     there.)                                                     */
  2379.     /*  2) If there is one char left in the buffer, we have the case of*/
  2380.     /*     a line with the CR but no LF present. So... do the same     */
  2381.     /*     because the LF will be coming next time.                    */
  2382.     if (optr+2 > endptr) {            /* drat! ran off the end */
  2383.     if (ofile_dump && (srcptr == endptr)) {
  2384.         /* If the beginning and end ptrs are the same, then there the  */
  2385.         /* is empty. Good news, 'cause we're clsoing up.               */
  2386.         zoutcnt = 0;        /* No looping, please. */
  2387.         zoutptr = zoutbuffer;
  2388.     }
  2389.     else if (ofile_dump) {        /* but it's cool, we're closing up */
  2390.         /* Oops, we've got a line with no LF and maybe no CR. Well     */
  2391.         /* write it out and exit abnormally.                           */
  2392.         rab_ofile.rab$l_rbf = (char *)srcptr;
  2393.         rab_ofile.rab$w_rsz = optr-srcptr;
  2394.         rms_sts = sys$put(&rab_ofile);
  2395.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2396.         zoutcnt = 0;
  2397.         zoutptr = zoutbuffer;
  2398.         if (rms_sts != RMS$_NORMAL) {
  2399.         debug(F101, "zoutdump $put failed, status","",rms_sts);
  2400.         return(-1);
  2401.         }
  2402.     } else if (wrote_one_line) {    /* it's still cool, we did one... */
  2403.         zoutcnt = optr - srcptr;    /* number of chars left */
  2404.         if (optr < endptr) zoutcnt++; /*[jah083] including CR if present */
  2405.         if (zoutcnt) memmove(zoutbuffer, srcptr, zoutcnt);
  2406.             /* Move'em to front of buffer*/
  2407.         zoutptr = zoutbuffer+zoutcnt;
  2408.     } else {            /* WRONG!!! */
  2409.         /* We've got a buffer full of chars with no LF (it may or may  */
  2410.         /* not have a terminating CR. In either case its just plain too*/
  2411.         /* long. I suppose we could check here for the optr+1 == endptr*/
  2412.         /* which indicates that there was a CR but no LF so we could   */
  2413.         /* issue a "line barely too long", but, is it useful?          */
  2414.         debug(F100, "zoutdump: line too long","",0);
  2415.         zoutcnt = 0;        /* No looping, please. */
  2416.         zoutptr = zoutbuffer;
  2417.         return(-1);
  2418.     }
  2419.     debug(F101, "zoutdump exiting, zoutcnt","",zoutcnt);
  2420.     return(0);
  2421.     }
  2422.  
  2423.     /* We now have a line that we can write, so... */
  2424.  
  2425.     rab_ofile.rab$l_rbf = (char *)srcptr;
  2426.     rab_ofile.rab$w_rsz = optr-srcptr;
  2427.     rms_sts = sys$put(&rab_ofile);
  2428.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2429.     if (rms_sts != RMS$_NORMAL) {
  2430.     debug(F101, "zoutdump $put failed, status","",rms_sts);
  2431.     return(-1);
  2432.     }
  2433.     srcptr = optr + 2;        /* Account for CR, LF */
  2434.     wrote_one_line = 1;
  2435.     goto zoutdump_ascii;
  2436. }
  2437.  
  2438. /*  C H K F N  --  Internal function to verify file number is ok.  */
  2439.  
  2440. /*
  2441.  Returns:
  2442.   -1: File number n is out of range
  2443.    0: n is in range, but file is not open
  2444.    1: n in range and file is open
  2445. */
  2446. int
  2447. chkfn(n) int n; {
  2448.     int x;
  2449.     if (n != ZDFILE)
  2450.       debug(F101,"chkfn","",n);
  2451.     switch (n) {
  2452.       case ZCTERM:
  2453.       case ZSTDIO:
  2454.       case ZIFILE:
  2455.       case ZOFILE:
  2456.       case ZDFILE:
  2457.       case ZTFILE:
  2458.       case ZPFILE:
  2459.       case ZSFILE:
  2460.     break;
  2461.       case ZSYSFN:            /* System functions */
  2462.     return(0);
  2463.       case ZRFILE:            /* READ and WRITE files */
  2464.       case ZWFILE:
  2465.       case ZMFILE:
  2466.       case ZDIFIL:
  2467.     break;
  2468.       default:
  2469.     if (n >= ZNFILS) {
  2470.         debug(F101,"chkfn: file number out of range","",n);
  2471.         fprintf(stderr,"?File number out of range - %d\n",n);
  2472.         return(-1);
  2473.     }
  2474.     }
  2475.     x = (fp[n] == NULL) ? 0 : 1;
  2476.     if (n != ZDFILE)
  2477.       debug(F101,"chkfn return","",x);
  2478.     return(x);
  2479. }
  2480.  
  2481. static int zgetfs_active = 0;
  2482. static CK_OFF_T zgfs_size = -1L;
  2483. int zgfs_dir = -1;
  2484. int zgfs_link = 0;
  2485. char linkname[2] = { '\0', '\0' };    /* No symlinks in VMS */
  2486.  
  2487. CK_OFF_T
  2488. zgetfs(name) char *name; {
  2489.     CK_OFF_T x;
  2490.     if (!name)
  2491.       name = "";
  2492.     if (!*name)
  2493.       return(-1);
  2494.     zgfs_size = -1L;
  2495.     zgfs_dir = -1;
  2496.     zgetfs_active = 1;
  2497.     x = zchki(name);
  2498.     zgetfs_active = 0;
  2499.     if (zgfs_dir < 0)
  2500.       zgfs_dir = isdir(name);
  2501.     if (x < 0 && zgfs_size >= 0)
  2502.       return(zgfs_size);
  2503.     return((x < -2) ? (CK_OFF_T)-1 : x);
  2504. }
  2505.  
  2506. /*  Z C H K I  --  Check if input file exists and is readable.  */
  2507.  
  2508. /*
  2509.   Returns:
  2510.    >= 0 if the file can be read (returns the size).
  2511.      -1 if file doesn't exist or can't be accessed,
  2512.      -2 if file exists but is not readable (e.g. a directory file).
  2513.      -3 if file exists but protected against read access.
  2514. */
  2515. CK_OFF_T
  2516. zchki(name) char *name; {
  2517.     struct stat statbuf;
  2518.     extern int zchkid;
  2519.     int x;
  2520.     struct FAB fab_chki;
  2521.     struct XABFHC xabfhc_chki;
  2522.     CK_OFF_T iflen = (CK_OFF_T)-1;
  2523.  
  2524.     if (!zchkid && !zgetfs_active && isdir(name))
  2525.       return(-2);
  2526.  
  2527.     if (!name) name = "";
  2528.     if (!*name) return(-1);
  2529.  
  2530.     if (zgetfs_active) {
  2531.         if (stat(name,&statbuf) == 0) {
  2532.         zgfs_link = 0;
  2533. #ifdef S_ISDIR
  2534.         zgfs_dir = (S_ISDIR(statbuf.st_mode)) ? 1 : 0;
  2535.         debug(F110,"zgetfs S_ISDIR",name,zgfs_dir);
  2536. #else
  2537.         zgfs_dir = isdir(name);
  2538.         debug(F110,"zgetfs isdir",name,zgfs_dir);
  2539. #endif /* S_ISDIR */
  2540.         zgfs_size = statbuf.st_size;
  2541.         debug(F110,"zgetfs size",name,zgfs_size);
  2542.         return(zgfs_size);
  2543.     }
  2544.     /* If stat() failed try RMS... */
  2545.     }
  2546.     fab_chki = cc$rms_fab;
  2547.     fab_chki.fab$b_fac = FAB$M_BIO;
  2548.     fab_chki.fab$l_fna = name;
  2549.     fab_chki.fab$b_fns = strlen(name);
  2550.     fab_chki.fab$l_xab = (char *)&xabfhc_chki;
  2551.     xabfhc_chki = cc$rms_xabfhc;
  2552.     rms_sts = sys$open(&fab_chki);
  2553.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2554.     if (rms_sts == RMS$_PRV)            /* No privs */
  2555.       return(-3);
  2556.     if (rms_sts != RMS$_NORMAL) {
  2557.     debug(F101,"zchki $open failed, status","",rms_sts);
  2558.     return(-1);
  2559.     }
  2560.     iflen = ((xabfhc_chki.xab$l_ebk-1)*512)+xabfhc_chki.xab$w_ffb;
  2561.  
  2562.     rms_sts = sys$close(&fab_chki);
  2563.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2564.     if (rms_sts != RMS$_NORMAL) {
  2565.     debug(F101,"zchki $close failed, status","",rms_sts);
  2566.     return(-1);
  2567.     }
  2568.     ckstrncpy(nambuf,name,CKMAXPATH);    /* Keep local copy of name */
  2569.     debug(F111,"zchki access ok:",name,(int) iflen);
  2570.     return( (iflen > -1) ? iflen : 0 );
  2571. }
  2572.  
  2573. /*  Z C H K O  --  Check if output file can be created.  */
  2574.  
  2575. /*
  2576.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  2577. */
  2578. #include <devdef.h>
  2579.  
  2580. int
  2581. zchko(name) char *name; {
  2582.     extern int zchkod;                  /* Used by IF WRITEABLE */
  2583.  
  2584.     struct FAB fab;            /* let RMS do the work */
  2585.     struct NAM nam;
  2586.     char expanded_str[NAM$C_MAXRSS+1];  /* n.b. nam$b_ess is byte */
  2587.  
  2588.     if (!name) return(-1);              /* Watch out for null pointer. */
  2589.     debug(F111,"zchko",name,zchkod);
  2590.  
  2591.     fab = cc$rms_fab;
  2592.     fab.fab$l_nam = &nam;
  2593.     fab.fab$l_fna = name;
  2594.     fab.fab$b_fns = strlen(name);
  2595.     fab.fab$l_alq = 1;                   /* zero length file may not be writeable */
  2596.     fab.fab$b_fac = FAB$M_BIO | FAB$M_PUT;     /* block i/o setup faster ? */
  2597.     fab.fab$l_fop = FAB$M_DLT;       /* TMD does not check directory file perm */
  2598.  
  2599.     nam = cc$rms_nam;
  2600.     nam.nam$l_esa = (char *)&expanded_str;
  2601.     nam.nam$b_ess = NAM$C_MAXRSS;
  2602.  
  2603.     rms_sts = sys$parse(&fab);             /* check the file spec */
  2604.     if (!(rms_sts & 1)) {
  2605.         if (rms_sts == RMS$_SYN) {        /* nzrtol may fix */
  2606.             debug(F110,"zchko syntax error",name,0);
  2607.             return(0);
  2608.         } else {
  2609.             CHECK_ERR("zchko ", rms_sts);
  2610.             debug(F111,"zchko parse error",name,rms_sts);
  2611.             return(-1);
  2612.         }
  2613.     }                        /* file oriented device */
  2614.     if ((fab.fab$l_dev & DEV$M_NET) || (fab.fab$l_dev & DEV$M_FOD)) {
  2615.         *(nam.nam$l_ver+nam.nam$b_ver) = '\0';
  2616.         debug(F110,"zchko create",expanded_str,0);
  2617.         rms_sts = sys$create(&fab);        /* test write capability */
  2618.         if (rms_sts & 1) {
  2619.             rms_sts = sys$close(&fab);
  2620.             return(0);
  2621.         } else {
  2622.             CHECK_ERR("zchko ", rms_sts);
  2623.             debug(F101,"zchko create error","",rms_sts);
  2624.             return(-1);
  2625.         }
  2626.      } else {
  2627.         *(nam.nam$l_dir) = '\0';
  2628.         if (fab.fab$l_dev & DEV$M_REC) {     /* record-oriented device */
  2629.             debug(F110,"zchko non-fod",expanded_str,0);
  2630.             return(0);                /* omit access test */
  2631.         } else {
  2632.             debug(F111,"zchko invalid device",expanded_str,fab.fab$l_dev);
  2633.             return(-1);
  2634.         }
  2635.      }
  2636. }
  2637.  
  2638.  
  2639. /*  Z C H K S P A  --  Check if there is enough space to store the file.  */
  2640.  
  2641. /*
  2642.  Call with file specification f, size n in bytes.
  2643.  Returns -1 on error, 0 if not enough space, 1 if enough space.
  2644. */
  2645.  
  2646. int
  2647. zchkspa(f,n) char *f; CK_OFF_T n; {
  2648.  
  2649. /*
  2650.  * This is complicated. The user could have specified an explicit path when
  2651.  * sending the file, or could have done a CWD, or could be using the default
  2652.  * directory. If not the latter, the path may not even be a disk device, as
  2653.  * CWD LPA0: is perfect legal for uploading to the lineprinter. After that,
  2654.  * if it's a disk, we should check the user's quota.  However, the user may
  2655.  * have SYSPRV, EXQUOTA, BYPASS, or maybe even GRPPRV, and it would be hard
  2656.  * to  properly check for all these cases.  So, if the file will fit on the
  2657.  * disk, we'll accept it.
  2658.  */
  2659.  
  2660.     char   *zgtdir();
  2661.  
  2662.     struct itmlstdef {
  2663.     short int buflen;
  2664.     short int itmcod;
  2665.     char *bufaddr;
  2666.     long int *retlen;
  2667.     };
  2668.  
  2669.     static char device[64];
  2670.  
  2671.     struct dsc$descriptor_s
  2672.     dev_desc = {sizeof(device), DSC$K_DTYPE_T, DSC$K_CLASS_S,
  2673.               (char *)&device};
  2674.     unsigned long freeblocks, devclass, fileblocks;
  2675.     long freelength, classlength;
  2676.  
  2677.     struct itmlstdef itmlst[] =
  2678.     {4,DVI$_FREEBLOCKS,0,0,4,DVI$_DEVCLASS,0,0,0,0,0,0};
  2679.  
  2680.     int rms_sts;
  2681.  
  2682. /* First, figure out the device we're interested in */
  2683.  
  2684.     ckstrncpy(device,zgtdir(),64);    /* Handles default or CWD */
  2685.  
  2686.     if (strchr(f, ':'))            /* If user specified path */
  2687.       ckstrncpy(device, f, 64);
  2688.  
  2689.     debug(F110,"zchkspa target device is ",device,0);
  2690.  
  2691. /* Next, ask for free block count and device type (disk vs. non-disk) */
  2692.  
  2693.     itmlst[0].bufaddr = (char *)&freeblocks;
  2694.     itmlst[0].retlen = &freelength;
  2695.     itmlst[1].bufaddr = (char *)&devclass;
  2696.     itmlst[1].retlen = &classlength;
  2697.  
  2698.     rms_sts = sys$getdviw(0,0,&dev_desc,&itmlst,0,0,0,0);
  2699.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  2700.     debug(F101,"zchkspa $getdvi returned rms_sts","",rms_sts);
  2701.  
  2702.     if (devclass != DC$_DISK)
  2703.       return(1);                /* assume space if not disk */
  2704.  
  2705.     if (rms_sts != SS$_NORMAL)
  2706.       return(1);                /* assume free space if err */
  2707.  
  2708.     debug(F101,"zchkspa $getdvi returned freeblocks","",freeblocks);
  2709.  
  2710. /* Pad file size if it's a text file */
  2711.  
  2712.     if (ofile_bmode == XYFT_T)
  2713.       n += (n/40) * 3;
  2714.  
  2715.     fileblocks = n / 512 + 1;            /* compute file size in blks */
  2716.                         /* we may want some fuzz */
  2717.     if (fileblocks >= freeblocks)
  2718.       return(0);                /* Won't fit */
  2719.     else
  2720.       return(1);                /* Will fit */
  2721. }
  2722.  
  2723. /*  Z D E L E T  --  Delete the named file.  */
  2724.  
  2725. int
  2726. zdelet(name) char *name; {
  2727.     return(delete(name));
  2728. }
  2729.  
  2730. /*  Z R T O L  --  Convert remote filename into local form.  */
  2731.  
  2732. VOID
  2733. zrtol(name,name2) char *name, *name2; {
  2734.     nzrtol(name,name2,1,0,CKMAXPATH);
  2735. }
  2736. /*  N Z R T O L  --  New zrtol handles native and standard-format pathnames */
  2737.  
  2738. VOID
  2739. nzrtol(name,name2,fncnv,fnrpath,max)
  2740.     char *name, *name2; int fncnv, fnrpath, max;
  2741. { /* nzrtol */
  2742.     int start, sfncnv, colon = 0;
  2743.     char *np, *vp = NULL, c;
  2744.     char *ls = NULL;
  2745.     static char *spcl_set = "_-$[]<>:.\";";
  2746.     char tmpbuf[NAM$C_MAXRSS+2], *tmp;
  2747.     char * dotp = NULL;
  2748.  
  2749.     debug(F110,"nzrtol name",name,0);
  2750.     if (!name2) return;
  2751.     sfncnv = fncnv;
  2752.  
  2753.     tmpbuf[0] = NUL;
  2754.     tmpbuf[1] = NUL;
  2755.     tmpbuf[2] = NUL;
  2756.     start = 0;
  2757.     tmp = tmpbuf+start;
  2758.     {
  2759.     /* Autodetection of path format */
  2760.     int lb = 0, rb = 0, sl = 0, bl = 0;
  2761.     char *p = name;
  2762.     while (*p) {
  2763.         if (*p == '[' || *p == '<' ) lb++;
  2764.         if (*p == ']' || *p == '>' ) rb++;
  2765.         if (*p == ':') colon++;
  2766.         if (*p == '/') sl++;
  2767.         if (*p == '\\') bl++;
  2768.         p++;
  2769.     }
  2770.     if (lb == 1 && rb == 1 && !sl) { /* VMS detected */
  2771.         fncnv = 0;
  2772.         debug(F100,"nzrtol VMS brackets detected","",0);
  2773.     } else if (colon == 1 && !sl) { /* VMS detected */
  2774.         fncnv = 0;
  2775.         debug(F100,"nzrtol VMS colon detected","",0);
  2776.     } else if (!lb && !rb && sl > 0) { /* UNIX detected */
  2777.         fncnv = 1;
  2778.         debug(F101,"nzrtol UNIX slashes detected","",sl);
  2779.     }
  2780.     }
  2781.     debug(F101,"nzrtol fncnv","",fncnv);
  2782.  
  2783.     /* If converting pathnames, convert to VMS format */
  2784.  
  2785.     if (fncnv) {            /* Converting, so assume UNIX format */
  2786.     np = name;            /* Set name pointer */
  2787.     *tmp++ = '[';            /* Insert opening VMS bracket */
  2788.     if (*np == '/') {        /* It's an absolute pathname */
  2789.         np++;            /* Skip past leading slash */
  2790.     } else {            /* Relative pathname */
  2791.         *tmp++ = '.';        /* Insert '.' here */
  2792.     }
  2793.     while (*np) {            /* Convert to VMS format */
  2794.         if (*np == '/') {        /* Have directory separator */
  2795.         *tmp = '.';        /* So use this notation in VMS */
  2796.         ls = tmp;        /* Remember position of last slash */
  2797.         } else {
  2798.         *tmp = *np;
  2799.         }
  2800.         np++;
  2801.         tmp++;
  2802.     }
  2803.     *tmp = NUL;
  2804.     if (ls) {            /* Replace last slash by */
  2805.         *ls = ']';            /* closing directory bracket */
  2806.     } else {            /* No slashes */
  2807.         start += 2;            /* So skip past opening "[." */
  2808.     }
  2809.     } else {                /* Assume VMS format already */
  2810. #ifdef COMMENT
  2811. /* No, this would ruin any as-name the sender gave */
  2812. /* "Be conservative in what you send, liberal in what you receive" */
  2813.     int flag = 0;            /* Copy */
  2814.     np = name;            /* But strip node and device */
  2815.     while (*np) {
  2816.         if (*np == '[')
  2817.           flag = 1;
  2818.         if (flag)
  2819.           *tmp++ = *np;
  2820.         np++;
  2821.     }
  2822.     if (!flag)
  2823. #endif /* COMMENT */
  2824.       ckstrncpy(tmp,name,NAM$C_MAXRSS); /* Just copy */
  2825.     }
  2826.     tmpbuf[NAM$C_MAXRSS] = NUL;        /* Make sure buffer is terminated */
  2827.     tmp = tmpbuf + start;        /* Reset pointer */
  2828.     name = tmp;                /* Treat new string as original arg */
  2829.     debug(F110,"nzrtol tmp 1",tmp,0);
  2830.  
  2831.     /* Now we have VMS path format in tmpbuf */
  2832.  
  2833.     if (fnrpath == PATH_OFF) {        /* RECEIVE PATHNAMES OFF */
  2834.     zstrip(name,&np);
  2835.     ckstrncpy(tmpbuf,np,NAM$C_MAXRSS);
  2836.     tmp = tmpbuf;
  2837.     debug(F110,"nzrtol PATH_OFF",tmp,0);
  2838.     } else if (fnrpath == PATH_ABS) {    /* RECEIVE PATHNAMES ABSOLUTE */
  2839.     /* Nothing to do */
  2840.     debug(F110,"nzrtol PATH_ABS",tmp,0);
  2841.     } else if (isabsolute(name)) {    /* RECEIVE PATHNAMES RELATIVE */
  2842.     int x;
  2843.     char * bb = NULL;
  2844.     x = strlen(name);
  2845.     debug(F111,"nzrtol converting absolute to relative tmp",tmp,x);
  2846. #ifdef COMMENT
  2847.     if (bb = malloc(x+x+1)) {    /* Be safe */
  2848.         char * b = bb;
  2849.         np = name;
  2850.         while (*np) {
  2851.         *b++ = *np;
  2852.         if (*np == '[' && *(np+1) != '.')
  2853.           *b++ = '.';
  2854.         np++;
  2855.         }
  2856.         *b = NUL;
  2857.         ckstrncpy(tmpbuf,bb,NAM$C_MAXRSS);
  2858.         tmp = tmpbuf;
  2859.         free(bb);
  2860.     }
  2861. #else  /* Not COMMENT - From Lucas Hart, Oct 1999 */
  2862.        /* strip any node or device and move in place */
  2863.         start = ckindex(":",name,-1,1,1);
  2864.         np = name + start;        /* ptr to name in tmpbuf */
  2865.         bb = tmpbuf;            /* destination */
  2866.     if (tmpbuf[0] == '[') {        /* [179] If it starts with a bracket */
  2867.         *bb++ = *np++;        
  2868.         if (*np != '.')
  2869.           *bb++ = '.';        /* make relative */
  2870.     }
  2871.         memmove(bb,np,strlen(np)+1);    /* safe: tmpbuf[NAM$C_MAXRSS] = NUL */
  2872.         tmp = tmpbuf;
  2873. #endif  /* COMMENT */
  2874.     debug(F110,"nzrtol PATH_REL 1",tmp,0);
  2875.     } else {                /* Ditto */
  2876.     /* Nothing to do - it's already done */
  2877.     debug(F110,"nzrtol PATH_REL 2",tmp,0);
  2878.     }
  2879.     tmpbuf[NAM$C_MAXRSS] = NUL;
  2880.     debug(F110,"nzrtol tmp 2",tmp,0);
  2881.  
  2882. #ifdef COMMENT                /* Not needed for Edit 166 zmkdir */
  2883.     /* But wait, there's more... */
  2884.     /* Convert relative name to absolute or else zmkdir won't work */
  2885.     {
  2886.     char buf2[NAM$C_MAXRSS+2], *p, *b;
  2887.     int n = 0, flag = 0;
  2888.     np = tmp;            /* Source pointer */
  2889.     b = buf2;            /* Where to put new name */
  2890.     while (*np && n < NAM$C_MAXRSS) { /* And substitute it; */
  2891.         *b++ = *np;
  2892.         n++;
  2893.         if (*np == '[' && *(np+1) == '.') {
  2894.         p = zgtdir();        /* Get current directory */
  2895.         while (*p && *p != '[')    /* The part inside the brackets */
  2896.           p++;
  2897.         if (*p = '[') {        /* Substitute it in */
  2898.             p++;
  2899.             while (*p && *p != ']') {
  2900.             *b++ = *p++;
  2901.             if (n++ >= NAM$C_MAXRSS)
  2902.               break;
  2903.             }
  2904.         }
  2905.         }
  2906.         np++;
  2907.     }
  2908.     *b = NUL;
  2909.     tmp = buf2;
  2910.     debug(F110,"nzrtol tmp 3",tmp,0);
  2911.     }
  2912. #endif /* COMMENT */
  2913.     fncnv = sfncnv;            /* Restore original value of this */
  2914.     if (!fncnv) {            /* Now check it; if not converting */
  2915.     ckstrncpy(name2,tmp,max);    /* We're done. */
  2916.     debug(F110,"nzrtol name2",name2,0);
  2917.     return;
  2918.     }
  2919.     /* Now convert the characters themselves */
  2920.  
  2921.     name = tmp;
  2922.     for (np = name2; c = *name; name++) {
  2923.     if (islower(c))
  2924.       c = toupper(c);
  2925.     else if (c == '~' || c == SP)
  2926.       c = '_';
  2927.     else if (!isalnum(c) && !strchr(spcl_set,c))
  2928.       c = '$';
  2929.     *np++ = c;
  2930.     }
  2931.     *np = NUL;                /* End of name */
  2932.     {                    /* Now take care of periods. */
  2933.     int x;
  2934.     int ndots = 0;
  2935.     char * ld = NULL;        /* Last dot */
  2936.     char * nld = NULL;        /* Next to last dot */
  2937.     x = strlen(name2) - 1;
  2938.     for (; x >= 0; x--) {        /* Keep only the last one. */
  2939.         if (name2[x] == ']' ||    /* But only do this */
  2940.         name2[x] == ':' ||    /* in the filename part */
  2941.         name2[x] == '>'
  2942.         )
  2943.           break;
  2944.         if (name2[x] == '.') {    /* Turn prior ones to underscore. */
  2945.         ndots++;
  2946.         if (ndots == 1)
  2947.           ld = name2+x;
  2948.         else if (ndots == 2)
  2949.           nld = name2+x;
  2950.         else if (ndots > 2)
  2951.           name2[x] = '_';
  2952.         }
  2953.     }
  2954. /*
  2955.   Finally we check to see if the final dot was really a version-number
  2956.   introducer; if so, we turn it into a semicolon and keep the next-to-last
  2957.   dot, otherwise we replace the next-to-last dot with an underscore.  Then we
  2958.   have exactly one dot in the name.
  2959. */
  2960.     if (nld && ld) {        /* We have two dots left */
  2961.         char *sld = ld;        /* Save position of last one */
  2962.         ld++;            /* Point past it */
  2963.         while (*ld <= '9' && *ld >= '0') /* See if only digits follow */
  2964.           ld++;
  2965.         if (*ld) {            /* No */
  2966.         *nld = '_';        /* replace previous '.' by '_' */
  2967.         } else {            /* yes */
  2968.         *sld = ';';        /* replace '.' by ';' */
  2969.         }
  2970.     }
  2971.     }
  2972.     debug(F110,"nzrtol name2 1",name2,0);
  2973.  
  2974.     /* But wait, there's more -- each dotted segment must be <= 39 chars  */
  2975.  
  2976.     {
  2977.     char buf3[NAM$C_MAXRSS+2], *p, *b;
  2978.     int n = 0;
  2979.     for (b = buf3, p = name2; *p; p++) {
  2980.         if (*p == ';'
  2981. #ifdef VMSVERSIONS
  2982.         && !vmsrversions
  2983. #endif /* VMSVERSIONS */
  2984.         ) {            /* Discard version number */
  2985.         if (rdigits(p+1)) {
  2986.             *b++ = '\0';
  2987.             break;
  2988.         }
  2989.         }
  2990.         if (*p == '.' ||        /* Check for segment boundary */
  2991.         *p == '[' ||
  2992.         *p == ']' ||
  2993.         *p == ':' ||
  2994.         *p == '<' ||
  2995.         *p == '>' ||
  2996.         *p == ';')
  2997.           n = 0;
  2998.         if (n < 39) {        /* In segment copy up to 39 chars */
  2999.         *b++ = *p;
  3000.         n++;
  3001.         }
  3002.     }
  3003.     *b = '\0';
  3004.     ckstrncpy(name2,buf3,max);
  3005.     }
  3006.     debug(F110,"nzrtol name2 2",name2,0);
  3007. }
  3008.  
  3009. /*  Z L T O R  --  Convert filename from local format to common form.  */
  3010.  
  3011. VOID
  3012. zltor(name,name2) char *name, *name2; {
  3013.     nzltor(name,name2,1,0,CKMAXPATH);
  3014. }
  3015.  
  3016. VOID
  3017. nzltor(name,name2,fncnv,fnspath,cp_len)
  3018.     char *name, *name2; int fncnv, fnspath, cp_len;
  3019. /* nzltor */ {
  3020.     char *cp, *pp;
  3021.     int flag;
  3022.     struct FAB fab;
  3023.     struct NAM nam;
  3024.     char expanded_name[NAM$C_MAXRSS];
  3025.     char dirbuf[NAM$C_MAXRSS], *p, *q, *q2, *r, *s, *s2;
  3026.     int long rms_status;
  3027.     int cur_len = 0;
  3028.  
  3029.     debug(F111,"zltor fncnv",name,fncnv);
  3030.  
  3031.     flag = PARSE_NAME|PARSE_TYPE;
  3032.     if (fnspath == PATH_REL || fnspath == PATH_ABS)
  3033.       flag |= PARSE_DIRECTORY;
  3034. #ifdef VMSVERSIONS
  3035.     if (vmssversions)
  3036.       flag |= PARSE_VERSION;
  3037. #endif /* VMSVERSIONS */
  3038.  
  3039.     fab = cc$rms_fab;
  3040.     fab.fab$l_nam = &nam;        /* NAM block address */
  3041.     fab.fab$l_fna = name;        /* Name string */
  3042.     fab.fab$b_fns = strlen(name);    /* Length of name string */
  3043.     fab.fab$b_dns = 6;            /* Length of default name */
  3044.     fab.fab$l_dna = "NONAME";        /* Default name */
  3045.  
  3046.     nam = cc$rms_nam;
  3047.     nam.nam$l_esa = (char *)&expanded_name;
  3048.     nam.nam$b_ess = sizeof(expanded_name);
  3049.  
  3050.     if (!CHECK_ERR("%%CKERMIT-W-ZLTOR",sys$parse(&fab)))
  3051.       return;
  3052.  
  3053.     cp = name2;                /* Point to result */
  3054.     *cp = '\0';                /* Initialize it to empty */
  3055.  
  3056.     if ((PARSE_NODE & flag) && nam.nam$b_node && /* DECnet node:: */
  3057.     cur_len+nam.nam$b_node < cp_len) {
  3058.     cur_len += nam.nam$b_node;
  3059.     strncat(cp, nam.nam$l_node, (int)nam.nam$b_node);
  3060.     }
  3061.     if ((PARSE_DEVICE & flag) && nam.nam$b_dev && /* Device: */
  3062.         cur_len+nam.nam$b_dev < cp_len) {
  3063.     cur_len += nam.nam$b_dev;
  3064.     strncat(cp, nam.nam$l_dev, (int)nam.nam$b_dev);
  3065.     }
  3066.  
  3067.     /* Directory Name [] */
  3068.  
  3069.     if ((PARSE_DIRECTORY & flag) && nam.nam$b_dir &&
  3070.         cur_len+nam.nam$b_dir < cp_len) {
  3071.     int i; char * tmp;
  3072.         q = nam.nam$l_dir;        /* The directory name from RMS */
  3073.     i = nam.nam$b_dir;        /* Length; string not nul-terminated */
  3074.     debug(F111,"zltor nam$_dir",q,i);
  3075.     if (!q) q = "[]";
  3076.     if (!*q) q = "[]";
  3077.     if (i < 0) i = 0;
  3078.     tmp = NULL;
  3079.     if (i > 0) {            /* Copy directory part */
  3080.         if (tmp = malloc(i+1)) {
  3081.         p = tmp;
  3082.         for (; i > 0 ; i--)
  3083.           *p++ = *q++;
  3084.         *p = NUL;
  3085.         }
  3086.     }
  3087.     q = tmp;
  3088.     debug(F111,"zltor directory part",q,i);
  3089.  
  3090.     s = zgtdir();            /* Get current directory */
  3091.     debug(F110,"zltor zgtdir",s,0);
  3092.     if (!s) s = "[]";
  3093.     if (!*s) s = "[]";
  3094.     s2 = "";
  3095.     while (*s && *s != '[')
  3096.       s++;
  3097.     if (*s) {
  3098.         s2 = s+1;
  3099.         while (*s2 && *s2 != ']') s2++; /* Closing bracket */
  3100.     }
  3101.     if (*s2) if (!*(s2+1)) *(s2+1) = NUL;
  3102.     debug(F110,"zltor current dir",s,0);
  3103.  
  3104. /* First change the VMS pathname to relative format if fnspath == PATH_REL */
  3105.  
  3106.     p = dirbuf;            /* Result */
  3107.     *p++ = *q++;            /* Copy left bracket and... */
  3108.  
  3109.     s++;                /* Point past it */
  3110.     q2 = q;                /* Remember this place */
  3111.     if (fnspath == PATH_REL) {    /* Compare this and current dir */
  3112.         while (*s == *q && *s && *q && *s != ']') {
  3113.         s++;
  3114.         q++;
  3115.         }
  3116.     }
  3117.     if ( (*s != ']' && *q != ']' && *q != '.') ||
  3118.              (*q == ']' && *s != ']') ) {               /* No match */
  3119.         p-- ;            /* So rewind source pointer */
  3120.     } else if (*q == ']' && *s == ']') { /* Current directory */
  3121.         p = dirbuf;            /* So make directory part blank */
  3122.     } else {            /* Not current directory */
  3123.         while (*q) *p++ = *q++;    /* so copy the rest */
  3124.     }
  3125.     *p = NUL;
  3126.     debug(F110,"zltor result 1",dirbuf,0);
  3127. /*
  3128.    VMS directory name is now in dirbuf in either absolute or relative format.
  3129.    Now change it to standard (UNIX) format if desired.
  3130. */
  3131.     p = dirbuf;            /* Working pointer */
  3132.     r = dirbuf;            /* Result pointer */
  3133.     if (dirbuf[0]) {
  3134.         extern char whoareu[], * cksysid;
  3135.         /* Converting directory format */
  3136.         debug(F110,"zltor whoareu",whoareu,0);
  3137.         debug(F110,"zltor cksysid",cksysid,0);
  3138.         if (fncnv || (whoareu[0] && strcmp((char *)whoareu,cksysid))) {
  3139.         int xflag = 0;
  3140.         if (p[1] == '.') {    /* Directory name is relative */
  3141.             r += 2;        /* Point past the leading dot */
  3142.             p += 2;
  3143.         }
  3144.         while (*p) {        /* Now convert the rest */
  3145.             if (*p == '.' || *p == '[' || *p == ']') {
  3146.             if (!xflag) *p = '/';
  3147.             if (*p == ']')
  3148.               xflag = 1;
  3149.             }
  3150.             p++;
  3151.         }
  3152.         }
  3153.     }
  3154.     debug(F110,"zltor result 2",r,0);
  3155.     if (tmp) free(tmp);
  3156.     i = strlen(r);
  3157.     if (i > 0) {
  3158.         strncat(cp,r,cp_len);
  3159.         cur_len += i;
  3160.     }
  3161.     }
  3162.     if ((PARSE_NAME & flag) && nam.nam$b_name &&
  3163.         cur_len+nam.nam$b_name < cp_len) {
  3164.     cur_len += nam.nam$b_name;
  3165.     strncat(cp, nam.nam$l_name, (int)nam.nam$b_name);
  3166.     }
  3167.     if ((PARSE_TYPE & flag) && nam.nam$b_type &&
  3168.         cur_len+nam.nam$b_type < cp_len) {
  3169.     cur_len += nam.nam$b_type;
  3170.     strncat(cp, nam.nam$l_type, (int)nam.nam$b_type);
  3171.     }
  3172.     if ((PARSE_VERSION & flag) && nam.nam$b_ver &&
  3173.         cur_len+nam.nam$b_ver < cp_len) {
  3174.     cur_len += nam.nam$b_ver;
  3175.     strncat(cp, nam.nam$l_ver, (int)nam.nam$b_ver);
  3176.     }
  3177.     if (fncnv && name2[cur_len] == NUL && name2[cur_len-1] == '.')
  3178.       name2[cur_len-1] = NUL;
  3179.     name2[cp_len-1] = NUL;
  3180.     debug(F111,"zltor result",name2,cur_len);
  3181. }
  3182.  
  3183. /*  C V T D I R  --  Convert directory name from [FOO]BAR.DIR;1 to [FOO.BAR] */
  3184.  
  3185. /*
  3186.   Call with:
  3187.     s = pointer to a string that has already been verified as a directory name
  3188.     q = pointer to where to put result
  3189.     len = size of destination buffer
  3190.   Returns:
  3191.     n on success, n > 0, size of result
  3192.     0 if no conversion was needed
  3193.    -1 on failure
  3194. */
  3195. int
  3196. cvtdir(s,s2,len) char * s, * s2; int len; {
  3197.     int i, n;
  3198.     char dirbuf[NAM$C_MAXRSS+1];
  3199.     char * p = NULL;            /* Pointer to period */
  3200.     char * o = NULL;            /* Pointer to left bracket */
  3201.     char * r = NULL;            /* Pointer to right bracket */
  3202.     char * v = NULL;            /* Pointer to version */
  3203.     char * c = NULL;            /* Pointer to colon */
  3204.     char * q = NULL;
  3205.     char * t;
  3206.     int xx = NAM$C_MAXRSS+1;
  3207.  
  3208.     debug(F111,"cvtdir",s,len);
  3209.  
  3210.     if (!s || !s2)
  3211.       return(-1);
  3212.     n = strlen(s);
  3213.     if (n < 1)
  3214.       return(-1);
  3215.     q = malloc(n+1);
  3216.     if (!q)
  3217.       return(-1);
  3218.     t = q;
  3219.     while (*s) {
  3220.     *q++ = islower(*s) ? toupper(*s) : *s;
  3221.     s++;
  3222.     }
  3223.     *q = NUL;
  3224.     s = t;
  3225.     debug(F111,"cvtdir 2",s,n);
  3226.     for (i = n; i > 0; i--) {
  3227.     if (s[i] == ';')
  3228.       v = s+i;
  3229.     else if (s[i] == '.')
  3230.       p = s+i;
  3231.     else if (s[i] == ':')
  3232.       c = s+i;
  3233.     else if (s[i] == ']' || s[i] == '>')
  3234.       r = s+i;
  3235.     else if (s[i] == '[' || s[i] == '<')
  3236.       o = s+i;
  3237.     }
  3238.     debug(F110,"cvtdir c",c,0);
  3239.     debug(F110,"cvtdir r",r,0);
  3240.     debug(F110,"cvtdir o",o,0);
  3241.     debug(F110,"cvtdir p",p,0);
  3242.     debug(F110,"cvtdir v",v,0);
  3243.  
  3244.     dirbuf[0] = NUL;
  3245.  
  3246.     if (c) {                /* Have colon? */
  3247.     *c = NUL;
  3248.     ckstrncat(dirbuf,s,xx);
  3249.     ckstrncat(dirbuf,":",xx);
  3250.     s = c+1;
  3251.     }
  3252.     debug(F110,"cvtdir 3",dirbuf,0);
  3253.     if (o) {                /* Have opening bracket? */
  3254.     if (r) {
  3255.         *r = NUL;
  3256.         s = r+1;
  3257.         ckstrncat(dirbuf,"[",xx);
  3258.         ckstrncat(dirbuf,o+1,xx);
  3259.     } else
  3260.       return(-1);
  3261.     } else
  3262.       ckstrncat(dirbuf,"[",xx);
  3263.     debug(F110,"cvtdir 4",dirbuf,0);
  3264.  
  3265.     if (p) {
  3266.     if (*(p+1) == 'D' &&
  3267.         *(p+2) == 'I' &&
  3268.         *(p+3) == 'R' &&
  3269.         (*(p+4) == ';' || *(p+4) == '.' || *(p+4) == NUL))
  3270.       *p = NUL;
  3271.     else
  3272.       return(-1);
  3273.     ckstrncat(dirbuf,".",xx);
  3274.     ckstrncat(dirbuf,s,xx);
  3275.     }
  3276.     ckstrncat(dirbuf,"]",xx);
  3277.     debug(F110,"cvtdir 5",dirbuf,0);
  3278.     xx = ckstrncpy(s2,dirbuf,len);
  3279.     return((xx < (int)strlen(dirbuf)) ? -1 : xx);
  3280.  
  3281. #ifdef COMMENT
  3282.     if (r && p && v) {
  3283.     if (*(p+1) == 'D' &&
  3284.         *(p+2) == 'I' &&
  3285.         *(p+3) == 'R' &&
  3286.         *(p+4) == ';') {
  3287.         *r = NUL;
  3288.         strcpy(q,s);
  3289.         strcat(q,".");
  3290.         *p = NUL;
  3291.         strcat(q,p);
  3292.         strcat(q,"]");
  3293.     }
  3294.     debug(F110,"cvtdir result",q,0);
  3295.     return(1);
  3296.     }
  3297.     return(0);
  3298. #endif /* COMMENT */
  3299. }
  3300.  
  3301. /*  Z C H D I R  --  Change directory.  */
  3302.  
  3303. static int setddir = 0;
  3304. /*
  3305.   This one is for restoring the startup directory in case SYS$SETDDIR() was
  3306.   ever called -- see comments below in zchdir().
  3307. */
  3308. VOID
  3309. zrestoredir() {
  3310.     int status;
  3311.     struct dsc$descriptor_s indesc;
  3312.     if (!setddir)
  3313.       return;
  3314.     debug(F101,"zrestoredir setddir","",setddir);
  3315.     indesc.dsc$w_length = (int) strlen((char *)startupdir);
  3316.     indesc.dsc$a_pointer = (char *)startupdir;
  3317.     indesc.dsc$b_class = DSC$K_CLASS_S;
  3318.     indesc.dsc$b_dtype = DSC$K_DTYPE_T;
  3319.     status = sys$setddir(&indesc, 0, 0);
  3320.     if (!(status & 1)) vms_lasterr = status;
  3321.     debug(F111,"zrestoredir sys$setddir",startupdir,status);
  3322.     return;
  3323. }
  3324.  
  3325. int
  3326. zchdir(dirnam) char *dirnam; {
  3327. #ifndef NOSPL
  3328.     extern struct mtab *mactab;             /* Main macro table */
  3329.     extern int nmac;                        /* Number of macros */
  3330. #endif /* NOSPL */
  3331.     char dirbuf[NAM$C_MAXRSS+1];
  3332.     int status;
  3333.     struct dsc$descriptor_s indesc;
  3334.  
  3335.     if (!dirnam)            /* Watch out for null pointers */
  3336.       dirnam = "";
  3337.     debug(F110,"zchdir",dirnam,0);
  3338.  
  3339.     if (!*dirnam) {            /* No arg so back to home directory */
  3340. #ifdef COMMENT
  3341.     ckstrcpy(dirbuf,getenv("HOME"),NAM$C_MAXRSS);
  3342.     dirnam = (char *) dirbuf;
  3343.     debug(F110,"zchdir home","",0);
  3344.     if (!isdir(dirnam))
  3345.       return(0);
  3346. #else
  3347.     dirnam = "SYS$LOGIN";        /* Supply default */
  3348. #endif /* COMMENT */
  3349.     }
  3350.  
  3351. #ifdef COMMENT                /* Why is this commented out? */
  3352. /* We now assume that dirnam is a valid directory name */
  3353.     status = isdir(dirnam);        /* Preverify to prevent C lib traps */
  3354.     debug(F111,"zchdir isdir 1",dirnam,status);
  3355.     x = strlen(dirnam);
  3356.     if (!status) {            /* Not a directory */
  3357.     if (dirnam[0] == '[')
  3358.       return(0);
  3359.     if (x > 0 && (dirnam[x-1] == ']' || dirnam[x-1] == ':'))
  3360.       return(0);
  3361.     if (*dirnam == '.')        /* Be nice - is it a subdirectory */
  3362.       sprintf(dirbuf,"[%s]",dirnam); /* of the current directory? */
  3363.     else
  3364.       sprintf(dirbuf,"[.%s]",dirnam);
  3365.     status = isdir(dirbuf);
  3366.     debug(F111,"zchdir isdir 2",dirnam,status);
  3367.     if (!status)
  3368.       return(0);            /* Nope */
  3369.     dirnam = dirbuf;        /* Ayup */
  3370.     } /* ( We should really save "ayup" comments for the Maine program :-) */
  3371.  
  3372.     if (status == 2) {            /* It's [FOO]BAR.DIR;1 */
  3373.     /* Must convert to [FOO.BAR] */
  3374.     debug(F111,"zchdir malloc",dirnam,x);
  3375.     if (cvtdir(dirnam,(char *)dirbuf,NAM$C_MAXRSS+1) > 0) {
  3376.         debug(F110,"zchdir cvtdir",dirbuf,0);
  3377.         dirnam = dirbuf;
  3378.     }
  3379.     }
  3380. #endif /* COMMENT */
  3381. /*
  3382.   chdir() works as expected, when it works.  It only affects this process
  3383.   and those below it, and it handles search lists, hidden devices, etc.
  3384.   EXCEPT in VMS 6.2, in which a bug was introduced in the C runtime library.
  3385.   So we try chdir() first...
  3386. */
  3387.     debug(F110,"zchdir chdir",dirnam,0);
  3388.     status = chdir(dirnam);
  3389.     debug(F101,"zchdir status","",status);
  3390. /*
  3391.   chdir() fails in VMS 6.2 when given a logical name like SYS$LOGIN that
  3392.   refers to a search list (e.g. for the SYSMGR ID on a cluster) or to a
  3393.   hidden device.  In this case SYS$SETDDIR works, but it has the unfortunate
  3394.   side effect of changing the default directory for the whole job, not just
  3395.   C-Kermit and below.
  3396. */
  3397.     if (status == 0) {            /* chdir() worked */
  3398.     return(1);
  3399.     } else {                /* chdir() failed */
  3400.     indesc.dsc$w_length = (int) strlen(dirnam);
  3401.     indesc.dsc$a_pointer = dirnam;
  3402.     indesc.dsc$b_class = DSC$K_CLASS_S;
  3403.     indesc.dsc$b_dtype = DSC$K_DTYPE_T;
  3404.     status = sys$setddir(&indesc, 0, 0);
  3405.     if (!(status & 1)) vms_lasterr = status;
  3406.     debug(F111,"zchdir sys$setddir",dirnam,status);
  3407.     if (status == RMS$_NORMAL) {
  3408.         setddir = 1;        /* Set this if setddir() ever used */
  3409. #ifndef NOSPL
  3410.             if (nmac) {            /* Any macros defined? */
  3411.                 int k;            /* Yes */
  3412.                 static on_cd = 0;
  3413.                 if (!on_cd) {
  3414.                     on_cd = 1;
  3415.                     k = mlook(mactab,"on_cd",nmac);  /* Look this up */
  3416.                     if (k >= 0) {                    /* If found, */
  3417.                         if (dodo(k,zgtdir(),0) > -1) /* set it up, */
  3418.               parser(1);                 /* and execute it */
  3419.                     }
  3420.                     on_cd = 0;
  3421.                 }
  3422.             }
  3423. #endif /* NOSPL */
  3424.         return(1);
  3425.     } else
  3426.       return(0);
  3427.     }
  3428. }
  3429.  
  3430. /*  Z H O M E  --  Return pointer to user's home (login) directory.  */
  3431.  
  3432. char *
  3433. zhome() {
  3434.  
  3435. /*  return(getenv("HOME")); */          /* No... */
  3436. /*  return(getenv("SYS$LOGIN")); */     /* No... */
  3437. /*  return("SYS$LOGIN"); */             /* Maybe - but we need a terminator */
  3438.     return("SYS$LOGIN:");
  3439. }
  3440.  
  3441. /*  Z R E L N A M E  --  Make full pathname to relative to given directory */
  3442. /*
  3443.   Inputs:
  3444.     filespec: a full VMS pathname for a file, e.g.      DSK0:[DIR]OOFA.TXT;3
  3445.     dir:      a full VMS pathname for a directory, e.g. DSK0:[DIR.SUBDIR]
  3446.   Returns:
  3447.     Pointer to filespec relative to the given directory.
  3448.   Examples:
  3449.     1. filespec: DSK0:[OLGA]LOGIN.COM;63
  3450.        dir:      DSK0:[OLGA]
  3451.        result:   LOGIN.COM;63
  3452.     2. filespec: DSK0:[OLGA.LETTERS]JURY.TXT;1
  3453.        dir:      DSK0:[OLGA]
  3454.        result:   [.LETTERS]JURY.TXT;1
  3455.     3. filespec: DSK0:[OLGA.LETTERS.ANGRY]COMPLAINT.TXT;1
  3456.        dir:      DSK0:[OLGA]
  3457.        result:   [.LETTERS.ANGRY]COMPLAINT.TXT;1
  3458.     4. filespec: DSK0:[OLGA]LOGIN.COM;63
  3459.        dir:      DSK0:[OLGA.LETTERS.ANGRY]
  3460.        result:   [--]LOGIN.COM;63
  3461.     5. filespec: DSK0:[OLGA.LETTERS.NICE]PRAISE.TXT;3
  3462.        dir:      DSK0:[OLGA.LETTERS.ANGRY]
  3463.        result:   [-.NICE]PRAISE.TXT;3
  3464.     6. filespec: DSK1:[SYSTOOLS]KERMIT.EXE;1
  3465.        dir:      DSK0:[OLGA.LETTERS.ANGRY]
  3466.        result:   DSK1:[SYSTOOLS]KERMIT.EXE;1
  3467. */
  3468. static char relnambuf[CKMAXPATH];
  3469.  
  3470. char *
  3471. zrelname(filespec, dir) char *filespec, *dir; {
  3472.     char * xs = filespec;        /* Should be a complete filespec, */
  3473.     char * s = filespec;        /* like DEV:[DIR]NAME.EXT;n. */
  3474.     char * p = dir;            /* Should be complete DEV:[DIR] */
  3475.     char * xp = NULL;
  3476.     char indir = NUL;
  3477.  
  3478.     debug(F110,"zrelname filespec",filespec,0);
  3479.     debug(F110,"zrelname dir",dir,0);
  3480.  
  3481.     for (; *s; s++,p++) {        /* Compare */
  3482.     if (*s == *p) {
  3483.         if (*s == ':') {        /* End of device portion */
  3484.         xs = s;
  3485.         } else if (*s == '[') {    /* Begin directory portion */
  3486.         indir = ']';
  3487.         } else if (*s == '<') {    /* Ditto */
  3488.         indir = '>';
  3489.         } else if (*s == '.' && indir) { /* End of directory segment */
  3490.         xs = s;            /* Remember it */
  3491.         xp = p;
  3492.         } else if (*s == indir) {    /* End of directory portion */
  3493.         xs = s;
  3494.         break;
  3495.         } else if (*p == indir) {
  3496.         xp = p;
  3497.         break;
  3498.         }
  3499.         continue;
  3500.     } else if (*s == '.' && indir && *p == indir) {
  3501.         xp = NULL;
  3502.         xs = s;
  3503.     } else if (*p == '.' && indir && *s == indir) {
  3504.         xp = p;
  3505.         xs = s;
  3506.     }
  3507.     break;
  3508.     }
  3509.  
  3510.     debug(F110,"zrelname xs",xs,0);
  3511.     debug(F110,"zrelname xp",xp,0);
  3512.  
  3513.     if (xs == filespec || !*xs) {    /* Nothing to strip */
  3514.     ckstrncpy(relnambuf,filespec,CKMAXPATH);
  3515.     } else if (xp && (*s != indir || *p != indir)) { /* File is above here */
  3516.     int i, m = 0;
  3517.     char c, * s2 = xp;
  3518.     while (c = *s2++) {
  3519.         if (c == '.')        /* Count the levels */
  3520.           m++;
  3521.         else if (c == indir)
  3522.           break;
  3523.     }
  3524.     relnambuf[0] = (indir == ']') ? '[' : '<'; /* Emit opening bracket */
  3525.     for (i = 1; i <= m; i++)    /* then the right number of dashes */
  3526.       relnambuf[i] = '-';
  3527.     ckstrncpy(relnambuf+i,xs,CKMAXPATH-i); /* then the rest. */
  3528.     debug(F110,"zrelnam result 1",relnambuf,0);
  3529.     } else {                /* File is in a subdirectory */
  3530.     switch (*xs) {            /* so we can strip some parts */
  3531.       case ':':
  3532.       case ']':
  3533.       case '>':
  3534.         ckstrncpy(relnambuf,xs+1,CKMAXPATH);
  3535.         break;
  3536.       case '.':
  3537.         sprintf(relnambuf,"%c%s",(indir == ']') ? '[' : '<',xs);
  3538.         break;
  3539.       default:
  3540.         ckstrncpy(relnambuf,filespec,CKMAXPATH);
  3541.     }
  3542.     debug(F110,"zrelnam result 2",relnambuf,0);
  3543.     }
  3544.     return((char *)relnambuf);
  3545. }
  3546.  
  3547. /*  Z G T D I R  --  Return pointer to user's current directory.  */
  3548.  
  3549. char *
  3550. zgtdir() {
  3551.  
  3552. /* OLD_VMS (pre V5.0) is defined in the build file */
  3553.  
  3554. #ifdef OLD_VMS
  3555.     static char *gtdir_buf = 0;
  3556.     static char sysdisk[] = "SYS$DISK";
  3557.     char tmp_buf[NAM$C_MAXRSS+1];
  3558.     struct dsc$descriptor_s
  3559.     tmp_buf_dsc = {sizeof(tmp_buf),DSC$K_DTYPE_T,DSC$K_CLASS_S,&tmp_buf},
  3560.     sysdisk_dsc = {sizeof(sysdisk)-1,DSC$K_DTYPE_T,DSC$K_CLASS_S,&sysdisk};
  3561.     unsigned short int buf_len;
  3562. /*
  3563.  * Allocate buffer dynamically, first time through.  This makes the image
  3564.  * smaller.
  3565.  */
  3566.     if (!gtdir_buf) gtdir_buf = malloc(NAM$C_MAXRSS+1);
  3567. /*
  3568.  * Translate device name.
  3569.  */
  3570.     lib$sys_trnlog(     &sysdisk_dsc,
  3571.             &buf_len,
  3572.             &tmp_buf_dsc,
  3573.             0,
  3574.             0,
  3575.             0);
  3576.     tmp_buf[buf_len] = '\0';
  3577.     ckstrncpy(gtdir_buf,tmp_buf,NAM$C_MAXRSS);
  3578. /*
  3579.  * Get directory name.
  3580.  */
  3581.     sys$setddir(    0,      /* New dir addr */
  3582.             &buf_len, /* length addr */
  3583.             &tmp_buf_dsc);
  3584.     tmp_buf[buf_len] = '\0';
  3585.     strcat(gtdir_buf,tmp_buf);
  3586.  
  3587.     return(gtdir_buf);  /* Can't seem to make LINK find getcwd()... */
  3588.             /* (wbader: removed &) */
  3589. #else
  3590.     char *getcwd();
  3591.     char *buf;
  3592.  
  3593.     buf = cwdbuf;
  3594.     return(getcwd(buf,100));
  3595. #endif
  3596. }
  3597.  
  3598. /*  Z G P I D  --  Get process ID (of Kermit)  */
  3599.  
  3600. long
  3601. zgpid() {
  3602.     unsigned long sts, pid;
  3603.     struct itmlstdef {
  3604.     short int buflen;
  3605.     short int itmcod;
  3606.     char *bufaddr;
  3607.     long int *retlen;
  3608.     };
  3609.  
  3610. /* Should maybe this be JPI$_PROC_INDEX instead? */
  3611.  
  3612.     struct itmlstdef itmlst[] = {
  3613.     4, JPI$_PID, NULL, 0,
  3614.     0, 0, 0, 0
  3615.     };
  3616.     itmlst[0].bufaddr = (char *)&pid;
  3617.     sts = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0);
  3618.     if (!(sts & 1)) vms_lasterr = sts;
  3619.     debug(F101,"zgpid sys$getjpiw status", "", sts);
  3620.     if (sts != SS$_NORMAL)
  3621.       return(0);
  3622.     else
  3623.       return(pid);
  3624. }
  3625.  
  3626. /*  Z X C M D  --  Run a system command so its output can be read as a file. */
  3627.  
  3628. int
  3629. zxcmd(filnum, comand) int filnum; char *comand; {
  3630.     char mbxnam[21], inpchan[6] = "NLA0:";
  3631.     unsigned long sts, pid;
  3632.     int one=1;
  3633.  
  3634.     struct dsc$descriptor_s
  3635.     mbx_desc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
  3636.     cmd_line = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
  3637.     inp_desc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  3638.  
  3639.     struct itmlstdef {
  3640.     short int buflen;
  3641.     short int itmcod;
  3642.     char *bufaddr;
  3643.     long int *retlen;
  3644.     };
  3645.     struct itmlstdef itmlst[] = {
  3646.     4, JPI$_PID, NULL, 0,
  3647.     0, 0, 0, 0
  3648.     };
  3649.     itmlst[0].bufaddr = (char *)&pid;
  3650.  
  3651.     if (nopush) {
  3652.     debug(F100,"zxcmd fails: nopush","",0);
  3653.     return(-1);
  3654.     }
  3655.     debug(F111,"zxcmd filnum", comand, filnum);
  3656.     if (filnum != ZIFILE && filnum != ZRFILE)
  3657.       return(0);
  3658.  
  3659.     sts = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0);
  3660.     if (!(sts & 1)) vms_lasterr = sts;
  3661.     debug(F101,"zxcmd sys$getjpiw status", "", sts);
  3662.     if (sts != SS$_NORMAL)
  3663.       return(0);
  3664.  
  3665.     sprintf(mbxnam,"KERMIT$MBX_%08X", pid);
  3666.     debug(F110,"zxcmd mailbox logical", mbxnam, 0);
  3667.     mbx_desc.dsc$w_length = strlen(mbxnam);
  3668.     mbx_desc.dsc$a_pointer = mbxnam;
  3669.  
  3670.     sts = sys$crembx(0, &mbx_chan, SUB_BUF_SIZE, 0, 0, 0, &mbx_desc, 0);
  3671.     if (!(sts & 1)) vms_lasterr = sts;
  3672.     debug(F101,"zxcmd sys$crembx status", "", sts);
  3673.     if (sts != SS$_NORMAL)
  3674.       return(0);
  3675.  
  3676.     debug(F101,"zxcmd sys$crembx mbx_chan", "", mbx_chan);
  3677.     strcat(mbxnam, ":");
  3678.     mbx_desc.dsc$w_length++;
  3679.  
  3680.     cmd_line.dsc$w_length = strlen(comand);
  3681.     cmd_line.dsc$a_pointer = comand;
  3682.  
  3683.     inp_desc.dsc$w_length = strlen(inpchan);
  3684.     inp_desc.dsc$a_pointer = inpchan;
  3685.  
  3686.     sts = lib$spawn(&cmd_line, &inp_desc, &mbx_desc, &one, 0, &sub_pid,
  3687.             &pexitlong, 0, 0, &mbx_chan);
  3688.     if (!(sts & 1)) vms_lasterr = sts;
  3689.  
  3690.     debug(F101,"zxcmd lib$spawn status", "", sts);
  3691.     if (sts != SS$_NORMAL)
  3692.       return(0);
  3693.  
  3694.     subprocess_input = 1;
  3695.     ispipe[filnum] = 1;            /* Remember this file is a "pipe" */
  3696.  
  3697.     sub_count = 0;
  3698.     fp[filnum] = fopen("NLA0:","r");    /* It wants a fp, give it one */
  3699.     debug(F101,"zxcmd fp[filnum]", "", fp[filnum]);
  3700.     fp[ZSYSFN] = fp[filnum];        /* Set ZSYSFN too, so we remember */
  3701.     return(1);
  3702. }
  3703.  
  3704. /*  Z C L O S F  - close the suprocess output file.  */
  3705.  
  3706. int
  3707. zclosf(filnum) int filnum; {
  3708.     unsigned long sts;
  3709.  
  3710.     if (subprocess_input != 0) {
  3711.     sts = sys$delprc(&sub_pid, 0);
  3712.     if (!(sts & 1)) vms_lasterr = sts;
  3713.     debug(F101,"zclosf sys$delprc status", "", sts);
  3714.     sts = sys$delmbx(mbx_chan);
  3715.     if (!(sts & 1)) vms_lasterr = sts;
  3716.     debug(F101,"zclosf sys$delmbx status", "", sts);
  3717.     sts = sys$dassgn(mbx_chan);
  3718.     if (!(sts & 1)) vms_lasterr = sts;
  3719.     debug(F101,"zclosf sys$dassgn status", "", sts);
  3720.     sub_ptr = sub_buf;        /* flush remaining data */
  3721.     sub_count = 1;
  3722.     *sub_buf = '\0';
  3723.     zincnt = 0;
  3724.     fclose(fp[filnum]);        /* Close the place-holders */
  3725.     fp[filnum] = fp[ZSYSFN] = NULL;
  3726.     }
  3727.     debug(F101,"zxcmd pexitlong","",pexitlong);
  3728.     subprocess_input = 0;        /* Say we're done */
  3729.     pexitstat = (pexitlong & 0x7fff);    /* Set status */
  3730.     return(pexitstat & 1 ? 1 : -1);
  3731. }
  3732.  
  3733. /*  Z X P A N D  --  Expand a wildcard string into an array of strings.  */
  3734.  
  3735. /*
  3736.   Returns the number of files that match s, with data structures set up
  3737.   so that first file (if any) will be returned by the next znext() call.
  3738. */
  3739. static int xdironly = 0;
  3740. static int xfilonly = 0;
  3741. /* static int xmatchdot = 0; */
  3742. static int xrecursive = 0;
  3743. static int xnobackup = 0;
  3744.  
  3745. static int
  3746. zxpand(s) char * s; {
  3747.     int x;
  3748.  
  3749.     if (!s) s = "";
  3750.     x = strlen(s);
  3751.     debug(F111,"zxpand",s,x);
  3752.     if (x <= 0)                /* Nothing asked for, */
  3753.       return(0);            /* nothing returned. */
  3754.     fcount = (mtchs == NULL &&    /* Kermit */
  3755.           (mtchs = (char **)malloc(MAXWLD * sizeof(*mtchs))) == NULL)
  3756.       ? 0
  3757.     : fgen(s,mtchs,MAXWLD);        /* Look up the file. */
  3758.     if (fcount > 0) {
  3759.     mtchptr = mtchs;        /* Save pointer for next. */
  3760.     debug(F111,"zxpand fcount",mtchs[0],fcount);
  3761.     }
  3762.     nxpand = fcount;
  3763.     return(fcount);
  3764. }
  3765.  
  3766. int
  3767. nzxpand(s,flags) char * s; int flags; {
  3768.     int x;
  3769.  
  3770.     debug(F111,"nzxpand",s,flags);
  3771.     x = flags & (ZX_DIRONLY|ZX_FILONLY);
  3772.     xdironly = (x == ZX_DIRONLY);
  3773.     xfilonly = (x == ZX_FILONLY);
  3774.     if (xdironly && xfilonly) {
  3775.     xdironly = 0;
  3776.     xfilonly = 0;
  3777.     }
  3778.     xrecursive = (flags & ZX_RECURSE);
  3779.     xnobackup  = (flags & ZX_NOBACKUP);
  3780.  
  3781.     debug(F101,"nzxpand xdironly","",xdironly);
  3782.     debug(F101,"nzxpand xfilonly","",xfilonly);
  3783.     debug(F101,"nzxpand xrecursive","",xrecursive);
  3784.     debug(F101,"nzxpand xnobackup","",xnobackup);
  3785.  
  3786.     x = zxpand(s);
  3787.     xdironly = 0;
  3788.     xfilonly = 0;
  3789.     xrecursive = 0;
  3790.     xnobackup = 0;
  3791.     return(x);
  3792. }
  3793.  
  3794.  
  3795. /*  Z X R E W I N D  --  Rewinds the zxpand() list */
  3796.  
  3797. int
  3798. zxrewind() {
  3799.     if (!mtchsinit || !mtchs) return(-1);
  3800.     fcount = nxpand;
  3801.     mtchptr = mtchs;
  3802.     return(nxpand);
  3803. }
  3804.  
  3805.  
  3806. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  3807.  
  3808. /*
  3809.  Returns >0 if there's another file, with its name copied into the arg string,
  3810.  or 0 if no more files in list.
  3811. */
  3812. int
  3813. znext(fn) char *fn; {
  3814.  
  3815.     if (fcount-- > 0)
  3816.       strcpy(fn,*mtchptr++);
  3817.     else
  3818.       *fn = '\0';
  3819.     debug(F111,"znext",fn,fcount+1);
  3820.     return(fcount+1);
  3821. }
  3822.  
  3823. /*  Z N E W N  --  Make a new name for the given file.  */
  3824.  
  3825. VOID
  3826. znewn(fn,s) char *fn, **s; {
  3827.     static char buf[NAM$C_MAXRSS+8];
  3828.     int x, flag = 0;
  3829.     long v = -1L;
  3830.     x = strlen(fn) - 1;
  3831.     ckstrncpy(buf, fn, NAM$C_MAXRSS);
  3832.     while (x >= 0) {
  3833.     if (buf[x] == ';') {
  3834.         v = atol(buf+x+1);
  3835.         sprintf(buf+x+1,"%ld",v+1);
  3836.         flag = 1;
  3837.         break;
  3838.     }
  3839.     x--;
  3840.     }
  3841.     if (!flag) strcat(buf,";0");
  3842.     *s = buf;
  3843. }
  3844.  
  3845. #ifdef COMMENT
  3846. /*
  3847.   This returns the version number the given file, if it exists.  But it causes
  3848.   more trouble than it's worth.  For example, if zfnqfp() calls this to get
  3849.   the version number and includes it in its result, it breaks the LOG command.
  3850. */
  3851. int
  3852. getvnum(fn) char * fn; {
  3853.     int rc = -1;
  3854.     char * p;
  3855.     struct dsc$descriptor_s
  3856.     file_spec = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  3857.     result = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  3858.     deflt = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  3859.     unsigned long context = 0, status;
  3860.  
  3861.     file_spec.dsc$w_length  = strlen(fn);
  3862.     file_spec.dsc$a_pointer = fn;
  3863.  
  3864.     status = lib$find_file(&file_spec, &result, &context, &deflt);
  3865.     if (status == RMS$_NORMAL) {
  3866.     if (!(status & 1)) {
  3867.         vms_lasterr = status;
  3868.         debug(F101,"getvnum error", fn, status);
  3869.     } else if (result.dsc$a_pointer) {
  3870.         debug(F111,"getvnum", result.dsc$a_pointer, result.dsc$w_length);
  3871.         p = strchr(result.dsc$a_pointer,';');
  3872.         if (p) {
  3873.         if (*(p+1)) {
  3874.             if (rdigits(p+1)) {
  3875.             rc = atoi(p+1);
  3876.             }
  3877.         }
  3878.         }
  3879.     }
  3880.     }
  3881. #ifdef DVI$_ALT_HOST_TYPE
  3882.     lib$find_file_end(&context);    /* Only on VMS V4 and later */
  3883. #endif /* DVI$_ALT_HOST_TYPE */
  3884.     return(rc);
  3885. }
  3886. #endif /* COMMENT */
  3887.  
  3888. struct zfnfp *
  3889. zfnqfp(fn, buflen, buf)  char * fn; int buflen; char * buf; {
  3890.     int x = 0, y = 0;
  3891.     char * cp;
  3892.     static struct zfnfp fnfp;
  3893.  
  3894.     struct FAB fab;
  3895.     struct NAM nam;
  3896.     char expanded_name[NAM$C_MAXRSS];
  3897.     char tmpnam[NAM$C_MAXRSS+16];
  3898.     int long rms_status;
  3899.     int cur_len = 0;
  3900.     int cp_len = buflen;
  3901.  
  3902.     if (!fn)
  3903.       return(NULL);
  3904.  
  3905.     cp = fn;
  3906.     debug(F111,"zfnqfp 1",cp,buflen);
  3907.  
  3908. #ifdef COMMENT
  3909.     /* This works but it messes up LOG commands, etc. */
  3910.     if (!strchr(cp,';')) {
  3911.     int v;
  3912.     v = getvnum(cp);
  3913.     if (v > 0) {
  3914.         sprintf(tmpnam,"%s;%d",cp,v);
  3915.         cp = tmpnam;
  3916.         debug(F111,"zfnqfp getvnum",cp,v);
  3917.     }
  3918.     }
  3919. #endif /* COMMENT */
  3920.  
  3921.     /* initialize the data structure */
  3922.  
  3923.     fnfp.len = buflen;
  3924.     fnfp.fpath = buf;
  3925.     fnfp.fname = NULL;
  3926.  
  3927.     fab = cc$rms_fab;
  3928.     fab.fab$l_nam = &nam;
  3929.     fab.fab$l_fna = cp;
  3930.     fab.fab$b_fns = strlen(cp);
  3931.     fab.fab$l_dna = 0;
  3932.  
  3933.     nam = cc$rms_nam;
  3934.     nam.nam$l_esa = (char *)&expanded_name;
  3935.     nam.nam$b_ess = sizeof(expanded_name);
  3936.  
  3937.     if (!CHECK_ERR("%%CKERMIT-W-ZFNQFP",sys$parse(&fab)))
  3938.       return(NULL);
  3939.  
  3940.     /* if not a file oriented device, convert name to upper case */
  3941.  
  3942.     if (!(fab.fab$l_dev & DEV$M_NET) && !(fab.fab$l_dev & DEV$M_FOD)) {
  3943.         cp = fn;
  3944.         while (*cp) {
  3945.             if (islower(*cp))
  3946.               *cp = toupper(*cp);
  3947.             cp++;
  3948.         }
  3949.         return(NULL);
  3950.     }
  3951.     cp = buf;
  3952.     *cp = '\0';            /* Make a zero length string */
  3953.     fnfp.fpath = cp;
  3954.     if (nam.nam$b_node && nam.nam$b_node < cp_len) /* Node */
  3955.       cur_len = nam.nam$b_node;
  3956.     if (nam.nam$b_dev && cur_len+nam.nam$b_dev < cp_len) /* Device */
  3957.       cur_len += nam.nam$b_dev;
  3958.     if (nam.nam$b_dir && cur_len+nam.nam$b_dir < cp_len) /* Directory */
  3959.       cur_len += nam.nam$b_dir;
  3960.     if (nam.nam$b_name && cur_len+nam.nam$b_name < cp_len) { /* Name */
  3961.     fnfp.fname = buf + cur_len;
  3962.     cur_len += nam.nam$b_name;
  3963.     }
  3964.     if (nam.nam$b_type && cur_len+nam.nam$b_type < cp_len) /* Extension */
  3965.       cur_len += nam.nam$b_type;
  3966.     if (nam.nam$b_ver && cur_len+nam.nam$b_ver < cp_len)
  3967.       cur_len += nam.nam$b_ver;
  3968.     strncat(cp, nam.nam$l_node, cur_len);
  3969.     debug(F111,"zfnqfp 2",buf,cur_len);
  3970.     if (cur_len > 1) {
  3971.     if (buf[cur_len-1] == ';' && buf[cur_len-2] == '.') {
  3972.         buf[cur_len-2] = NUL;
  3973.         cur_len -= 2;
  3974.     }
  3975.     }
  3976.     fnfp.len = cur_len;
  3977.     debug(F111,"zfnqfp 3",buf,cur_len);
  3978.  
  3979.     return(&fnfp);
  3980. }
  3981.  
  3982. /*
  3983.  * fgen:
  3984.  *  This is the actual name generator.  It is passed a string,
  3985.  *  possibly containing wildcards, and an array of character pointers.
  3986.  *  It finds all the matching filenames and stores them into the array.
  3987.  *  The returned strings are allocated from a static buffer local to
  3988.  *  this module (so the caller doesn't have to worry about deallocating
  3989.  *  them); this means that successive calls to fgen will wipe out
  3990.  *  the results of previous calls.  This isn't a problem here
  3991.  *  because we process one wildcard string at a time.
  3992.  *
  3993.  * Input: a wildcard string, an array to write names to, the
  3994.  *        length of the array.
  3995.  * Returns: the number of matches.  The array is filled with filenames
  3996.  *          that matched the pattern.  If there wasn't enough room in the
  3997.  *        array, -1 is returned.
  3998.  */
  3999. int
  4000. fgen(pat,resarry,len) char *pat, *resarry[]; int len; {
  4001.     char * p, * f;
  4002.     int i, x, y;
  4003.     struct dsc$descriptor_s
  4004.     file_spec = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  4005.     result = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  4006.     deflt = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  4007.     unsigned long context = 0, status;
  4008.     int count = 0;
  4009.     char *def_str = "*.*";
  4010.     char dirbuf[NAM$C_MAXRSS+1];
  4011.     char tmpbuf[NAM$C_MAXRSS+1];
  4012.     char patbuf[NAM$C_MAXRSS+4];
  4013.  
  4014.     debug(F101,"fgen len","",len);
  4015.  
  4016.     if (!mtchsinit) {            /* Initialize filename array */
  4017.     mtchsinit = 1;
  4018.     for (i = 0; i < MAXWLD; i++)
  4019.       mtchs[i] = NULL;
  4020.     } else {
  4021.     for (i = 0; i < lastcount; i++) { /* Free previous filenames */
  4022.         if (resarry[i]) {
  4023.         free(resarry[i]);
  4024.         resarry[i] = NULL;
  4025.         }
  4026.     }
  4027.     }
  4028.     debug(F111,"fgen pat",pat,xrecursive);
  4029.     debug(F101,"fgen xdironly","",xdironly);
  4030.     debug(F101,"fgen xfilonly","",xfilonly);
  4031.  
  4032.     /* Too bad the LIB$FIND_FILE() flags word doesn't have a "recurse" bit */
  4033.     /* If recursive search requested stuff "..." into the directory name */
  4034.  
  4035.     if (xrecursive && !ckindex("...",pat,0,0,1)) {
  4036.     if (zfnqfp(pat, NAM$C_MAXRSS, patbuf)) {
  4037.         int x;
  4038.         char * p;
  4039.         x = strlen(patbuf);
  4040.         debug(F110,"fgen patbuf 1",patbuf,x);
  4041.         if (x > 0) {
  4042.         p = patbuf + x - 1;
  4043.         *(p+4) = '\0';
  4044.         while (*p) {
  4045.             *(p+3) = *p;
  4046.             if (*p == ']' || *p == '>') {
  4047.             *(p+2) = '.';
  4048.             *(p+1) = '.';
  4049.             *p = '.';
  4050.             break;
  4051.             }
  4052.             p--;
  4053.         }
  4054.         debug(F110,"fgen patbuf 2",patbuf,x);
  4055.         pat = patbuf;
  4056.         }
  4057.     }
  4058.     }
  4059.     file_spec.dsc$w_length  = strlen(pat);
  4060.     file_spec.dsc$a_pointer = pat;
  4061.  
  4062.     x = file_spec.dsc$w_length -1;    /* kludge to keep zero length name */
  4063.     p = def_str;                /* "[foo]"  -> "[foo]*.*" */
  4064.     if (pat[x] != ']' && pat[x] != '>')
  4065.       p++;                    /* "[foo]." -> "[foo].*"  */
  4066.     deflt.dsc$w_length  = strlen(p);
  4067.     deflt.dsc$a_pointer = p;
  4068.  
  4069.     while (count < len
  4070.        && (status = lib$find_file(&file_spec, &result, &context, &deflt))
  4071.         == RMS$_NORMAL) {
  4072.     if (!(status & 1)) vms_lasterr = status;
  4073.     f = tmpbuf;
  4074.     strncpy(f, result.dsc$a_pointer, result.dsc$w_length);
  4075.     tmpbuf[result.dsc$w_length] = '\0';
  4076.     x = isdir(tmpbuf);
  4077.     debug(F111,"fgen isdir",f,x);
  4078.     if (x != 0 && xfilonly)        /* Want files only and not a file */
  4079.       continue;
  4080.     if (x == 0 && xdironly)        /* Want dirs only but not a dir */
  4081.       continue;
  4082.     if (xdironly) {
  4083.         if (x == 2) {
  4084.         y = cvtdir(tmpbuf, (char *)dirbuf, NAM$C_MAXRSS+1);
  4085.         if (y > 0) f = dirbuf;
  4086.         }
  4087.     }
  4088.     resarry[count] = malloc((int) strlen(f) + 1);
  4089.     if (resarry[count]) {
  4090.         strcpy(resarry[count], f);
  4091.         count++;
  4092.         lastcount = count;
  4093.     } else {
  4094.         debug(F100,"fgen malloc failure","",0);
  4095.         return(-1);
  4096.     }
  4097.     }
  4098. #ifdef DVI$_ALT_HOST_TYPE
  4099.     lib$find_file_end(&context);    /* Only on VMS V4 and later */
  4100. #endif /* DVI$_ALT_HOST_TYPE */
  4101.  
  4102.     lib$sfree1_dd(&result);
  4103.  
  4104.     if (status == RMS$_FNF)        /* File Not Found */
  4105.       return((count <= len) ? 0 : -1);
  4106.     if (status == RMS$_NMF)        /* No More Found */
  4107.       return(count);
  4108.     /* Bernd Onasch says that VMS sometimes returns RMS$_NORMAL here, so... */
  4109.     if (status == RMS$_NORMAL)
  4110.       return(count);
  4111.  
  4112.     /* Some other status.  Return 0. */
  4113.     /* Improve this later based on results from following debug stmt. */
  4114.     debug(F101,"fgen unexpected failure status","",status);
  4115.     return(0);
  4116. }
  4117.  
  4118.  
  4119. /*  Z C O P Y  --  Copy a single file. */
  4120.  
  4121. /*
  4122.   Call with source and destination names.
  4123.   If destination is a directory (specification, not directory file name),
  4124.   the source file is copied to that directory with its original name.
  4125.   Returns:
  4126.    0 on success.
  4127.   <0 on failure:
  4128.   -2 = source file is a directory file {or link on Unix}.
  4129.   -3 = source file not found or not accessible {locked}.
  4130.   -4 = permission denied {read, isguest}.
  4131.   -5 = source and destination are same file {not used on VMS}
  4132.   -6 = i/o error.
  4133.   -7 = open output file error {permissions, existing file, directory file}
  4134.   -1 = other error.
  4135. */
  4136.  
  4137. #include <shrdef.h>
  4138. #include <stsdef.h>
  4139. #ifdef __DECC
  4140. #ifndef BUGFILL7
  4141. #ifndef NOCONVROUTINES
  4142. #include <conv$routines.h>
  4143. #endif /* NOCONVROUTINES */
  4144. #endif /* BUGFILL7 */
  4145. #endif /* __DECC */
  4146.  
  4147. int
  4148. zcopy(source,destination) char *source, *destination; {
  4149.     char *src = source, *dst = destination; /* Local pointers to filenames */
  4150.  
  4151.     int  rc, n;
  4152.     unsigned long int sts;
  4153.     unsigned long int options[] = {2,1,1}; /* CONV$ options CREATE SHARE */
  4154.     char *p, buf[NAM$C_MAXRSS+1];    /* File name buffer */
  4155.  
  4156.  
  4157.     struct dsc$descriptor_s  ifd = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  4158.     struct dsc$descriptor_s  ofd = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  4159.  
  4160.     debug(F110,"zcopy src arg",src,0);
  4161.     debug(F110,"zcopy dst arg",dst,0);
  4162.  
  4163. #ifdef CK_LOGIN
  4164.     if (inserver && isguest)
  4165.       return(-4);
  4166. #endif /* CK_LOGIN */
  4167.  
  4168.     switch (zchki(src)) {
  4169.         case -1 : return(-3);    /* not found or not accessible */
  4170.         case -2 : return(-2);    /* a directory file */
  4171.         case -3 : return(-4);    /* no read permission */
  4172.     }
  4173.  
  4174. /* If destination is a directory name, append the source file name */
  4175.  
  4176.     switch (isdir(dst)) {
  4177.       case 0:
  4178.     debug(F100,"zcopy destination is not a directory","",0);
  4179.     break;
  4180.       case 2:
  4181.     debug(F100,"zcopy destination is a directory file","",0);
  4182.     return(-7);
  4183.       case 1: {
  4184.       char *q = NULL;
  4185.       debug(F100,"zcopy destination is a directory spec","",0);
  4186.       zstrip(src,&q);
  4187.       n = strlen(dst);
  4188.       if (strlen(q) + n < sizeof(buf)) {
  4189.           ckstrncpy(buf,dst,NAM$C_MAXRSS); /* dst dir */
  4190.           p = buf + n - 1;
  4191.           if (*p != ']' && *p != '>' && *p != ':' )
  4192.         *(++p) = ':';        /* logical w/o ":" */
  4193.           strcpy(++p,q);        /* nam from src */
  4194.           dst = buf;
  4195.           debug(F110,"zcopy dst result",dst,0);
  4196.           strcpy(destination,dst);
  4197.           break;
  4198.       } else {
  4199.           debug(F100,"zcopy dir+name overflow","",0);
  4200.           return(-7);
  4201.       }
  4202.       }
  4203.     }
  4204.     ifd.dsc$a_pointer = src;
  4205.     ifd.dsc$w_length  = strlen(src);
  4206.     ofd.dsc$a_pointer = dst;
  4207.     ofd.dsc$w_length  = strlen(dst);
  4208.  
  4209.     sts = conv$pass_files(&ifd, &ofd);
  4210.     if (sts != SS$_NORMAL) {
  4211.     debug(F101,"zcopy conv$pass_files error","", sts);
  4212.     return(-1);
  4213.     }
  4214. /*
  4215.   CONV option CREATE limits zcopy dst to a new file; to overwrite
  4216.   an existing file, delete the old version first
  4217. */
  4218.     sts = conv$pass_options(&options);
  4219.     if (sts != SS$_NORMAL) {
  4220.         debug(F101,"zcopy conv$pass_option error","",sts);
  4221.         return(-1);
  4222.     }
  4223.     sts = conv$convert();
  4224.  
  4225. /* CONV$_ errors are shared error codes (facility 178) */
  4226.  
  4227.     if (sts != SS$_NORMAL) {
  4228.         debug(F101,"zcopy conv$convert error","", sts);
  4229.         switch (sts & STS$M_CODE) {
  4230.       case SHR$_OPENOUT:
  4231.         rc = -7;
  4232.         break;
  4233.       case SHR$_OPENIN:
  4234.         rc = -4;
  4235.         break;
  4236.       case SHR$_READERR || SHR$_WRITEERR:
  4237.         rc = -6;
  4238.         break;
  4239.       default : rc = -1;
  4240.     }
  4241.     } else
  4242.       rc = 0;
  4243.     debug(F101,"zcopy status","",rc);
  4244.  
  4245. #ifdef CKSYSLOG
  4246.     if (rc > -1 && ckxsyslog >= SYSLG_FC && ckxlogging) {
  4247.     if (rc)
  4248.       syslog(LOG_INFO,"file[] %s: copy to %s failed (%u)", src, dst, sts);
  4249.     else
  4250.       syslog(LOG_INFO,"file[] %s: copy to %s ok", src, dst);
  4251.     }
  4252. #endif /* CKSYSLOG */
  4253.  
  4254.     return(rc);
  4255. }
  4256.  
  4257.  
  4258. /*  Z R E N A M E  --  Rename a file.  */
  4259.  
  4260. /*  Call with old and new names */
  4261. /*  Returns 0 on success, -1 on failure. */
  4262. int
  4263. zrename(old,new) char *old, *new; {
  4264.     int sts;
  4265.  
  4266.     sts = rename(old,new);
  4267.  
  4268.     return((sts ? -1 : 0));
  4269. }
  4270.  
  4271. /*  Z C F D A T  --  Return a file's modification time.  */
  4272.  
  4273. char *
  4274. zfcdat(name) char *name; {
  4275. /*
  4276.   Returns modification date/time of file whose name is given in the argument
  4277.   string.  Return value is a pointer to a string of the form:
  4278.  
  4279.     yyyymmdd hh:mm:ss
  4280.  
  4281.   for example 19931231 23:59:59, which represents the local time (no timezone
  4282.   or daylight savings time finagling required).  Returns an empty string ("")
  4283.   on failure.  The text pointed to by the string pointer is in a static
  4284.   buffer, and so should be copied to a safe place by the caller before any
  4285.   subsequent calls to this function.
  4286. */
  4287.  
  4288. /*
  4289.   Contributed by William Bader, 9 Nov 93, based on UNIX version: "It would
  4290.   probably be possible to get the date by opening the file and requesting a
  4291.   NAM block like ckvfio.c does, but stat seems to do the trick."
  4292. */
  4293.     struct stat statbuf;
  4294.     struct tm *tm;
  4295.     static char datebuf[20];
  4296.  
  4297.     datebuf[0] = '\0';
  4298.  
  4299.     if (name &&
  4300.         *name &&
  4301.         stat(name,&statbuf) != -1 &&
  4302. #ifdef COMMENT
  4303.     /* Modification date/time */
  4304.         (tm = localtime((const time_t *)&statbuf.st_mtime)))
  4305. #else
  4306.         /* Creation date/time */
  4307.         (tm = localtime((const time_t *)&statbuf.st_ctime)))
  4308. #endif /* COMMENT */
  4309.       sprintf(datebuf, "%04d%02d%02d %02d:%02d:%02d",
  4310.         tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  4311.         tm->tm_hour, tm->tm_min, tm->tm_sec);
  4312.  
  4313.     return(datebuf);
  4314. }
  4315.  
  4316.  
  4317. /*  Z S T I M E  --  Set or compare a file's creation date/time.  */
  4318.  
  4319. /*
  4320.  * Note: There's an additional value for parameter X on VAX/VMS systems. As
  4321.  * it's horribly painful to change a file's creation date after-the-fact we
  4322.  * call zstime with an argument  of 2 to pre-set the date when creating the
  4323.  * file. An argument of 0 (which the main-line code thinks sets the date of
  4324.  * the output file) returns success but does nothing.  Note that an invalid
  4325.  * or missing attribute packet will cause $bintim to return an error, which
  4326.  * causes the routine to exit. Since we pre-set the binary time to zero, we
  4327.  * will create the file "now", or say the incoming file is newer, whichever
  4328.  * is appropriate.
  4329.  */
  4330. static char mth[13][4] = {
  4331.     "JAN","FEB","MAR","APR",
  4332.     "MAY","JUN","JUL","AUG",
  4333.     "SEP","OCT","NOV","DEC",
  4334.     ""
  4335. };
  4336.  
  4337. int
  4338. zstime(f,yy,x) char *f; struct zattr *yy; int x; {
  4339.     int rms_sts;
  4340.     unsigned short attr_pro = 0;
  4341.     static char cdate[24];          /* File date yyyy-mm-dd hh:mm:ss.00 */
  4342.     static char mnum[2];
  4343.     struct dsc$descriptor_s
  4344.     bintim_desc = {sizeof(cdate) - 1, DSC$K_DTYPE_T, DSC$K_CLASS_S,
  4345.              (char *)&cdate};
  4346.     unsigned long file_date[2], attr_date[2];
  4347.     char *dptr;
  4348.     int setperms = 0;
  4349.     int setdate = 0;
  4350. #ifdef DEBUG
  4351.     char xbuf[24];            /* Hex buffer for debugging only */
  4352. #endif /* DEBUG */
  4353.  
  4354.     debug(F111,"zstime entry",f,x);
  4355.     if (yy->date.len == 0) {        /* No date in struct */
  4356.     if (yy->lprotect.len != 0) {    /* So go do permissions */
  4357.         goto zsperms;
  4358.     } else {
  4359.         debug(F100,"zstime: nothing to do","",0);
  4360.         return(0);
  4361.     }
  4362.     }
  4363.  
  4364. /* First, make a system quadword date from our attribute struct parameter */
  4365.  
  4366.     dptr = yy->date.val;
  4367.     strncpy(cdate,"dd-mmm-yyyy 00:00:00.00",24);
  4368.     attr_date[0]=0;            /* Clear time in case of error */
  4369.     attr_date[1]=0;
  4370.     strncpy(cdate+7, dptr, 4);        /* yyyy */
  4371.     dptr += 4;
  4372.     strncpy(mnum, dptr, 2);
  4373.     strncpy(cdate+3, mth[atoi(mnum)-1], 3); /* mm */
  4374.     dptr += 2;
  4375.     strncpy(cdate, dptr, 2);        /* dd */
  4376.     dptr += 3;
  4377.     strncpy(cdate+12, dptr, 8);        /* hhmmss */
  4378.     cdate[23] = '\0';            /* terminate */
  4379.     rms_sts = sys$bintim(&bintim_desc, &attr_date); /* Convert to internal */
  4380.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4381.     if (rms_sts != SS$_NORMAL) {            /* time format */
  4382.     debug(F111,"zstime - $bintim returns",cdate,rms_sts);
  4383.     return(-1);
  4384.     }
  4385.     debug(F110,"zstime built",cdate,0);
  4386.     sprintf(cdate, "%08X%08X", attr_date[1], attr_date[0]);
  4387.     debug(F110,"zstime $bintim attr_date", cdate, 0);
  4388.     setdate = 1;
  4389.  
  4390.   zsperms:                /* File protection */
  4391.     if (x != 1) {            /* We're not just comparing dates... */
  4392.     int i, xx, flag = 0;
  4393.     char * s;
  4394. #ifdef DEBUG
  4395.     if (deblog) {
  4396.         debug(F111,"zstime lperms",yy->lprotect.val,yy->lprotect.len);
  4397.         debug(F111,"zstime gperms",yy->gprotect.val,yy->gprotect.len);
  4398.         debug(F110,"zstime system id",yy->systemid.val,0);
  4399.         sprintf(xbuf,"%X",xabpro_ofile.xab$w_pro);
  4400.         debug(F110,"zstime xabpro_ofile.xab$w_pro 1",xbuf,0);
  4401.     }
  4402. #endif /* DEBUG */
  4403.     if ((yy->lprotect.len > 0 &&    /* Have sys-dependent permissions */
  4404.          yy->systemid.len > 0 &&    /* from A-packet... */
  4405.          !strcmp(yy->systemid.val,"D7"))) { /* AND you are VMS like me */
  4406.         attr_pro = 0;
  4407.         flag = 1;
  4408.         s = yy->lprotect.val;    /* VMS protections */
  4409.         xx = yy->lprotect.len;
  4410.         if (xx < 0)            /* Length < 0 means inheritance */
  4411.           xx = 0 - xx;        /* (ignored in VMS) */
  4412.         if (xx > 4) {        /* Bad format - must be 4 bytes */
  4413.         flag = 0;
  4414.         } else {
  4415.         for (i = 0; i < xx; i++) {    /* Decode hex string */
  4416.             if (*s <= '9' && *s >= '0') {
  4417.             attr_pro = 16 * attr_pro + (int)(*s) - '0';
  4418.             } else if ((*s <= 'F' && *s >= 'A')) {
  4419.             attr_pro = 16 * attr_pro + 10 + (int)(*s) - 'A';
  4420.             } else if ((*s <= 'f' && *s >= 'a')) {
  4421.             attr_pro = 16 * attr_pro + 10 + (int)(*s) - 'a';
  4422.             } else {
  4423.             flag = 0;
  4424.             break;
  4425.             }
  4426.             s++;
  4427.         }
  4428.         }
  4429. #ifdef DEBUG
  4430.         if (deblog) {
  4431.         debug(F101,"zstime VMS local protection flag","",flag);
  4432.         sprintf(xbuf,"%X",attr_pro);
  4433.         debug(F111,"zstime VMS local protection",xbuf,attr_pro);
  4434.         }
  4435. #endif /* DEBUG */
  4436.     } else if (!flag && yy->gprotect.len > 0) { /* Systems differ */
  4437.         int g;                        /* Use the generic one */
  4438.         unsigned long dfpro = 0L, mask = 0L;
  4439.         unsigned short tmp;
  4440.         tmp = xabpro_ofile.xab$w_pro;
  4441.             rms_sts = sys$setdfprot(0,&dfpro); /* Get default protection */
  4442.         if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4443. #ifdef DEBUG
  4444.         if (deblog) {
  4445.         sprintf(xbuf,"%X",dfpro);
  4446.         debug(F111,"zstime sys$setdfprot",xbuf,rms_sts);
  4447.         }
  4448. #endif /* DEBUG */
  4449.         if (rms_sts & 1)        /* Succeeded so use it */
  4450.           tmp = dfpro;
  4451.         tmp &= 0xff0f;        /* But remove Owner field */
  4452.  
  4453. /* Convert generic protection to number and translate to internal VMS form */
  4454.  
  4455.         g = xunchar(*(yy->gprotect.val));
  4456.         attr_pro = 0x0F;           /* 1111 Initial value = No access */
  4457.         if (g &  1)              /* 1=Read */
  4458.           attr_pro &= ~XAB$M_NOREAD;  /* 0001 */
  4459.         if (g &  2)              /* 2=Write */
  4460.           attr_pro &= ~XAB$M_NOWRITE; /* 0010 */
  4461.         if (g &  4)              /* 4=Execute */
  4462.           attr_pro &= ~XAB$M_NOEXE;   /* 0100 */
  4463.         if (g &  8)              /* 8=Append */
  4464.           attr_pro &= ~XAB$M_NOWRITE; /* 0010 */
  4465.         if (g & 16)              /* 16=Delete */
  4466.           attr_pro &= ~XAB$M_NODEL;   /* 1000 */
  4467.         mask = attr_pro    |      /* Don't give S,G,W any */
  4468.           (attr_pro <<  4) |      /* more access than Owner... */
  4469.           (attr_pro <<  8) |
  4470.           (attr_pro << 12);
  4471.         attr_pro = attr_pro << 4;    /* Shift to owner field */
  4472.         tmp |= attr_pro;        /* OR in Owner protection */
  4473.         attr_pro = tmp | mask;    /* Mask off any higher permissions */
  4474.         flag = 1;            /* Flag we have the protection value */
  4475. #ifdef DEBUG
  4476.         if (deblog) {
  4477.         debug(F101,"zstime g attr_pro 4","",attr_pro);
  4478.         sprintf(xbuf,"%X",attr_pro);
  4479.         debug(F111,"zstime generic protections",xbuf,attr_pro);
  4480.         }
  4481. #endif /* DEBUG */
  4482.     }
  4483.     if (flag)
  4484.       setperms = 1;
  4485.     }
  4486.     debug(F101,"zstime setperms","",setperms);
  4487.     debug(F101,"zstime setdate","",setdate);
  4488.  
  4489.     switch (x) {
  4490.       case 1:                /* Function code 1, compare dates */
  4491.     if (!setdate)            /* Do we have a date? */
  4492.       return(-1);            /* Nothing to do */
  4493.     fab_ifile = cc$rms_fab;
  4494.     fab_ifile.fab$b_fac = FAB$M_BIO | FAB$M_GET;
  4495.     fab_ifile.fab$l_fna = f;
  4496.     fab_ifile.fab$b_fns = strlen(f);
  4497.     fab_ifile.fab$l_xab = (char *)&xabdat_ifile;
  4498.     rab_ifile = cc$rms_rab;
  4499.     rab_ifile.rab$l_fab = &fab_ifile;
  4500.     xabdat_ifile = cc$rms_xabdat;
  4501.     rms_sts = sys$open(&fab_ifile);
  4502.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4503.     if (rms_sts != RMS$_NORMAL) {
  4504.         debug(F101,"zstime $open failed, status","",rms_sts);
  4505.         return(-1);
  4506.     }
  4507.     memcpy(file_date, &xabdat_ifile.xab$q_cdt, 8);
  4508.     sprintf(cdate, "%08x%08x", file_date[1], file_date[0]);
  4509.     debug(F110,"zstime $bintim file_date", cdate, 0);
  4510.     rms_sts = sys$close(&fab_ifile);
  4511.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4512.     if (rms_sts != RMS$_NORMAL) {
  4513.         debug(F101,"zstime $close failed, status","",rms_sts);
  4514.         return(-1);
  4515.     }
  4516.     if (attr_date[1] < file_date[1]) {
  4517.         debug(F100,"zstime incoming file is older","",0);
  4518.         return(1);
  4519.     }
  4520.     if (attr_date[1] == file_date[1]) {
  4521.         if (attr_date[0] <= file_date[0]) {
  4522.         debug(F100,"zstime incoming file is older, not by much","",0);
  4523.         return(1);
  4524.         }
  4525.         debug(F100,"zstime incoming file is newer","",0);
  4526.         return(0);
  4527.     }
  4528.     break;
  4529.  
  4530.       case 0:                /* Function code 0, set attributes */
  4531.     return(0);            /* Say we did (see comments above) */
  4532.  
  4533.       case 2:                /* Function code 2, really set them */
  4534.     if (setdate) {            /* Do we have a date? */
  4535.         debug(F100,"zstime setting date","",0);
  4536.         memcpy(&xabdat_ofile.xab$q_cdt, attr_date, 8); /* File date */
  4537.     }
  4538.     if (setperms) {            /* File protection... */
  4539.         debug(F100,"zstime setting protection","",0);
  4540.         xabpro_ofile.xab$w_pro = attr_pro;
  4541.     }
  4542.     return(0);
  4543.     }
  4544.     return(-1);
  4545. }
  4546.  
  4547.  
  4548. /*  Z K E R M I N I  --  Find initialization file.  */
  4549. /*
  4550.   Places name of init file in buffer pointed to by s.
  4551.   If no init file found, the device name of the null device is used.
  4552.   returns 0 always.
  4553. */
  4554. int
  4555. zkermini(s, s_len, def) char *s; int s_len; char *def; {
  4556.     FILE fd;
  4557.     struct dsc$descriptor_s
  4558.         dsc_in = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  4559.         dsc_out = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  4560.         dsc_def = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  4561.     int max_len;
  4562.     long unsigned int rms_s;
  4563.     unsigned long find_file_context = 0;
  4564.  
  4565.     struct TRNLIST {
  4566.     char *name;            /* ASCII file or logical name */
  4567.     unsigned char flag;        /* Zero to use default filename */
  4568.     } *p;
  4569.  
  4570.     static struct TRNLIST slist[] = {
  4571.     {"", 0},            /* Dummy first entry points to file */
  4572.     {"ckermit_ini:", 0},        /* CKERMIT_INI: points to directory */
  4573.     {"ckermit_init", 1},        /* CKERMIT_INIT points to file      */
  4574.     {"sys$login:",   0},        /* CKERMIT.INI in login directory   */
  4575.     {"", 0}
  4576.     };
  4577.     p = slist;                /* Point to list */
  4578.     if (rcflag) {            /* Name given on command line? */
  4579.     slist[0].name = def;        /* Yes, stuff its name into slist */
  4580.     slist[1].name = "";
  4581.     } else {                /* No, */
  4582.     *p++;                /* skip past dummy entry. */
  4583.     }
  4584.     while(*(p->name)) {            /* Search the list top to bottom */
  4585.  
  4586.     dsc_in.dsc$w_length = strlen(p->name); /* Length of work area */
  4587.     dsc_in.dsc$a_pointer = p->name; /* Address of string */
  4588.  
  4589.     if (!(p->flag)) {
  4590.         dsc_def.dsc$w_length = strlen(def); /* Length of work area */
  4591.         dsc_def.dsc$a_pointer = def; /* Address of string */
  4592.     } else {
  4593.         dsc_def.dsc$w_length = 0;    /* Length of work area */
  4594.         dsc_def.dsc$a_pointer = 0;    /* Address of string */
  4595.     }
  4596.     rms_s = lib$find_file(
  4597.                 &dsc_in,    /* File spec */
  4598.                 &dsc_out,    /* Result file spec */
  4599.                 &find_file_context, /* Context */
  4600.                 &dsc_def,    /* Default file spec */
  4601.                 0,        /* Related spec */
  4602.                 0,        /* STV error */
  4603.                 0);        /* Flags */
  4604.  
  4605.     if (!(rms_s & 1)) vms_lasterr = rms_s;
  4606.     if (rms_s == RMS$_NORMAL) {
  4607.         max_len = ((unsigned short int) dsc_out.dsc$w_length < s_len ?
  4608.                (unsigned short int) dsc_out.dsc$w_length : 0);
  4609.         if (!max_len) {
  4610.         fprintf(stderr,
  4611.         "%%ZKERMINI out string not long enough, ignoring .ini file\n");
  4612.         } else {
  4613.         strncpy(s,dsc_out.dsc$a_pointer,max_len);
  4614.         s[max_len] = '\0';
  4615.         }
  4616.         lib$find_file_end(&find_file_context);
  4617.         lib$sfree1_dd(&dsc_out);    /* Return dyno memory */
  4618.         return(1);
  4619.     }
  4620.     p++;
  4621.     lib$find_file_end(&find_file_context);
  4622.     }
  4623. /*
  4624.  * No initialization file found.  We can't return the null string because the
  4625.  * runtime library will successfully open it if the file ".;" exists in the
  4626.  * user's directory.  Instead we return the name of the null device.
  4627.  */
  4628.     strcpy(s, "NLA0:");                /* Return null init file */
  4629.     lib$sfree1_dd(&dsc_out);
  4630.     return(0);
  4631. }
  4632.  
  4633. #ifdef COMMENT
  4634. static int
  4635. parse_fname(cp, cp_len, defnam, flag, fncnv, fnspath)
  4636. char *cp;        /* Pointer to file spec to parse */
  4637. int cp_len;        /* Length of cp field */
  4638. char *defnam;        /* Default file spec */
  4639. int flag;        /* Flag word PARSE_xxx */
  4640. int fncnv;        /* Filename conversion */
  4641. int fnspath;        /* Pathname handling */
  4642. {
  4643.     struct FAB fab;
  4644.     struct NAM nam;
  4645.     char expanded_name[NAM$C_MAXRSS];
  4646.     char dirbuf[NAM$C_MAXRSS], *p, *q, *q2, *r, *s, *s2;
  4647.     int long rms_status;
  4648.     int cur_len = 0;
  4649.  
  4650.     debug(F110,"zltor entry",defnam,0);
  4651.  
  4652.     fab = cc$rms_fab;
  4653.     fab.fab$l_nam = &nam;
  4654.     fab.fab$l_fna = cp;
  4655.     fab.fab$b_fns = strlen(cp);
  4656.     if (defnam) {
  4657.     fab.fab$b_dns = strlen(defnam);
  4658.     fab.fab$l_dna = defnam;
  4659.     } else
  4660.       fab.fab$l_dna = 0;
  4661.  
  4662.     nam = cc$rms_nam;
  4663.     nam.nam$l_esa = (char *)&expanded_name;
  4664.     nam.nam$b_ess = sizeof(expanded_name);
  4665.  
  4666.     if (!CHECK_ERR("%%CKERMIT-W-PARSE, ",
  4667.         sys$parse(&fab)))
  4668.     return(-1);
  4669.  
  4670.     *cp = '\0';                /* Start with an empty result */
  4671.  
  4672.     if ((PARSE_NODE & flag) && nam.nam$b_node && /* DECnet node:: */
  4673.         cur_len+nam.nam$b_node < cp_len) {
  4674.     cur_len += nam.nam$b_node;
  4675.     strncat(cp, nam.nam$l_node, (int)nam.nam$b_node);
  4676.     }
  4677.     if ((PARSE_DEVICE & flag) && nam.nam$b_dev && /* Device: */
  4678.         cur_len+nam.nam$b_dev < cp_len) {
  4679.     cur_len += nam.nam$b_dev;
  4680.     strncat(cp, nam.nam$l_dev, (int)nam.nam$b_dev);
  4681.     }
  4682.  
  4683.     /* Directory Name [] */
  4684.  
  4685.     if ((PARSE_DIRECTORY & flag) && nam.nam$b_dir &&
  4686.         cur_len+nam.nam$b_dir < cp_len) {
  4687.     int i; char * tmp;
  4688.         q = nam.nam$l_dir;        /* The directory name from RMS */
  4689.     i = nam.nam$b_dir;        /* Length; string not nul-terminated */
  4690.     debug(F111,"zltor nam$_dir",q,i);
  4691.     if (!q) q = "[]";
  4692.     if (!*q) q = "[]";
  4693.     if (i < 0) i = 0;
  4694.     tmp = NULL;
  4695.     if (i > 0) {            /* Copy directory part */
  4696.         if (tmp = malloc(i+1)) {
  4697.         p = tmp;
  4698.         for ( ; i > 0 ; i--)
  4699.           *p++ = *q++;
  4700.         *p = NUL;
  4701.         }
  4702.     }
  4703.     q = tmp;
  4704.     debug(F111,"zltor directory part",q,i);
  4705.  
  4706.     s = zgtdir();            /* Get current directory */
  4707.     debug(F110,"zltor zgtdir",s,0);
  4708.     if (!s) s = "[]";
  4709.     if (!*s) s = "[]";
  4710.     s2 = "";
  4711.     while (*s && *s != '[')
  4712.       s++;
  4713.     if (*s) {
  4714.         s2 = s+1;
  4715.         while (*s2 && *s2 != ']') s2++; /* Closing bracket */
  4716.     }
  4717.     if (!*s)
  4718.       s = "[]";
  4719.     else
  4720.       if (*s2) if (!*(s2+1)) *(s2+1) = NUL;
  4721.     debug(F110,"zltor current dir",s,0);
  4722.  
  4723. /* First change the VMS pathname to relative format if fnspath == PATH_REL */
  4724.  
  4725.     p = dirbuf;            /* Result */
  4726.     *p++ = *q++;            /* Copy left bracket and... */
  4727.  
  4728.     s++;                /* Point past it */
  4729.     q2 = q;                /* Remember this place */
  4730.     if (fnspath == PATH_REL) {    /* Compare this and current dir */
  4731.         while (*s == *q && *s && *q && *s != ']') {
  4732.         s++;
  4733.         q++;
  4734.         }
  4735.     }
  4736.     if (*s != ']' && *q != ']' && *q != '.') /* No match */
  4737.       q = q2;            /* So rewind source pointer */
  4738.  
  4739.     while (*q) *p++ = *q++;        /* Now copy the rest */
  4740.     *p = NUL;
  4741.     debug(F110,"zltor result 1",dirbuf,0);
  4742. /*
  4743.    VMS directory name is now in dirbuf in either absolute or relative format.
  4744.    Now change it to standard (UNIX) format if desired.
  4745. */
  4746.     p = dirbuf;            /* Working pointer */
  4747.     r = dirbuf;            /* Result pointer */
  4748.     if (fncnv) {            /* Converting directory format */
  4749.         int flag = 0;
  4750.         if (p[1] == '.') {        /* Directory name is relative */
  4751.         r += 2;            /* Point past the leading dot */
  4752.         p += 2;
  4753.         }
  4754.         while (*p) {        /* Now convert the rest */
  4755.         if (*p == '.' || *p == '[' || *p == ']') {
  4756.             if (!flag) *p = '/';
  4757.             if (*p == ']')
  4758.               flag = 1;
  4759.         }
  4760.         p++;
  4761.         }
  4762.     }
  4763.     debug(F110,"zltor result 2",r,0);
  4764.     if (tmp) free(tmp);
  4765.     strncat(cp, r, (int)nam.nam$b_dir);
  4766.     cur_len += strlen(r);
  4767.     }
  4768.     if ((PARSE_NAME & flag) && nam.nam$b_name &&
  4769.         cur_len+nam.nam$b_name < cp_len) {
  4770.     cur_len += nam.nam$b_name;
  4771.     strncat(cp, nam.nam$l_name, (int)nam.nam$b_name);
  4772.     }
  4773.     if ((PARSE_TYPE & flag) && nam.nam$b_type &&
  4774.         cur_len+nam.nam$b_type < cp_len) {
  4775.     cur_len += nam.nam$b_type;
  4776.     strncat(cp, nam.nam$l_type, (int)nam.nam$b_type);
  4777.     }
  4778.     if ((PARSE_VERSION & flag) && nam.nam$b_ver &&
  4779.         cur_len+nam.nam$b_ver < cp_len) {
  4780.     cur_len += nam.nam$b_ver;
  4781.     strncat(cp, nam.nam$l_ver, (int)nam.nam$b_ver);
  4782.     }
  4783.     return(cur_len);
  4784. }
  4785. #endif /* COMMENT */
  4786.  
  4787. /* Z G P E R M  --  Returns the permissions (protection) of the given file. */
  4788.  
  4789. static char zgpbuf[24];
  4790.  
  4791. char *
  4792. zgperm(f) char *f; {
  4793.     int x, x1, x2, x3, x4;
  4794.     struct FAB fab_perm;
  4795.     struct XABFHC xabfhc_perm;
  4796.     struct XABPRO xabpro_perm;
  4797.     struct XABDAT xabdat_perm;
  4798.     long iflen = -1L;
  4799.  
  4800.     fab_perm = cc$rms_fab;
  4801.     fab_perm.fab$b_fac = FAB$M_BIO;
  4802.     fab_perm.fab$l_fna = f;
  4803.     fab_perm.fab$b_fns = strlen(f);
  4804.     fab_perm.fab$l_xab = (char *)&xabdat_perm;
  4805.     xabdat_perm = cc$rms_xabdat;
  4806.     xabdat_perm.xab$l_nxt = (char *)&xabfhc_perm;
  4807.     xabfhc_perm = cc$rms_xabfhc;
  4808.     xabfhc_perm.xab$l_nxt = (char *)&xabpro_perm;
  4809.     xabpro_perm = cc$rms_xabpro;
  4810.     rms_sts = sys$open(&fab_perm);
  4811.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4812.     if (rms_sts == RMS$_PRV)
  4813.       return(NULL);
  4814.     if (rms_sts != RMS$_NORMAL) {
  4815.     debug(F111,"zperm $open failed, status",f,rms_sts);
  4816.     return(NULL);
  4817.     }
  4818.     rms_sts = sys$close(&fab_perm);
  4819.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4820.     if (rms_sts != RMS$_NORMAL) {
  4821.     debug(F101,"zgperm $close failed, status","",rms_sts);
  4822.     return(NULL);
  4823.     }
  4824.     debug(F111,"zgperm xabpro_perm.xab$w_pro",f,xabpro_perm.xab$w_pro);
  4825.     sprintf(zgpbuf,"%04X",xabpro_perm.xab$w_pro); /* Convert to hex string */
  4826.     debug(F110,"zgperm hex",zgpbuf,0);
  4827.     return(zgpbuf);
  4828. }
  4829.  
  4830. static char zgibuf[24];
  4831.  
  4832. char *
  4833. ziperm(f) char * f; {
  4834.     char * p, pbuf[6];
  4835.     int i, x;
  4836.     char c;
  4837.     p = zgperm(f);
  4838.     if (!p)
  4839.       return("(,,,)");
  4840.     if ((int)strlen(p) != 4)
  4841.       return("(,,,)");
  4842.     ckstrncpy(pbuf,p,6);
  4843.     p = zgibuf;
  4844.     *p = '\0';
  4845.     for (i = 3; i >= 0; i--) {
  4846.     c = pbuf[i];
  4847.     if (i < 3)
  4848.       *p++ = ',';
  4849.     else
  4850.       *p++ = '(';
  4851.     if (c >= '0' && c <= '9')
  4852.       x = c - '0';
  4853.     else if (c >= 'A' && c <= 'F')
  4854.       x = c - 'A' + 10;
  4855.     else if (c >= 'a' && c <= 'f')
  4856.       x = c - 'a' + 10;
  4857.     else
  4858.       return(NULL);
  4859.     if (!(x & XAB$M_NOREAD))  *p++ = 'R';
  4860.     if (!(x & XAB$M_NOWRITE)) *p++ = 'W';
  4861.     if (!(x & XAB$M_NOEXE))   *p++ = 'E';
  4862.     if (!(x & XAB$M_NODEL))   *p++ = 'D';
  4863.     }
  4864.     *p++ = ')';
  4865.     *p = '\0';
  4866.     debug(F110,"ziperm",zgibuf,0);
  4867.     return((char *) zgibuf);
  4868. }
  4869.  
  4870. /*  Z S A T T R  --  Fill in a Kermit attribute structure for current file.  */
  4871.  
  4872. /*
  4873.  Fills in a Kermit file attribute structure for the file which is to be sent.
  4874.  Returns 0 on success with the structure filled in, or -1 on failure.
  4875.  If any string member is null, then it should be ignored.
  4876.  If any numeric member is -1, then it should be ignored.
  4877. */
  4878. int
  4879. zsattr(xx) struct zattr *xx; {
  4880.     CK_OFF_T k;
  4881.     int x;
  4882.     static char mth[13][4] = {
  4883.     "JAN","FEB","MAR","APR",
  4884.     "MAY","JUN","JUL","AUG",
  4885.     "SEP","OCT","NOV","DEC",
  4886.     ""
  4887.     };
  4888.     static char recfm[15];        /* Record format */
  4889.     static char cdate[20];          /* Creation date [yy]yymmdd[hh:mm[:ss]] */
  4890.     static char creater_id[31];        /* Creator ID string */
  4891.     static char genprot;            /* Generic protection */
  4892.     static char gpbuf[2];        /* String representation thereof */
  4893.     static unsigned short lclprot;  /* Local protection */
  4894.     static char lpbuf[32];        /* String representation thereof */
  4895.     static long sysparam_size=0;    /* Length of system paramater buffer */
  4896.     static char *sysparam_adr=0;    /* Address of system paramater buffer */
  4897.     char type;                /* File type */
  4898.     short int asctim_retlen;
  4899.     char asctim_buf[24];        /* Work buffer for ASCTIM() */
  4900.     struct dsc$descriptor_s
  4901.       asctim_dsc = {sizeof(asctim_buf),DSC$K_DTYPE_T,DSC$K_CLASS_S,NULL};
  4902. /*  static long int i;  */
  4903.     static unsigned short id_len;
  4904.     static struct dsc$descriptor_s id_str =
  4905.       {31,DSC$K_DTYPE_T,DSC$K_CLASS_S,creater_id};
  4906.  
  4907. /* Zero out strings */
  4908.  
  4909.     type = 0;
  4910.     recfm[0] = '\0';
  4911.     cdate[0] = '\0';
  4912.     creater_id[0] = '\0';
  4913.     id_len = 0;
  4914.     genprot = 0;            /* Blank protections by default */
  4915.     gpbuf[0] = '\0';
  4916.     lclprot = 0;
  4917.     lpbuf[0] = '\0';
  4918.     asctim_dsc.dsc$a_pointer = (char *)&asctim_buf;
  4919.  
  4920. /* See if we are sending "attributes" from a REMOTE command response */
  4921.  
  4922.     if (*nambuf == '\0') {
  4923.     xx->lengthk = 1;        /* Number of 1K blocks rounded up */
  4924.     xx->type.len = 0;        /* File type can't be filled in here */
  4925.     xx->type.val = "";
  4926.     xx->date.len = strlen(cdate);    /* File creation date */
  4927.     xx->date.val = (char *)&cdate;
  4928.     xx->creator.len = strlen(creater_id); /* File creator */
  4929.     xx->creator.val = (char *)&creater_id;
  4930.     xx->account.len = 0;        /* File account */
  4931.     xx->account.val = "";
  4932.     xx->area.len = 0;        /* File area */
  4933.     xx->area.val = "";
  4934.     xx->password.len = 0;        /* Area password */
  4935.     xx->password.val = "";
  4936.     xx->blksize = -1L;        /* File blocksize */
  4937.     xx->xaccess.len = 0;        /* File access */
  4938.     xx->xaccess.val = "";
  4939.     xx->encoding.len = 1;        /* Transfer syntax */
  4940.     xx->encoding.val = "A";        /* ASCII */
  4941.     xx->disp.len = 0;        /* Disposition upon arrival */
  4942.     xx->disp.val = "";
  4943.     xx->lprotect.len = strlen(lpbuf); /* Local protection */
  4944.     xx->lprotect.val = (char *)lpbuf;
  4945.     xx->gprotect.len = strlen(gpbuf); /* Generic protection */
  4946.     xx->gprotect.val = (char *)gpbuf;
  4947.     xx->systemid.len = 2;        /* System ID for DEC VMS */
  4948.     xx->systemid.val = "D7";
  4949.     xx->recfm.len = strlen(recfm);    /* Record format */
  4950.     xx->recfm.val = (char *)&recfm;
  4951.     xx->sysparam.len = sysparam_size; /* System-dependent parameters */
  4952.     xx->sysparam.val = sysparam_adr;
  4953.     xx->length = 1;            /* Length */
  4954.     return(0);            /* mumble sweet nothings at it */
  4955.     }
  4956.  
  4957. /* Load the generic protection */
  4958.  
  4959. #ifdef COMMENT
  4960.     x = xabpro_ifile.xab$w_pro >> XAB$V_WLD;    /* World protection */
  4961. #else
  4962.     x = (xabpro_ifile.xab$w_pro >> 4) & 0xFF;    /* Owner protection */
  4963. #endif /* COMMENT */
  4964.     if (!(x & XAB$M_NOREAD))  genprot |= 1;    /* Read */
  4965.     if (!(x & XAB$M_NOWRITE)) genprot |= 2+8;    /* Write (and append) */
  4966.     if (!(x & XAB$M_NOEXE))   genprot |= 4;    /* Execute */
  4967.     if (!(x & XAB$M_NODEL))   genprot |= 16;    /* Delete */
  4968.     gpbuf[0] = tochar(genprot);                /* Generic prot as string */
  4969.     gpbuf[1] = '\0';
  4970.     lclprot = xabpro_ifile.xab$w_pro;        /* Local-format protection */
  4971.     sprintf(lpbuf,"%04X",lclprot);        /* Convert to hex string */
  4972.  
  4973. /* Convert creation date from an internal value to common ascii string */
  4974.  
  4975.     sys$asctim(&asctim_retlen,&asctim_dsc,&xabdat_ifile.xab$q_cdt,0);
  4976.     asctim_buf[asctim_retlen] = '\0';
  4977.     debug(F110,"zsattr asctim_buf",asctim_buf,0);
  4978.     for (x = 0; strncmp(mth[x], asctim_buf+3,3); x++) /* Find month */
  4979.       ;
  4980.     strncpy(cdate,asctim_buf+7,4);    /* 'yyyy' */
  4981.     sprintf(cdate+4,"%02d",x+1);    /* 'mm' */
  4982.     strncpy(cdate+6,asctim_buf+0,2);    /* 'dd' */
  4983.     strncpy(cdate+8,asctim_buf+11,9);    /* ' hh:mm:ss' */
  4984.     if (cdate[6] == ' ')
  4985.     cdate[6] = '0';
  4986.     debug(F110,"zsattr cdate",cdate,0);
  4987.  
  4988. /* Convert the owner UIC into an alpha name */
  4989.  
  4990.     creater_id[0] = '\0';
  4991.     rms_sts = sys$idtoasc(xabpro_ifile.xab$l_uic,&id_len,&id_str,0,0,0);
  4992.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  4993.     creater_id[id_len] = '\0';            /* terminating null, please */
  4994.     debug(F111,"zsattr $idtoasc owner",creater_id,strlen(creater_id));
  4995.     if (rms_sts == SS$_NOSUCHID ||
  4996. #ifdef SS$_NORIGHTSDB    /* only vms 5 and higher */
  4997.     rms_sts == SS$_NORIGHTSDB ||
  4998. #endif /* SS$_NORIGHTSDB */
  4999.     rms_sts == SS$_IVIDENT) {
  5000.     creater_id[0] = '\0';
  5001.     rms_sts = SS$_NORMAL;            /* if unknown, null it out */
  5002.     }
  5003.     if (!(rms_sts & 1)) {
  5004.     debug(F101,"zsattr $idtoasc failed, status","",rms_sts);
  5005.     return(-1);                /* fatal */
  5006.     }
  5007.  
  5008. /* Fill in the record format blockette */
  5009.  
  5010.     if (fab_ifile.fab$b_rat & (FAB$M_CR | FAB$M_FTN | FAB$M_PRN)) {
  5011.     strcpy(recfm,"AMJ");
  5012.     } else {
  5013.     strcpy(recfm,"F");
  5014.     sprintf(recfm+1,"%05d",xabfhc_ifile.xab$w_lrl);
  5015.     }
  5016.     debug(F111,"zsattr recfm",recfm,strlen(recfm));
  5017.  
  5018. /* Fill in the returned data structure */
  5019.  
  5020.     xx->lengthk = (iflen/1024)+1;    /* Number of 1K blocks rounded up */
  5021.     xx->type.len = 0;            /* File type can't be filled in here */
  5022.     xx->type.val = "";
  5023.     xx->date.len = strlen(cdate);    /* File creation date */
  5024.     xx->date.val = (char *)&cdate;
  5025.     xx->creator.len = strlen(creater_id); /* File creator */
  5026.     xx->creator.val = (char *)&creater_id;
  5027.     xx->account.len = 0;        /* File account */
  5028.     xx->account.val = "";
  5029.     xx->area.len = 0;            /* File area */
  5030.     xx->area.val = "";
  5031.     xx->password.len = 0;        /* Area password */
  5032.     xx->password.val = "";
  5033.     xx->blksize = -1L;            /* File blocksize */
  5034.     xx->xaccess.len = 0;        /* File access */
  5035.     xx->xaccess.val = "";
  5036.     xx->encoding.len = 1;        /* Transfer syntax */
  5037.     xx->encoding.val = "A";        /* ASCII */
  5038.     xx->disp.len = 0;            /* Disposition upon arrival */
  5039.     xx->disp.val = "";
  5040.     xx->lprotect.len = strlen(lpbuf);    /* Local protection */
  5041.     xx->lprotect.val = (char *)lpbuf;
  5042.     xx->gprotect.len = strlen(gpbuf);    /* Generic protection */
  5043.     xx->gprotect.val = (char *)gpbuf;
  5044.     xx->systemid.len = 2;        /* System ID for (Open)VMS */
  5045.     xx->systemid.val = "D7";
  5046.     xx->recfm.len = strlen(recfm);    /* Record format */
  5047.     xx->recfm.val = (char *)&recfm;
  5048.     xx->sysparam.len = sysparam_size;    /* System-dependent parameters */
  5049.     xx->sysparam.val = sysparam_adr;
  5050.     xx->length = iflen;            /* Length */
  5051.     debug(F111,"zsattr lengthk","",xx->lengthk);
  5052.     debug(F111,"zsattr length","",xx->length);
  5053.     return(0);
  5054. }
  5055.  
  5056. /* Z M K D I R  --  Create directory(s) if necessary */
  5057. /*
  5058.    Call with:
  5059.      A pointer to a file specification that might contain directory
  5060.      information.  The filename is expected to be included.
  5061.      If the file specification does not include any directory separators,
  5062.      then it is assumed to be a plain file.
  5063.      If one or more directories are included in the file specification,
  5064.      this routine tries to create them if they don't already exist.
  5065.    Returns:
  5066.      0 on success, i.e. the directory was created, or didn't need to be.
  5067.     -1 on failure to create the directory
  5068.    VMS version by Mark Berryman, Feb 94.
  5069. */
  5070. int
  5071. zmkdir(path) char *path; {
  5072.     struct FAB dir_fab;
  5073.     struct NAM dir_nam;
  5074.     struct dsc$descriptor_s expanded_filename;
  5075.  
  5076.     char expanded_name[NAM$C_MAXRSS];
  5077.  
  5078.     dir_fab = cc$rms_fab;
  5079.     dir_fab.fab$l_fna = path;
  5080.     dir_fab.fab$b_fns = strlen(path);
  5081.     dir_fab.fab$l_nam = &dir_nam;
  5082.  
  5083.     dir_nam = cc$rms_nam;
  5084.     dir_nam.nam$l_esa = (char *) &expanded_name;
  5085.     dir_nam.nam$b_ess = sizeof(expanded_name);
  5086.  
  5087.     rms_sts = sys$parse(&dir_fab,0,0);
  5088.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  5089.     switch (rms_sts) {
  5090.         case RMS$_NORMAL :
  5091.         debug(F110,"zmkdir path already exists",path,0);
  5092.         return 0;
  5093.  
  5094.         case RMS$_FNM :            /* E.g. too long... (99628) */
  5095.         debug(F110,"zmkdir bad filename",path,0);
  5096.         return 0;            /* Let zrtol() handle it. */
  5097.  
  5098.         case RMS$_DNF :            /* Directory Not Found, create it */
  5099.             debug(F110,"zmkdir RMS$_DNF",path,0);
  5100.             break;
  5101.  
  5102.         default :            /*  Any other result is fatal. */
  5103.             debug(F111,"zmkdir status",path,rms_sts);
  5104.         return -1;
  5105.     }
  5106.  
  5107. /*  The parse succeeded but said the directory didn't exist, so create it. */
  5108.     expanded_filename.dsc$b_class = DSC$K_CLASS_S;
  5109.     expanded_filename.dsc$b_dtype = DSC$K_DTYPE_T;
  5110.     expanded_filename.dsc$a_pointer = (char *) &expanded_name;
  5111.     expanded_filename.dsc$w_length = dir_nam.nam$b_esl;
  5112.  
  5113. /* Locate the closing bracket, ']' or '>', using the $PARSE results */
  5114. /* node names are not allowed, but let lib$create_dir return the error */
  5115.  
  5116.     expanded_filename.dsc$w_length = dir_nam.nam$b_node +
  5117.                                      dir_nam.nam$b_dev +
  5118.                                      dir_nam.nam$b_dir;
  5119.  
  5120.     rms_sts = lib$create_dir(&expanded_filename,0,0,0,0,0);
  5121.     expanded_name[expanded_filename.dsc$w_length] = '\0';
  5122.     debug(F111,"zmkdir create_dir status",(char *)&expanded_name, rms_sts);
  5123.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  5124.     CHECK_ERR("zmkdir create error", rms_sts);
  5125.     return (rms_sts == SS$_CREATED ? 1 : -1);
  5126. }
  5127.  
  5128. /*  Z R M D I R  --  Delete a directory  */
  5129.  
  5130. int
  5131. zrmdir(path) char *path; {
  5132.     unsigned int status;
  5133.  
  5134.     struct FAB dir_fab;
  5135.     struct NAM dir_nam;
  5136.     struct dsc$descriptor_s expanded_filename;
  5137.     $DESCRIPTOR(close_bracket,"]");
  5138.  
  5139.     char expanded_name[NAM$C_MAXRSS];
  5140.     char tmpbuf[NAM$C_MAXRSS];
  5141.     char * result;
  5142.  
  5143.     char *p = path;
  5144.     int lb = 0, rb = 0;
  5145.  
  5146.     if (!p) p = "";
  5147.  
  5148.     debug(F110,"zrmdir path 1",path,0);
  5149.     if (!*p) return(-1);
  5150. /*
  5151.   I'm sure there must be an API for deleting directories, but I don't know
  5152.   what it is, so if any VMS programmers care to replace this routine by one
  5153.   that does it right, be my guest (fdc, Nov 1997).
  5154. */
  5155.     while (*p) {
  5156.     if (*p == '[') lb++;
  5157.     else if (*p == ']') rb++;
  5158.     p++;
  5159.     }
  5160.     if (lb != 1 && rb != 1 && p > path && *(p-1) != ':') {
  5161.     sprintf(tmpbuf,"[.%s]",path);
  5162.     } else {
  5163.     ckstrncpy(tmpbuf,path,NAM$C_MAXRSS);
  5164.     }
  5165.     path = tmpbuf;
  5166.     debug(F110,"zrmdir path 2",path,0);
  5167.  
  5168.     dir_fab = cc$rms_fab;
  5169.     dir_fab.fab$l_fna = path;
  5170.     dir_fab.fab$b_fns = strlen(path);
  5171.     dir_fab.fab$l_nam = &dir_nam;
  5172.  
  5173.     dir_nam = cc$rms_nam;
  5174.     dir_nam.nam$l_esa = (char *) &expanded_name;
  5175.     dir_nam.nam$b_ess = sizeof(expanded_name);
  5176.  
  5177.     status = sys$parse(&dir_fab,0,0);
  5178.     if (!(status & 1)) vms_lasterr = status;
  5179.     if (status != RMS$_NORMAL)        /* Does not exist */
  5180.       return(-1);
  5181.     debug(F100,"zrmdir directory exists",path,0);
  5182.     {
  5183.       char * fb = NULL;        /* First bracket */
  5184.     char * lb = NULL;        /* Last bracket */
  5185.     char * lp = NULL;        /* Last period */
  5186.     unsigned long int sts;
  5187.     p = path;
  5188.     result = expanded_name;
  5189.     while (*p) {
  5190.         if (*p == '[') fb = p;
  5191.             if (*p == '.') lp = p;
  5192.           if (*p == ']') lb = p;
  5193.         p++;
  5194.     }
  5195.     if (!fb || !lb)            /* No brackets */
  5196.       return(-1);
  5197.     if (*(lb+1))            /* Something after right bracket */
  5198.       return(-1);
  5199.     if (*lp) {            /* Last period */
  5200.         *lp = ']';
  5201.         *lb = NUL;
  5202.         sprintf(result,"%s.DIR;1",path);
  5203.     } else {
  5204.         *lb = NUL;
  5205.         sprintf(result,"[000000]%s.DIR;1",path);
  5206.     }
  5207.     if (result[0] == '[' && result[1] == ']')
  5208.       result += 2;
  5209.     debug(F110,"zrmdir result",result,0);
  5210. /*
  5211.    Here we set the protection to something that allows us to delete it but
  5212.    also allows us to access it again in case it can't be deleted, e.g. because
  5213.    it contained some files.
  5214. */
  5215.     sprintf(tmpbuf,"set protection=owner:RWED %s",result);
  5216.     sts = system(tmpbuf);
  5217.     debug(F111,"zrmdir system() status ",tmpbuf,sts);
  5218. #ifdef COMMENT
  5219. /* If this failed it doesn't necessarily mean there is no Delete access */
  5220.     if ((sts&1) != 1)
  5221.       return(-1);
  5222. #endif /* COMMENT */
  5223.     sprintf(tmpbuf,"delete %s",result); /* delete() won't do it. */
  5224.     sts = system(tmpbuf);
  5225.     debug(F111,"zrmdir system() status ",tmpbuf,sts);
  5226.     if ((sts&1) != 1)
  5227.       return(-1);
  5228.     return(0);
  5229.     }
  5230. }
  5231.  
  5232. /*  Z M A I L  --  Send file f as mail to address p.  */
  5233. /*
  5234.   Returns 0 on success
  5235.    2 if mail delivered but temp file can't be deleted
  5236.   -2 if mail can't be delivered
  5237. */
  5238. int
  5239. zmail(p,f) char *p; char *f; {
  5240.     char *zmbuf;
  5241.     static char spbuf[] = "$ mail %s %s/subj=\"Enclosed file %s\"";
  5242.     static char spbuf2[] = "%s;";
  5243.     unsigned long int sts;
  5244.  
  5245.     zmbuf = malloc(strlen(p)+(2*strlen(f))+sizeof(spbuf));
  5246.     sprintf(zmbuf,spbuf, f, p, f);
  5247.     sts = system(zmbuf);
  5248.     debug(F111,"zmail: system returns status ",zmbuf,sts);
  5249.     free(zmbuf);
  5250.     if ((sts&1) != 1) {
  5251.     debug(F101,"zmail: returning","",-2);
  5252.     return(-2);
  5253.     }
  5254.     zmbuf = malloc(strlen(f)+sizeof(spbuf2));
  5255.     sprintf(zmbuf,spbuf2, f);
  5256.     sts = delete(zmbuf);
  5257.     debug(F111,"zmail: delete returns status ",zmbuf,sts);
  5258.     free(zmbuf);
  5259.     if (sts) sts = 2;
  5260.     debug(F101,"zmail: returning","",sts);
  5261.     return(sts);
  5262. }
  5263.  
  5264. /* Z P R I N T  --  Print file f with options p.  */
  5265. /*
  5266.   Returns 0 on success, -3 on failure.
  5267. */
  5268. int
  5269. zprint(p,f) char *p; char *f; {
  5270.     char *zmbuf;
  5271.     static char spbuf[] = "$ print/delete %s %s";
  5272.     unsigned long int sts;
  5273.  
  5274.     zmbuf = malloc(strlen(p)+strlen(f)+sizeof(spbuf));
  5275.     sprintf(zmbuf,spbuf, p, f);
  5276.     sts = system(zmbuf);
  5277.     debug(F111,"zprint: system returns status ",zmbuf,sts);
  5278.     free(zmbuf);
  5279.     debug(F101,"zprint: returning","",(sts&1) ? 0 : -3);
  5280.     return((sts&1) ? 0 : -3);
  5281. }
  5282.  
  5283. /* Z S Y S C M D  --  Execute a DCL command with direct output.  */
  5284.  
  5285. /*
  5286.  * Since it's really difficult to have an alternate CLI under VMS (since the
  5287.  * MCR interface isn't documented and POSIX hasn't published the interface,
  5288.  * we'll just assume everybody uses DCL and hand it of to zshcmd().
  5289.  */
  5290. int
  5291. zsyscmd(s) char *s; {
  5292.     if (nopush) {
  5293.     debug(F100,"zsyscmd fails: nopush","",0);
  5294.     return(-1);
  5295.     }
  5296.     return(zshcmd(s));
  5297. }
  5298.  
  5299. /*
  5300.  * Dummy function, since SIG_DFL seems to be broken with DEC C
  5301.  */
  5302. VOID
  5303. sig_dum() {
  5304.  
  5305. }
  5306.  
  5307. /* Z S H C M D  --  Execute a default CLI command with direct output.  */
  5308.  
  5309. /*
  5310.  * As it's _REALLY_ unlikely that the user is using MCR as his default CLI,
  5311.  * and DEC doesn't document how to write any other alternate CLIs, use DCL.
  5312.  */
  5313.  
  5314. #ifndef    SS$_EXPRCLM        /* VMS doesn't return this yet, but let's */
  5315. #define SS$_EXPRCLM 10804    /* be forward-thinking and anticpate VMS */
  5316. #endif /* SS$_EXPRCLM */    /* V6.0, which will return it. */
  5317.  
  5318. int
  5319. zshcmd(s) char *s; {
  5320.     unsigned long sts, cc;
  5321.     void (*cct)();
  5322.     void (*sig_dum_ptr)() = sig_dum;
  5323.     struct dsc$descriptor_s
  5324.       cmd_line = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  5325.     char *i;
  5326.  
  5327.     pexitstat = -1;
  5328.     if (nopush) {
  5329.     debug(F100,"zshcmd fails: nopush","",0);
  5330.     return(-1);
  5331.     }
  5332.     if (!s) s = "";
  5333.     debug(F110,"zshcmd",s,0);
  5334.     if (check_spawn() != 0) {
  5335.     debug(F100,"zshcmd: spawning prohibited by UAF flags","",0);
  5336.     return(0);
  5337.     }
  5338.  
  5339.     i = strstr(zgtdir(),"::");
  5340.     if (i != NULL) {
  5341.         debug(F100,"zshcmd: spawn prohibited on remote node","",0);
  5342.         printf("Cannot SPAWN with remote node as default directory\n");
  5343.      if (*s)
  5344.       printf("therefore, cannot execute the DCL command \"%s\"\n", s);
  5345.         return(0);
  5346.     }
  5347.  
  5348.     cct = signal(SIGINT,sig_dum_ptr);    /* Let inferior process catch ^C */
  5349.  
  5350.     cmd_line.dsc$w_length = strlen(s);
  5351.     cmd_line.dsc$a_pointer = s;
  5352.  
  5353.     if (!(*s))
  5354.       printf("Type LOGOUT to return to VMS C-Kermit.\n\n");
  5355.     sts = lib$spawn(&cmd_line, 0, 0, 0, 0, 0, &cc, 0, 0, 0, 0, 0);
  5356.     if (!(sts & 1)) vms_lasterr = sts;
  5357.     signal(SIGINT,cct);
  5358. /*
  5359.  * Note: We can't check for this beforehand as doing a getjpi for prclm will
  5360.  *     only return the UAF value, not the available value. So we try it and
  5361.  *     print this message if it didn't work.
  5362.  */
  5363.     if ((sts == SS$_EXQUOTA) || (sts == SS$_EXPRCLM)) {
  5364.     printf(
  5365. "Your account does not have sufficient quotas to use this command.\n");
  5366.     printf(
  5367. "Please ask your system manager to increase your UAF PRCLM quota.\n");
  5368.     }
  5369.     debug(F101,"zshcmd lib$spawn sts", "", sts);
  5370.     debug(F101,"zshcmd lib$spawn cc ", "", cc);
  5371.     debug(F101,"zshcmd lib$spawn SS$_NORMAL", "", SS$_NORMAL);
  5372.     if (sts == SS$_NORMAL) {
  5373.     pexitstat = cc;
  5374.     return((cc & 1) ? 1 : 0);    /* Success */
  5375.     } else {
  5376.     return(0);            /* Failure */
  5377.     }
  5378. }
  5379.  
  5380. /*  Z S T R I P  --  Strip device & directory name from file specification.  */
  5381.  
  5382. /*  Strip pathname from filename "name", return pointer to result in name2 */
  5383.  
  5384. static char work[1100];    /* buffer for use by zstrip and zltor */
  5385.  
  5386. VOID
  5387. zstrip(name,name2) char *name, **name2; {
  5388.     char *cp, *pp;
  5389.     char last;
  5390.     int len;
  5391.     debug(F110,"zstrip entry",name,0);
  5392.     pp = work;                /* Default return is empty string */
  5393.     *name2 = work;
  5394.     *pp = '\0';
  5395.     if (!name)
  5396.       return;
  5397.     if (!*name)
  5398.       return;
  5399.  
  5400. /*  NODE::DEV:[DIR] terminates on on final ':', '>' or ']'.  */
  5401.  
  5402.     for (cp = name; *cp; cp++) {
  5403.     last = *cp;
  5404.         if (*cp == '/' || *cp == ':' || *cp == '>' || *cp == ']') /* slash? */
  5405.       pp = work;
  5406.     else if (*cp == ';')        /* Chop off any version number */
  5407.       break;
  5408.     else                /* Part of filename */
  5409.       *pp++ = *cp;
  5410.     }
  5411.     *pp = '\0';                /* Terminate the string */
  5412.     debug(F000,"zstrip 2",work,last);
  5413. #ifdef COMMENT
  5414. /*
  5415.   This is a bad idea because the result will need to be passed thru zstrip()
  5416.   again, but zstrip() isn't designed to call itself.
  5417. */
  5418.     if (work[0] == '\0' && last == ':') { /* Result is empty? */
  5419.     char * q;            /* Maybe it's a logical name */
  5420.     q = (char *)malloc(1100);
  5421.     if (q) {
  5422.         ckstrncpy(q,name,1100);
  5423.         len = strlen(q);
  5424.         if (len > 0) {
  5425.         if (q[len-1] == ':') {
  5426.             char *t = q;
  5427.             q[len-1] = '\0';
  5428.             while (*t) { if (islower(*t)) *t = toupper(*t); t++; }
  5429.             debug(F110,"zstrip checking",q,0);
  5430.             pp = getenv(q);
  5431.             if (!pp) pp = "";
  5432.             ckstrncpy(work,pp,1100);
  5433.             debug(F110,"zstrip getenv",work,0);
  5434.         }
  5435.         }
  5436.         free(q);
  5437.     }
  5438.     }
  5439.     debug(F110,"zstrip 3",work,0);
  5440. #endif /* COMMENT */
  5441.  
  5442. /* The following should allow us to receive files to LPT:, LTA1:, etc. */
  5443.  
  5444.     if (work[0] == '\0') {        /* Still empty? */
  5445.     debug(F000,"zstrip last",name,last); /* If it's a device name */
  5446.     if (last == ':')        /* put it back */
  5447.       ckstrncpy(work,name,1100);
  5448.     }
  5449.     debug(F110,"zstrip result",*name2,0);
  5450. }
  5451.  
  5452. int
  5453. zchkpath(s) char *s; {
  5454. #ifdef COMMENT
  5455. /*
  5456.   This needs to be replaced with something more intelligent.
  5457.   The idea is to see if the file, whose specification is pointed to by s,
  5458.   is in the current directory.  This function should return 0 if it is,
  5459.   nonzero otherwise.  Presently we rely on being called with a full
  5460.   filespec of the form DISK:[DEV]NAME.TYP;V, so this works more or less
  5461.   by accident.  What we really need is to call some kind of VMS service
  5462.   to get the NODE::DEV:[DIR] of the file, and compare with the current
  5463.   NODE::DEV:[DIR].
  5464. */
  5465.     char *p;
  5466.     p = zgtdir();            /* Get current dir. */
  5467.     debug(F110,"zchkpath file",s,0);
  5468.     debug(F110,"zchkpath current dir",p,0);
  5469.     return(strncmp(p,s,strlen(p)));    /* Compare it. */
  5470. #else
  5471. /*
  5472.     The purpose of this routine is to determine whether the file specified
  5473.     by *s would be in the current default directory or not.
  5474.  
  5475.     Inputs:
  5476.     *s = filename to check, can be just a filename or full path
  5477.  
  5478.     Outputs:
  5479.     0 = in the current default directory
  5480.         nonzero = not in the current directory
  5481.  
  5482.     Algorithm:
  5483.     Uses SYS$PARSE.  As long as the current default directory is not
  5484.     on a remote DECnet node, SYS$PARSE will return the Directory ID
  5485.     of the directory where the parsed filename would be found.  This
  5486.     means all we need do is parse both the default directory and the
  5487.     filename passed to us and compare DIDs (as well as make sure the
  5488.     two DIDs reference the same disk).  If so, it doesn't matter how
  5489.     convoluted the filename passed to us is, or what logical names
  5490.     may be involved, SYS$PARSE will tell us with certainty if it
  5491.     resolves to the current directory or not.
  5492.  
  5493.     However, if the current default directory is on a remote DECnet
  5494.     node, this information is not available (since SYS$PARSE executes
  5495.     locally, not remotely).  In this case, SYS$PARSE will still expand
  5496.     the filename as much as possible and then we can simply compare
  5497.     the default directory with the directory reference returned by the
  5498.     parse of the filename in *s using a simple string compare.  Since
  5499.     we cannot evaluate remote logical names, this may cause a false
  5500.     negative to occur if the user is playing games with the filename,
  5501.     but it guarantees that we do not get a false positive.
  5502.  */
  5503.     unsigned int dd_len, status;
  5504.     struct FAB my_fab;
  5505.     struct NAM dd_nam, s_nam;
  5506.     const char *default_dir = "dummy.name";    /* guaranteed to resolve to */
  5507.                         /* the current default dir. */
  5508.  
  5509.     char dd_expanded_name[NAM$C_MAXRSS];
  5510.     char s_expanded_name[NAM$C_MAXRSS];
  5511.  
  5512.     my_fab = cc$rms_fab;
  5513.     my_fab.fab$l_fna = (char *) default_dir;
  5514.     my_fab.fab$b_fns = strlen(default_dir);
  5515.     my_fab.fab$l_nam = &dd_nam;
  5516.  
  5517.     dd_nam = cc$rms_nam;
  5518.     dd_nam.nam$l_esa = (char *) &dd_expanded_name;
  5519.     dd_nam.nam$b_ess = sizeof(dd_expanded_name);
  5520.  
  5521. /* PARSE the current default directory */
  5522.     status = sys$parse(&my_fab,0,0);
  5523.     if (!(status & 1)) {                  /* any $PARSE errors are fatal */
  5524.         return (status);
  5525.     }
  5526.  
  5527.    s_nam = cc$rms_nam;
  5528.    s_nam.nam$l_esa = (char *) &s_expanded_name;
  5529.    s_nam.nam$b_ess = sizeof(s_expanded_name);
  5530.  
  5531.    my_fab.fab$l_fna = s;
  5532.    my_fab.fab$b_fns = strlen(s);
  5533.    my_fab.fab$l_nam = &s_nam;
  5534.  
  5535. /* PARSE the filename in question */
  5536.     status = sys$parse(&my_fab,0,0);
  5537.     if (!(status & 1)) {                  /* any $PARSE errors are fatal */
  5538.         return (status);
  5539.     }
  5540.  
  5541.    if (dd_nam.nam$b_node == 0) {    /* default dir is not via DECnet */
  5542.     if (dd_nam.nam$t_dvi[0] != s_nam.nam$t_dvi[0])
  5543.         return (1);        /* disk name sizes do not match*/
  5544.     if (strncmp( &dd_nam.nam$t_dvi[1], &s_nam.nam$t_dvi[1],
  5545.             dd_nam.nam$t_dvi[0] ))
  5546.         return (1);        /* disk names do not match */
  5547.  
  5548. /*    At this point it is known that the current default directory is
  5549.     a local directory and that the device portion of the default
  5550.     directory and that of the filename passed to us are identical.
  5551.     Now compare the major and minor number of the DID (but not the RVN,
  5552.     we do not care if the result is on another disk of a bound volumset)
  5553.     of the two parse results.  If they are the same, the filename passed
  5554.     to us is definitely a reference to the default directory.
  5555.  */
  5556.     if ( (dd_nam.nam$w_did_num == s_nam.nam$w_did_num) &&
  5557.          (dd_nam.nam$w_did_seq == s_nam.nam$w_did_seq) )
  5558.          return (0);        /* in the same dir */
  5559.     return (1);            /* not the same dir */
  5560.    }
  5561.  
  5562. /*
  5563.    The default file spec contains a DECnet node reference.  SYS$PARSE does
  5564.    not return as much data when done across DECnet, mainly because the parse
  5565.    is still executed on the local node, not the remote node.  The result of
  5566.    the parse will still expand the string as much as possible however
  5567.    (.e.g., if one does a SET DEFAULT to NODE::DEV:[DIR] then a result of the
  5568.     parse of FILENAME.EXT will return NODE::DEV:[DIR]FILENAME.EXT).  Because
  5569.    of this, we simply compare the two resultant strings from the two SYS$PARSE
  5570.    operations in order to check the path of a file that contains a node name.
  5571.    This still has the benefit of no longer requiring that a full path name
  5572.    be passed to this routine.
  5573.  */
  5574.    dd_len =                /* only want node::dev:[dir] */
  5575.        dd_nam.nam$l_name - dd_nam.nam$l_node;
  5576.    dd_expanded_name[ dd_len ] = '\0';    /* terminate after the ] */
  5577.    s_expanded_name[ s_nam.nam$l_name - s_nam.nam$l_node ] = '\0';
  5578.    return (strncmp (dd_expanded_name, s_expanded_name, dd_len) );
  5579. #endif /* COMMENT */
  5580. }
  5581.  
  5582. #ifdef OLD_VMS
  5583. #ifndef VMS_V46    /* rename() included in VAXCRTL as of VMS v4.6*/
  5584.  
  5585. static VOID
  5586. descname(desc,name) struct dsc$descriptor_s *desc; char *name; {
  5587.     desc->dsc$w_length = strlen(name);    /* Length of name */
  5588.     desc->dsc$a_pointer = name;        /* Address */
  5589.     desc->dsc$b_class = DSC$K_CLASS_S;    /* String descriptor class */
  5590.     desc->dsc$b_dtype = DSC$K_DTYPE_T;    /* ASCII string data type */
  5591. }
  5592.  
  5593. /* VMS version of RENAME */
  5594. int /* ? */
  5595. rename(oldname, newname) char oldname[], newname[]; {
  5596.     struct dsc$descriptor_s old_desc, new_desc;
  5597.     int lib$rename_file();
  5598.  
  5599.     /* Build string descriptors */
  5600.  
  5601.     descname(&old_desc, oldname);
  5602.     descname(&new_desc, newname);
  5603.  
  5604.     /* Call lib$rename_file routine */
  5605.  
  5606.     return(lib$rename_file(&old_desc, &new_desc, 0,0,0,0,0,0,0,0,0,0));
  5607. }
  5608. #endif /* !VMS_46 */
  5609. #endif /* OLD_VMS */
  5610.  
  5611. /*
  5612.  * Check to see if we have spawn priv's.
  5613.  */
  5614. int
  5615. check_spawn() {
  5616.     struct itmlstdef {
  5617.     short int buflen;
  5618.     short int itmcod;
  5619.     char *bufaddr;
  5620.     long int *retlen;
  5621.     };
  5622.  
  5623.     struct itmlstdef itmlst[] =
  5624.     {4,JPI$_UAF_FLAGS,0,0,0,0,0,0};
  5625.  
  5626.     long uaf_flags_size;
  5627.     unsigned long uaf_flags;
  5628.  
  5629.     itmlst[0].bufaddr = (char *)&uaf_flags;
  5630.     itmlst[0].retlen = &uaf_flags_size;
  5631.  
  5632.  
  5633.     vms_status = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0);
  5634.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  5635.     if ((vms_status) != SS$_NORMAL)
  5636.       return(-1);                /* Assume the worst... */
  5637.  
  5638. #ifdef UAI$M_CAPTIVE
  5639.     if (uaf_flags & UAI$M_CAPTIVE) {
  5640.     printf(
  5641. "\nThis command cannot be executed. Your account is CAPTIVE.\n\n");
  5642.     return(-1);
  5643.     }
  5644. #endif  /* UAI$M_CAPTIVE */
  5645. #ifdef    UAI$M_RESTRICTED            /* for pre-V5.2 systems */
  5646.     if (uaf_flags & UAI$M_RESTRICTED) {
  5647.     printf(
  5648. "\nThis command cannot be executed. Your account is CAPTIVE.\n\n");
  5649.     return(-1);
  5650.     }
  5651. #endif    /* uai$v_restricted */
  5652.     return(0);
  5653. }
  5654.  
  5655. /*
  5656.  * Stuff having to do with SET FILE TYPE LABELED
  5657.  */
  5658. char *
  5659. get_vms_vers() {
  5660.     static char sysver[9];
  5661.     int len;
  5662.     struct itmlst {
  5663.           short int buflen;
  5664.           short int code;
  5665.           char *bufadr;
  5666.           int *retlen;
  5667.           } vms_sysver[2];
  5668.  
  5669.     vms_sysver[0].buflen = 8;
  5670.     vms_sysver[0].code = SYI$_VERSION;
  5671.     vms_sysver[0].bufadr = (char *)&sysver;
  5672.     vms_sysver[0].retlen = &len;
  5673.     vms_sysver[1].buflen = 0;
  5674.     vms_sysver[1].code = 0;
  5675.     sys$getsyiw(0,0,0,&vms_sysver,0,0,0);
  5676.     sysver[8]='\0';
  5677.     len = 7;
  5678.     while (sysver[len] == ' ') {
  5679.     sysver[len] = '\0';
  5680.     len--;
  5681.     }
  5682.     return(sysver);
  5683. }
  5684.  
  5685. /* Find where a relative file specification starts in the expanded file
  5686.  * name.  Returns the number of characters preceeding the relative portion
  5687.  */
  5688.  
  5689. int
  5690. fnd_rel(name) char *name; {
  5691.  
  5692.     int ix=0;
  5693.     char relnam[NAM$C_MAXRSS];
  5694.     char *ip;
  5695.  
  5696.     nzltor(name, relnam, 0, PATH_REL, NAM$C_MAXRSS);
  5697.  
  5698.     debug(F111, " fnd_rel: absolute name and length", name, strlen(name));
  5699.     debug(F111, " fnd_rel: relative name and length", relnam, strlen(relnam));
  5700.  
  5701.     if ( (*relnam == '[') || (*relnam == '<') ) {
  5702.         ip = strstr(name, &relnam[1]);
  5703.         if (ip) ix = ip - name;
  5704.     } else {                       /* no common path; send as PATH OFF */
  5705.         ip = strstr(name, relnam);
  5706.         if (ip) ix = ip - name -1;  /* do_label_rcv will use the bracket */
  5707.     }
  5708.     return(ix);
  5709. }
  5710.  
  5711. int
  5712. do_label_send(name) char *name; {
  5713.     int pad_size;
  5714.     int rel_dspec;
  5715.     extern int fnspath;
  5716.     char * zp;                /* To shut up compilers whining */
  5717.                     /* about signed vs unsigned chars */
  5718.     zp = (char *)zinptr;
  5719.     zp += sprintf(zp,"KERMIT LABELED FILE:02D704VERS");
  5720.     zp += sprintf(zp,"%08d%s", strlen(get_vms_vers()), get_vms_vers());
  5721.     zp += sprintf(zp,"05KVERS00000008%08ld", vernum);
  5722.     if (fnspath == PATH_REL) {
  5723.         rel_dspec = fnd_rel(name);
  5724.         debug(F101," do_label_send: rel dir at char","", rel_dspec);
  5725.         zp += sprintf(zp,"07REL_DIR00000008%08ld", rel_dspec);
  5726.     }
  5727.     zp += sprintf(zp,"07VMSNAME%08d", strlen(name));
  5728.     zp += sprintf(zp,"%s", name);
  5729.     zp += sprintf(zp,"07VMSFILE%08d", 70);
  5730.     memmove(zp, &xabpro_ifile.xab$w_pro, 2);
  5731.     zp += 2;
  5732.     memmove(zp, &xabpro_ifile.xab$l_uic, 4);
  5733.     zp += 4;
  5734.     memmove(zp, &fab_ifile.fab$b_rfm, 1);
  5735.     zp += 1;
  5736.     memmove(zp, &fab_ifile.fab$b_org, 1);
  5737.     zp += 1;
  5738.     memmove(zp, &fab_ifile.fab$b_rat, 1);
  5739.     zp += 1;
  5740.     memmove(zp, &xuchar, 4);    /* Dummy for file chars. */
  5741.     zp += 4;
  5742.     memmove(zp, &fab_ifile.fab$b_fsz, 1);
  5743.     zp += 1;
  5744.     memmove(zp, &xabfhc_ifile.xab$w_lrl, 2);
  5745.     zp += 2;
  5746.     memmove(zp, &fab_ifile.fab$w_mrs, 2);
  5747.     zp += 2;
  5748.     memmove(zp, &xabfhc_ifile.xab$l_ebk, 4);
  5749.     zp += 4;
  5750.     memmove(zp, &xabfhc_ifile.xab$w_ffb, 2);
  5751.     zp += 2;
  5752.     memmove(zp, &xabfhc_ifile.xab$l_hbk, 4);
  5753.     zp += 4;
  5754.     memmove(zp, &fab_ifile.fab$w_deq, 2);
  5755.     zp += 2;
  5756.     memmove(zp, &fab_ifile.fab$b_bks, 1);
  5757.     zp += 1;
  5758.     memmove(zp, &fab_ifile.fab$w_gbc, 2);
  5759.     zp += 2;
  5760.     memmove(zp, &xabfhc_ifile.xab$w_verlimit, 2);
  5761.     zp += 2;
  5762.     memmove(zp, &fab_ifile.fab$b_rfm+1, 1);    /* This is fab$b_journal */
  5763.     zp += 1;
  5764.     memmove(zp, &xabdat_ifile.xab$q_cdt, 8);
  5765.     zp += 8;
  5766.     memmove(zp, &xabdat_ifile.xab$q_rdt, 8);
  5767.     zp += 8;
  5768.     memmove(zp, &xabdat_ifile.xab$w_rvn, 2);
  5769.     zp += 2;
  5770.     memmove(zp, &xabdat_ifile.xab$q_edt, 8);
  5771.     zp += 8;
  5772.     memmove(zp, &xabdat_ifile.xab$q_bdt, 8);
  5773.     zp += 8;
  5774.     if (xabpro_ifile.xab$w_acllen != 0) {
  5775.         debug(F101, "do_label_send: acllen", "", xabpro_ifile.xab$w_acllen);
  5776.     zp += sprintf(zp,"06VMSACL%08d", xabpro_ifile.xab$w_acllen);
  5777.     memmove(zp, &aclbuf, xabpro_ifile.xab$w_acllen);
  5778.     zp += xabpro_ifile.xab$w_acllen;
  5779.     }
  5780.     zp += sprintf(zp,"04DATA00000000");
  5781.     zincnt = (zp - (char *)zinbuffer);    /* How big */
  5782.     zinptr = zinbuffer;            /* Reset pointer for readout */
  5783.     return(1);
  5784. }
  5785.  
  5786. /*
  5787.   D O _ L A B E L _ R E C V
  5788.  
  5789.   Note that we don't honor SET FILE COLLISION APPEND for labeled receives --
  5790.   the whole point of labeled receives is to generate an exact copy of the
  5791.   source file, attributes and all.
  5792. */
  5793. #define CK_LBLBUFLEN 16
  5794. #define CK_VMSFILELEN 70
  5795.  
  5796. int
  5797. do_label_recv() {
  5798.  
  5799.     extern int fnrpath;            /* (look at this later...) */
  5800.  
  5801.     char *recv_ptr;
  5802.     char buffer[CK_LBLBUFLEN+1];
  5803.     char vmsfile[CK_VMSFILELEN];
  5804.     char *filptr = vmsfile;
  5805.     int lblen, alen;
  5806.     int gotname = 0, gotfile = 0, gotacl = 0, gotrel = 0;
  5807.     char *i, *j;
  5808.     unsigned short jnlflg;
  5809.  
  5810.     debug(F101,"do_label_recv: options","",ofile_lblopts);
  5811.     ofile_lblproc = 1;            /* Don't come here again */
  5812.  
  5813.     if (strncmp((char *)zoutbuffer,"KERMIT LABELED FILE:02D704VERS",30) != 0)
  5814.       return(0);            /* Just continue if unlabeled */
  5815.  
  5816.     recv_ptr = (char *)zoutbuffer+30;    /* start at front of buffer */
  5817.  
  5818.     memcpy(buffer, recv_ptr, 8);
  5819.     recv_ptr += 8;
  5820.     buffer[8] = '\0';
  5821.     lblen = atoi(buffer);
  5822.     if (lblen > CK_LBLBUFLEN) {
  5823.     debug(F101,"do_label_recv: lblen too long 1","",lblen);
  5824.     return(-1);
  5825.     }
  5826.     memcpy(buffer, recv_ptr, lblen);
  5827.     recv_ptr += lblen;
  5828.     buffer[lblen] = '\0';
  5829.     debug(F110,"do_label_recv: file created under {Open}VMS: ",buffer,0);
  5830.  
  5831.     memcpy(buffer, recv_ptr, 7);
  5832.     recv_ptr += 7;
  5833.     if (strncmp(buffer, "05KVERS", 7) != 0) {
  5834.     debug(F100,"do_label_recv: lost sync at KVERS","",0);
  5835.     return(-1);
  5836.     }
  5837.     memcpy(buffer, recv_ptr, 8);
  5838.     recv_ptr += 8;
  5839.     buffer[8] = '\0';
  5840.     lblen = atoi(buffer);
  5841.     if (lblen > CK_LBLBUFLEN) {
  5842.     debug(F101,"do_label_recv: lblen too long 2","",lblen);
  5843.     return(-1);
  5844.     }
  5845.     memcpy(buffer, recv_ptr, lblen);
  5846.     recv_ptr += lblen;
  5847.     buffer[lblen] = '\0';
  5848.     debug(F110,"do_label_recv: file created with C-Kermit/VMS: ",buffer,0);
  5849.  
  5850.   next_label:
  5851.     memcpy(buffer, recv_ptr, 2);
  5852.     recv_ptr += 2;
  5853.     buffer[2] = '\0';
  5854.     lblen = atoi(buffer);
  5855.     if (lblen == 0) {
  5856.     debug(F100,"do_label_recv: lost sync at next_label: ","",0);
  5857.     return(-1);
  5858.     } else if (lblen > CK_LBLBUFLEN) {
  5859.     debug(F101,"do_label_recv: lblen too long 3","",lblen);
  5860.     return(-1);
  5861.     }
  5862.     memcpy(buffer, recv_ptr, lblen);
  5863.     recv_ptr += lblen;
  5864.     buffer[lblen] = '\0';
  5865.     debug(F110,"do_label_recv: found tag: ",buffer,0);
  5866.     if (strcmp(buffer, "VMSNAME") == 0) {
  5867.     memcpy(buffer, recv_ptr, 8);
  5868.         recv_ptr += 8;
  5869.     buffer[8] = '\0';
  5870.     lblen = atoi(buffer);
  5871.     if (lblen > CKMAXPATH) {    /* fdc 23 Jun 96 */
  5872.         debug(F101,"do_label_recv: lblen too long 4","",lblen);
  5873.         return(-1);
  5874.     }
  5875.     memcpy(ofile_vmsname, recv_ptr, lblen);
  5876.         recv_ptr += lblen;
  5877.     ofile_vmsname[lblen] = '\0';
  5878.     gotname++;
  5879.     debug(F110,"do_label_recv: loaded file name block as: ",
  5880.           ofile_vmsname,
  5881.           0
  5882.           );
  5883.     debug(F110,"do_label_recv: ofile_vmsname 2",ofile_vmsname,0);
  5884.         if ((ofile_lblopts & LBL_PTH) == 0) {
  5885.             if ( (fnrpath == PATH_REL) && (gotrel > 0) ) {
  5886.                 i = strrchr(ofile_vmsname, '[');
  5887.                 if (i != NULL) {
  5888.                     ofile_vmsname[0] = '[';
  5889.                 } else {
  5890.                     ofile_vmsname[0] = '<';
  5891.                 }
  5892.                 ckstrncpy(&ofile_vmsname[1],
  5893.               &ofile_vmsname[gotrel],
  5894.               CKMAXPATH
  5895.               );
  5896.             } else {
  5897.                 i = strstr(ofile_vmsname, "::");
  5898.         if (i != NULL) {
  5899.             i += 2;
  5900.             memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  5901.                 }
  5902.             i = strrchr(ofile_vmsname, ':');
  5903.             j = strrchr(ofile_vmsname, ']');
  5904.             if (j == NULL)
  5905.         j = strrchr(ofile_vmsname, '>');
  5906.             if (j > i)
  5907.           i = j;
  5908.             if (i) {            /* fdc 6-12-96 */
  5909.             i++;
  5910.             memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  5911.             }
  5912.             }
  5913.         } else {
  5914.             i = strstr(ofile_vmsname, "::");
  5915.         if (i != NULL) {
  5916.             i += 2;
  5917.             memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  5918.         }
  5919.     }
  5920.     debug(F110,"do_label_recv: ofile_vmsname 3",ofile_vmsname,0);
  5921.     if (strchr(ofile_vmsname, ';') != NULL) {
  5922.         for (alen = strlen(ofile_vmsname);
  5923.          ofile_vmsname[alen] != ';' && alen > 0;
  5924.          alen--)
  5925.           ;
  5926.         ofile_vmsname[alen] = '\0';
  5927.     }
  5928.     debug(F110,"do_label_recv: resultant filespec: ",ofile_vmsname,0);
  5929.     goto next_label;
  5930.     } else if (strcmp(buffer, "REL_DIR") == 0) {
  5931.     memcpy(buffer, recv_ptr, 8);
  5932.     recv_ptr += 8;
  5933.     buffer[8] = '\0';
  5934.     lblen = atoi(buffer);
  5935.     if (lblen > CK_LBLBUFLEN) {
  5936.         debug(F101,"do_label_recv: lblen too long 5","",lblen);
  5937.         return(-1);
  5938.     }
  5939.         memcpy(buffer, recv_ptr, lblen);
  5940.         recv_ptr += lblen;
  5941.         buffer[lblen] = '\0';
  5942.         gotrel = atoi(buffer);
  5943.     if ( (gotrel < 3) || (gotrel > NAM$C_MAXRSS) ) {
  5944.         debug(F101,
  5945.           "do_label_recv: rel dir head position wrong",
  5946.           "",
  5947.           gotrel
  5948.           );
  5949.         return(-1);
  5950.     }
  5951.     debug(F101,"do_label_recv: relative directory position","", gotrel);
  5952.     goto next_label;
  5953.     } else if (strcmp(buffer, "VMSFILE") == 0) {
  5954.     memcpy(buffer, recv_ptr, 8);
  5955.     recv_ptr += 8;
  5956.     buffer[8] = '\0';
  5957.     lblen = atoi(buffer);
  5958.     if (lblen > CK_VMSFILELEN) {
  5959.         debug(F101,"do_label_recv: lblen too long 5","",lblen);
  5960.         return(-1);
  5961.     }
  5962.     memcpy(vmsfile, recv_ptr, lblen);
  5963.     recv_ptr += lblen;
  5964.     vmsfile[lblen] = '\0';
  5965.     gotfile++;
  5966.     debug(F100,"do_label_recv: loaded file attribute block","",0);
  5967.     goto next_label;
  5968.     } else if (strcmp(buffer, "VMSACL") == 0) {
  5969.     memcpy(buffer, recv_ptr, 8);
  5970.     recv_ptr += 8;
  5971.     buffer[8] = '\0';
  5972.     ofile_acllen = atoi(buffer);
  5973.     if (ofile_acllen > sizeof(ofile_vmsacl) ) {
  5974.         debug(F101,"do_label_recv: ofile_acllen too long","",ofile_acllen);
  5975.         return(-1);
  5976.     }
  5977.     memcpy(ofile_vmsacl, recv_ptr, ofile_acllen);
  5978.     recv_ptr += ofile_acllen;
  5979. /*    ofile_vmsacl[ofile_acllen] = '\0'; */    /* ACL buffer is binary */
  5980.     gotacl++;
  5981.     debug(F100,"do_label_recv: loaded file ACL block","",0);
  5982.     goto next_label;
  5983.     } else if (strcmp(buffer, "DATA") == 0) {
  5984.     memcpy(buffer, recv_ptr, 8);
  5985.     recv_ptr += 8;
  5986.     buffer[8] = '\0';
  5987.     lblen = atoi(buffer);
  5988.     if (lblen != 0) {
  5989.         debug(F101,"do_label_recv: length of DATA tag not zero","",lblen);
  5990.         return(-1);
  5991.     }
  5992.     debug(F100,"do_label_recv: positioned at start of file data","",0);
  5993.     goto all_set;
  5994.     } else {
  5995.     debug(F110,"do_label_recv: unrecognized label: ",buffer,0);
  5996.     memcpy(buffer, recv_ptr, 8);
  5997.     recv_ptr += 8;
  5998.     buffer[8] = '\0';
  5999.     lblen = atoi(buffer);
  6000.     recv_ptr += lblen;
  6001.     goto next_label;
  6002.     }
  6003.   all_set:
  6004.     if (gotfile != 1 || gotname != 1) {
  6005.     debug(F100,"do_label_recv: missing one or more required labels","",0);
  6006.     return(-1);
  6007.     }
  6008.  
  6009. /* Prep the characteristics */
  6010.  
  6011.     fab_ofile.fab$b_fac = FAB$M_BIO | FAB$M_PUT;
  6012.     fab_ofile.fab$l_fop = FAB$M_MXV;
  6013.     if ((ofile_lblopts & LBL_NAM) != 0) {
  6014.     fab_ofile.fab$l_fna = ofile_vmsname;
  6015.     fab_ofile.fab$b_fns = strlen(ofile_vmsname);
  6016.     }
  6017.     fab_ofile.fab$l_xab = (char *)&xabdat_ofile;
  6018.     rab_ofile = cc$rms_rab;
  6019.     rab_ofile.rab$l_fab = &fab_ofile;
  6020.     xabdat_ofile = cc$rms_xabdat;
  6021.     xabdat_ofile.xab$l_nxt = (char *)&xabrdt_ofile;
  6022.     xabrdt_ofile = cc$rms_xabrdt;
  6023.     xabrdt_ofile.xab$l_nxt = (char *)&xabfhc_ofile;
  6024.     xabfhc_ofile = cc$rms_xabfhc;
  6025.     xabfhc_ofile.xab$l_nxt = (char *)&xabpro_ofile;
  6026.     xabpro_ofile = cc$rms_xabpro;
  6027.     xabpro_ofile.xab$l_nxt = (char *)&xaball_ofile;
  6028.     xaball_ofile = cc$rms_xaball;
  6029.  
  6030. /* Load 'em up */
  6031.  
  6032.     memmove(&xabpro_ofile.xab$w_pro, filptr, 2);
  6033.     filptr += 2;
  6034.     if ((ofile_lblopts & LBL_OWN) != 0)
  6035.       memmove(&xabpro_ofile.xab$l_uic, filptr, 4);
  6036.     filptr += 4;
  6037.     memmove(&fab_ofile.fab$b_rfm, filptr, 1);
  6038.     filptr += 1;
  6039.     memmove(&fab_ofile.fab$b_org, filptr, 1);
  6040.     filptr += 1;
  6041.     memmove(&fab_ofile.fab$b_rat, filptr, 1);
  6042.     filptr += 1;
  6043.     filptr += 4;            /* reserved */
  6044.     memmove(&fab_ofile.fab$b_fsz, filptr, 1);
  6045.     filptr += 1;
  6046.     memmove(&xabfhc_ofile.xab$w_lrl, filptr, 2);
  6047.     filptr += 2;
  6048.     memmove(&fab_ofile.fab$w_mrs, filptr, 2);
  6049.     filptr += 2;
  6050.     memmove(&xabfhc_ofile.xab$l_ebk, filptr, 4);
  6051.     filptr += 4;
  6052. /* preserve this as RMS won't remember it for us */
  6053.     memmove(&ofile_ffb, filptr, 2);
  6054.     filptr += 2;
  6055.     memmove(&xaball_ofile.xab$l_alq, filptr, 4);
  6056.     filptr += 4;
  6057.     memmove(&xaball_ofile.xab$w_deq, filptr, 2);
  6058.     filptr += 2;
  6059.  
  6060. #ifdef BUGFILL7
  6061. /*
  6062.   When DEC C first came out for the VAX, the xab$b_bkz definition was missing
  6063.   and they used xaballdef$$_fill_7 instead.  But that was a long time ago.
  6064.   (This change made for C-Kermit 6.0, 19 Sep 96.)
  6065. */
  6066. #ifdef COMMENT
  6067.     memmove((char *)&xaball_ofile.xaballdef$$_fill_7, (char *)filptr, 1);
  6068. #else
  6069.     {
  6070.     char * s1, * s2;
  6071.     s1 = (char *)&xaball_ofile.xaballdef$$_fill_7;
  6072.     s2 = (char *)filptr;
  6073.     *s1 = *s2;
  6074.     }
  6075. #endif /* COMMENT */
  6076. #else
  6077.     memmove(&xaball_ofile.xab$b_bkz, filptr, 1);
  6078. #endif /* BUGFILL7 */
  6079.  
  6080.     filptr += 1;
  6081.     memmove(&fab_ofile.fab$w_gbc, filptr, 2);
  6082.     filptr += 2;
  6083.     memmove(&xabfhc_ofile.xab$w_verlimit, filptr, 2);
  6084.     filptr += 2;
  6085.     memmove(&jnlflg, filptr, 1);
  6086.     if (jnlflg !=0)
  6087.       debug(F100,"do_label_recv: journaling status removed for file","",0);
  6088.     filptr += 1;
  6089.     memmove(&xabdat_ofile.xab$q_cdt, filptr, 8);
  6090.     filptr += 8;
  6091.     memmove(&revdat, filptr, 8);
  6092.     filptr += 8;
  6093.     memmove(&revnum, filptr, 2);
  6094.     filptr += 2;
  6095.     memmove(&xabdat_ofile.xab$q_edt, filptr, 8);
  6096.     filptr += 8;
  6097.     if ((ofile_lblopts & LBL_BCK) != 0)
  6098.     memmove(&xabdat_ofile.xab$q_bdt, filptr, 8);
  6099.     filptr += 8;
  6100.  
  6101. /* ACL's? */
  6102.  
  6103.     if ((ofile_lblopts & LBL_ACL) != 0 && gotacl != 0) {
  6104.     xabpro_ofile.xab$l_aclbuf = (char *)&ofile_vmsacl;
  6105.     xabpro_ofile.xab$w_aclsiz = ofile_acllen;
  6106.     }
  6107.  
  6108. /* Try to create the file */
  6109.  
  6110.     rms_sts = sys$create(&fab_ofile);
  6111.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  6112.     if (!(rms_sts & 1)) {
  6113.     debug(F101,"do_label_recv: $create failed, status","",rms_sts);
  6114.     return(-1);
  6115.     }
  6116.     if ((ofile_lblopts & LBL_ACL) != 0 && gotacl != 0) {
  6117.     if (!(xabpro_ofile.xab$l_aclsts & 1)) {
  6118.         debug(F101,"do_label_recv: ACL chain failed, status",
  6119.           "",
  6120.           xabpro_ofile.xab$l_aclsts
  6121.           );
  6122.         return(-1);
  6123.     }
  6124.     }
  6125.     rms_sts = sys$connect(&rab_ofile);
  6126.     if (!(rms_sts & 1)) vms_lasterr = rms_sts;
  6127.     if (!(rms_sts & 1)) {
  6128.     debug(F101,"do_label_recv: $connect failed, status","",rms_sts);
  6129.     return(-1);
  6130.     }
  6131. /*
  6132.   Slide the remainder of the data to the head of the buffer and adjust the
  6133.   counter and pointer. This will cause the buffer to be re-filled to the full
  6134.   32Kb capacity, which is necessary for proper operation of zoutdump().
  6135. */
  6136.     zoutcnt -= ((char *)recv_ptr - (char *)zoutbuffer);
  6137.     debug(F101,"do_label_recv: zoutcnt","",zoutcnt);
  6138.     memcpy(zoutbuffer, recv_ptr, zoutcnt);
  6139.     zoutptr = zoutbuffer + zoutcnt;
  6140.     return(1);                /* Go fill some more */
  6141. }
  6142.  
  6143. static char xxvmsmsg[PMSG_MSG_SIZE];
  6144.  
  6145. char *
  6146. ckvmserrstr(x) unsigned long x; {
  6147.     long int n = 0;
  6148.     struct dsc$descriptor_s b = {
  6149.     PMSG_BUF_SIZE - 1,
  6150.     DSC$K_DTYPE_T,
  6151.     DSC$K_CLASS_S,
  6152.     NULL
  6153.     };
  6154.     if (x < 1) x = vms_lasterr;
  6155.     b.dsc$a_pointer = (char *)&xxvmsmsg;
  6156.     if (!((vms_status = sys$getmsg(x, &n, &b, 0, 0)) & 1))
  6157.       return("%CKERMIT-W-UNKERR, Unknown error");
  6158.     xxvmsmsg[n] = '\0';
  6159.     return((char *)xxvmsmsg);
  6160. }
  6161.  
  6162. /* End of CKVFIO.C */
  6163.