home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv200.zip / ckvfio.c < prev    next >
C/C++ Source or Header  |  2001-06-27  |  194KB  |  5,977 lines

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