home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv192.zip / ckvfio.c < prev    next >
C/C++ Source or Header  |  1996-12-28  |  119KB  |  3,730 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, 6.0.130, 6 Sep 96";
  21.  
  22. char *ckzsys = CKVFIO_OS_ARCH_STRING;
  23.  
  24. /* lt. 1992-10-08 End
  25.  */
  26.  
  27. /* C K V F I O  --  Kermit file system support for VAX/VMS.  */
  28.  
  29. /*
  30.   Author: Frank da Cruz <fdc@columbia.edu>
  31.   Columbia University Academic Information Systems, New York City.
  32.  
  33.   Copyright (C) 1985, 1996, Trustees of Columbia University in the City of New
  34.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  35.   sold for profit as a software product itself, nor may it be included in or
  36.   distributed with commercial products or otherwise distributed by commercial
  37.   concerns to their clients or customers without written permission of the
  38.   Office of Kermit Development and Distribution, Columbia University.  This
  39.   copyright notice must not be removed, altered, or obscured.
  40. */
  41.  
  42. /*
  43.   Originally adapted to VMS by:
  44.   Stew Rubenstein, Harvard University Chemical Labs, 1985,
  45.   Contributors:
  46.   Frank da Cruz (fdc), Columbia University Center, New York, NY (1985-present)
  47.   Stew Rubenstein, Harvard University Chemical Labs, Cambridge, MA (1985)
  48.   Martin Minow (MM), Digital Equipment Corporation, Maynard MA (1985)
  49.   Dan Schullman (DS), Digital Equipment Corporation, Maynard MA (1985)
  50.   Mark Buda (MAB), Digital Equipment Corporation, Nashua, NH (1989-90)
  51.   Terry Kennedy (TMK), St. Peter's College, Jersey City, NJ (1990-present)
  52.   William Bader (WB), Lehigh University, Bethlehem, PA (1990-93)
  53.   Gary Mussar (GM), Bell-Northern Research, Ottawa, Canada (1991)
  54.   James Sturdevant (JS) (1992)
  55.   Tarjei T. Jensen (ttj), Norwegian Hydrographic Service (1993-94)
  56.   Mark Berryman (mb), SAIC (1994)
  57.   Martin PJ Zinzer (mpjz), Gesellschaft fuer Schwerionenforschung GSI Darmstadt
  58. */
  59. /* Edit history
  60.  * 003 20-Mar-85 MM  fixed fprintf bug in zsout.c
  61.  * 004 21-Mar-84 MM  create text files in variable-stream.
  62.  * 005  8-May-85 MM  filled in zkself (not tested), fixed other minor bugs
  63.  * 006  5-Jul-85 DS  handle version number in zltor, zrtol
  64.  * 007 11-Jul-85 fdc fix zclose() to give return codes
  65.  * 008 19-Mar-86 fdc Fix system() for "!", zopeni() for REMOTE commands.
  66.  * 008 17-Sep-87 fdc Define PWDCMD.
  67.  * 009 (???)
  68.  * 010 24-Jan-88 fdc Add zgtdir() function, even tho it doesn't work...
  69.  * 011 14-Feb-89 mab Make zgtdir() work in V2/V3 C envirements,
  70.  *             Make zkself work using delprc() using Will Wood's changes.
  71.  * 012 26-Feb-89 mab Add function that searches for kermit.ini file in various
  72.  *                   ways
  73.  * 013 05-Mar-89 mab Add Barry Archers enhancements/fixes.
  74.  * 014 15-Mar-89 mab Check for non-null data, not array of pointers in
  75.  *                   zkermini
  76.  * 015 04-Apr-89 mab Add latent support for attribute packet.  Clean up
  77.  *             file name translation code.
  78.  * 016 05-Apr-89 mab Add PWP code to optimize packetizing.
  79.  * 017 16-Apr-89 mab PWP changes broke REMOTE command.  Fixed.
  80.  * 018 18-Apr-89 mab #ifdef chkfn.  This removes a lot of overhead.
  81.  *             Add code to gtdir() for V4.x.
  82.  * 019 12-Jun-89 mab Add PWP's encode logic
  83.  * 020 09-Jul-89 mab Add logic to check for system() availability
  84.  * 021 10-Jul-89 mab Fix SHOW USER USERNAME.  Added space after 'SHOW USER'.
  85.  * 022 27-Sep-89 mab Added zmail/zprint, plus added changes from CKUFIO.C
  86.  * 023 01-Dec-89 mab Add RMS file support
  87.  * 024 20-Jul-90 wb  Add support for old VAX C & VMS versions + zstrip & rename
  88.  * 025 29-Jul-90 tmk Change space command to show avail, not used (match spec)
  89.  * 026 29-Jul-90 tmk Hack out the RMS stuff - it can come back when it works
  90.  * 027 29-Jul-90 tmk Likewise the VMS V3 stuff - ancient history
  91.  * 028 29-Jul-90 tmk Replace the attribute stuff. It now works.
  92.  * 029 31-Jul-90 tmk Fix CWD command (via hack)
  93.  * 030 31-Jul-90 tmk Fix assorted bugs preventing remote commands from working
  94.  * 031 31-Jul-90 tmk Correctly handle interrupted remote commands
  95.  * 032 04-Aug-90 tmk Start work on full RMS support for input files
  96.  * 033 04-Aug-90 tmk Tack LF on end of subprocess output lines
  97.  * 034 04-Aug-90 tmk Complete work on full RMS support for input files
  98.  * 035 04-Aug-90 tmk Add support for Fortran CC, fill in recfm data
  99.  * 036 05-Aug-90 tmk Add trailing CRLF on print format files
  100.  * 037 12-Aug-90 tmk Start work on full RMS support for output files
  101.  * 038 12-Aug-90 tmk Honor first free byte (FFB) on SENDs
  102.  * 039 13-Aug-90 tmk Finished first cut of full RMS support for output files
  103.  * 040 29-Sep-90 tmk Add iswild() from FDC for edit 157
  104.  * 041 06-Oct-90 tmk Add filetype IMAGE support for outbound transfers. Note
  105.  *             that this doesn't currently work as the receiver overrides
  106.  *             it (must talk to fdc).
  107.  * 042 06-Oct-90 tmk Make logfiles MRS=80. Being able to edit them outweighs
  108.  *             any use for un-split lines.
  109.  * 043 17-Oct-90 wb  Make zclosf() remove delete mailboxes & deassign channels
  110.  *                   used to talk to the subprocess, so quotas are not used
  111.  *                   up after repeated mailbox use (installed by fdc).
  112.  * 044 19-Oct-90 fdc Changed zxcmd() to use the fp[] arrays in the normal way,
  113.  *                   and zsyscmd to call zxcmd(ZIFILE) rather than
  114.  *                   zxcmd(ZSYSFN).  Got rid of all calls to system(), used
  115.  *                   zsyscmd() instead, so commands like DIR could be
  116.  *                   interrupted.  Made zoutdump() return(-1) rather than
  117.  *                   exit() when "line too long for buffer", and increased
  118.  *                   line output buffer from 1K to 4K.
  119.  * 045 01-Nov-90 tmk Corrected behavior of error check on $create call so a
  120.  *             file supersede would work properly.
  121.  * 046 01-Nov-90 tmk Clone binary flag to ofile_bmode so we have a consistent
  122.  *             view of this flag during file operations - the binary flag
  123.  *             tends to toggle when we don't want/expect it to.
  124.  * 047 01-Nov-90 tmk Make IMAGE mode work. Note that image mode is only used
  125.  *             when VMS is sending a file, and includes all record
  126.  *             control characters not normally sent. Only useful in
  127.  *             unusual circumstances.
  128.  * 048 01-Nov-90 tmk Remove spurious \n from zsoutl() which caused debug logs
  129.  *             to have spurious <CR>'s when viewed with editors.
  130.  * 049 02-Nov-90 fdc Adapt to dynamic allocation of file i/o buffers.  Changes
  131.  *                   are within #ifdef DYNAMIC..#else..#endif brackets.
  132.  * 050 02-Nov-90 fdc Make zsyscmd() close inferior process.
  133.  * 051 ??-???-?? ??? Add ckermit_init logical, return 0 on wildcard operations.
  134.  * 052 24-Dec-90 tmk Fix performance problems after 32Kb w/ ASCII receives, fix
  135.              2-nulls-per-32Kb in binary mode bug (actually in ckcker.h,
  136.              this is a placeholder).
  137.  * 053 13-Jan-91 tmk Add support for SET FILE RECORD-LENGTH.
  138.  * 054 14-Jan-91 tmk Fix cases of /x/CR/LF/y/ and /x/CR/LF/y/CR/LF/ in ASCII
  139.  *             file receives.
  140.  * 055 16-Jan-91 tmk Log requested file type to debug log when receiving.
  141.  * 056 16-Jan-91 tmk Add support for all zstime() functions.
  142.  * 057 17-Jan-91 tmk Add support for zchkspa() function.
  143.  * 058 17-Jan-91 tmk Move debug() call into if clause in zxpand, per fdc.
  144.  * 059 18-Jan-91 tmk Support remote (DECnet) file accesses.
  145.  * 060 18-Jan-91 tmk Fix READ command.
  146.  * 061 30-Jan-91 tmk Support creation of UNDEFINED file types for brain-dead
  147.  *             BASIC implementation.
  148.  * 062 30-Jan-91 tmk Fix REMOTE commands when VERIFY is set.
  149.  * 063 29-Mar-91 tmk Add padding factor for received text files to accomodate
  150.  *             space taken up by record delimiters (per fdc).
  151.  * 064 29-Mar-91 gm  Remove unnecessary mem-mem moves during ASCII receives.
  152.  *             (Installed by tmk. To back out, #define OLD_WAY).
  153.  * 065 30-Mar-91 tmk First pass at implementing LABELED. Send only, dummy
  154.  *             data records.
  155.  * 066 02-Apr-91 tmk Finish first pass at LABELED. Send VMS filename, attri-
  156.  *             butes. Still need ACL's, "hidden" char. longword, recep-
  157.  *             tion.
  158.  * 067 09-Apr-91 tmk LABELED bugfixes - VMSFILE is 70 bytes, not 74, use the
  159.  *             xab$w_lrl field instead of rab$w_rsz, fab$w_deq instead
  160.  *             of xab$w_rsz, fab$b_bks instead of xab$b_bkz, always pro-
  161.  *             cess an even multiple of 512 bytes when LABELED.
  162.  * 068 14-Apr-91 tmk Don't use C definition of fab$b_journal as it doesn't ex-
  163.  *             ist before C V3.1. Compute it ourselves instead.
  164.  * 069 15-Apr-91 tmk Initial work on retrieving ACL information for LABELED.
  165.  * 070 16-Apr-91 tmk Make edits 066-069 compatible with DECnet.
  166.  * 071 21-May-91 tmk Address R. Weiner QAR item 2 (filesize).
  167.  * 072 21-Jun-91 tmk Check (and prohibit) spawns from captive accounts.
  168.  * 073 21-Jun-91 tmk Fix session logging (for Charlie Luce/DECUServe).
  169.  * 074 21-Jun-91 tmk Rework 071 to only apply to SPAWN/PUSH and not to the
  170.  *             pseudo-builtins like DEL, SPACE, WHO, PWD, etc.
  171.  * 075 21-Jun-91 tmk Fix possible endless loop when flushing output file in
  172.  *             zclosf() after zoutdump() error.
  173.  * 076 21-Jun-91 tmk First pass on handling inbound LABELED files.
  174.  * 077 14-Nov-91 tmk Fix zprint(), zmail() (need to use system() for these).
  175.  *                   This is a partial backout of 044.
  176.  * 078 14-Nov-91 tmk Various cleanups.  Delete files after successful mailing
  177.  *             or printing, remove dead code inside #ifdef COMMENT and
  178.  *                   #ifdef OLD_WAY, fix typo in spawning message, make sure
  179.  *                   all source lines < 80 chars.
  180.  * 079 22-Nov-91 fdc Change zmail(), zprint() error return values to improve
  181.  *             error reporting.
  182.  * 080 18-Jan-92 tmk Fix REMOTE so output from a remote command correctly dis-
  183.  *             plays on terminal. This has been broken since 040 or so.
  184.  * 081 10-Jun-92 tmk Add William Bader's fix for fixed-length files which have
  185.  *                   record attributes.
  186.  * 082 03-Jul-92 tmk Fix really bad bug introduced in 081 (which made *all*
  187.  *                   fixed-format files be sent as text).
  188.  * 083 15-Jul-92 jah Fix fencepost error in zoutdump when line breaks at 32K.
  189.  * 084 03-Aug-92 fdc Remove current directory from init file search.
  190.  * 085 26-Aug-92 tmk Add Bernd Onasch's fix for fgen().
  191.  * 086 28-Aug-92 tmk Fix bug reported by Bill Hoelzer where C-K would execute
  192.  *             a file named "." as a C-K initialization file.
  193.  * 087 04-Sep-92 tmk Fix bug reported by Chuck McMichael where C-K would not
  194.  *             set the FFB properly when receiving a labeled file which
  195.  *             did not have the FFB on a record boundary.
  196.  * 088 09-Sep-92 tmk Fix Hunter Goatley's problem with SPAWN command ignoring
  197.  *             Ctrl-C.
  198.  * 089 11-Sep-92  js Fixed malloc() in zmail().
  199.  * 090 28-Oct-92 tmk Fix null-byte error introduced by 087. Gee, this looked
  200.  *             so simple when I designed it.
  201.  * 091 02-Nov-92 tmk Start work on fixing spawn/push/remote commands, due to
  202.  *             popular whining.
  203.  * 092 03-Nov-92 tmk Finish up initial 091 work. Vote for Kermit!
  204.  * 093 03-Nov-92 fdc Change zkermini() to work with "-y" command-line option.
  205.  * 094 04-Nov-92 tmk Make zxpand() not return all files if given null string.
  206.  * 095 05-Nov-92 fdc Make zxcmd(), zclose(), etc, handle ZRFILE (OPEN !READ).
  207.  * 096 17-Feb-93 fdc prevent zopeno from calling zstime if date struct is NULL,
  208.  *                   and add support for ZMFILE (misc output file).
  209.  * 097 08-Apr-93 tmk Correctly handle "international VMS" which uses <> instead
  210.  *             of [] for directory delimiters.
  211.  * 098 16-May-93 fdc ANSIfication for GNU CC, from James Sturdevant, plus
  212.  *                   add FAB$M_PRN to list of text-file types, for VMS batch
  213.  *                   logs.
  214.  * 099 07-Jun-93 fdc Fix calculation of file size in zchki(), fix declaration
  215.  *                   of mbxnam[] (add one to size) to prevent overflow, which
  216.  *                   would result in failure of server to respond to REMOTE
  217.  *                   directory, etc.  Both fixes from Bill Glass.
  218.  * 100 21-Jun-93 fdc file_date[] and attr_date[] declarations in zstime()
  219.  *                   changed from long to unsigned long to prevent signed date
  220.  *                   comparisons, which could prevent SET FILE COLLISION
  221.  *                   UPDATE from working.  From James Sturdevant.
  222.  * 101  8-Aug-93 fdc Add types to all function declarations.
  223.  * 102 18-Aug-93 ttj Minor updates in zsattr() and do_label_recv() mainly to
  224.  *                   quieten the compiler (which had every reason to complain).
  225.  * 103  5-Nov-93 wb   Add isdir() function.
  226.  * 104  8-Nov-93 wb   Add zfcdate() function.
  227.  * 105 25-Nov-93 fdc  Correct record format for session log; change name of
  228.  *                    password structure member of zattr struct.
  229.  * 106 22-Dec-93 tmk  Correct fd "leakage" in OPEN READ/CLOSE READ pairs.
  230.  * 107 26-Feb-94 mb   Addition of zmkdir() routine.
  231.  * 108 27-Mar-94 tmk  Increase max record size for logs from 80 to 254.
  232.  *                    Add support for file append operations.
  233.  *                    Make zkself() retry a few times to avoid zombies on BYE.
  234.  * 109  5-Apr-94 tmk  Fix xx->lengthk not being set in zsattr().
  235.  * 110  8-Jun-94 tmk  Use private fab in zchki (fixes OPEN READ bug).
  236.  * 111 17-Jun-94 tmk  Let zsattr() work even if there is no rights database.
  237.  * 112  7-Jul-94 js   A couple small ANSIfications for gcc.
  238.  * 113  7-Aug-94 fdc  Make zshcmd()/zsyscmd() return proper return code, with
  239.  *                    help from Larry Henry at TWG.  Still not quite right...
  240.  * 114 29-Sep-94 fdc  Increase max wildcard matches from 500 to 4096.
  241.  * 115  4-Oct-94 mb   Add support for RESEND.
  242.  * 116 26-Oct-94 mb   Minor fix to 115.
  243.  * 117  1-Nov-94 wb   A couple #ifdefs added to allow compilation in VMS v4.
  244.  * 118 25-Feb-95 mpjz Fix for DECC on VAX.
  245.  * 119 11-May-96 fdc  Change znewn() to change version to x+1, or add ;0.
  246.  * 120 31-May-96 fdc  Fix zfnqfp() to remove trailing .; if directory name.
  247.  * 121 12-Jun-96 fdc  Prevent dereferencing null pointer in do_label_recv().
  248.  * 122 23-Jun-96 fdc  Fix a bug in do_label_recv that I introduced in edit 121.
  249.  * 123 21-Aug-96 fdc  Separate ZRFILE from ZIFILE so now we can have both.
  250.  * 124 21-Aug-96 fdc  Fix includes for getenv & strcpy for DECC vs VAXC.
  251.  * 125 25-Aug-96 mpjz More DECC/VAX fixes.
  252.  * 126 05-Sep-96 fdc  Remove #module, remove refs to xaballdef$$_fill_7.
  253.  * 127 06-Sep-96 fdc  Recover from interrupted labeled-mode receives.
  254.  * 128 06-Sep-96 fdc  Recover from the previous edit.
  255.  * 129 06-Sep-96 fdc  Fix zchki() to return -2 if blah.DIR;n is a directory.
  256.  * 130 06-Sep-96 fdc  Fixes to isdir(), zstrip(), zchdir(). 
  257.  */
  258.  
  259. /* Definitions of some VMS system commands */
  260.  
  261. char *DIRCMD = "directory ";        /* For directory listing */
  262. char *DIRCM2 = "directory ";        /* For directory listing, no args */
  263. char *DELCMD = "delete ";        /* For file deletion */
  264. char *TYPCMD = "type ";            /* For typing a file */
  265. char *SPACMD = "show quota ";         /* Space/quota of current directory */
  266. char *SPACM2 = "show quota ";         /* Space/quota of specified dir */
  267. char *WHOCMD = "show users ";        /* For seeing who's logged in */
  268. char *PWDCMD = "show default ";        /* For seeing current directory */
  269.  
  270. /*
  271.   Functions (n is one of the predefined file numbers from ckermi.h):
  272.  
  273.    zopeni(n,name)   -- Opens an existing file for input.
  274.    zopeno(n,name)   -- Opens a new file for output.
  275.    zclose(n)        -- Closes a file.
  276.    zchin(n)         -- Gets the next character from an input file.
  277.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  278.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  279.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  280.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  281.    zchki(name)      -- Check if named file exists and is readable, return size.
  282.    zchko(name)      -- Check if named file can be created.
  283.    zchkspa(name,n)  -- Check if n bytes available to create new file, name.
  284.    znewn(name,s)    -- Make a new unique file name based on the given name.
  285.    zdelet(name)     -- Delete the named file.
  286.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  287.    znext(string)    -- Returns the next file from the list in "string".
  288.    zxcmd(n,cmd)     -- Execute the command in a lower fork on file number n.
  289.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  290.    zrtol(n1,n2)     -- Convert remote filename into local form.
  291.    zltor(n1,n2)     -- Convert local filename into remote form.
  292.    zchdir(dirnam)   -- Change working directory.
  293.    zhome()          -- Return pointer to home directory name string.
  294.    zkself()         -- Log self out
  295.    zsattr(struc zattr *) -- Return attributes for file which is being sent.
  296.    zkermini(n1,n2)  -- Find kermit.ini using default scanning process
  297.  */
  298.  
  299. /* Includes */
  300.  
  301. #define CKVFIO_C
  302. #include "ckcdeb.h"
  303. #include "ckcasc.h"
  304. #include "ckcker.h"
  305. #include "ckvvms.h"
  306. #include <stdio.h>
  307. #include <types.h>
  308. #include <stat.h>
  309. #include <ctype.h>
  310. #include <time.h>
  311. #include <errno.h>
  312. #include <rms.h>
  313. #include <ssdef.h>
  314. #include <descrip.h>
  315. #include <dvidef.h>
  316. #include <dcdef.h>
  317. #include <iodef.h>
  318. #include <jpidef.h>
  319. #include <signal.h>
  320. #include <string.h>
  321. #include <syidef.h>
  322. #ifndef OLD_VMS
  323. #include <uaidef.h>
  324. #endif  /* OLD_VMS */
  325.  
  326. /* rms.h above includes nam, fab, xab and rmsdef */
  327. /* vms v4 headers do not check against multiple inclusion */
  328. /* ifndefs below prevent multiple declaration of FAB and NAM structs */
  329.  
  330. #ifndef FAB$C_BID
  331. #include <fab.h>            /* These are needed for isdir() */
  332. #endif /* FAB$C_BID */
  333.  
  334. #ifndef NAM$C_BID
  335. #include <nam.h>
  336. #endif /* NAM$C_BID */
  337.  
  338. #include <lnmdef.h>
  339. #include <rmsdef.h>
  340.  
  341. #define MAXWLD 4096            /* Maximum wildcard filenames */
  342.  
  343. /* external def. of things used in buffered file input and output */
  344.  
  345. #ifdef DYNAMIC
  346. extern CHAR *zinbuffer, *zoutbuffer;
  347. #else
  348. extern CHAR zinbuffer[], zoutbuffer[];
  349. #endif /* DYNAMIC */
  350.  
  351. static CHAR rinbuffer[INBUFSIZE], *rinptr;
  352. static int rincnt;
  353.  
  354. extern CHAR *zinptr, *zoutptr;
  355. extern int zincnt, zoutcnt;
  356. extern int binary;
  357. extern int frecl;
  358. extern int rcflag;
  359.  
  360. extern long vernum;
  361.  
  362. /* Declarations */
  363.  
  364. FILE *fp[ZNFILS] = {             /* File pointers */
  365.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  366. };
  367.  
  368. static long iflen = -1;            /* Input file length */
  369. static long rflen = -1;
  370. static long oflen = -1;            /* Output file length */
  371. static int fcount;            /* Number of files in wild group */
  372. static char nambuf[255];        /* maximum size of a file spec */
  373. static char cwdbuf[NAM$C_MAXRSS];
  374. static struct iosb_struct tmpiosb;    /* For QIOW */
  375.  
  376. extern unsigned long vms_status;    /* Used by CHECK_ERR */
  377.  
  378. static int cflag;            /* Flag indicating console in use */
  379.  
  380. int check_spawn(void);
  381. int do_label_recv(void);
  382. int do_label_send(char *name);
  383. static int parse_fname(char *cp, int cp_len, char *defnam, int flag);
  384. int fgen(char *pat, char *resarry[], int len);
  385. static int rinfill(void);
  386.  
  387. #ifdef COMMENT
  388. #ifdef __DECC
  389. #include <stdlib.h>
  390. #else
  391. char *getenv(), *strcpy();
  392. #endif /* __DEC */
  393. #else
  394. /* Recommended by Lee Tibbert */
  395. #ifdef __DECC
  396. #include <stdlib.h>
  397. #include <string.h>
  398. #else
  399. char *getenv(), *strcpy();
  400. #endif /* __DECC */
  401. #endif /* COMMENT */
  402.  
  403. #ifdef __DECC
  404. #include <lib$routines.h>
  405. #include <starlet.h>
  406. #endif
  407.  
  408. /* static */                /* Not static any more! */
  409. char *mtchs[MAXWLD],            /* Matches found for filename */
  410.   **mtchptr;                /* Pointer to current match */
  411.  
  412. static unsigned short mbx_chan;        /* Mailbox chan for REMOTE commands */
  413. static int subprocess_input = 0, sub_count;
  414. static char *sub_ptr, sub_buf[SUB_BUF_SIZE];
  415.  
  416. #define    SUPERSAFE            /* For safe subprocesses */
  417. static unsigned long sub_pid;
  418.  
  419. /*
  420.  * Structures for input (SEND) file
  421.  */
  422.  
  423. static    struct FAB fab_ifile;        /* For SEND file */
  424. static    struct RAB rab_ifile;
  425. static    struct XABDAT xabdat_ifile;
  426. static    struct XABFHC xabfhc_ifile;
  427. static    struct XABPRO xabpro_ifile;
  428. static    struct XABALL xaball_ifile;
  429.  
  430. static    int ifile_bmode;        /* For SEND file */
  431. static    int ifile_bcount;
  432. static    char aclbuf[512];
  433. static    unsigned long xuchar = 0;
  434.  
  435. static    struct FAB fab_rfile;        /* For OPEN READ file */
  436. static    struct RAB rab_rfile;
  437. static    struct XABDAT xabdat_rfile;
  438. static    struct XABFHC xabfhc_rfile;
  439. static    struct XABPRO xabpro_rfile;
  440. static    struct XABALL xaball_rfile;
  441.  
  442. static    int rfile_bmode;        /* For READ file */
  443. static    int rfile_bcount;
  444. static    char raclbuf[512];
  445.  
  446. /*
  447.  * Structures for output (RECEIVE) file
  448.  */
  449.  
  450. static    struct FAB fab_ofile;
  451. static    struct RAB rab_ofile;
  452. static    struct XABDAT xabdat_ofile;
  453. static    struct XABFHC xabfhc_ofile;
  454. static    struct XABPRO xabpro_ofile;
  455. static    struct XABALL xaball_ofile;
  456. static    struct XABRDT xabrdt_ofile;
  457. static    int ofile_dump;
  458. static    int ofile_bmode;
  459. static    int ofile_lblopts;
  460. static    int ofile_lblproc = 0;
  461. static    char revdat[8];
  462. static    unsigned short revnum;
  463.  
  464. static    char ofile_vmsname[CKMAXPATH+1];
  465. static    char ofile_vmsacl[512];
  466. static    int ofile_acllen;
  467. static    short ofile_ffb;
  468.  
  469. /*
  470.  * Common RMS items
  471.  */
  472. static    unsigned long int rms_sts;
  473.  
  474. /*  I S W I L D  --  Tells whether filespec "str" is wild  */
  475. /*  Returns 0 if not wild, 1 if wild */
  476.  
  477. int
  478. iswild(str) char *str; {
  479.     char c;
  480.     while ((c = *str++) != '\0')
  481.       if (c == '*' || c == '%') return(1);
  482.     return(0);
  483. }
  484.  
  485. #ifdef CK_TMPDIR
  486.  
  487. /*  I S D I R  --  Tells if string pointer s is the name of a directory. */
  488. /*  Returns 1 if directory, 0 if not a directory. */
  489. /*  Contributed by William Bader. */
  490.  
  491. int
  492. isdir(s) char *s; {
  493.     int x;
  494.     int i, s_len;
  495.     char *full_name;
  496.     char *p = NULL;
  497.  
  498.     static char dot_dir[] = ".dir";
  499.     static char zero_dir[] = "[000000]";
  500.  
  501.     char name_buf[255];            /* Was 512 but that's too big */
  502.     char tmpbuf[255];            /* for unsigned char... */
  503.  
  504.     struct FAB fab;
  505.     struct NAM nam;
  506.  
  507.     struct dsc$descriptor_s indesc;
  508.     $DESCRIPTOR(lnmtable, "LNM$FILE_DEV");
  509.     int new_len;
  510.     int serial_num;
  511.     struct {
  512.     short length;
  513.     short code;
  514.     char *address;
  515.     int *len;
  516.     int term;
  517.     } itemlist;
  518.  
  519.     if (!s) return(0);
  520.     if (!*s) return(0);
  521.  
  522.     /* Determine if this is something we can SET DEFAULT to...  */
  523.  
  524.     s_len = strlen(s);
  525.     debug(F111,"isdir",s,s_len);
  526. /*
  527.   But what about "SYS$DEVICE:[FOO]BLAH.DIR;1"?  We can test for that too
  528.   (it's easy enough -- just use stat() as in zchki() -- and say yes, it is
  529.   a directory, but that does not mean we can SET DEFAULT to it, which is 
  530.   the main intention of this routine.  We would have to change the string 
  531.   into (e.g.) "SYS$DEVICE:[FOO.BLAH]"...
  532. */
  533.     /* Does it look like a directory name? */
  534.  
  535.     if (s[s_len-1] != ':' && s[s_len-1] != ']' && s[s_len-1] != '>') {
  536. #ifdef COMMENT
  537. /*
  538.   Bad idea.  No, the user really has to include the colon, otherwise the
  539.   intention is ambiguous.
  540. */
  541.     p = tmpbuf;            /* No, maybe it's a logical name */
  542.     strncpy(tmpbuf,getenv(s),254);
  543.     if (*p) {
  544.         s = p;
  545.         s_len = strlen(s);
  546.         if (s < 1)            /* No definition */
  547.           return(0);
  548.  
  549.         /* It is a logical name, but does it look like a dir name? */
  550.         if (s[s_len-1] != ':' && s[s_len-1] != ']' && s[s_len-1] != '>')
  551.           return(0);        /* No, it doesn't */
  552.     }
  553. #else
  554.     return(0);
  555. #endif /*  */
  556.     }
  557.     /* Check that the directory part is valid... */
  558.  
  559.     if (s[s_len-1] == ']' || s[s_len-1] == '>') {
  560.         fab = cc$rms_fab;
  561.         nam = cc$rms_nam;
  562.  
  563.         fab.fab$l_dna = 0;
  564.         fab.fab$b_dns = 0;
  565.  
  566.         fab.fab$l_fna = s;
  567.         fab.fab$b_fns = s_len;
  568.  
  569.         fab.fab$l_fop = 0;
  570.         fab.fab$w_ifi = 0;
  571.         fab.fab$l_nam = &nam;
  572.  
  573.         nam.nam$l_esa = name_buf;
  574.         nam.nam$b_ess = sizeof(name_buf);
  575.         nam.nam$b_nop = 0;
  576.         nam.nam$l_rlf = 0;
  577.         nam.nam$l_rsa = 0;
  578.         nam.nam$b_rsl = 0;
  579.         nam.nam$l_fnb = 0;
  580.  
  581.         i = sys$parse(&fab, 0, 0);
  582. #ifdef COMMENT
  583.     printf("parse returned %d 0x%x, nam fnb is %d 0x%x\n",
  584.            i, i, nam.nam$l_fnb, nam.nam$l_fnb);
  585. #endif /* COMMENT */
  586.         if ((i & 1) == 0) return(0);
  587.     }
  588.  
  589.     /* Check that the logical name is valid */
  590.  
  591.     i = s_len - 1;
  592.     while (i >= 0 && s[i] != ':') i--;
  593.  
  594.     if (i >= 0 && s[i] == ':') {
  595.         if (i == 0) return(0);        /* Single colon (:) */
  596.         if (s[i-1] == ':') {
  597.             if (i > 1) return(1);    /* DECnet node name (blah::) */
  598.             else return(0);        /* or :: alone. */
  599.         }
  600.     s_len = i;
  601.     full_name = malloc(s_len + 1);
  602.     if (!full_name) return(0);
  603.     /* Logicals must be upper case */
  604.     for (i = 0; i < s_len; i++) {
  605.         full_name[i] = s[i];
  606.         if (full_name[i] >= 'a' && full_name[i] <= 'z')
  607.         full_name[i] -= ('a' - 'A');
  608.     }
  609.     
  610.     indesc.dsc$w_length = s_len;
  611.     indesc.dsc$a_pointer = full_name;
  612.     indesc.dsc$b_class = DSC$K_CLASS_S;
  613.     indesc.dsc$b_dtype = DSC$K_DTYPE_T;
  614.  
  615.     itemlist.length = new_len = sizeof(name_buf);
  616.     itemlist.code = LNM$_STRING;
  617.     itemlist.address = name_buf;
  618.     itemlist.len = &new_len;
  619.     itemlist.term = 0;
  620.  
  621.     i = sys$trnlnm(0, &lnmtable, &indesc, 0, &itemlist);
  622.     if (i != SS$_NORMAL || new_len < 0) new_len = 0;
  623.     if (new_len >= sizeof(name_buf)) new_len = sizeof(name_buf) - 1;
  624.     name_buf[new_len] = '\0';
  625.  
  626. #ifdef COMMENT
  627.     printf("trnlnm result %d 0x%x, '%.*s'\n", i, i, new_len, name_buf);
  628. #endif /* COMMENT */
  629.  
  630.     free(full_name);
  631.  
  632.     if (new_len == 0) {
  633.         /* Could still be a device name. */
  634.         /* Only disks have serial numbers... */
  635.         serial_num = 0;
  636.         itemlist.length = new_len = sizeof(serial_num);
  637.         itemlist.code = DVI$_SERIALNUM;
  638.         itemlist.address = (char *) &serial_num;
  639.         itemlist.len = &new_len;
  640.         itemlist.term = 0;
  641.         i = sys$getdvi(0, 0, &indesc, &itemlist, 0, 0, 0, 0);
  642. #ifdef COMMENT
  643.         printf("getdvi ret %d 0x%x, serial %d len %d\n",
  644.            i, i, serial_num, new_len);
  645. #endif /* COMMENT */
  646.         return(((i & 1) == 1 && new_len > 0) ? 1: 0);
  647.  
  648.     } else if (name_buf[new_len-1] == ':' ||
  649.             name_buf[new_len-1] == ']' ||
  650.             name_buf[new_len-1] == '>') {
  651.         /* Check returned value */
  652.         if (new_len > 2 &&
  653.             (name_buf[new_len-1] == ']' || name_buf[new_len-1] == '>') &&
  654.             name_buf[new_len-2] == '.') {
  655.             /* Remove trailing dot in directory of logical name */
  656.             name_buf[new_len-2] = name_buf[new_len-1];
  657.             name_buf[new_len-1] = '\0';
  658.         }
  659.         return( isdir(name_buf) );
  660.     } else {
  661.         /* Logical name is just a random string signifying nothing */
  662.         return(0);
  663.     }
  664.     }
  665.     return(1);
  666. }
  667. #endif /* CK_TMPDIR */
  668.  
  669. /*  Z K S E L F  --  Log self out.  */
  670.  
  671. int
  672. zkself() {
  673.     int i;
  674.     unsigned long int rms_s;
  675. /*
  676.   We need a better way.  If C-Kermit was spawned, this does not log out the
  677.   whole job.    It also does not hang up LAT terminal sessions.
  678. */
  679.     for (i = 0; i < 10; i++) {
  680.     rms_s = sys$delprc(0,0);    /* Maybe some output is still */
  681.     debug(F101,"zkself rms_s","",rms_s);
  682.     if (rms_s == SS$_NORMAL)    /* queued; try a few times... */
  683.       exit(1);
  684.     sleep(1);
  685.     }
  686.     exit(rms_s == SS$_NORMAL);
  687.     return(0); /* dummy - required as this is called in a non-void context */
  688. }
  689.  
  690. /*  Z O P E N I  --  Open an existing file for input. */
  691.  
  692. int
  693. zopeni(n,name) int n; char *name; {
  694.     debug(F111," zopeni",name,n);
  695.     debug(F101,"  fp","",(int) fp[n]);
  696.     if (chkfn(n)) return(0);
  697.     if (n == ZSYSFN) {            /* Input from a system function? */
  698.     debug(F110," zopeni called with ZSYSFN, failing!",name,0);
  699.     *nambuf = '\0';            /* No filename. */
  700.     return(0);            /* fail. */
  701.     }
  702.     zincnt = 0;                /* Initializing these couldn't hurt */
  703.     zinptr = zinbuffer;
  704.     if (n == ZSTDIO) {            /* Standard input? */
  705.     if (isatty(0)) {
  706.         ermsg("?Terminal input not allowed\n");
  707.         debug(F110," zopeni attempted input from unredirected stdin","",0);
  708.         return(0);
  709.     }
  710.     fp[ZIFILE] = stdin;
  711.     return(1);
  712.     }
  713. /*
  714.  * We open the file but waffle on the access mode we're going to use. We then
  715.  * inspect the file characteristics to see if the organization is fixed or un-
  716.  * defined. If it is, we convert to block mode operation. This is needed since
  717.  * VMS maintains a "first free byte" field to tell us how much of the last rec-
  718.  * ord really contains data, but won't terminate reads at that point. Thus, if
  719.  * we want to SEND the exact same file we RECEIVEd, we have to honor the FFB
  720.  * internally.
  721.  */
  722.     if (n == ZIFILE) {
  723.     ifile_bmode = 0;
  724.     ifile_bcount = 0;
  725.     fab_ifile = cc$rms_fab;
  726.     fab_ifile.fab$b_fac = FAB$M_BRO | FAB$M_GET;
  727. /*
  728.  * Some non-VMS DECnet implementations don't allow switching modes, so set
  729.  * block-I/O only mode if the user said SET FILE TYPE IMAGE or LABELED.
  730.  */
  731.     if (binary == XYFT_I || binary == XYFT_L)
  732.       fab_ifile.fab$b_fac = FAB$M_BIO | FAB$M_GET;
  733.     fab_ifile.fab$l_fna = name;
  734.     fab_ifile.fab$b_fns = strlen(name);
  735.     fab_ifile.fab$l_xab = (char *)&xabdat_ifile;
  736.     rab_ifile = cc$rms_rab;
  737.     rab_ifile.rab$l_fab = &fab_ifile;
  738.     xabdat_ifile = cc$rms_xabdat;
  739.     xabdat_ifile.xab$l_nxt = (char *)&xabfhc_ifile;
  740.     xabfhc_ifile = cc$rms_xabfhc;
  741.     xabfhc_ifile.xab$l_nxt = (char *)&xabpro_ifile;
  742.     xabpro_ifile = cc$rms_xabpro;
  743.     memset(&aclbuf, 0, sizeof(aclbuf));
  744.     xabpro_ifile.xab$l_aclsts = SS$_NORMAL;        /* Oh Joy! DECnet */
  745.     xabpro_ifile.xab$l_aclbuf = (char *)&aclbuf;
  746.     xabpro_ifile.xab$w_aclsiz = sizeof(aclbuf);
  747.     xabpro_ifile.xab$l_aclctx = 0;
  748.     xabpro_ifile.xab$l_nxt = (char *)&xaball_ifile;
  749.     xaball_ifile = cc$rms_xaball;
  750.  
  751.     rms_sts = sys$open(&fab_ifile);
  752.     if (rms_sts != RMS$_NORMAL) {
  753.         debug(F101," zopeni $open failed, status","",rms_sts);
  754.         debug(F101," zopeni $open failed, stv","",fab_ifile.fab$l_stv);
  755.         return(0);
  756.     }
  757.     if (!(xabpro_ifile.xab$l_aclsts & 1)) {
  758.         if (xabpro_ifile.xab$l_aclsts != SS$_ACLEMPTY) {
  759.         debug(F101," zopeni $open ACL failed, status","",
  760.           xabpro_ifile.xab$l_aclsts);
  761.         return(0);
  762.         }
  763.     }
  764.  
  765. /* We have the file opened. See if it's fixed or undefined format... */
  766.  
  767.     iflen = ((xabfhc_ifile.xab$l_ebk-1)*512)+xabfhc_ifile.xab$w_ffb;
  768.     if (fab_ifile.fab$b_rfm == FAB$C_UDF) {
  769.         debug(F100," zopeni undefined file format - using blk I/O","",0);
  770.         ifile_bmode = 1;
  771.     }
  772.     if (fab_ifile.fab$b_rfm == FAB$C_FIX) {
  773.         if ((fab_ifile.fab$b_rat & (FAB$M_FTN | FAB$M_CR | FAB$M_PRN))
  774.         == 0) {
  775.         debug(F100," zopeni fixed file format - using blk I/O","",0);
  776.         ifile_bmode = 1;
  777.         }
  778.       }
  779.     debug(F101," zopeni binary flag at open","",binary);
  780.     if (binary == XYFT_I) {
  781.         debug(F100," zopeni using IMAGE mode by user request","",0);
  782.         ifile_bmode = 1;
  783.     }
  784.     if (binary == XYFT_L) {
  785.         debug(F100," zopeni using LABELED mode by user request","",0);
  786.         ifile_bmode = 2;
  787.     }
  788.     rab_ifile.rab$l_rop = 0;
  789.     rms_sts = sys$connect(&rab_ifile);
  790.     if (rms_sts != RMS$_NORMAL) {
  791.         debug(F101," zopeni $connect failed, status","",rms_sts);
  792.         return(0);
  793.     }
  794.     debug(F100," zopeni RMS operations completed ok","",0);
  795.     fp[n] = fopen("NLA0:","r");    /* it wants a fp, give it one */
  796.     zincnt = 0;            /* reset input buffer */
  797.     if (binary == XYFT_L)
  798.         do_label_send(name);    /* make a file label */
  799.     return(1);
  800.     } else if (n == ZRFILE) {        /* READ file */
  801.     rfile_bmode = 0;
  802.     rfile_bcount = 0;
  803.     fab_rfile = cc$rms_fab;
  804.         /* Always READ in text mode */
  805.     fab_rfile.fab$b_fac = FAB$M_BRO | FAB$M_GET;
  806.     fab_rfile.fab$l_fna = name;
  807.     fab_rfile.fab$b_fns = strlen(name);
  808.     fab_rfile.fab$l_xab = (char *)&xabdat_rfile;
  809.     rab_rfile = cc$rms_rab;
  810.     rab_rfile.rab$l_fab = &fab_rfile;
  811.     xabdat_rfile = cc$rms_xabdat;
  812.     xabdat_rfile.xab$l_nxt = (char *)&xabfhc_rfile;
  813.     xabfhc_rfile = cc$rms_xabfhc;
  814.     xabfhc_rfile.xab$l_nxt = (char *)&xabpro_rfile;
  815.     xabpro_rfile = cc$rms_xabpro;
  816.     memset(&raclbuf, 0, sizeof(raclbuf)); /* Do we need this for READ? */
  817.     xabpro_rfile.xab$l_aclsts = SS$_NORMAL; /* Oh Joy! DECnet */
  818.     xabpro_rfile.xab$l_aclbuf = (char *)&raclbuf;
  819.     xabpro_rfile.xab$w_aclsiz = sizeof(raclbuf);
  820.     xabpro_rfile.xab$l_aclctx = 0;
  821.     xabpro_rfile.xab$l_nxt = (char *)&xaball_rfile;
  822.     xaball_rfile = cc$rms_xaball;
  823.  
  824.     rms_sts = sys$open(&fab_rfile);
  825.     if (rms_sts != RMS$_NORMAL) {
  826.         debug(F101," zopeni $open ZRFILE failed, status","",rms_sts);
  827.         debug(F101," zopeni $open ZRFILE failed, stv","",
  828.           fab_rfile.fab$l_stv);
  829.         return(0);
  830.     }
  831. #ifdef COMMENT                /* who needs it */
  832.     if (!(xabpro_rfile.xab$l_aclsts & 1)) {
  833.         if (xabpro_rfile.xab$l_aclsts != SS$_ACLEMPTY) {
  834.         debug(F101," zopeni $open ACL failed, status","",
  835.           xabpro_rfile.xab$l_aclsts);
  836.         return(0);
  837.         }
  838.     }
  839. #endif /* COMMENT */
  840.  
  841. /* We have the file opened. See if it's fixed or undefined format... */
  842.  
  843.     rflen = ((xabfhc_rfile.xab$l_ebk-1)*512)+xabfhc_rfile.xab$w_ffb;
  844.     if (fab_rfile.fab$b_rfm == FAB$C_UDF) {
  845.         debug(F100," zopeni ZRFILE undefined file format - fail","",0);
  846.         return(0);
  847.     }
  848.     if (fab_rfile.fab$b_rfm == FAB$C_FIX) {
  849.         if ((fab_rfile.fab$b_rat & (FAB$M_FTN | FAB$M_CR | FAB$M_PRN))
  850.         == 0) {
  851.         debug(F100," zopeni ZRFILE fixed file format - fail","",0);
  852.         return(0);
  853.         }
  854.       }
  855.     rab_rfile.rab$l_rop = 0;
  856.     rms_sts = sys$connect(&rab_rfile);
  857.     if (rms_sts != RMS$_NORMAL) {
  858.         debug(F101," zopeni $connect failed, status","",rms_sts);
  859.         return(0);
  860.     }
  861.     debug(F100," zopeni RFILE RMS operations completed ok","",0);
  862.     fp[n] = fopen("NLA0:","r");    /* it wants a fp, give it one */
  863.     rincnt = 0;            /* reset input buffer */
  864.     return(1);
  865.     }
  866.  
  867.     zincnt = 0;                /* Reset input buffer */
  868.     fp[n] = fopen(name,"r");        /* Real file. */
  869.     debug(F111," zopeni", name, (int) fp[n]);
  870.     if (fp[n] == NULL) perror("zopeni");
  871.     return((fp[n] != NULL) ? 1 : 0);
  872. }
  873.  
  874. /*  Z O P E N O  --  Open a new file for output.  */
  875.  
  876. int
  877. zopeno(n,name,zz,fcb)
  878.     int n; char *name; struct zattr *zz; struct filinfo *fcb; {
  879. /*
  880.   As of Version 5A, the attribute structure and the file information
  881.   structure are included in the arglist.
  882. */
  883.     int x;    
  884.     int fildes;
  885.     int writeover = 0;
  886.     extern int rs_len;
  887.     char p[4];
  888.  
  889.     if (n != ZDFILE)
  890.       debug(F111,"zopeno",name,n);
  891.     x = chkfn(n);
  892.     if (n != ZDFILE)
  893.       debug(F101,"zopeno chkfn","",x);
  894.     if (x != 0)
  895.       return(0);
  896.     zoutcnt = 0;            /* Reset output buffer */
  897.     zoutptr = zoutbuffer;
  898.     cflag = 0;                /* Default to not using console */
  899.     ofile_lblproc = 0;
  900.  
  901.     /* Open terminal or STDIO */
  902.  
  903.     if ((n == ZCTERM) || (n == ZSTDIO)) {
  904.     fp[ZOFILE] = stdout;
  905.     cflag = 1;            /* Say using console */
  906.     debug(F101," fp[]=stdout", "", (int) fp[n]);
  907.     return(1);
  908.     }
  909. /*
  910.   Open Debug, Transaction, Packet, Session logfile, or a Write file.
  911.   The only other possibility at this point is the output file, so we test that.
  912. */
  913. #ifdef DEBUG
  914.     if (deblog) {
  915.     if (fcb)
  916.       debug(F101," zopeno fcb disposition", "", fcb->dsp);
  917.     if (zz)
  918.       debug(F111," zopeno zz disposition",zz->disp.val,zz->disp.len);
  919.     }
  920. #endif /* DEBUG */
  921.     if (n != ZOFILE) {
  922.     strcpy(p,"w");                /* Assume write/create mode */
  923.     if (fcb) {                /* If called with an FCB... */
  924.         if (fcb->dsp == XYFZ_A)        /* Does it say Append? */
  925.         strcpy(p,"a");            /* Yes. */
  926.     }
  927.     if (n != ZSFILE)
  928.       /* was mrs = 80; 254 is max record size for EDT */
  929.       fp[n] = fopen(name, p, "rat=cr", "rfm=var", "mrs=254");
  930.     else
  931.       fp[n] = fopen(name, p, "ctx=stm", "rfm=stm");
  932.     if (fp[n] == NULL) {        /* Failed */
  933.         if (n != ZDFILE)
  934.           debug(F101,"zopeno VAXCRTL failed errno","",errno);
  935.         perror(name);        /* Print system error message */
  936.     } else {            /* Didn't fail */
  937.         debug(F100,"zopeno VAXCRTL ok", "", 0);
  938.     }
  939.     return((fp[n] != NULL) ? 1 : 0);
  940.     }
  941.  
  942. /* Open a file to store data being RECEIVEd */
  943.  
  944.     if (n == ZOFILE) {
  945. #ifdef DEBUG
  946.     if (deblog)
  947.       switch (binary) {
  948.         case XYFT_T:
  949.         debug(F100," zopeno receiving TEXT file","",0);
  950.         break;
  951.         case XYFT_B:
  952.         debug(F100," zopeno receiving BINARY file","",0);
  953.         break;
  954.         case XYFT_I:
  955.         debug(F100," zopeno receiving IMAGE file-program bug!","",0);
  956.         break;
  957.         case XYFT_L:
  958.         debug(F100," zopeno receiving LABELED file","",0);
  959.         break;
  960.         case XYFT_U:
  961.         debug(F100," zopeno receiving UNDEFINED file","",0);
  962.         break;
  963.         default:
  964.         debug(F101," zopeno unknown file type","",binary);
  965.     }
  966. #endif /* DEBUG */
  967.     ofile_bmode = binary;
  968.     ofile_dump = 0;
  969.     ofile_ffb = -1;
  970.     fab_ofile = cc$rms_fab;
  971.     fab_ofile.fab$l_fna = name;
  972.     fab_ofile.fab$b_fns = strlen(name);
  973.     fab_ofile.fab$l_fop = FAB$M_MXV;
  974.     fab_ofile.fab$b_fac = FAB$M_PUT;
  975. /*
  976.   Note that we could actually implement a true overwrite (step on existing
  977.   file version) operation here by testing for a new XYFZ_* type as well in
  978.   the line below, and then simply not doing a RAB$M_EOF about 25 lines down
  979.   from here. We might also have to do something to reset the file allocation
  980.   so we aren't left with a large number of "leftover" blocks if the new file
  981.   is smaller than the old one.
  982. */
  983.     if (fcb) {
  984.         if (fcb->dsp == XYFZ_A) {
  985.         fab_ofile.fab$l_fop = FAB$M_CIF;
  986. /*
  987.   This is for RESEND.  If the output mode is APPEND and the incoming
  988.   attributes structure (zz) says "Resend", then we know this file is
  989.   being resent.
  990. */
  991.         if (*(zz->disp.val) == 'R' && ofile_bmode)
  992.           writeover = 1;
  993.         debug(F101," zopeno APPENDing, writeover","",writeover);
  994.         if (writeover)
  995.           debug(F101," zopeno RESEND, rs_len","",rs_len);
  996. /*
  997.   So if writeover != 0, we can take rs_len to be the length of the existing
  998.   file to keep, and to write over the rest, which normally should be the final
  999.   block.
  1000. */
  1001.         }
  1002.     }
  1003.     if (ofile_bmode) {
  1004.         fab_ofile.fab$b_fac = FAB$M_BIO;
  1005.         debug(F101," zopeno using record size","",frecl);
  1006.         fab_ofile.fab$w_mrs = frecl;
  1007.         if (ofile_bmode == XYFT_U)
  1008.           fab_ofile.fab$b_rfm = FAB$C_UDF;
  1009.         else
  1010.           fab_ofile.fab$b_rfm = FAB$C_FIX;
  1011.     } else {
  1012.         fab_ofile.fab$b_rat = FAB$M_CR;
  1013.         fab_ofile.fab$b_rfm = FAB$C_VAR;
  1014.     }
  1015.     fab_ofile.fab$b_shr = FAB$M_NIL;
  1016.     fab_ofile.fab$l_xab = (char *)&xabdat_ofile;
  1017.     rab_ofile = cc$rms_rab;
  1018.     rab_ofile.rab$l_fab = &fab_ofile;
  1019.     if (fcb)
  1020.         if ((fcb->dsp == XYFZ_A) && (writeover == 0))
  1021.         rab_ofile.rab$l_rop = RAB$M_EOF;
  1022.     xabdat_ofile = cc$rms_xabdat;
  1023.     xabdat_ofile.xab$l_nxt = (char *)&xabfhc_ofile;
  1024.     xabfhc_ofile = cc$rms_xabfhc;
  1025.     if (zz)
  1026.       zstime(name, zz, 2);        /* Set creation date from A packet */
  1027.     if (ofile_bmode == XYFT_L) {    /* DEFER OPEN IF LABELED <-- NOTE  */
  1028.         ofile_lblproc = 0;        /* (Haven't processed labels yet.) */
  1029.         ofile_lblopts = fcb->lblopts;
  1030.         debug(F101," zopeno lblopts","",ofile_lblopts);
  1031.         debug(F100," zopeno RMS operations deferred","",0);
  1032.     } else {
  1033.         rms_sts = sys$create(&fab_ofile);
  1034.         if (!(rms_sts & 1)) {
  1035.         debug(F101," zopeno $create failed, status","",rms_sts);
  1036.         return(0);
  1037.         }
  1038.         rms_sts = sys$connect(&rab_ofile);
  1039.         if (rms_sts != RMS$_NORMAL) {
  1040.         debug(F101," zopeno $connect failed, status","",rms_sts);
  1041.         return(0);
  1042.         }
  1043.         if (writeover == 1) {        /* if resend ... */
  1044.         rab_ofile.rab$l_bkt = (unsigned long) rs_len >> 9;
  1045.         rms_sts = sys$space(&rab_ofile);/* space forward to last */
  1046.         rab_ofile.rab$l_bkt = 0;    /* complete block */
  1047.         if (rms_sts != RMS$_NORMAL) {
  1048.             debug(F101," zopeno $space failed, status","",rms_sts);
  1049.             return(0);
  1050.         }
  1051.         }
  1052.         debug(F100," zopeno RMS operations completed ok","",0);
  1053.     }
  1054.     fp[n] = fopen("NLA0:","r");    /* CK wants a fp, give it one */
  1055.     return(1);
  1056.     }
  1057. }
  1058.  
  1059. /*  Z C L O S E  --  Close the given file.  */
  1060.  
  1061. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  1062.  
  1063. int
  1064. zclose(n) int n; {
  1065.     int x = 0;
  1066.  
  1067.     debug(F101,"zclose n","",n);
  1068.     if (chkfn(n) < 1)
  1069.       return(0);
  1070.  
  1071. /* If this is the subprocess file, close it to flush output */
  1072.  
  1073.     if ((n == ZIFILE || (n == ZRFILE)) && (subprocess_input != 0)) {
  1074.         debug(F100, "zclose calling zclosf", "", 0);
  1075.     return (zclosf(n));
  1076.     }
  1077.     if (n == ZIFILE) {            /* Input (e.g. SEND) file */
  1078.     rms_sts = sys$close(&fab_ifile);
  1079.     if (rms_sts != RMS$_NORMAL) {
  1080.         debug(F101,"zclose ZIFILE $close failed, status","",rms_sts);
  1081.         return(-1);
  1082.     }
  1083.     debug(F100,"zclose ZIFILE RMS operations completed ok","",0);
  1084.     x = fclose(fp[n]);        /* Close the dummy C library file */
  1085.     fp[n] = NULL;            /* Mark it closed */
  1086.     iflen = -1;            /* Invalidate length */
  1087.     return(1);
  1088.     } else if (n == ZRFILE) {        /* READ file */
  1089.     rms_sts = sys$close(&fab_rfile); /* Close it */
  1090.     if (rms_sts != RMS$_NORMAL) {    /* Check status */
  1091.         debug(F101,"zclose ZRFILE $close failed, status","",rms_sts);
  1092.         return(-1);
  1093.     }
  1094.     debug(F100,"zclose ZRFILE RMS operations completed ok","",0);
  1095.     x = fclose(fp[n]);        /* Close the dummy C-Library file */
  1096.     fp[n] = NULL;            /* and mark it closed */
  1097.     rflen = -1;            /* invalidate its length */
  1098.     return(1);
  1099.     }
  1100.     if (n == ZOFILE) {            /* Output (e.g. RECEIVE) file */
  1101.     ofile_dump = 1;            /* Force complete dump */
  1102.     while (zoutcnt != 0) {
  1103.         rms_sts = zoutdump();    /* Flush buffers to disk */
  1104.         if (rms_sts != 0) {
  1105.         x = fclose(fp[n]);    /* Close the associated dummy file */
  1106.         fp[n] = NULL;        /* Mark it closed */
  1107.         iflen = -1;        /* Invalidate length */
  1108.         return(-1);
  1109.         }
  1110.     }
  1111.     if (ofile_bmode == XYFT_L) {    /* Update revisions if labeled */
  1112.         memmove(&xabrdt_ofile.xab$q_rdt, revdat, 8);
  1113.         memmove(&xabrdt_ofile.xab$w_rvn, &revnum, 2);
  1114.         debug(F100,"zclose ZOFILE updated labeled revision count","",0);
  1115.     }
  1116.     if (cflag != 1) {        /* Not console */
  1117.         rms_sts = sys$close(&fab_ofile); /* So close */
  1118.         if (rms_sts != RMS$_NORMAL) {
  1119.         debug(F101,"zclose ZOFILE $close failed, status","",rms_sts);
  1120.         return(-1);
  1121.         }
  1122.         x = fclose(fp[n]);        /* Close the associated dummy file */
  1123.     } else                /* Console */
  1124.       cflag = 0;            /* So not console any more */
  1125.  
  1126.     ofile_lblproc = 0;        /* Done with this file's labels */
  1127.     debug(F100,"zclose ZOFILE RMS operations completed ok","",0);
  1128.     fp[n] = NULL;            /* Mark it closed */
  1129.     iflen = -1;            /* Invalidate length */
  1130.     return(1);
  1131.     }
  1132.     if ((fp[n] != stdout) && (fp[n] != stdin)) /* Other kind of file */
  1133.       x = fclose(fp[n]);        /* C-Libary close */
  1134.     fp[n] = NULL;            /* Mark it closed */
  1135.     iflen = -1;                /* Invalidate file length */
  1136.     debug(F101,"zclose OTHER x","",x);
  1137.     if (x == EOF)            /* If we got a close error */
  1138.       return (-1);            /* fail */
  1139.     else                /* otherwise */
  1140.       return (1);            /* succeed */
  1141. }
  1142.  
  1143. int
  1144. get_subprc_line() {
  1145.     struct iosb_struct subiosb;
  1146.  
  1147.     unsigned int sts;
  1148. /*
  1149.  * Someone complained that subprocess deletion would hang the Kermit server.
  1150.  * This can be triggered by sending something silly like REMOTE HOST STOP/ID=0.
  1151.  * If SUPERSAFE is defined we will check to make sure the subprocess still
  1152.  * exists before every read from the mailbox. This will slow things down a bit,
  1153.  * but should stop the "C-Kermit just dies" reports.
  1154.  */
  1155. #ifdef    SUPERSAFE
  1156.     unsigned short pid;
  1157.  
  1158.     struct itmlstdef {
  1159.     short int buflen;
  1160.     short int itmcod;
  1161.     char *bufaddr;
  1162.     long int *retlen;
  1163.     };
  1164.     struct itmlstdef itmlst[] = {
  1165.     4, JPI$_PID, NULL, 0,
  1166.     0, 0, 0, 0
  1167.     };
  1168.     
  1169.     itmlst[0].bufaddr = (char *)&pid;
  1170.     
  1171.     sts = sys$getjpiw(0, &sub_pid, 0, &itmlst, 0, 0, 0);
  1172.  
  1173.     debug(F101,"get_subprc_line sys$getjpiw status", "", sts);
  1174.     if (sts == SS$_NONEXPR)
  1175.     return(-1);
  1176. #endif    /* SUPERSAFE */
  1177.  
  1178.     sts = sys$qiow(QIOW_EFN, mbx_chan, IO$_READVBLK, &subiosb, 0, 0, sub_buf,
  1179.      sizeof(sub_buf), 0, 0, 0, 0);
  1180.  
  1181.     debug(F101,"get_subprc_line sys$qiow status", "", sts);
  1182.     if (sts != SS$_NORMAL)
  1183.     return(-1);
  1184.  
  1185.     debug(F101,"get_subprc_line sys$qiow subiosb.status", "", subiosb.status);
  1186.     if (subiosb.status == SS$_ENDOFFILE)
  1187.     return(-1);
  1188.  
  1189.     if (subiosb.status != SS$_NORMAL)
  1190.     return(-1);
  1191.  
  1192.     sub_buf[subiosb.size] = '\r';
  1193.     sub_buf[subiosb.size + 1] = '\n';
  1194.     sub_buf[subiosb.size + 2] = '\0';
  1195.     sub_count = subiosb.size + 2;
  1196.     sub_ptr = sub_buf;
  1197.  
  1198.     return(0);
  1199. }
  1200.  
  1201. /*  Z C H I N  --  Get a character from the input file.  */
  1202.  
  1203. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  1204.  
  1205. int
  1206. zchin(n,c) int n, *c; {
  1207.     int a = -1;
  1208.  
  1209. #ifdef DEBUG
  1210.     if (chkfn(n) < 1) return(-1);
  1211. #endif /* DEBUG */
  1212.  
  1213.     if (n == ZIFILE) {
  1214.     if (subprocess_input) {
  1215.         if (--sub_count < 0)
  1216.           if (get_subprc_line()) return(-1);
  1217.         a = *sub_ptr++;
  1218.     } else {
  1219.         a = zminchar();
  1220.     }
  1221.     } else if (n == ZRFILE) {
  1222.     a = ((--rincnt)>=0) ? ((int)(*rinptr++) & 0377) : rinfill();
  1223.     }    
  1224.     if (a == -1)
  1225.       return(-1);
  1226.     *c = (unsigned char)a;
  1227.     return(0);
  1228. }
  1229.  
  1230. /*  Z S I N L  --  Read a line from a file.  */
  1231.  
  1232. /*
  1233.   Writes the line into the address provided by the caller.
  1234.   n is the Kermit "channel number".
  1235.   Writing terminates when newline is encountered, newline is not copied.
  1236.   Writing also terminates upon EOF or if length x is exhausted.
  1237.   Returns 0 on success, -1 on EOF or error.
  1238. */
  1239.  
  1240. int
  1241. zsinl(n,s,x) int n, x; char *s; {
  1242.     int a, z = 0;
  1243.     int old;
  1244.  
  1245.     if (chkfn(n) < 1) {            /* Make sure file is open */
  1246.     return(-1);
  1247.     }
  1248.     a = -1;
  1249.     while (x--) {
  1250.     old = a;            /* Previous character */
  1251.     if (zchin(n,&a) < 0)        /* Read a character from the file */
  1252.       return(-1);            /* Signal EOF if problem */
  1253.     a = a & 0377;
  1254. #ifdef NLCHAR
  1255.     if (a == (char) NLCHAR) break;    /* Single-character line terminator */
  1256. #else
  1257.     if (a == '\r') {
  1258.         continue;
  1259.     }
  1260.     if (old == '\r') {
  1261.         if (a == '\n') break;
  1262.         else *s++ = '\r';
  1263.     }
  1264. #endif /* NLCHAR */
  1265.     *s = a;
  1266.     s++;
  1267.     }
  1268.     *s = '\0';
  1269.     return(z);
  1270. }
  1271.  
  1272. /*  Z I N F I L L  --  Read a line from a file.  */
  1273.  
  1274. /*
  1275.  * (re)fill the buffered file input buffer with data.  All file input
  1276.  * should go through this routine, usually by calling the zminchar()
  1277.  * macro (defined in ckcker.h).
  1278.  */
  1279.  
  1280. int
  1281. zinfill() {
  1282.     char cchar;
  1283.     int linelen;
  1284.  
  1285.     if (subprocess_input) {
  1286.     if (get_subprc_line()) return(-1);
  1287. /*
  1288.  * The size problem should never happen.  sub_buf of a size greater then
  1289.  * 1k is highly unlikely to be needed.
  1290.  */
  1291.     if (INBUFSIZE < SUB_BUF_SIZE) {
  1292.         fprintf(stderr,"zinfill: sub_buf too large for zinbuffer");
  1293.         exit(BAD_EXIT);
  1294.     }
  1295.     zinptr = sub_buf;
  1296.     zincnt = sub_count;
  1297.     } else {
  1298.     if (ifile_bmode != 0) {
  1299.         rab_ifile.rab$l_rop = RAB$M_BIO;    /* block mode I/O */
  1300. #ifdef DYNAMIC
  1301.         rab_ifile.rab$l_ubf = zinbuffer;
  1302. #else
  1303. /* NOTE: Might need "(char *)&zinbuffer" here */
  1304.         rab_ifile.rab$l_ubf = &zinbuffer;
  1305. #endif /* DYNAMIC */
  1306. /*
  1307.   There is a serious flaw here, namely that reading blocks rather than
  1308.   records *includes* the record pad byte (NUL) when the record has an
  1309.   odd length.  All RMS records are stored on even-numbered byte boundaries.
  1310.   Kermit-32 did it right, sigh.  So we need a total rewrite to allow for
  1311.   odd-length records.  Hmmm.. I wonder what the impact on RESEND is...
  1312. */
  1313.         rab_ifile.rab$w_usz = 512;
  1314.         rms_sts = sys$read(&rab_ifile);
  1315.         if (rms_sts == RMS$_EOF)
  1316.         return(-1);            /* end of file */
  1317.         if (rms_sts != RMS$_NORMAL) {
  1318.         debug(F101," zinfill $read failed, status","",rms_sts);
  1319.         return(-1);            /* fatal */
  1320.         }
  1321.         ifile_bcount++;        /* Say another block read */
  1322.         zincnt = 512;
  1323.         zinptr = zinbuffer;
  1324.  
  1325.         if (rab_ifile.rab$l_bkt != 0) { /* If just a file position... */
  1326.         ifile_bcount = rab_ifile.rab$l_bkt; /* update block counter */
  1327.         }
  1328.         if (ifile_bcount == xabfhc_ifile.xab$l_ebk) {
  1329.         if (ifile_bmode == 1)    /* BINARY but not LABELED */
  1330.             zincnt = xabfhc_ifile.xab$w_ffb;
  1331.         }
  1332.         if (rab_ifile.rab$l_bkt != 0) { /* If just a file position... */
  1333.         return(0);            /*...then done                */
  1334.         }
  1335.         zincnt--;            /* one less char in buffer */
  1336.         return((int)(*zinptr++) & 0377); /* because we return the first */
  1337.     }
  1338.     if (fab_ifile.fab$b_rat & FAB$M_FTN) {
  1339. #ifdef DYNAMIC
  1340.         rab_ifile.rab$l_ubf = zinbuffer+2;
  1341. #else
  1342. /* NOTE: Might need "(char *)&zinbuffer+2" here */
  1343.         rab_ifile.rab$l_ubf = &zinbuffer+2;
  1344. #endif /* DYNAMIC */
  1345.         rab_ifile.rab$w_usz = INBUFSIZE-4;    /* space for carriage ctl */
  1346.     } else {
  1347. #ifdef DYNAMIC
  1348.         rab_ifile.rab$l_ubf = zinbuffer;
  1349. #else
  1350. /* NOTE: Might need "(char *)&zinbuffer" here */
  1351.         rab_ifile.rab$l_ubf = &zinbuffer;
  1352. #endif
  1353.         rab_ifile.rab$w_usz = INBUFSIZE-2;    /* space for possible CR/LF */
  1354.     }
  1355.     rab_ifile.rab$l_rop = 0;        /* doing record I/O */
  1356.     rms_sts = sys$get(&rab_ifile);
  1357.     if (rms_sts == RMS$_EOF)
  1358.         return(-1);                /* end of file */
  1359.     if (rms_sts != RMS$_NORMAL) {
  1360.         debug(F101," zinfill $get failed, status","",rms_sts);
  1361.        return(-1);                /* fatal */
  1362.     }
  1363. /*
  1364.  * Do assorted contortions with Fortran carriage control to make it formatted
  1365.  * ASCII instead, since many systems don't know about Fortran format in files.
  1366.  */
  1367.     if (fab_ifile.fab$b_rat & FAB$M_FTN) {
  1368.         linelen = rab_ifile.rab$w_rsz-1;    /* sans control code */
  1369.         cchar = zinbuffer[2];        /* control code */
  1370.         switch (cchar) {
  1371.         case '\0':            /* data<CR> */
  1372.         case '+':
  1373.             zinbuffer[linelen+3] = '\r';/*  insert return */
  1374.             zinptr = zinbuffer+3;
  1375.             zincnt = linelen+1;        /* count it */
  1376.             break;
  1377.         case '$':            /* <LF>data<CR> */
  1378.         case ' ':
  1379.             zinbuffer[2] = '\n';    /*  insert newline */
  1380.             zinbuffer[linelen+3] = '\r';/*  insert return */
  1381.             zinptr = zinbuffer+2;
  1382.             zincnt = linelen+2;        /*  count 'em */
  1383.             break;
  1384.         case '0':            /* <LF><CR><LF>data<CR> */
  1385.             zinbuffer[0] = '\n';    /*  insert 1st newline */
  1386.             zinbuffer[1] = '\r';    /*  insert 1st return */
  1387.             zinbuffer[2] = '\n';    /*  insert 2nd newline */
  1388.             zinbuffer[linelen+3] = '\r';/*  insert 2nd return */
  1389.             zinptr = zinbuffer;
  1390.             zincnt = linelen+4;        /*  count 'em */
  1391.             break;
  1392.         case '1':            /* <FF>data<CR> */
  1393.             zinbuffer[2] = '\f';    /*  insert formfeed */
  1394.             zinbuffer[linelen+3] = '\r';/*  insert return */
  1395.             zinptr = zinbuffer+2;
  1396.             zincnt = linelen+2;        /*  count 'em */
  1397.             break;
  1398.         default:            /* <LF>data<CR> */
  1399.             zinbuffer[2] = '\n';    /*  insert newline */
  1400.             zinbuffer[linelen+3] = '\r';/*  insert return */
  1401.             zinptr = zinbuffer+2;
  1402.             zincnt = linelen+2;        /*  count 'em */
  1403.             break;
  1404.         }
  1405.     } else {
  1406.         zincnt = rab_ifile.rab$w_rsz;
  1407.         zinptr = zinbuffer;            /* reset pointer */
  1408.     }
  1409. /*
  1410.  * Here we see if we need to insert CR/LF pairs at the record boundary. For
  1411.  * the moment, we will add them if the file has "carriage return carriage
  1412.  * control" when looked at by a DIRECTORY command. As of edit 036 we also do
  1413.  * this for "print file carriage control" files. I'm open to comments de-
  1414.  * scribing cases where this doesn't work...
  1415.  */
  1416.     if (fab_ifile.fab$b_rat & (FAB$M_CR | FAB$M_PRN)) {
  1417.         zinbuffer[zincnt] = '\r';
  1418.         zinbuffer[zincnt + 1] = '\n';
  1419.         zincnt += 2;
  1420.     }
  1421.     }
  1422.     zincnt--;            /* one less char in buffer */
  1423.     return((int)(*zinptr++) & 0377); /* because we return the first */
  1424. }
  1425.  
  1426. static int
  1427. rinfill() {
  1428.     char cchar;
  1429.     int linelen;
  1430.  
  1431.     if (rfile_bmode != 0) {
  1432.     rab_rfile.rab$l_rop = RAB$M_BIO;    /* block mode I/O */
  1433. #ifdef DYNAMIC
  1434.     rab_rfile.rab$l_ubf = rinbuffer;
  1435. #else
  1436. /* NOTE: Might need "(char *)&rinbuffer" here */
  1437.     rab_rfile.rab$l_ubf = &rinbuffer;
  1438. #endif /* DYNAMIC */
  1439.     rab_rfile.rab$w_usz = 512;
  1440.     rms_sts = sys$read(&rab_rfile);
  1441.     if (rms_sts == RMS$_EOF)
  1442.       return(-1);            /* end of file */
  1443.     if (rms_sts != RMS$_NORMAL) {
  1444.         debug(F101," rinfill $read failed, status","",rms_sts);
  1445.         return(-1);            /* fatal */
  1446.     }
  1447.     rfile_bcount++;            /* Say another block read */
  1448.     rincnt = 512;
  1449.     rinptr = rinbuffer;
  1450.  
  1451.     if (rab_rfile.rab$l_bkt != 0) { /* If just a file position... */
  1452.         rfile_bcount = rab_rfile.rab$l_bkt; /* update block counter */
  1453.     }
  1454.     if (rfile_bcount == xabfhc_rfile.xab$l_ebk) {
  1455.         if (rfile_bmode == 1)    /* BINARY but not LABELED */
  1456.           rincnt = xabfhc_rfile.xab$w_ffb;
  1457.     }
  1458.     if (rab_rfile.rab$l_bkt != 0) { /* If just a file position... */
  1459.         return(0);            /*...then done                */
  1460.     }
  1461.     rincnt--;            /* one less char in buffer */
  1462.     return((int)(*rinptr++) & 0377); /* because we return the first */
  1463.     }
  1464.     if (fab_rfile.fab$b_rat & FAB$M_FTN) {
  1465. #ifdef DYNAMIC
  1466.     rab_rfile.rab$l_ubf = rinbuffer+2;
  1467. #else
  1468. /* NOTE: Might need "(char *)&rinbuffer" here */
  1469.     rab_rfile.rab$l_ubf = &rinbuffer+2;
  1470. #endif
  1471.     rab_rfile.rab$w_usz = INBUFSIZE-4; /* space for carriage ctl */
  1472.     } else {
  1473. #ifdef DYNAMIC
  1474.     rab_rfile.rab$l_ubf = rinbuffer;
  1475. #else
  1476. /* NOTE: Might need "(char *)&rinbuffer" here */
  1477.     rab_rfile.rab$l_ubf = &rinbuffer;
  1478. #endif
  1479.     rab_rfile.rab$w_usz = INBUFSIZE-2; /* space for possible CR/LF */
  1480.     }
  1481.     rab_rfile.rab$l_rop = 0;        /* doing record I/O */
  1482.     rms_sts = sys$get(&rab_rfile);
  1483.     if (rms_sts == RMS$_EOF)
  1484.       return(-1);            /* end of file */
  1485.     if (rms_sts != RMS$_NORMAL) {
  1486.     debug(F101," rinfill $get failed, status","",rms_sts);
  1487.     return(-1);            /* fatal */
  1488.     }
  1489. /*
  1490.  * Do assorted contortions with Fortran carriage control to make it formatted
  1491.  * ASCII instead, since many systems don't know about Fortran format in files.
  1492.  */
  1493.     if (fab_rfile.fab$b_rat & FAB$M_FTN) {
  1494.     linelen = rab_rfile.rab$w_rsz-1; /* sans control code */
  1495.     cchar = rinbuffer[2];        /* control code */
  1496.     switch (cchar) {
  1497.       case '\0':            /* data<CR> */
  1498.       case '+':
  1499.         rinbuffer[linelen+3] = '\r';/*  insert return */
  1500.         rinptr = rinbuffer+3;
  1501.         rincnt = linelen+1;        /* count it */
  1502.         break;
  1503.       case '$':            /* <LF>data<CR> */
  1504.       case ' ':
  1505.         rinbuffer[2] = '\n';    /*  insert newline */
  1506.         rinbuffer[linelen+3] = '\r';/*  insert return */
  1507.         rinptr = rinbuffer+2;
  1508.         rincnt = linelen+2;        /*  count 'em */
  1509.         break;
  1510.       case '0':            /* <LF><CR><LF>data<CR> */
  1511.         rinbuffer[0] = '\n';    /*  insert 1st newline */
  1512.         rinbuffer[1] = '\r';    /*  insert 1st return */
  1513.         rinbuffer[2] = '\n';    /*  insert 2nd newline */
  1514.         rinbuffer[linelen+3] = '\r';/*  insert 2nd return */
  1515.         rinptr = rinbuffer;
  1516.         rincnt = linelen+4;        /*  count 'em */
  1517.         break;
  1518.       case '1':            /* <FF>data<CR> */
  1519.         rinbuffer[2] = '\f';    /*  insert formfeed */
  1520.         rinbuffer[linelen+3] = '\r';/*  insert return */
  1521.         rinptr = rinbuffer+2;
  1522.         rincnt = linelen+2;        /*  count 'em */
  1523.         break;
  1524.       default:            /* <LF>data<CR> */
  1525.         rinbuffer[2] = '\n';    /*  insert newline */
  1526.         rinbuffer[linelen+3] = '\r';/*  insert return */
  1527.         rinptr = rinbuffer+2;
  1528.         rincnt = linelen+2;        /*  count 'em */
  1529.         break;
  1530.     }
  1531.     } else {
  1532.     rincnt = rab_rfile.rab$w_rsz;
  1533.     rinptr = rinbuffer;        /* reset pointer */
  1534.     }
  1535. /*
  1536.  * Here we see if we need to insert CR/LF pairs at the record boundary. For
  1537.  * the moment, we will add them if the file has "carriage return carriage
  1538.  * control" when looked at by a DIRECTORY command. As of edit 036 we also do
  1539.  * this for "print file carriage control" files. I'm open to comments de-
  1540.  * scribing cases where this doesn't work...
  1541.  */
  1542.     if (fab_rfile.fab$b_rat & (FAB$M_CR | FAB$M_PRN)) {
  1543.     rinbuffer[rincnt] = '\r';
  1544.     rinbuffer[rincnt + 1] = '\n';
  1545.     rincnt += 2;
  1546.     }
  1547.     rincnt--;            /* one less char in buffer */
  1548.     return((int)(*rinptr++) & 0377); /* because we return the first */
  1549. }
  1550.  
  1551. /*  Z F S E E K -- Seek to a given position with an input file */
  1552. /*                 Assumes block-mode I/O being used           */
  1553.  
  1554. int
  1555. zfseek(long pos) {
  1556.     long offset;
  1557.  
  1558.     rab_ifile.rab$l_bkt = (unsigned long) pos >> 9; /* Get block number */
  1559.     rab_ifile.rab$l_bkt++;        /* VBN's are 1-based */
  1560.     offset = (unsigned long) pos & 511;    /* Get offset with block */
  1561.     if (zinfill() != 0) {        /* Read in the block */
  1562.     rab_ifile.rab$l_bkt = 0;    /* Sequentially from now on */
  1563.     return(-1);
  1564.     }
  1565.     rab_ifile.rab$l_bkt = 0;        /* Sequentially from now on */
  1566.     if (offset != 0) {            /* if not block boundary... */
  1567.     zincnt = zincnt - offset;    /* ...adjust count and pointer */
  1568.     zinptr = zinptr + offset;
  1569.     }
  1570.     return(0);
  1571. }
  1572.  
  1573. /*  Z S O U T  --  Write a string to the given file, buffered.  */
  1574.  
  1575. int
  1576. zsout(n,s) int n; char *s; {
  1577. #ifdef DEBUG
  1578.     if (chkfn(n) < 1) return(-1);
  1579. #endif
  1580.     fputs(s, fp[n]);            /* Don't use fprintf here MM */
  1581.     return(0);
  1582. }
  1583.  
  1584.  
  1585. /*  Z S O U T L  --  Write string to file, with line terminator, buffered.  */
  1586.  
  1587. int
  1588. zsoutl(n,s) int n; char *s; {
  1589. #ifdef DEBUG
  1590.     if (chkfn(n) < 1) return(-1);
  1591. #endif
  1592.     fputs(s, fp[n]);            /* Don't use fprintf MM */
  1593.     putc('\n', fp[n]);
  1594.     return(0);
  1595. }
  1596.  
  1597.  
  1598. /*  Z S O U T X  --  Write x characters to file, unbuffered.  */
  1599.  
  1600. int
  1601. zsoutx(n,s,x) int n, x; char *s; {
  1602. #ifdef DEBUG
  1603.     if (chkfn(n) < 1) return(-1);
  1604. #endif
  1605.     return(write(fileno(fp[n]),s,x));
  1606. }
  1607.  
  1608.  
  1609. /*  Z C H O U T  --  Add a character to the given file.  */
  1610.  
  1611. int
  1612. #ifdef CK_ANSIC
  1613. zchout(register int n, char c)
  1614. #else
  1615. zchout(n,c) register int n; char c;
  1616. #endif /* CK_ANSIC */
  1617. /* zchout */ {
  1618. #ifdef DEBUG
  1619.     if (chkfn(n) < 1) return(-1);
  1620. #endif
  1621.     if (n == ZSFILE) {
  1622.         return(write(fileno(fp[n]),&c,1)); /* Use unbuffered for session log */
  1623.     } else {
  1624.     if (putc(c,fp[n]) == EOF)    /* If true, maybe there was an error */
  1625.       return(ferror(fp[n]) ? -1 : 0); /* Check to make sure */
  1626.     else                /* Otherwise... */
  1627.       return(0);            /* There was no error. */
  1628.     }
  1629. }
  1630.  
  1631. /*  Z O U T D U M P  --  dump buffered output characters to file.  */
  1632.  
  1633. /* Buffered file output, buffer dump */
  1634.  
  1635. /*
  1636.   No, this isn't an entry in the 199x Obfuscated C programming contest, nor
  1637.   did we get it at an all-night convenience store.  VMS requires that stream
  1638.   format files be written as records, so we have to do _lots_ of contortions
  1639.   to make sure we write whole lines as records.  Not pretty.  - TMK
  1640. */
  1641. int
  1642. zoutdump() {
  1643.     int ocnt;
  1644.     int wrote_one_line = 0;
  1645.     CHAR *optr, *srcptr, *endptr;
  1646.     char csave;
  1647.  
  1648.     debug(F101," zoutdump zoutcnt","",zoutcnt);
  1649.     debug(F101," zoutdump ofile_bmode","",ofile_bmode);
  1650. /*
  1651.   Well, this could be to the console. If it is, chop it into itty-bitty parts
  1652.   (the VMS CRTL can't handle a %s spec bigger than 512 bytes) and print it.
  1653. */
  1654.     if (cflag == 1) {            /* If we're dumping to console */
  1655.     endptr = zoutbuffer + zoutcnt;
  1656.     for (optr = zoutbuffer; optr < endptr; optr += 511) {
  1657.         if (optr+511 < endptr) {    /* More than 511, break up */
  1658.         csave = *(optr+511);
  1659.         *(optr+511) = '\0';
  1660.         printf("%s", optr);
  1661.         *(optr+511) = csave;
  1662.         } else {
  1663.         *endptr = '\0';        /* Make sure null-terminated */
  1664.         printf("%s", optr);
  1665.         }
  1666.     }
  1667.     zoutcnt = 0;
  1668.     zoutptr = zoutbuffer;
  1669.     return(0);
  1670.     }
  1671.  
  1672. /* Do we need to processed TYPE LABELED contortions? */
  1673.  
  1674.     if (ofile_bmode == XYFT_L) {    /* Is it labeled? */
  1675.     if (ofile_lblproc == 0)    {    /* I've never gone this way before? */
  1676.         rms_sts = do_label_recv();    /* Beyond revolving rainbow door... */
  1677.         if (rms_sts == -1)
  1678.           return(-1);        /* Got a hard error in label proc. */
  1679.         if (rms_sts == 1 && ofile_dump != 1)
  1680.           return(0);        /* Exit so we can fill up the buffer */
  1681.     }
  1682.     }
  1683. /*
  1684.  * Well, we could be lucky...
  1685.  */
  1686.     if (zoutcnt == 0)
  1687.     return(0);
  1688. /*
  1689.  * Oh well. See if doing binary - that's easy...
  1690.  */
  1691.     if (ofile_bmode) {
  1692.     if (zoutcnt == OBUFSIZE) {
  1693. #ifdef DYNAMIC
  1694.         rab_ofile.rab$l_rbf = zoutbuffer;
  1695. #else
  1696. /* NOTE: Might need "(char *)&zoutbuffer" here */
  1697.         rab_ofile.rab$l_rbf = &zoutbuffer;
  1698. #endif /* DYNAMIC */
  1699.         rab_ofile.rab$w_rsz = OBUFSIZE;
  1700.         if (ofile_ffb != -1 && ofile_dump == 1) {
  1701.         /*
  1702.          * Only do this when doing _last_ file segment.
  1703.          */
  1704.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  1705.         if (ofile_ffb)
  1706.             rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  1707.         debug(F101," zoutdump ofile_ffb","",(int)ofile_ffb);
  1708.         debug(F101," zoutdump rab$w_rsz","",rab_ofile.rab$w_rsz);
  1709.         }
  1710.         rms_sts = sys$write(&rab_ofile);
  1711.         if (rms_sts != RMS$_NORMAL) {
  1712.         debug(F101," zoutdump $write failed, status","",rms_sts);
  1713.         return(-1);
  1714.         }
  1715.     } else {
  1716. #ifdef DYNAMIC
  1717.         rab_ofile.rab$l_rbf = zoutbuffer;
  1718. #else
  1719. /* NOTE: Might need "(char *)&zoutbuffer" here */
  1720.         rab_ofile.rab$l_rbf = &zoutbuffer;
  1721. #endif
  1722.         rab_ofile.rab$w_rsz = zoutcnt;
  1723.         xabfhc_ofile.xab$w_ffb = (zoutcnt & 511)+1;
  1724.         if (ofile_ffb != -1) {
  1725.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  1726.         if (ofile_ffb)
  1727.             rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  1728.         debug(F101," zoutdump ofile_ffb","",(int)ofile_ffb);
  1729.         debug(F101," zoutdump rab$w_rsz","",rab_ofile.rab$w_rsz);
  1730.         }
  1731.         rms_sts = sys$write(&rab_ofile);
  1732.         if (rms_sts != RMS$_NORMAL) {
  1733.         debug(F101," zoutdump $write failed, status","",rms_sts);
  1734.         return(-1);
  1735.         }
  1736.     }
  1737.     debug(F100," zoutdump RMS operations completed ok","",0);
  1738.     zoutcnt = 0;
  1739.     zoutptr = zoutbuffer;
  1740.     return(0);
  1741.     }
  1742.  
  1743. /*
  1744.  * Must be ASCII. This is harder, and weirder... It's actually easier than
  1745.  * it looks, but there's (unfortunately) no really easy way to _implement_
  1746.  * it. (sigh, whimper, groan)
  1747.  */
  1748.  
  1749.     srcptr = zoutbuffer;            /* Points to first line in buffer */
  1750.     endptr = zoutbuffer + zoutcnt;  /* Points to location after last char */
  1751. zoutdump_ascii:
  1752.     /* Scan through buffer until we find a CR or we run out of chars */
  1753.     for (optr = srcptr; optr < endptr; optr++) if (*optr == CR) break;
  1754.  
  1755.     /* If there are at least 2 chars left in the buffer when we stop   */
  1756.     /* scanning, then it is assumed the above loop terminated because  */
  1757.     /* it found the CR and that both the CR and LF are present in the  */
  1758.     /* buffer (situation normal.                                       */
  1759.     /* If there are not 2 chars left in the buffer, we have one of two */
  1760.     /* cases which we treat identically:                               */
  1761.     /*  1) If there are 0 chars left in the buffer, then the line's    */
  1762.     /*     terminating CR LF are yet to come. So... we copy the data   */
  1763.     /*     to the front of the buffer and exit (next time it should be */
  1764.     /*     there.)                                                     */
  1765.     /*  2) If there is one char left in the buffer, we have the case of*/
  1766.     /*     a line with the CR but no LF present. So... do the same     */
  1767.     /*     because the LF will be coming next time.                    */
  1768.     if (optr+2 > endptr) {            /* drat! ran off the end */
  1769.     if (ofile_dump && (srcptr == endptr)) {
  1770.         /* If the beginning and end ptrs are the same, then there the  */
  1771.         /* is empty. Good news, 'cause we're clsoing up.               */
  1772.         zoutcnt = 0;        /* No looping, please. */
  1773.         zoutptr = zoutbuffer;
  1774.     }
  1775.     else if (ofile_dump) {        /* but it's cool, we're closing up */
  1776.         /* Oops, we've got a line with no LF and maybe no CR. Well     */
  1777.         /* write it out and exit abnormally.                           */
  1778.         rab_ofile.rab$l_rbf = srcptr;
  1779.         rab_ofile.rab$w_rsz = optr-srcptr;
  1780.         rms_sts = sys$put(&rab_ofile);
  1781.         zoutcnt = 0;
  1782.         zoutptr = zoutbuffer;
  1783.         if (rms_sts != RMS$_NORMAL) {
  1784.         debug(F101, " zoutdump $put failed, status","",rms_sts);
  1785.         return(-1);
  1786.         }
  1787.     } else if (wrote_one_line) {    /* it's still cool, we did one... */
  1788.         zoutcnt = optr - srcptr;    /* number of chars left */
  1789.         if (optr < endptr) zoutcnt++; /*[jah083] including CR if present */
  1790.         if (zoutcnt) memmove(zoutbuffer, srcptr, zoutcnt);
  1791.             /* Move'em to front of buffer*/
  1792.         zoutptr = zoutbuffer+zoutcnt;
  1793.     } else {            /* WRONG!!! */
  1794.         /* We've got a buffer full of chars with no LF (it may or may  */
  1795.         /* not have a terminating CR. In either case its just plain too*/
  1796.         /* long. I suppose we could check here for the optr+1 == endptr*/
  1797.         /* which indicates that there was a CR but no LF so we could   */
  1798.         /* issue a "line barely too long", but, is it useful?          */
  1799.         debug(F100, "zoutdump: line too long","",0);
  1800.         zoutcnt = 0;        /* No looping, please. */
  1801.         zoutptr = zoutbuffer;
  1802.         return(-1);
  1803.     }
  1804.     debug(F101, " zoutdump exiting, zoutcnt","",zoutcnt);
  1805.     return(0);
  1806.     }
  1807.  
  1808.     /* We now have a line that we can write, so... */
  1809.  
  1810.     rab_ofile.rab$l_rbf = srcptr;
  1811.     rab_ofile.rab$w_rsz = optr-srcptr;
  1812.     rms_sts = sys$put(&rab_ofile);
  1813.     if (rms_sts != RMS$_NORMAL) {
  1814.     debug(F101, " zoutdump $put failed, status","",rms_sts);
  1815.     return(-1);
  1816.     }
  1817.     srcptr = optr + 2;        /* Account for CR, LF */
  1818.     wrote_one_line = 1;
  1819.     goto zoutdump_ascii;
  1820. }
  1821.  
  1822. /*  C H K F N  --  Internal function to verify file number is ok.  */
  1823.  
  1824. /*
  1825.  Returns:
  1826.   -1: File number n is out of range
  1827.    0: n is in range, but file is not open
  1828.    1: n in range and file is open
  1829. */
  1830. int
  1831. chkfn(n) int n; {
  1832.     int x;
  1833.     if (n != ZDFILE)
  1834.       debug(F101,"chkfn","",n);
  1835.     switch (n) {
  1836.     case ZCTERM:
  1837.     case ZSTDIO:
  1838.     case ZIFILE:
  1839.     case ZOFILE:
  1840.     case ZDFILE:
  1841.     case ZTFILE:
  1842.     case ZPFILE:
  1843.     case ZSFILE:
  1844.         break;
  1845.     case ZSYSFN:            /* System functions */
  1846.         return(0);
  1847.     case ZRFILE:            /* READ and WRITE files */
  1848.     case ZWFILE:
  1849.         case ZMFILE:
  1850.         break;
  1851.     default:
  1852.         debug(F101,"chkfn: file number out of range","",n);
  1853.         fprintf(stderr,"?File number out of range - %d\n",n);
  1854.         return(-1);
  1855.     }
  1856.     x = (fp[n] == NULL) ? 0 : 1; 
  1857.     if (n != ZDFILE)
  1858.       debug(F101,"chkfn return","",x);
  1859.     return(x);
  1860. }
  1861.  
  1862. /*  Z C H K I  --  Check if input file exists and is readable.  */
  1863.  
  1864. /*
  1865.   Returns:
  1866.    >= 0 if the file can be read (returns the size).
  1867.      -1 if file doesn't exist or can't be accessed,
  1868.      -2 if file exists but is not readable (e.g. a directory file).
  1869.      -3 if file exists but protected against read access.
  1870. */
  1871.  
  1872. long
  1873. zchki(name) char *name; {
  1874.     struct stat statbuf;
  1875.     int x; long y;
  1876.     struct FAB fab_chki;
  1877.     struct XABFHC xabfhc_chki;
  1878.     long iflen = -1L;
  1879.  
  1880. /* This is _really_ bad. But there's a fundamental assumption in the upper
  1881.  * levels that one can call zchki() without any context to validate file-
  1882.  * names, directory names, etc. which would be painful (to the other imple-
  1883.  * mentations) to change. So, if we get an argument which ends in ':', '>',
  1884.  * or ']', we'll return an immediate OK with a size of 0. Bad directory
  1885.  * names will be caught in zchdir anyway. This has the nice side-effect that
  1886.  * saying (for example) GET dir-spec will implicitly get all files in that
  1887.  * directory. Not bad for a total kludge, huh?
  1888.  */
  1889.     x = strlen(name);
  1890.     if (name[x-1] == ':')
  1891.     return(0);
  1892.     if (name[x-1] == ']')
  1893.     return(0);
  1894.     if (name[x-1] == '>')
  1895.     return(0);
  1896.  
  1897. /* And here's another one... I don't know how to do this in RMS-speak... */
  1898.  
  1899. #ifdef __DECC                /* DEC C */
  1900. #ifndef __DECC_VER            /* version < 5.0 */
  1901. /*
  1902.   In DECC 5.0 (?) and higher, S_ISDIR() seems to be a library function
  1903.   rather than a macro (contrary to POSIX.1), and so we can't use #ifdef to
  1904.   see if it is defined.  On the other hand, in DEC C 5.3, the S_IFMT and 
  1905.   S_IFDIR definitions have been removed, so we can't always use the following
  1906.   ruse.  In VAXC 3.1, we also have to do it....
  1907. */
  1908. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  1909. #endif /* __DECC_VER */
  1910. #else  /* VAXC */
  1911. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  1912. #endif /* __DECC */
  1913.  
  1914.     x = stat((char *)name,&statbuf);
  1915.     if (S_ISDIR(statbuf.st_mode))
  1916.       return(-2);
  1917. /*
  1918.   It's needed because the following RMS$blah code does not detect that
  1919.   a file (such as SYS$SYSDEVICE[USER]TEMP.DIR;1) is a directory.  So, for
  1920.   example, when we go to send such a file, it actually sends the directory
  1921.   itself.  The real fix is to make the following code return -2 if it is a
  1922.   directory. 
  1923. */
  1924.     fab_chki = cc$rms_fab;
  1925.     fab_chki.fab$b_fac = FAB$M_BIO;
  1926.     fab_chki.fab$l_fna = name;
  1927.     fab_chki.fab$b_fns = strlen(name);
  1928.     fab_chki.fab$l_xab = (char *)&xabfhc_chki;
  1929.     xabfhc_chki = cc$rms_xabfhc;
  1930.     rms_sts = sys$open(&fab_chki);
  1931.     if (rms_sts == RMS$_PRV)            /* No privs */
  1932.     return(-3);
  1933.     if (rms_sts != RMS$_NORMAL) {
  1934.     debug(F101," zchki $open failed, status","",rms_sts);
  1935.     return(-1);
  1936.     }
  1937.     iflen = ((xabfhc_chki.xab$l_ebk-1)*512)+xabfhc_chki.xab$w_ffb;
  1938.  
  1939.     rms_sts = sys$close(&fab_chki);
  1940.     if (rms_sts != RMS$_NORMAL) {
  1941.     debug(F101," zchki $close failed, status","",rms_sts);
  1942.     return(-1);
  1943.     }
  1944.     strcpy(nambuf,name);        /* preserve name */
  1945.     debug(F111," zchki access ok:",name,(int) iflen); /* Yes */
  1946.     return( (iflen > -1) ? iflen : 0 );
  1947. }
  1948.  
  1949. /*  Z C H K O  --  Check if output file can be created.  */
  1950.  
  1951. /*
  1952.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  1953. */
  1954. int
  1955. zchko(name) char *name; {
  1956. /*
  1957.   This really needs to be filled in.  What if it's a device or logical name?
  1958. */
  1959.     return(0);                /* Always creates new version */
  1960. }
  1961.  
  1962. /*  Z C H K S P A  --  Check if there is enough space to store the file.  */
  1963.  
  1964. /*
  1965.  Call with file specification f, size n in bytes.
  1966.  Returns -1 on error, 0 if not enough space, 1 if enough space.
  1967. */
  1968.  
  1969. int
  1970. zchkspa(f,n) char *f; long n; {
  1971.  
  1972. /*
  1973.  * This is complicated. The user could have specified an explicit path when
  1974.  * sending the file, or could have done a CWD, or could be using the default
  1975.  * directory. If not the latter, the path may not even be a disk device, as
  1976.  * CWD LPA0: is perfect legal for uploading to the lineprinter. After that,
  1977.  * if it's a disk, we should check the user's quota.  However, the user may
  1978.  * have SYSPRV, EXQUOTA, BYPASS, or maybe even GRPPRV, and it would be hard
  1979.  * to  properly check for all these cases.  So, if the file will fit on the
  1980.  * disk, we'll accept it.
  1981.  */
  1982.  
  1983.     char   *zgtdir();
  1984.  
  1985.     struct itmlstdef {
  1986.     short int buflen;
  1987.     short int itmcod;
  1988.     char *bufaddr;
  1989.     long int *retlen;
  1990.     };
  1991.  
  1992.     static char device[64];
  1993.  
  1994.     struct dsc$descriptor_s
  1995.     dev_desc = {sizeof(device), DSC$K_DTYPE_T, DSC$K_CLASS_S,
  1996.               (char *)&device};
  1997.     unsigned long freeblocks, devclass, fileblocks;
  1998.     long freelength, classlength;
  1999.  
  2000.     struct itmlstdef itmlst[] =
  2001.     {4,DVI$_FREEBLOCKS,0,0,4,DVI$_DEVCLASS,0,0,0,0,0,0};
  2002.  
  2003.     int rms_sts;
  2004.  
  2005. /* First, figure out the device we're interested in */
  2006.  
  2007.     strcpy(device, zgtdir());            /* Handles default or CWD */
  2008.  
  2009.     if (strchr(f, ':'))                /* If user specified path */
  2010.     strncpy(device, f, 63);
  2011.  
  2012.     debug(F110," zchkspa target device is ",device,0);
  2013.  
  2014. /* Next, ask for free block count and device type (disk vs. non-disk) */
  2015.  
  2016.     itmlst[0].bufaddr = (char *)&freeblocks;
  2017.     itmlst[0].retlen = &freelength;
  2018.     itmlst[1].bufaddr = (char *)&devclass;
  2019.     itmlst[1].retlen = &classlength;
  2020.  
  2021.     rms_sts = sys$getdviw(0,0,&dev_desc,&itmlst,0,0,0,0);
  2022.  
  2023.     debug(F101," zchkspa $getdvi returned rms_sts","",rms_sts);
  2024.  
  2025.     if (devclass != DC$_DISK)
  2026.     return(1);                /* assume space if not disk */
  2027.  
  2028.     if (rms_sts != SS$_NORMAL)
  2029.     return(1);                /* assume free space if err */
  2030.  
  2031.     debug(F101," zchkspa $getdvi returned freeblocks","",freeblocks);
  2032.  
  2033. /* Pad file size if it's a text file */
  2034.  
  2035.     if (ofile_bmode == XYFT_T)
  2036.     n += (n/40) * 3;
  2037.  
  2038.     fileblocks = n / 512 + 1;            /* compute file size in blks */
  2039.                         /* we may want some fuzz */
  2040.     if (fileblocks >= freeblocks)
  2041.     return(0);                /* Won't fit */
  2042.     else
  2043.     return(1);                /* Will fit */
  2044. }
  2045.  
  2046. /*  Z D E L E T  --  Delete the named file.  */
  2047.  
  2048. int
  2049. zdelet(name) char *name; {
  2050.     return(delete(name));
  2051. }
  2052.  
  2053. /*  Z R T O L  --  Convert remote filename into local form.  */
  2054.  
  2055. VOID
  2056. zrtol(name,name2) char *name, *name2; {
  2057.     int count = 9, vflag = 0;
  2058.     char *cp, c;
  2059.     static char *spcl_set = "_-$[]<>:.\";";
  2060.  
  2061.     for (cp = name2; c = *name; name++) {
  2062.     if (islower(c)) c = toupper(c);
  2063.     if (!isalnum(c) && !strchr(spcl_set,c))
  2064.       c = 'X';
  2065.     *cp++ = c;
  2066.     }
  2067.     *cp = '\0';                /* End of name */
  2068.     debug(F110," zrtol: ",name2,0);
  2069. }
  2070.  
  2071. /*  Z L T O R  --  Convert filename from local format to common form.  */
  2072.  
  2073. VOID
  2074. zltor(name,name2) char *name, *name2; {
  2075.     char *cp, *pp;
  2076.  
  2077. /*
  2078.  * Copy name to output string
  2079.  */
  2080.  
  2081.     strcpy(name2,name);
  2082.  
  2083. /*
  2084.  * Parse the filename and type, with the default filename of "X"
  2085.  */
  2086.  
  2087.     parse_fname(name2, 100, "X", PARSE_NAME|PARSE_TYPE);
  2088.     debug(F110," zltor: ",name2,0);
  2089. }
  2090.  
  2091. /*  Z C H D I R  --  Change directory.  */
  2092.  
  2093. int
  2094. zchdir(dirnam) char *dirnam; {
  2095.  
  2096.     char dirbuf[NAM$C_MAXRSS+1];
  2097.     int status, x;
  2098.  
  2099.     if (!dirnam)            /* Watch out for null pointers */
  2100.       dirnam = "";
  2101.  
  2102.     if (*dirnam == '\0') {        /* No arg so back to home directory */
  2103.     strcpy(dirbuf,getenv("HOME"));
  2104.     dirnam = (char *) dirbuf;
  2105.     debug(F110,"zchdir home","",0);
  2106.     }
  2107.     status = isdir(dirnam);        /* Preverify to prevent C lib traps */
  2108.     debug(F111,"zchdir isdir 1",dirnam,status);
  2109.     if (!status) {            /* Not a directory */
  2110.     if (dirnam[0] == '[')
  2111.       return(0);
  2112.     x = strlen(dirnam);
  2113.     if (x > 0 && (dirnam[x-1] == ']' || dirnam[x-1] == ':'))
  2114.       return(0);
  2115.     if (*dirnam == '.')        /* Be nice - is it a subdirectory */
  2116.       sprintf(dirbuf,"[%s]",dirnam); /* of the current directory? */
  2117.     else
  2118.       sprintf(dirbuf,"[.%s]",dirnam);
  2119.     status = isdir(dirbuf);
  2120.     debug(F111,"zchdir isdir 2",dirnam,status);
  2121.     if (!status)
  2122.       return(0);            /* Nope */
  2123.     dirnam = dirbuf;        /* Ayup */
  2124.     } /* (we should really save "ayup" comments for the Maine program...)   */
  2125.     status = chdir(dirnam);        /* Try to CD and hope we don't trap */
  2126.     debug(F101,"zchdir status","",status);
  2127.     return(status == 0);
  2128. }
  2129.  
  2130. /*  Z H O M E  --  Return pointer to user's home directory.  */
  2131.  
  2132. char *
  2133. zhome() {
  2134.     return(getenv("HOME"));
  2135. }
  2136.  
  2137. /*  Z G T D I R  --  Return pointer to user's current directory.  */
  2138.  
  2139. char *
  2140. zgtdir() {
  2141. #ifdef VMS_V40
  2142. #define    OLD_VMS
  2143. #endif
  2144. #ifdef VMS_V42
  2145. #define    OLD_VMS
  2146. #endif
  2147. #ifdef VMS_V44
  2148. #define    OLD_VMS
  2149. #endif
  2150. #ifdef VAXC023
  2151. #define    OLD_VMS
  2152. #endif
  2153. #ifdef VAXC024
  2154. #define    OLD_VMS
  2155. #endif
  2156.  
  2157. #ifdef OLD_VMS
  2158.     static char *gtdir_buf = 0;
  2159.     static char sysdisk[] = "SYS$DISK";
  2160.     char tmp_buf[NAM$C_MAXRSS+1];
  2161.     struct dsc$descriptor_s
  2162.     tmp_buf_dsc = {sizeof(tmp_buf),DSC$K_DTYPE_T,DSC$K_CLASS_S,&tmp_buf},
  2163.     sysdisk_dsc = {sizeof(sysdisk)-1,DSC$K_DTYPE_T,DSC$K_CLASS_S,&sysdisk};
  2164.     unsigned short int buf_len;
  2165.  
  2166. /*
  2167.  * Allocate buffer dynamically, first time through.  This makes the image
  2168.  * smaller.
  2169.  */
  2170.  
  2171.     if (!gtdir_buf) gtdir_buf = malloc(NAM$C_MAXRSS+1);
  2172.  
  2173. /*
  2174.  * Translate device name.
  2175.  */
  2176.  
  2177.     lib$sys_trnlog(     &sysdisk_dsc,
  2178.             &buf_len,
  2179.             &tmp_buf_dsc,
  2180.             0,
  2181.             0,
  2182.             0);
  2183.     tmp_buf[buf_len] = '\0';
  2184.     strcpy(gtdir_buf,tmp_buf);
  2185.  
  2186. /*
  2187.  * Get directory name.
  2188.  */
  2189.  
  2190.     sys$setddir(    0,      /* New dir addr */
  2191.             &buf_len, /* length addr */
  2192.             &tmp_buf_dsc);
  2193.     tmp_buf[buf_len] = '\0';
  2194.     strcat(gtdir_buf,tmp_buf);
  2195.  
  2196.     return(gtdir_buf);  /* Can't seem to make LINK find getcwd()... */
  2197.             /* (wbader: removed &) */
  2198. #else
  2199.     char *getcwd();
  2200.     char *buf;
  2201.  
  2202.     buf = cwdbuf;
  2203.     return(getcwd(buf,100));
  2204. #endif
  2205. }
  2206.  
  2207. /*  Z X C M D  --  Run a system command so its output can be read as a file. */
  2208.  
  2209. int
  2210. zxcmd(filnum, comand) int filnum; char *comand; {
  2211.     char mbxnam[21], inpchan[6] = "NLA0:";
  2212.     unsigned long sts, pid;
  2213.     int one=1;
  2214.  
  2215.     struct dsc$descriptor_s
  2216.     mbx_desc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
  2217.     cmd_line = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
  2218.     inp_desc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  2219.  
  2220.     struct itmlstdef {
  2221.     short int buflen;
  2222.     short int itmcod;
  2223.     char *bufaddr;
  2224.     long int *retlen;
  2225.     };
  2226.  
  2227.     struct itmlstdef itmlst[] = {
  2228.     4, JPI$_PID, NULL, 0,
  2229.     0, 0, 0, 0
  2230.     };
  2231.  
  2232.     itmlst[0].bufaddr = (char *)&pid;
  2233.  
  2234.     debug(F101,"zxcmd filnum", "", filnum);
  2235.     if (filnum != ZIFILE && filnum != ZRFILE)
  2236.     return(0);
  2237.  
  2238.     sts = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0);
  2239.  
  2240.     debug(F101,"zxcmd sys$getjpiw status", "", sts);
  2241.     if (sts != SS$_NORMAL)
  2242.     return(0);
  2243.  
  2244.     sprintf(mbxnam,"KERMIT$MBX_%08X", pid);
  2245.     debug(F110,"zxcmd mailbox logical", mbxnam, 0);
  2246.  
  2247.     mbx_desc.dsc$w_length = strlen(mbxnam);
  2248.     mbx_desc.dsc$a_pointer = mbxnam;
  2249.  
  2250.     sts = sys$crembx(0, &mbx_chan, SUB_BUF_SIZE, 0, 0, 0, &mbx_desc, 0);
  2251.  
  2252.     debug(F101,"zxcmd sys$crembx status", "", sts);
  2253.     if (sts != SS$_NORMAL)
  2254.     return(0);
  2255.  
  2256.     debug(F101,"zxcmd sys$crembx mbx_chan", "", mbx_chan);
  2257.  
  2258.     strcat(mbxnam, ":");
  2259.     mbx_desc.dsc$w_length++;
  2260.  
  2261.     cmd_line.dsc$w_length = strlen(comand);
  2262.     cmd_line.dsc$a_pointer = comand;
  2263.  
  2264.     inp_desc.dsc$w_length = strlen(inpchan);
  2265.     inp_desc.dsc$a_pointer = inpchan;
  2266.  
  2267.     sts = lib$spawn(&cmd_line, &inp_desc, &mbx_desc, &one, 0, &sub_pid,
  2268.             0, 0, 0, &mbx_chan);
  2269.  
  2270.     debug(F101,"zxcmd lib$spawn status", "", sts);
  2271.     if (sts != SS$_NORMAL)
  2272.     return(0);
  2273.  
  2274.     subprocess_input = 1;
  2275.     sub_count = 0;
  2276.     fp[filnum] = fopen("NLA0:","r");    /* It wants a fp, give it one */
  2277.     debug(F101,"zxcmd fp[filnum]", "", fp[filnum]);
  2278.     fp[ZSYSFN] = fp[filnum];        /* Set ZSYSFN too, so we remember */
  2279.     return(1);
  2280. }
  2281.  
  2282. /*  Z C L O S F  - close the suprocess output file.  */
  2283.  
  2284. int
  2285. zclosf(filnum) int filnum; {
  2286.     unsigned long sts;
  2287.  
  2288.     if (subprocess_input != 0) {
  2289.     sts = sys$delprc(&sub_pid, 0);
  2290.  
  2291.     debug(F101,"zclosf sys$delprc status", "", sts);
  2292.  
  2293.     sts = sys$delmbx(mbx_chan);
  2294.  
  2295.     debug(F101,"zclosf sys$delmbx status", "", sts);
  2296.  
  2297.     sts = sys$dassgn(mbx_chan);
  2298.  
  2299.     debug(F101,"zclosf sys$dassgn status", "", sts);
  2300.  
  2301.     sub_ptr = sub_buf;            /* flush remaining data */
  2302.     sub_count = 1;
  2303.     *sub_buf = '\0';
  2304.     zincnt = 0;
  2305.  
  2306.     fclose(fp[filnum]);            /* Close the place-holders */
  2307.     fp[filnum] = fp[ZSYSFN] = NULL;
  2308.     }
  2309.     subprocess_input = 0;            /* Say we're done */
  2310.     return(1);
  2311. }
  2312.  
  2313. /*  Z X P A N D  --  Expand a wildcard string into an array of strings.  */
  2314.  
  2315. /*
  2316.   Returns the number of files that match fn1, with data structures set up
  2317.   so that first file (if any) will be returned by the next znext() call.
  2318. */
  2319. int
  2320. zxpand(fn) char *fn; {
  2321.     if (strlen(fn) == 0)        /* Nothing asked for, */
  2322.       return(0);            /* nothing returned. */
  2323.     fcount = fgen(fn,mtchs,MAXWLD);    /* Look up the file. */
  2324.     if (fcount > 0) {
  2325.     mtchptr = mtchs;        /* Save pointer for next. */
  2326.     debug(F111," zxpand",mtchs[0],fcount);
  2327.     }
  2328.     return(fcount);
  2329. }
  2330.  
  2331. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  2332.  
  2333. /*
  2334.  Returns >0 if there's another file, with its name copied into the arg string,
  2335.  or 0 if no more files in list.
  2336. */
  2337. int
  2338. znext(fn) char *fn; {
  2339.  
  2340.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  2341.     else *fn = '\0';
  2342.     debug(F111," znext",fn,fcount+1);
  2343.     return(fcount+1);
  2344. }
  2345.  
  2346. /*  Z N E W N  --  Make a new name for the given file.  */
  2347.  
  2348. VOID
  2349. znewn(fn,s) char *fn, **s; {
  2350.     static char buf[NAM$C_MAXRSS+8];
  2351.     int x, flag = 0;
  2352.     long v = -1L;
  2353.     x = strlen(fn) - 1;
  2354.     strncpy(buf, fn, NAM$C_MAXRSS);
  2355.     while (x >= 0) {
  2356.     if (buf[x] == ';') {
  2357.         v = atol(buf+x+1);
  2358.         sprintf(buf+x+1,"%ld",v+1);
  2359.         flag = 1;
  2360.         break;
  2361.     }
  2362.     x--;
  2363.     }
  2364.     if (!flag) strcat(buf,";0");
  2365.     *s = buf;
  2366. }
  2367.  
  2368. struct zfnfp *
  2369. zfnqfp(fname, buflen, buf)  char * fname; int buflen; char * buf; {
  2370.     int x = 0, y = 0;
  2371.     char * cp;
  2372.     static struct zfnfp fnfp;
  2373.  
  2374.     struct FAB fab;
  2375.     struct NAM nam;
  2376.     char expanded_name[NAM$C_MAXRSS];
  2377.     int long rms_status;
  2378.     int cur_len = 0;
  2379.     int cp_len = buflen;
  2380.  
  2381.     if (!fname)
  2382.       return(NULL);
  2383.  
  2384.     /* initialize the data structure */
  2385.     cp = fname;
  2386.     debug(F111,"zfnqfp 1",cp,buflen);
  2387.     fnfp.len = buflen;
  2388.     fnfp.fpath = buf;
  2389.     fnfp.fname = NULL;
  2390.  
  2391.     fab = cc$rms_fab;
  2392.     fab.fab$l_nam = &nam;
  2393.     fab.fab$l_fna = cp;
  2394.     fab.fab$b_fns = strlen(cp);
  2395.     fab.fab$l_dna = 0;
  2396.  
  2397.     nam = cc$rms_nam;
  2398.     nam.nam$l_esa = (char *)&expanded_name;
  2399.     nam.nam$b_ess = sizeof(expanded_name);
  2400.  
  2401.     if (!CHECK_ERR("%%CKERMIT-W-PARSE, ",
  2402.            sys$parse(&fab)))
  2403.       return((struct zfnfp *)-1);
  2404.  
  2405.     cp = buf;
  2406.     *cp = '\0';            /* Make a zero length string */
  2407.     fnfp.fpath = cp;
  2408.     if (nam.nam$b_node && cur_len+nam.nam$b_node < cp_len) {
  2409.     cur_len += nam.nam$b_node;
  2410.     strncat(cp, nam.nam$l_node, (int)nam.nam$b_node);
  2411.     }
  2412.     if (nam.nam$b_dev && cur_len+nam.nam$b_dev < cp_len) {
  2413.     cur_len += nam.nam$b_dev;
  2414.     strncat(cp, nam.nam$l_dev, (int)nam.nam$b_dev);
  2415.     }
  2416.     if (nam.nam$b_dir && cur_len+nam.nam$b_dir < cp_len) {
  2417.     cur_len += nam.nam$b_dir;
  2418.     strncat(cp, nam.nam$l_dir, (int)nam.nam$b_dir);
  2419.     }
  2420.     if (nam.nam$b_name && cur_len+nam.nam$b_name < cp_len) {
  2421.     fnfp.fname = buf + cur_len;    /* Got it, set pointer */
  2422.     cur_len += nam.nam$b_name;
  2423.     strncat(cp, nam.nam$l_name, (int)nam.nam$b_name);
  2424.     }
  2425.     if (nam.nam$b_type && cur_len+nam.nam$b_type < cp_len) {
  2426.     cur_len += nam.nam$b_type;
  2427.     strncat(cp, nam.nam$l_type, (int)nam.nam$b_type);
  2428.     }
  2429.     if (nam.nam$b_ver && cur_len+nam.nam$b_ver < cp_len) {
  2430.     cur_len += nam.nam$b_ver;
  2431.     strncat(cp, nam.nam$l_ver, (int)nam.nam$b_ver);
  2432.     }
  2433.     debug(F111,"zfnqfp 2",buf,cur_len);
  2434.     if (cur_len > 1) {
  2435.     if (buf[cur_len-1] == ';' && buf[cur_len-2] == '.') {
  2436.         buf[cur_len-2] = NUL;
  2437.         cur_len -= 2;
  2438.     }
  2439.     }
  2440.     debug(F111,"zfnqfp 3",buf,cur_len); 
  2441.     return(&fnfp);
  2442. }
  2443.  
  2444. /*
  2445.  * fgen:
  2446.  *  This is the actual name generator.  It is passed a string,
  2447.  *  possibly containing wildcards, and an array of character pointers.
  2448.  *  It finds all the matching filenames and stores them into the array.
  2449.  *  The returned strings are allocated from a static buffer local to
  2450.  *  this module (so the caller doesn't have to worry about deallocating
  2451.  *  them); this means that successive calls to fgen will wipe out
  2452.  *  the results of previous calls.  This isn't a problem here
  2453.  *  because we process one wildcard string at a time.
  2454.  *
  2455.  * Input: a wildcard string, an array to write names to, the
  2456.  *        length of the array.
  2457.  * Returns: the number of matches.  The array is filled with filenames
  2458.  *          that matched the pattern.  If there wasn't enough room in the
  2459.  *        array, -1 is returned.
  2460.  */
  2461. int
  2462. fgen(pat,resarry,len) char *pat,*resarry[]; int len; {
  2463.     struct dsc$descriptor_s
  2464.     file_spec = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  2465.     result = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  2466.     deflt = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  2467.     unsigned long context = 0, status;
  2468.     int count = 0;
  2469.     char *def_str = "*.*";
  2470.  
  2471.     file_spec.dsc$w_length  = strlen(pat);
  2472.     file_spec.dsc$a_pointer = pat;
  2473.  
  2474.     deflt.dsc$w_length  = sizeof(def_str)-1;
  2475.     deflt.dsc$a_pointer = def_str;
  2476.  
  2477.     while (count < len
  2478.        && (status = lib$find_file(&file_spec, &result, &context, &deflt))
  2479.         == RMS$_NORMAL) {
  2480.         resarry[count] = malloc(result.dsc$w_length + 1);
  2481.     strncpy(resarry[count], result.dsc$a_pointer, result.dsc$w_length);
  2482.     resarry[count][result.dsc$w_length] = '\0';
  2483.     count++;
  2484.     }
  2485. #ifdef DVI$_ALT_HOST_TYPE
  2486.     lib$find_file_end(&context);    /* Only on V4 and later */
  2487. #endif
  2488.     lib$sfree1_dd(&result);
  2489.     if (status == RMS$_FNF) return((count <= len) ? 0 : -1);
  2490.     if (status == RMS$_NMF) return(count);
  2491.     /* Bernd Onasch says that VMS sometimes returns RMS$_NORMAL here, so... */
  2492.     if (status == RMS$_NORMAL) return(count);
  2493.     /* Some other status.  Return 0. */
  2494.     /* Improve this later based on results from following debug stmt. */
  2495.     debug(F101,"fgen unexpected failure status","",status);
  2496.     return(0);
  2497. }
  2498.  
  2499. /*  Z R E N A M E  --  Rename a file.  */
  2500.  
  2501. /*  Call with old and new names */
  2502. /*  Returns 0 on success, -1 on failure. */
  2503. int
  2504. zrename(old,new) char *old, *new; {
  2505.     int sts;
  2506.  
  2507.     sts = rename(old,new);
  2508.  
  2509.     return((sts ? -1 : 0));
  2510. }
  2511.  
  2512. /*  Z C F D A T  --  Return a file's modification time.  */
  2513.  
  2514. char *
  2515. zfcdat(name) char *name; {
  2516. /*
  2517.   Returns modification date/time of file whose name is given in the argument
  2518.   string.  Return value is a pointer to a string of the form:
  2519.   
  2520.     yyyymmdd hh:mm:ss
  2521.  
  2522.   for example 19931231 23:59:59, which represents the local time (no timezone
  2523.   or daylight savings time finagling required).  Returns an empty string ("")
  2524.   on failure.  The text pointed to by the string pointer is in a static
  2525.   buffer, and so should be copied to a safe place by the caller before any
  2526.   subsequent calls to this function.
  2527. */
  2528.  
  2529. /*
  2530.   Contributed by William Bader, 9 Nov 93, based on UNIX version: "It would
  2531.   probably be possible to get the date by opening the file and requesting a
  2532.   NAM block like ckvfio.c does, but stat seems to do the trick."
  2533. */
  2534.     struct stat statbuf;
  2535.     struct tm *tm;
  2536.     static char datebuf[20];
  2537.  
  2538.     datebuf[0] = '\0';
  2539.  
  2540.     if (name &&
  2541.         *name &&
  2542.         stat(name,&statbuf) != -1 &&
  2543.         (tm = localtime((const time_t *)&statbuf.st_mtime)))
  2544.       sprintf(datebuf, "%04d%02d%02d %02d:%02d:%02d",
  2545.         tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  2546.         tm->tm_hour, tm->tm_min, tm->tm_sec);
  2547.  
  2548.     return(datebuf);
  2549. }
  2550.  
  2551.  
  2552. /*  Z S T I M E  --  Set or compare a file's creation date/time.  */
  2553.  
  2554. /*
  2555.  * Note: There's an additional value for parameter X on VAX/VMS systems. As
  2556.  * it's horribly painful to change a file's creation date after-the-fact we
  2557.  * call zstime with an argument  of 2 to pre-set the date when creating the
  2558.  * file. An argument of 0 (which the main-line code thinks sets the date of
  2559.  * the output file) returns success but does nothing.  Note that an invalid
  2560.  * or missing attribute packet will cause $bintim to return an error, which
  2561.  * causes the routine to exit. Since we pre-set the binary time to zero, we
  2562.  * will create the file "now", or say the incoming file is newer, whichever
  2563.  * is appropriate.
  2564.  */
  2565. int
  2566. zstime(f,yy,x) char *f; struct zattr *yy; int x; {
  2567.     int rms_sts;
  2568.     static char mth[13][4] = {    "JAN","FEB","MAR","APR",
  2569.                 "MAY","JUN","JUL","AUG",
  2570.                 "SEP","OCT","NOV","DEC",
  2571.                 ""};
  2572.     static char cdate[23];          /* Creation date yyyymmdd hh:mm:ss.00 */
  2573.     static char mnum[2];
  2574.     struct dsc$descriptor_s
  2575.     bintim_desc = {sizeof(cdate), DSC$K_DTYPE_T, DSC$K_CLASS_S,
  2576.              (char *)&cdate};
  2577.     unsigned long file_date[2], attr_date[2];
  2578.  
  2579. /* First, make a system quadword date from what we got passed */
  2580.  
  2581.     char *dptr = yy->date.val;
  2582.     if (!dptr) return(-1);
  2583.     strcpy(cdate,"dd-mmm-yyyy 00:00:00.00");
  2584.     attr_date[0]=0;                /* clear time in case of err */
  2585.     attr_date[1]=0;
  2586.     strncpy(cdate+7, dptr, 4);            /* yyyy */
  2587.     dptr += 4;
  2588.     strncpy(mnum, dptr, 2);
  2589.     strncpy(cdate+3, mth[atoi(mnum)-1], 3);    /* mm */
  2590.     dptr += 2;
  2591.     strncpy(cdate, dptr, 2);            /* dd */
  2592.     dptr += 3;
  2593.     strncpy(cdate+12, dptr, 8);            /* hhmmss */
  2594.     cdate[23] = '\0';                /* terminate */
  2595.     rms_sts = sys$bintim(&bintim_desc, &attr_date);
  2596.     if (rms_sts != SS$_NORMAL) {
  2597.     debug(F101," zstime - $bintim returns","",rms_sts);
  2598.     return(-1);
  2599.     }
  2600.     debug(F110," zstime built",cdate,0);
  2601.     sprintf(cdate, "%08X%08X", attr_date[1], attr_date[0]);
  2602.     debug(F110," $bintim attr_date", cdate, 0);
  2603.  
  2604.     if (x == 1) {
  2605.     fab_ifile = cc$rms_fab;
  2606.     fab_ifile.fab$b_fac = FAB$M_BIO | FAB$M_GET;
  2607.     fab_ifile.fab$l_fna = f;
  2608.     fab_ifile.fab$b_fns = strlen(f);
  2609.     fab_ifile.fab$l_xab = (char *)&xabdat_ifile;
  2610.     rab_ifile = cc$rms_rab;
  2611.     rab_ifile.rab$l_fab = &fab_ifile;
  2612.     xabdat_ifile = cc$rms_xabdat;
  2613.     rms_sts = sys$open(&fab_ifile);
  2614.     if (rms_sts != RMS$_NORMAL) {
  2615.         debug(F101," zstime $open failed, status","",rms_sts);
  2616.         return(-1);
  2617.     }
  2618.     memcpy(file_date, &xabdat_ifile.xab$q_cdt, 8);
  2619.     sprintf(cdate, "%08x%08x", file_date[1], file_date[0]);
  2620.     debug(F110," $bintim file_date", cdate, 0);
  2621.     rms_sts = sys$close(&fab_ifile);
  2622.     if (rms_sts != RMS$_NORMAL) {
  2623.         debug(F101," zstime $close failed, status","",rms_sts);
  2624.         return(-1);
  2625.     }
  2626.     if (attr_date[1] < file_date[1]) {
  2627.         debug(F100," zstime incoming file is older","",0);
  2628.         return(1);
  2629.     }
  2630.     if (attr_date[1] == file_date[1]) {
  2631.         if (attr_date[0] <= file_date[0]) {
  2632.         debug(F100," zstime incoming file is older, not by much","",0);
  2633.         return(1);
  2634.         }
  2635.     debug(F100," zstime incoming file is newer","",0);
  2636.     return(0);
  2637.     }
  2638.     }
  2639.  
  2640.     if (x == 0) {
  2641.     return(0);            /* say we did it (see header) */
  2642.     }
  2643.  
  2644.     if (x == 2) {
  2645.     memcpy(&xabdat_ofile.xab$q_cdt, attr_date, 8);
  2646.     return(0);            /* Set date in output file */
  2647.     }
  2648.  
  2649.     return(-1);
  2650. }
  2651.  
  2652. /*  Z K E R M I N I  --  Find initialization file.  */
  2653. /*
  2654.   Places name of init file in buffer pointed to by s.
  2655.   If no init file found, the device name of the null device is used.
  2656.   returns 0 always.
  2657. */
  2658. int
  2659. zkermini(s, s_len, def) char *s; int s_len; char *def; {
  2660.     FILE fd;
  2661.     struct dsc$descriptor_s
  2662.         dsc_in = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  2663.         dsc_out = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  2664.         dsc_def = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  2665.     int max_len;
  2666.     long unsigned int rms_s;
  2667.     unsigned long find_file_context = 0;
  2668.  
  2669.     struct TRNLIST {
  2670.     char *name;            /* ASCII file or logical name */
  2671.     unsigned char flag;        /* Zero to use default filename */
  2672.     } *p;
  2673.  
  2674.     static struct TRNLIST slist[] = {
  2675.     {"", 0},            /* Dummy first entry points to file */
  2676.     {"ckermit_ini:", 0},        /* CKERMIT_INI: points to directory */
  2677.     {"ckermit_init", 1},        /* CKERMIT_INIT points to file      */
  2678.     {"sys$login:",   0},        /* CKERMIT.INI in login directory   */
  2679.     {"", 0}
  2680.     };
  2681.     p = slist;                /* Point to list */
  2682.     if (rcflag) {            /* Name given on command line? */
  2683.     slist[0].name = def;        /* Yes, stuff its name into slist */
  2684.     slist[1].name = "";
  2685.     } else {                /* No, */
  2686.     *p++;                /* skip past dummy entry. */
  2687.     }
  2688.     while(*(p->name)) {            /* Search the list top to bottom */
  2689.  
  2690.     dsc_in.dsc$w_length = strlen(p->name); /* Length of work area */
  2691.     dsc_in.dsc$a_pointer = p->name; /* Address of string */
  2692.  
  2693.     if (!(p->flag)) {
  2694.         dsc_def.dsc$w_length = strlen(def); /* Length of work area */
  2695.         dsc_def.dsc$a_pointer = def; /* Address of string */
  2696.     } else {
  2697.         dsc_def.dsc$w_length = 0;    /* Length of work area */
  2698.         dsc_def.dsc$a_pointer = 0;    /* Address of string */
  2699.     }
  2700.     rms_s = lib$find_file(
  2701.                 &dsc_in,    /* File spec */
  2702.                 &dsc_out,    /* Result file spec */
  2703.                 &find_file_context, /* Context */
  2704.                 &dsc_def,    /* Default file spec */
  2705.                 0,        /* Related spec */
  2706.                 0,        /* STV error */
  2707.                 0);        /* Flags */
  2708.  
  2709.     if (rms_s == RMS$_NORMAL) {
  2710.         max_len = ((unsigned short int) dsc_out.dsc$w_length < s_len ?
  2711.                (unsigned short int) dsc_out.dsc$w_length : 0);
  2712.         if (!max_len)
  2713.           fprintf(stderr,
  2714.         "%%ZKERMINI out string not long enough, ignoring .ini file\n");
  2715.         else
  2716.           strncpy(s,dsc_out.dsc$a_pointer,max_len);
  2717.         lib$find_file_end(&find_file_context);
  2718.         lib$sfree1_dd(&dsc_out);    /* Return dyno memory */
  2719.         return(0);
  2720.     }
  2721.     p++;
  2722.     lib$find_file_end(&find_file_context);
  2723.     }
  2724. /*
  2725.  * No initialization file found.  We can't return the null string because the
  2726.  * runtime library will successfully open it if the file ".;" exists in the
  2727.  * user's directory.  Instead we return the name of the null device.
  2728.  */
  2729.     strcpy(s, "NLA0:");                /* Return null init file */
  2730.     lib$sfree1_dd(&dsc_out);
  2731.     return(0);
  2732. }
  2733.  
  2734. static int
  2735. parse_fname(cp, cp_len, defnam, flag)
  2736. char *cp;        /* Pointer to file spec to parse */
  2737. int cp_len;        /* Length of cp field */
  2738. char *defnam;        /* Default file spec */
  2739. int flag;        /* Flag word PARSE_xxx */
  2740. {
  2741.     struct FAB fab;
  2742.     struct NAM nam;
  2743.     char expanded_name[NAM$C_MAXRSS];
  2744.     int long rms_status;
  2745.     int cur_len = 0;
  2746.  
  2747.     fab = cc$rms_fab;
  2748.     fab.fab$l_nam = &nam;
  2749.     fab.fab$l_fna = cp;
  2750.     fab.fab$b_fns = strlen(cp);
  2751.     if (defnam) {
  2752.     fab.fab$b_dns = strlen(defnam);
  2753.     fab.fab$l_dna = defnam;
  2754.     } else
  2755.     fab.fab$l_dna = 0;
  2756.  
  2757.     nam = cc$rms_nam;
  2758.     nam.nam$l_esa = (char *)&expanded_name;
  2759.     nam.nam$b_ess = sizeof(expanded_name);
  2760.  
  2761.     if (!CHECK_ERR("%%CKERMIT-W-PARSE, ",
  2762.         sys$parse(&fab)))
  2763.     return(-1);
  2764.  
  2765.     *cp = '\0';            /* Make a zero length string */
  2766.     if ((PARSE_NODE & flag) && nam.nam$b_node &&
  2767.         cur_len+nam.nam$b_node < cp_len) {
  2768.     cur_len += nam.nam$b_node;
  2769.     strncat(cp, nam.nam$l_node, (int)nam.nam$b_node);
  2770.     }
  2771.     if ((PARSE_DEVICE & flag) && nam.nam$b_dev &&
  2772.         cur_len+nam.nam$b_dev < cp_len) {
  2773.     cur_len += nam.nam$b_dev;
  2774.     strncat(cp, nam.nam$l_dev, (int)nam.nam$b_dev);
  2775.     }
  2776.     if ((PARSE_DIRECTORY & flag) && nam.nam$b_dir &&
  2777.         cur_len+nam.nam$b_dir < cp_len) {
  2778.     cur_len += nam.nam$b_dir;
  2779.     strncat(cp, nam.nam$l_dir, (int)nam.nam$b_dir);
  2780.     }
  2781.     if ((PARSE_NAME & flag) && nam.nam$b_name &&
  2782.         cur_len+nam.nam$b_name < cp_len) {
  2783.     cur_len += nam.nam$b_name;
  2784.     strncat(cp, nam.nam$l_name, (int)nam.nam$b_name);
  2785.     }
  2786.     if ((PARSE_TYPE & flag) && nam.nam$b_type &&
  2787.         cur_len+nam.nam$b_type < cp_len) {
  2788.     cur_len += nam.nam$b_type;
  2789.     strncat(cp, nam.nam$l_type, (int)nam.nam$b_type);
  2790.     }
  2791.     if ((PARSE_VERSION & flag) && nam.nam$b_ver &&
  2792.         cur_len+nam.nam$b_ver < cp_len) {
  2793.     cur_len += nam.nam$b_ver;
  2794.     strncat(cp, nam.nam$l_ver, (int)nam.nam$b_ver);
  2795.     }
  2796.     return(cur_len);
  2797. }
  2798.  
  2799. /*  Z S A T T R  --  Fill in a Kermit attribute structure for current file.  */
  2800.  
  2801. /*
  2802.  Fills in a Kermit file attribute structure for the file which is to be sent.
  2803.  Returns 0 on success with the structure filled in, or -1 on failure.
  2804.  If any string member is null, then it should be ignored.
  2805.  If any numeric member is -1, then it should be ignored.
  2806. */
  2807. int
  2808. zsattr(xx) struct zattr *xx; {
  2809.     long k;
  2810.     int x;
  2811.     static char mth[13][4] = {    "JAN","FEB","MAR","APR",
  2812.                 "MAY","JUN","JUL","AUG",
  2813.                 "SEP","OCT","NOV","DEC",
  2814.                 ""};
  2815.     static char recfm[15];        /* record format */
  2816.     static char cdate[20];          /* Creation date [yy]yymmdd[hh:mm[:ss]]*/
  2817.     static char creater_id[31];        /* Creator ID string */
  2818.     static char genprot;            /* Generic protection */
  2819.     static unsigned short lclprot;  /* Local protection */
  2820.     static long sysparam_size=0;    /* Length of system paramater buffer */
  2821.     static char *sysparam_adr=0;    /* Address of system paramater buffer */
  2822.     char type;                /* File type */
  2823.     short int asctim_retlen;
  2824.     char asctim_buf[24];        /* Work buffer for ASCTIM() */
  2825.     struct dsc$descriptor_s
  2826.      asctim_dsc = {sizeof(asctim_buf),DSC$K_DTYPE_T,DSC$K_CLASS_S,
  2827.            NULL};
  2828. /*  static long int i;  */
  2829.     static unsigned short id_len;
  2830.     static struct dsc$descriptor_s id_str =
  2831.       {31,DSC$K_DTYPE_T,DSC$K_CLASS_S,creater_id};
  2832.  
  2833. /*
  2834.  * Zero out strings
  2835.  */
  2836.  
  2837.     type = 0;
  2838.     recfm[0] = '\0';
  2839.     cdate[0] = '\0';
  2840.     creater_id[0] = '\0';
  2841.     id_len = 0;
  2842.     genprot = 0;            /* Blank protection by default */
  2843.     lclprot = 0;
  2844.     asctim_dsc.dsc$a_pointer = (char *)&asctim_buf;
  2845.  
  2846. /*
  2847.  * See if we are sending "attributes" from a REMOTE command response
  2848.  */
  2849.  
  2850.     if (*nambuf == '\0') {
  2851.     xx->lengthk = 1;        /* Number of 1K blocks rounded up */
  2852.     xx->type.len = 0;        /* File type can't be filled in here */
  2853.     xx->type.val = "";
  2854.     xx->date.len = strlen(cdate);    /* File creation date */
  2855.     xx->date.val = (char *)&cdate;
  2856.     xx->creator.len = strlen(creater_id); /* File creator */
  2857.     xx->creator.val = (char *)&creater_id;
  2858.     xx->account.len = 0;        /* File account */
  2859.     xx->account.val = "";
  2860.     xx->area.len = 0;        /* File area */
  2861.     xx->area.val = "";
  2862.     xx->password.len = 0;        /* Area password */
  2863.     xx->password.val = "";
  2864.     xx->blksize = -1L;        /* File blocksize */
  2865.     xx->xaccess.len = 0;        /* File access */
  2866.     xx->xaccess.val = "";
  2867.     xx->encoding.len = 1;        /* Transfer syntax */
  2868.     xx->encoding.val = "A";        /* ASCII */
  2869.     xx->disp.len = 0;        /* Disposition upon arrival */
  2870.     xx->disp.val = "";
  2871.     xx->lprotect.len = sizeof(lclprot); /* Local protection */
  2872.     xx->lprotect.val = (char *)&lclprot;
  2873.     xx->gprotect.len = sizeof(genprot); /* Generic protection */
  2874.     xx->gprotect.val = &genprot;
  2875.     xx->systemid.len = 2;        /* System ID for DEC/VMS */
  2876.     xx->systemid.val = "D7";
  2877.     xx->recfm.len = strlen(recfm);    /* Record format */
  2878.     xx->recfm.val = (char *)&recfm;
  2879.     xx->sysparam.len = sysparam_size; /* System-dependent parameters */
  2880.     xx->sysparam.val = sysparam_adr;
  2881.     xx->length = 1;            /* Length */
  2882.     return(0);            /* mumble sweet nothings at it */
  2883.     }
  2884.  
  2885. /*
  2886.  * Load the generic protection
  2887.  */
  2888.  
  2889.     x = xabpro_ifile.xab$w_pro >> XAB$V_WLD;    /* grab returned info */
  2890.     if (!(x & XAB$M_NOREAD))  genprot |= 1+32;    /* Read access */
  2891.     if (!(x & XAB$M_NOWRITE)) genprot |= 2+8;    /* Write+Append access */
  2892.     if (!(x & XAB$M_NOEXE))   genprot |= 4;    /* Execute protection */
  2893.     if (!(x & XAB$M_NODEL))   genprot |= 16;    /* Delete Access */
  2894.     lclprot = xabpro_ifile.xab$w_pro;        /* local protection */
  2895.  
  2896. /*
  2897.  * Convert creation date from an internal value to common ascii string
  2898.  */
  2899.  
  2900.     sys$asctim(&asctim_retlen,&asctim_dsc,&xabdat_ifile.xab$q_cdt,0);
  2901.     asctim_buf[asctim_retlen] = '\0';
  2902.     debug(F110," zsattr asctim_buf",asctim_buf,0);
  2903.     for (x = 0; strncmp(mth[x], asctim_buf+3,3); x++) /* Find month */
  2904.       ;
  2905.     strncpy(cdate,asctim_buf+7,4);    /* 'yyyy' */
  2906.     sprintf(cdate+4,"%02d",x+1);    /* 'mm' */
  2907.     strncpy(cdate+6,asctim_buf+0,2);    /* 'dd' */
  2908.     strncpy(cdate+8,asctim_buf+11,9);    /* ' hh:mm:ss' */
  2909.     if (cdate[6] == ' ')
  2910.     cdate[6] = '0';
  2911.     debug(F110," zsattr cdate",cdate,0);
  2912.  
  2913. /*
  2914.  * Convert the owner UIC into an alpha name
  2915.  */
  2916.  
  2917.     creater_id[0] = '\0';
  2918.     rms_sts = sys$idtoasc(xabpro_ifile.xab$l_uic,&id_len,&id_str,0,0,0);
  2919.     creater_id[id_len] = '\0';            /* terminating null, please */
  2920.     debug(F111," zsattr $idtoasc owner",creater_id,strlen(creater_id));
  2921.     if (rms_sts == SS$_NOSUCHID ||
  2922. #ifdef SS$_NORIGHTSDB    /* only vms 5 and higher */
  2923.     rms_sts == SS$_NORIGHTSDB ||
  2924. #endif /* SS$_NORIGHTSDB */
  2925.     rms_sts == SS$_IVIDENT) {
  2926.     creater_id[0] = '\0';
  2927.     rms_sts = SS$_NORMAL;            /* if unknown, null it out */
  2928.     }
  2929.     if (!(rms_sts & 1)) {
  2930.     debug(F101," zsattr $idtoasc failed, status","",rms_sts);
  2931.     return(-1);                /* fatal */
  2932.     }
  2933.  
  2934. /*
  2935.  * Fill in the record format blockette
  2936.  */
  2937.  
  2938.     if (fab_ifile.fab$b_rat & (FAB$M_CR | FAB$M_FTN | FAB$M_PRN)) {
  2939.     strcpy(recfm,"AMJ");
  2940.     } else {
  2941.     strcpy(recfm,"F");
  2942.     sprintf(recfm+1,"%05d",xabfhc_ifile.xab$w_lrl);
  2943.     }
  2944.     debug(F111," zsattr recfm",recfm,strlen(recfm));
  2945.  
  2946. /*
  2947.  * Fill in the returned data structure
  2948.  */
  2949.  
  2950.     xx->lengthk = (iflen/1024)+1;    /* Number of 1K blocks rounded up */
  2951.     xx->type.len = 0;            /* File type can't be filled in here */
  2952.     xx->type.val = "";
  2953.     xx->date.len = strlen(cdate);    /* File creation date */
  2954.     xx->date.val = (char *)&cdate;
  2955.     xx->creator.len = strlen(creater_id); /* File creator */
  2956.     xx->creator.val = (char *)&creater_id;
  2957.     xx->account.len = 0;        /* File account */
  2958.     xx->account.val = "";
  2959.     xx->area.len = 0;            /* File area */
  2960.     xx->area.val = "";
  2961.     xx->password.len = 0;        /* Area password */
  2962.     xx->password.val = "";
  2963.     xx->blksize = -1L;            /* File blocksize */
  2964.     xx->xaccess.len = 0;        /* File access */
  2965.     xx->xaccess.val = "";
  2966.     xx->encoding.len = 1;        /* Transfer syntax */
  2967.     xx->encoding.val = "A";        /* ASCII */
  2968.     xx->disp.len = 0;            /* Disposition upon arrival */
  2969.     xx->disp.val = "";
  2970.     xx->lprotect.len = sizeof(lclprot); /* Local protection */
  2971.     xx->lprotect.val = (char *)&lclprot;
  2972.     xx->gprotect.len = sizeof(genprot);    /* Generic protection */
  2973.     xx->gprotect.val = &genprot;
  2974.     xx->systemid.len = 2;        /* System ID for DEC/VMS */
  2975.     xx->systemid.val = "D7";
  2976.     xx->recfm.len = strlen(recfm);    /* Record format */
  2977.     xx->recfm.val = (char *)&recfm;
  2978.     xx->sysparam.len = sysparam_size;    /* System-dependent parameters */
  2979.     xx->sysparam.val = sysparam_adr;
  2980.     xx->length = iflen;            /* Length */
  2981.     debug(F111," zsattr lengthk","",xx->lengthk);
  2982.     debug(F111," zsattr length","",xx->length);
  2983.     return(0);
  2984. }
  2985.  
  2986. /* Z M K D I R  --  Create directory(s) if necessary */
  2987. /*
  2988.    Call with:
  2989.      A pointer to a file specification that might contain directory
  2990.      information.  The filename is expected to be included.
  2991.      If the file specification does not include any directory separators,
  2992.      then it is assumed to be a plain file.
  2993.      If one or more directories are included in the file specification,
  2994.      this routine tries to create them if they don't already exist.
  2995.    Returns:
  2996.      0 on success, i.e. the directory was created, or didn't need to be.
  2997.     -1 on failure to create the directory
  2998.    VMS version by Mark Berryman, Feb 94.
  2999. */
  3000.  
  3001. int
  3002. zmkdir(path) char *path; {
  3003.     unsigned int
  3004.       status,
  3005. /*    SYS$PARSE(), */
  3006.       lib$create_dir(),
  3007.       lib$locc();
  3008.  
  3009.     struct FAB dir_fab;
  3010.     struct NAM dir_nam;
  3011.     struct dsc$descriptor_s expanded_filename;
  3012.     $DESCRIPTOR(close_bracket,"]");
  3013.  
  3014.     char expanded_name[NAM$C_MAXRSS];
  3015.  
  3016.     dir_fab = cc$rms_fab;
  3017.     dir_fab.fab$l_fna = path;
  3018.     dir_fab.fab$b_fns = strlen(path);
  3019.     dir_fab.fab$l_nam = &dir_nam;
  3020.  
  3021.     dir_nam = cc$rms_nam;
  3022.     dir_nam.nam$l_esa = (char *) &expanded_name;
  3023.     dir_nam.nam$b_ess = sizeof(expanded_name);
  3024.  
  3025.     status = sys$parse(&dir_fab,0,0);
  3026.  
  3027. /*  If the result of SYS$PARSE is RMS$_NORMAL we need do nothing. */
  3028.     if (status == RMS$_NORMAL) {
  3029.     debug(F100,"zmkdir path already exists",path,0);
  3030.     return 0;
  3031.     } 
  3032.     debug(F111,"zmkdir status",path,status);
  3033.     debug(F101,"zmkdir RMS$_DNF","",RMS$_DNF);
  3034.  
  3035. /*  If the result is anything other than RMS$_DNF, it is fatal. */
  3036.     if (status != RMS$_DNF)
  3037.     return -1;
  3038.  
  3039. /*  The parse succeeded but said the directory didn't exist, so create it. */
  3040.     expanded_filename.dsc$b_class = DSC$K_CLASS_S;
  3041.     expanded_filename.dsc$b_dtype = DSC$K_DTYPE_T;
  3042.     expanded_filename.dsc$a_pointer = (char *) &expanded_name;
  3043.     expanded_filename.dsc$w_length = dir_nam.nam$b_esl;
  3044.  
  3045. /*  Strip the resulting specification to include only device and directory */
  3046.     status = lib$locc(&close_bracket,&expanded_filename);
  3047.     expanded_filename.dsc$w_length = status;
  3048.  
  3049.     debug(F110,"zmkdir creating",(char *) &expanded_name,0);
  3050.     status = lib$create_dir(&expanded_filename,0,0,0,0,0);
  3051.     debug(F101,"zmkdir final status","",status);
  3052.     return (status == SS$_CREATED ? 0 : -1);
  3053. }
  3054.  
  3055. /*  Z M A I L  --  Send file f as mail to address p.  */
  3056. /*
  3057.   Returns 0 on success
  3058.    2 if mail delivered but temp file can't be deleted
  3059.   -2 if mail can't be delivered
  3060. */
  3061. int
  3062. zmail(p,f) char *p; char *f; {
  3063.     char *zmbuf;
  3064.     static char spbuf[] = "$ mail %s %s/subj=\"Enclosed file %s\"";
  3065.     static char spbuf2[] = "%s;";
  3066.     unsigned long int sts;
  3067.  
  3068.     zmbuf = malloc(strlen(p)+(2*strlen(f))+sizeof(spbuf));
  3069.     sprintf(zmbuf,spbuf, f, p, f);
  3070.     sts = system(zmbuf);
  3071.     debug(F111," zmail: system returns status ",zmbuf,sts);
  3072.     free(zmbuf);
  3073.     if ((sts&1) != 1) {
  3074.       debug(F101," zmail: returning","",-2);
  3075.       return(-2);
  3076.     }
  3077.     zmbuf = malloc(strlen(f)+sizeof(spbuf2));
  3078.     sprintf(zmbuf,spbuf2, f);
  3079.     sts = delete(zmbuf);
  3080.     debug(F111," zmail: delete returns status ",zmbuf,sts);
  3081.     free(zmbuf);
  3082.     if (sts) sts = 2;
  3083.     debug(F101," zmail: returning","",sts);
  3084.     return(sts);
  3085. }
  3086.  
  3087. /* Z P R I N T  --  Print file f with options p.  */
  3088. /*
  3089.   Returns 0 on success, -3 on failure.
  3090. */
  3091. int
  3092. zprint(p,f) char *p; char *f; {
  3093.     char *zmbuf;
  3094.     static char spbuf[] = "$ print/delete %s %s";
  3095.     unsigned long int sts;
  3096.  
  3097.     zmbuf = malloc(strlen(p)+strlen(f)+sizeof(spbuf));
  3098.     sprintf(zmbuf,spbuf, p, f);
  3099.     sts = system(zmbuf);
  3100.     debug(F111," zprint: system returns status ",zmbuf,sts);
  3101.     free(zmbuf);
  3102.     debug(F101," zprint: returning","",(sts&1) ? 0 : -3);
  3103.     return((sts&1) ? 0 : -3);
  3104. }
  3105.  
  3106. /* Z S Y S C M D  --  Execute a DCL command with direct output.  */
  3107.  
  3108. /*
  3109.  * Since it's really difficult to have an alternate CLI under VMS (since the
  3110.  * MCR interface isn't documented and POSIX hasn't published the interface,
  3111.  * we'll just assume everybody uses DCL and hand it of to zshcmd().
  3112.  */
  3113. int
  3114. zsyscmd(s) char *s; {
  3115.     return(zshcmd(s));
  3116. }
  3117.  
  3118. /*
  3119.  * Dummy function, since SIG_DFL seems to be broken with DEC C 
  3120.  */
  3121. VOID
  3122.  sig_dum()
  3123.  {
  3124.  } 
  3125.  
  3126. /* Z S H C M D  --  Execute a default CLI command with direct output.  */
  3127.  
  3128. /*
  3129.  * As it's _REALLY_ unlikely that the user is using MCR as his default CLI,
  3130.  * and DEC doesn't document how to write any other alternate CLIs, use DCL.
  3131.  */
  3132.  
  3133. #ifndef    SS$_EXPRCLM        /* VMS doesn't return this yet, but let's */
  3134. #define SS$_EXPRCLM 10804    /* be forward-thinking and anticpate VMS */
  3135. #endif /* SS$_EXPRCLM */    /* V6.0, which will return it. */
  3136. int
  3137. zshcmd(s) char *s; {
  3138.     unsigned long sts, cc;
  3139.     void (*cct)();
  3140.     void (*sig_dum_ptr)() = sig_dum;
  3141.     struct dsc$descriptor_s
  3142.     cmd_line = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  3143.  
  3144.     if (check_spawn() != 0) {
  3145.     debug(F100," zshcmd: spawning prohibited by UAF flags","",0);
  3146.     return(0);
  3147.     }
  3148.     cct = signal(SIGINT,sig_dum_ptr);    /* Let inferior process catch ^C */
  3149.  
  3150.     cmd_line.dsc$w_length = strlen(s);
  3151.     cmd_line.dsc$a_pointer = s;
  3152.  
  3153.     if (!(*s))
  3154.     printf("Type LOGOUT to return to VMS C-Kermit.\n\n");
  3155.     sts = lib$spawn(&cmd_line, 0, 0, 0, 0, 0, &cc, 0, 0, 0, 0, 0);
  3156.     signal(SIGINT,cct);
  3157. /*
  3158.  * Note: We can't check for this beforehand as doing a getjpi for prclm will
  3159.  *     only return the UAF value, not the available value. So we try it and
  3160.  *     print this message if it didn't work.
  3161.  */
  3162.  
  3163.     if ((sts == SS$_EXQUOTA) || (sts == SS$_EXPRCLM)) {
  3164.     printf(
  3165. "Your account does not have sufficient quotas to use this command.\n");
  3166.     printf(
  3167. "Please ask your system manager to increase your UAF PRCLM quota.\n");
  3168.     }
  3169.     debug(F101,"zshcmd lib$spawn sts", "", sts);
  3170.     debug(F101,"zshcmd lib$spawn cc ", "", cc);
  3171.     if (sts == SS$_NORMAL)
  3172.       return(cc == SS$_NORMAL ? 1 : 0);    /* Success */
  3173.     else
  3174.       return(0);            /* Failure */
  3175. }
  3176.  
  3177. /*  Z S T R I P  --  Strip device & directory name from file specification.  */
  3178.  
  3179. /*  Strip pathname from filename "name", return pointer to result in name2 */
  3180.  
  3181. static char work[1100];    /* buffer for use by zstrip and zltor */
  3182.  
  3183. VOID
  3184. zstrip(name,name2) char *name, **name2; {
  3185.     char *cp, *pp;
  3186.     char last;
  3187.     int len;
  3188.     debug(F110,"zstrip entry",name,0);
  3189.     pp = work;                /* Default return is empty string */
  3190.     *name2 = work;
  3191.     *pp = '\0';
  3192.     if (!name)
  3193.       return;
  3194.     if (!*name)
  3195.       return;
  3196.  
  3197. /*  NODE::DEV:[DIR] terminates on on final ':', '>' or ']'.  */
  3198.  
  3199.     for (cp = name; *cp; cp++) {
  3200.     last = *cp;
  3201.         if (*cp == '/' || *cp == ':' || *cp == '>' || *cp == ']') /* slash? */
  3202.       pp = work;
  3203.     else if (*cp == ';')        /* Chop off any version number */
  3204.       break;
  3205.     else                /* Part of filename */
  3206.       *pp++ = *cp;
  3207.     }
  3208.     *pp = '\0';                /* Terminate the string */
  3209.     debug(F000,"zstrip 2",work,last);
  3210. #ifdef COMMENT
  3211. /*
  3212.   This is a bad idea because the result will need to be passed thru zstrip()
  3213.   again, but zstrip() isn't designed to call itself.
  3214. */
  3215.     if (work[0] == '\0' && last == ':') { /* Result is empty? */
  3216.     char * q;            /* Maybe it's a logical name */
  3217.     q = (char *)malloc(1100);
  3218.     if (q) {
  3219.         strncpy(q,name,1099);
  3220.         len = strlen(q);
  3221.         if (len > 0) {
  3222.         if (q[len-1] == ':') {
  3223.             char *t = q;
  3224.             q[len-1] = '\0';
  3225.             while (*t) { if (islower(*t)) *t = toupper(*t); t++; }
  3226.             debug(F110,"zstrip checking",q,0);
  3227.             pp = getenv(q);
  3228.             if (!pp) pp = "";
  3229.             strncpy(work,pp,1099);
  3230.             debug(F110,"zstrip getenv",work,0);
  3231.         }
  3232.         }
  3233.         free(q);
  3234.     }
  3235.     }    
  3236.     debug(F110,"zstrip 3",work,0);
  3237. #endif /* COMMENT */
  3238.  
  3239. /* The following should allow us to receive files to LPT:, LTA1:, etc. */
  3240.  
  3241.     if (work[0] == '\0') {        /* Still empty? */
  3242.     debug(F000,"zstrip last",name,last); /* If it's a device name */
  3243.     if (last == ':')        /* put it back */
  3244.       strncpy(work,name,1099);
  3245.     }    
  3246.     debug(F110,"zstrip result",*name2,0);
  3247. }
  3248.  
  3249. int
  3250. zchkpath(s) char *s; {
  3251. /*
  3252.   This needs to be replaced with something more intelligent.
  3253.   The idea is to see if the file, whose specification is pointed to by s,
  3254.   is in the current directory.  This function should return 0 if it s,
  3255.   nonzero otherwise.  Presently we rely on being called with a full
  3256.   filespec of the form DISK:[DEV]NAME.TYP;V, so this works more or less
  3257.   by accident.  What we really need is to call some kind of VMS service
  3258.   to get the NODE::DEV:[DIR] of the file, and compare with the current
  3259.   NODE::DEV:[DIR].
  3260. */
  3261.     char *p;
  3262.     p = zgtdir();            /* Get current dir. */
  3263.     debug(F110,"zchkpath file",s,0);
  3264.     debug(F110,"zchkpath current dir",p,0);
  3265.     return(strncmp(p,s,strlen(p)));    /* Compare it. */
  3266. }
  3267.  
  3268. #ifdef OLD_VMS
  3269. static VOID
  3270. descname(desc,name) struct dsc$descriptor_s *desc; char *name; {
  3271.     desc->dsc$w_length = strlen(name);    /* Length of name */
  3272.     desc->dsc$a_pointer = name;        /* Address */
  3273.     desc->dsc$b_class = DSC$K_CLASS_S;    /* String descriptor class */
  3274.     desc->dsc$b_dtype = DSC$K_DTYPE_T;    /* ASCII string data type */
  3275. }
  3276.  
  3277. /* VMS version of RENAME */
  3278. int /* ? */
  3279. rename(oldname, newname) char oldname[], newname[]; {
  3280.     struct dsc$descriptor_s old_desc, new_desc;
  3281.     int lib$rename_file();
  3282.  
  3283.     /* Build string descriptors */
  3284.  
  3285.     descname(&old_desc, oldname);
  3286.     descname(&new_desc, newname);
  3287.  
  3288.     /* Call lib$rename_file routine */
  3289.  
  3290.     return(lib$rename_file(&old_desc, &new_desc, 0,0,0,0,0,0,0,0,0,0));
  3291. }
  3292. #endif /* OLD_VMS */
  3293.  
  3294. /*
  3295.  * Check to see if we have spawn priv's.
  3296.  */
  3297. int
  3298. check_spawn() {
  3299.     struct itmlstdef {
  3300.     short int buflen;
  3301.     short int itmcod;
  3302.     char *bufaddr;
  3303.     long int *retlen;
  3304.     };
  3305.  
  3306.     struct itmlstdef itmlst[] =
  3307.     {4,JPI$_UAF_FLAGS,0,0,0,0,0,0};
  3308.  
  3309.     long uaf_flags_size;
  3310.     unsigned long uaf_flags;
  3311.  
  3312.     itmlst[0].bufaddr = (char *)&uaf_flags;
  3313.     itmlst[0].retlen = &uaf_flags_size;
  3314.  
  3315.     if ((vms_status = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0)) != SS$_NORMAL)
  3316.     return(-1);                /* Assume the worst... */
  3317.  
  3318. #ifdef UAI$M_CAPTIVE
  3319.     if (uaf_flags & UAI$M_CAPTIVE) {
  3320.     printf(
  3321. "\nThis command cannot be executed. Your account is CAPTIVE.\n\n");
  3322.     return(-1);
  3323.     }
  3324. #endif  /* UAI$M_CAPTIVE */
  3325. #ifdef    UAI$M_RESTRICTED            /* for pre-V5.2 systems */
  3326.     if (uaf_flags & UAI$M_RESTRICTED) {
  3327.     printf(
  3328. "\nThis command cannot be executed. Your account is CAPTIVE.\n\n");
  3329.     return(-1);
  3330.     }
  3331. #endif    /* uai$v_restricted */
  3332.     return(0);
  3333. }
  3334.  
  3335. /*
  3336.  * Stuff having to do with SET FILE TYPE LABELED
  3337.  */
  3338. char *
  3339. get_vms_vers() {
  3340.     static char sysver[9];
  3341.     int len;
  3342.     struct itmlst {
  3343.           short int buflen;
  3344.           short int code;
  3345.           char *bufadr;
  3346.           int *retlen;
  3347.           } vms_sysver[2];
  3348.  
  3349.     vms_sysver[0].buflen = 8;
  3350.     vms_sysver[0].code = SYI$_VERSION;
  3351.     vms_sysver[0].bufadr = (char *)&sysver;
  3352.     vms_sysver[0].retlen = &len;
  3353.     vms_sysver[1].buflen = 0;
  3354.     vms_sysver[1].code = 0;
  3355.     sys$getsyiw(0,0,0,&vms_sysver,0,0,0);
  3356.     sysver[8]='\0';
  3357.     len = 7;
  3358.     while (sysver[len] == ' ') {
  3359.     sysver[len] = '\0';
  3360.     len--;
  3361.     }
  3362.     return(sysver);
  3363. }
  3364.  
  3365. int
  3366. do_label_send(name) char *name; {
  3367.     int pad_size;
  3368.  
  3369.     zinptr += sprintf(zinptr,"KERMIT LABELED FILE:02D704VERS");
  3370.     zinptr += sprintf(zinptr,"%08d%s", strlen(get_vms_vers()), get_vms_vers());
  3371.     zinptr += sprintf(zinptr,"05KVERS00000008%08ld", vernum);
  3372.     zinptr += sprintf(zinptr,"07VMSNAME%08d", strlen(name));
  3373.     zinptr += sprintf(zinptr,"%s", name);
  3374.     zinptr += sprintf(zinptr,"07VMSFILE%08d", 70);
  3375.     memmove(zinptr, &xabpro_ifile.xab$w_pro, 2);
  3376.     zinptr += 2;
  3377.     memmove(zinptr, &xabpro_ifile.xab$l_uic, 4);
  3378.     zinptr += 4;
  3379.     memmove(zinptr, &fab_ifile.fab$b_rfm, 1);
  3380.     zinptr += 1;
  3381.     memmove(zinptr, &fab_ifile.fab$b_org, 1);
  3382.     zinptr += 1;
  3383.     memmove(zinptr, &fab_ifile.fab$b_rat, 1);
  3384.     zinptr += 1;
  3385.     memmove(zinptr, &xuchar, 4);    /* Dummy for file chars. */
  3386.     zinptr += 4;
  3387.     memmove(zinptr, &fab_ifile.fab$b_fsz, 1);
  3388.     zinptr += 1;
  3389.     memmove(zinptr, &xabfhc_ifile.xab$w_lrl, 2);
  3390.     zinptr += 2;
  3391.     memmove(zinptr, &fab_ifile.fab$w_mrs, 2);
  3392.     zinptr += 2;
  3393.     memmove(zinptr, &xabfhc_ifile.xab$l_ebk, 4);
  3394.     zinptr += 4;
  3395.     memmove(zinptr, &xabfhc_ifile.xab$w_ffb, 2);
  3396.     zinptr += 2;
  3397.     memmove(zinptr, &xabfhc_ifile.xab$l_hbk, 4);
  3398.     zinptr += 4;
  3399.     memmove(zinptr, &fab_ifile.fab$w_deq, 2);
  3400.     zinptr += 2;
  3401.     memmove(zinptr, &fab_ifile.fab$b_bks, 1);
  3402.     zinptr += 1;
  3403.     memmove(zinptr, &fab_ifile.fab$w_gbc, 2);
  3404.     zinptr += 2;
  3405.     memmove(zinptr, &xabfhc_ifile.xab$w_verlimit, 2);
  3406.     zinptr += 2;
  3407.     memmove(zinptr, &fab_ifile.fab$b_rfm+1, 1);    /* This is fab$b_journal */
  3408.     zinptr += 1;
  3409.     memmove(zinptr, &xabdat_ifile.xab$q_cdt, 8);
  3410.     zinptr += 8;
  3411.     memmove(zinptr, &xabdat_ifile.xab$q_rdt, 8);
  3412.     zinptr += 8;
  3413.     memmove(zinptr, &xabdat_ifile.xab$w_rvn, 2);
  3414.     zinptr += 2;
  3415.     memmove(zinptr, &xabdat_ifile.xab$q_edt, 8);
  3416.     zinptr += 8;
  3417.     memmove(zinptr, &xabdat_ifile.xab$q_bdt, 8);
  3418.     zinptr += 8;
  3419.     if (xabpro_ifile.xab$w_acllen != 0) {
  3420.     zinptr += sprintf(zinptr,"06VMSACL%08d", xabpro_ifile.xab$w_acllen);
  3421.     memmove(zinptr, &aclbuf, xabpro_ifile.xab$w_acllen);
  3422.     zinptr += xabpro_ifile.xab$w_acllen;
  3423.     }
  3424.     zinptr += sprintf(zinptr,"04DATA00000000");
  3425.     zincnt = (zinptr - zinbuffer);        /* Size of this beast */
  3426.     zinptr = zinbuffer;                /* Reset pointer for readout */
  3427.     return(1);
  3428. }
  3429.  
  3430. /*
  3431.   D O _ L A B E L _ R E C V
  3432.  
  3433.   Note that we don't honor SET FILE COLLISION APPEND for labeled receives --
  3434.   the whole point of labeled receives is to generate an exact copy of the
  3435.   source file, attributes and all.
  3436. */
  3437. #define CK_LBLBUFLEN 16
  3438. #define CK_VMSFILELEN 70
  3439.  
  3440. int
  3441. do_label_recv() {
  3442.     char *recv_ptr;
  3443.     char buffer[CK_LBLBUFLEN+1];
  3444.     char vmsfile[CK_VMSFILELEN];
  3445.     char *filptr = vmsfile;
  3446.     int lblen, alen;
  3447.     int gotname = 0, gotfile = 0, gotacl = 0;
  3448.     char *i, *j;
  3449.     unsigned short jnlflg;
  3450.  
  3451.     debug(F101,"do_label_recv: options","",ofile_lblopts);
  3452.     ofile_lblproc = 1;                /* Don't come here again */
  3453.  
  3454.     if (strncmp(zoutbuffer,"KERMIT LABELED FILE:02D704VERS",30) != 0)
  3455.     return(0);            /* Just continue if unlabeled */
  3456.  
  3457.     recv_ptr = zoutbuffer+30;        /* start at front of buffer */
  3458.  
  3459.     memcpy(buffer, recv_ptr, 8);
  3460.     recv_ptr += 8;
  3461.     buffer[8] = '\0';
  3462.     lblen = atoi(buffer);
  3463.     if (lblen > CK_LBLBUFLEN) {
  3464.     debug(F101,"do_label_recv: lblen too long 1","",lblen);
  3465.     return(-1);
  3466.     }
  3467.     memcpy(buffer, recv_ptr, lblen);
  3468.     recv_ptr += lblen;
  3469.     buffer[lblen] = '\0';
  3470.     debug(F110,"do_label_recv: file created under VAX/VMS: ",buffer,0);
  3471.  
  3472.     memcpy(buffer, recv_ptr, 7);
  3473.     recv_ptr += 7;
  3474.     if (strncmp(buffer, "05KVERS", 7) != 0) {
  3475.     debug(F100,"do_label_recv: lost sync at KVERS","",0);
  3476.     return(-1);
  3477.     }
  3478.     memcpy(buffer, recv_ptr, 8);
  3479.     recv_ptr += 8;
  3480.     buffer[8] = '\0';
  3481.     lblen = atoi(buffer);
  3482.     if (lblen > CK_LBLBUFLEN) {
  3483.     debug(F101,"do_label_recv: lblen too long 2","",lblen);
  3484.     return(-1);
  3485.     }
  3486.     memcpy(buffer, recv_ptr, lblen);
  3487.     recv_ptr += lblen;
  3488.     buffer[lblen] = '\0';
  3489.     debug(F110,"do_label_recv: file created with C-Kermit/VMS: ",buffer,0);
  3490.  
  3491.     next_label:
  3492.     memcpy(buffer, recv_ptr, 2);
  3493.     recv_ptr += 2;
  3494.     buffer[2] = '\0';
  3495.     lblen = atoi(buffer);
  3496.     if (lblen == 0) {
  3497.     debug(F100,"do_label_recv: lost sync at next_label: ","",0);
  3498.     return(-1);
  3499.     } else if (lblen > CK_LBLBUFLEN) {
  3500.     debug(F101,"do_label_recv: lblen too long 3","",lblen);
  3501.     return(-1);
  3502.     }
  3503.     memcpy(buffer, recv_ptr, lblen);
  3504.     recv_ptr += lblen;
  3505.     buffer[lblen] = '\0';
  3506.     debug(F110,"do_label_recv: found tag: ",buffer,0);
  3507.     if (strcmp(buffer, "VMSNAME") == 0) {
  3508.     memcpy(buffer, recv_ptr, 8);
  3509.         recv_ptr += 8;
  3510.     buffer[8] = '\0';
  3511.     lblen = atoi(buffer);
  3512.     if (lblen > CKMAXPATH) {    /* fdc 23 Jun 96 */
  3513.         debug(F101,"do_label_recv: lblen too long 4","",lblen);
  3514.         return(-1);
  3515.     }
  3516.     memcpy(ofile_vmsname, recv_ptr, lblen);
  3517.         recv_ptr += lblen;
  3518.     ofile_vmsname[lblen] = '\0';
  3519.     gotname++;
  3520.     debug(F110,"do_label_recv: loaded file name block as: ",
  3521.           ofile_vmsname,
  3522.           0
  3523.           );
  3524.     i = strstr(ofile_vmsname, "::");
  3525.     if (i != NULL) {
  3526.         i += 2;
  3527.         memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  3528.     }
  3529.     debug(F110,"do_label_recv: ofile_vmsname 2",ofile_vmsname,0);
  3530.         if ((ofile_lblopts & LBL_PTH) == 0) {
  3531.         i = strrchr(ofile_vmsname, ':');
  3532.         j = strrchr(ofile_vmsname, ']');
  3533.         if (j == NULL)
  3534.         j = strrchr(ofile_vmsname, '>');
  3535.         if (j > i)
  3536.         i = j;
  3537.         if (i) {            /* fdc 6-12-96 */
  3538.         i++;
  3539.         memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  3540.         }
  3541.     }
  3542.     debug(F110,"do_label_recv: ofile_vmsname 3",ofile_vmsname,0);
  3543.     if (strchr(ofile_vmsname, ';') != NULL) {
  3544.         for (alen = strlen(ofile_vmsname);
  3545.          ofile_vmsname[alen] != ';' && alen > 0;
  3546.          alen--)
  3547.         ;
  3548.         ofile_vmsname[alen] = '\0';
  3549.     }
  3550.     debug(F110,"do_label_recv: resultant filespec: ",ofile_vmsname,0);
  3551.     goto next_label;
  3552.     } else if (strcmp(buffer, "VMSFILE") == 0) {
  3553.     memcpy(buffer, recv_ptr, 8);
  3554.     recv_ptr += 8;
  3555.     buffer[8] = '\0';
  3556.     lblen = atoi(buffer);
  3557.     if (lblen > CK_VMSFILELEN) {
  3558.         debug(F101,"do_label_recv: lblen too long 5","",lblen);
  3559.         return(-1);
  3560.     }
  3561.     memcpy(vmsfile, recv_ptr, lblen);
  3562.     recv_ptr += lblen;
  3563.     vmsfile[lblen] = '\0';
  3564.     gotfile++;
  3565.     debug(F100,"do_label_recv: loaded file attribute block","",0);
  3566.     goto next_label;
  3567.     } else if (strcmp(buffer, "VMSACL") == 0) {
  3568.     memcpy(buffer, recv_ptr, 8);
  3569.     recv_ptr += 8;
  3570.     buffer[8] = '\0';
  3571.     ofile_acllen = atoi(buffer);
  3572.     if (ofile_acllen > CK_LBLBUFLEN) {
  3573.         debug(F101,"do_label_recv: ofile_acllen too long","",lblen);
  3574.         return(-1);
  3575.     }
  3576.     memcpy(ofile_vmsacl, recv_ptr, ofile_acllen);
  3577.     recv_ptr += ofile_acllen;
  3578.     ofile_vmsacl[ofile_acllen] = '\0';
  3579.     gotacl++;
  3580.     debug(F100,"do_label_recv: loaded file ACL block","",0);
  3581.     goto next_label;
  3582.     } else if (strcmp(buffer, "DATA") == 0) {
  3583.     memcpy(buffer, recv_ptr, 8);
  3584.     recv_ptr += 8;
  3585.     buffer[8] = '\0';
  3586.     lblen = atoi(buffer);
  3587.     if (lblen != 0) {
  3588.         debug(F101,"do_label_recv: length of DATA tag not zero","",lblen);
  3589.         return(-1);
  3590.     }
  3591.     debug(F100,"do_label_recv: positioned at start of file data","",0);
  3592.     goto all_set;
  3593.     } else {
  3594.     debug(F110,"do_label_recv: unrecognized label: ",buffer,0);
  3595.     memcpy(buffer, recv_ptr, 8);
  3596.     recv_ptr += 8;
  3597.     buffer[8] = '\0';
  3598.     lblen = atoi(buffer);
  3599.     recv_ptr += lblen;
  3600.     goto next_label;
  3601.     }
  3602. all_set:
  3603.     if (gotfile != 1 || gotname != 1) {
  3604.     debug(F100,"do_label_recv: missing one or more required labels","",0);
  3605.     return(-1);
  3606.     }
  3607.  
  3608. /* Prep the characteristics */
  3609.  
  3610.     fab_ofile.fab$b_fac = FAB$M_BIO | FAB$M_PUT;
  3611.     fab_ofile.fab$l_fop = FAB$M_MXV;
  3612.     if ((ofile_lblopts & LBL_NAM) != 0) {
  3613.     fab_ofile.fab$l_fna = ofile_vmsname;
  3614.     fab_ofile.fab$b_fns = strlen(ofile_vmsname);
  3615.     }
  3616.     fab_ofile.fab$l_xab = (char *)&xabdat_ofile;
  3617.     rab_ofile = cc$rms_rab;
  3618.     rab_ofile.rab$l_fab = &fab_ofile;
  3619.     xabdat_ofile = cc$rms_xabdat;
  3620.     xabdat_ofile.xab$l_nxt = (char *)&xabrdt_ofile;
  3621.     xabrdt_ofile = cc$rms_xabrdt;
  3622.     xabrdt_ofile.xab$l_nxt = (char *)&xabfhc_ofile;
  3623.     xabfhc_ofile = cc$rms_xabfhc;
  3624.     xabfhc_ofile.xab$l_nxt = (char *)&xabpro_ofile;
  3625.     xabpro_ofile = cc$rms_xabpro;
  3626.     xabpro_ofile.xab$l_nxt = (char *)&xaball_ofile;
  3627.     xaball_ofile = cc$rms_xaball;
  3628.  
  3629. /* Load 'em up */
  3630.  
  3631.     memmove(&xabpro_ofile.xab$w_pro, filptr, 2);
  3632.     filptr += 2;
  3633.     if ((ofile_lblopts & LBL_OWN) != 0)
  3634.     memmove(&xabpro_ofile.xab$l_uic, filptr, 4);
  3635.     filptr += 4;
  3636.     memmove(&fab_ofile.fab$b_rfm, filptr, 1);
  3637.     filptr += 1;
  3638.     memmove(&fab_ofile.fab$b_org, filptr, 1);
  3639.     filptr += 1;
  3640.     memmove(&fab_ofile.fab$b_rat, filptr, 1);
  3641.     filptr += 1;
  3642.     filptr += 4;            /* reserved */
  3643.     memmove(&fab_ofile.fab$b_fsz, filptr, 1);
  3644.     filptr += 1;
  3645.     memmove(&xabfhc_ofile.xab$w_lrl, filptr, 2);
  3646.     filptr += 2;
  3647.     memmove(&fab_ofile.fab$w_mrs, filptr, 2);
  3648.     filptr += 2;
  3649.     memmove(&xabfhc_ofile.xab$l_ebk, filptr, 4);
  3650.     filptr += 4;
  3651. /* preserve this as RMS won't remember it for us */
  3652.     memmove(&ofile_ffb, filptr, 2);
  3653.     filptr += 2;
  3654.     memmove(&xaball_ofile.xab$l_alq, filptr, 4);
  3655.     filptr += 4;
  3656.     memmove(&xaball_ofile.xab$w_deq, filptr, 2);
  3657.     filptr += 2;
  3658.  
  3659. #ifdef COMMENT /* was: defined(VAX) && defined(__DECC) */
  3660. /*
  3661.   When DEC C first came out for the VAX, the xab$b_bkz definition was missing
  3662.   and they used xaballdef$$_fill_7 instead.  But that was a long time ago.
  3663.   (This change made for C-Kermit 6.0, 19 Sep 96.)
  3664. */
  3665.     memmove(&xaball_ofile.xaballdef$$_fill_7, filptr, 1);
  3666. #else
  3667.     memmove(&xaball_ofile.xab$b_bkz, filptr, 1);
  3668. #endif /* COMMENT */
  3669.  
  3670.     filptr += 1;
  3671.     memmove(&fab_ofile.fab$w_gbc, filptr, 2);
  3672.     filptr += 2;
  3673.     memmove(&xabfhc_ofile.xab$w_verlimit, filptr, 2);
  3674.     filptr += 2;
  3675.     memmove(&jnlflg, filptr, 1);
  3676.     if (jnlflg !=0)
  3677.     debug(F100,"do_label_recv: journaling status removed for file","",0);
  3678.     filptr += 1;
  3679.     memmove(&xabdat_ofile.xab$q_cdt, filptr, 8);
  3680.     filptr += 8;
  3681.     memmove(&revdat, filptr, 8);
  3682.     filptr += 8;
  3683.     memmove(&revnum, filptr, 2);
  3684.     filptr += 2;
  3685.     memmove(&xabdat_ofile.xab$q_edt, filptr, 8);
  3686.     filptr += 8;
  3687.     if ((ofile_lblopts & LBL_BCK) != 0)
  3688.     memmove(&xabdat_ofile.xab$q_bdt, filptr, 8);
  3689.     filptr += 8;
  3690.  
  3691. /* ACL's? */
  3692.  
  3693.     if ((ofile_lblopts & LBL_ACL) != 0 && gotacl != 0) {
  3694.     xabpro_ofile.xab$l_aclbuf = (char *)&ofile_vmsacl;
  3695.     xabpro_ofile.xab$w_aclsiz = ofile_acllen;
  3696.     }
  3697.  
  3698. /* Try to create the file */
  3699.  
  3700.     rms_sts = sys$create(&fab_ofile);
  3701.     if (!(rms_sts & 1)) {
  3702.     debug(F101,"do_label_recv: $create failed, status","",rms_sts);
  3703.     return(-1);
  3704.     }
  3705.     if((ofile_lblopts & LBL_ACL) != 0 && gotacl != 0) {
  3706.     if (!(xabpro_ofile.xab$l_aclsts & 1)) {
  3707.         debug(F101,"do_label_recv: ACL chain failed, status",
  3708.           "",
  3709.           xabpro_ofile.xab$l_aclsts
  3710.           );
  3711.         return(-1);
  3712.     }
  3713.     }
  3714.     rms_sts = sys$connect(&rab_ofile);
  3715.     if (!(rms_sts & 1)) {
  3716.     debug(F101,"do_label_recv: $connect failed, status","",rms_sts);
  3717.     return(-1);
  3718.     }
  3719. /*
  3720.   Slide the remainder of the data to the head of the buffer and adjust the
  3721.   counter and pointer. This will cause the buffer to be re-filled to the full
  3722.   32Kb capacity, which is necessary for proper operation of zoutdump().
  3723. */
  3724.     zoutcnt -= ((char *)recv_ptr - (char *)zoutbuffer);
  3725.     debug(F101,"do_label_recv: zoutcnt","",zoutcnt);
  3726.     memcpy(zoutbuffer, recv_ptr, zoutcnt);
  3727.     zoutptr = zoutbuffer + zoutcnt;
  3728.     return(1);                /* Go fill some more */
  3729. }
  3730.