home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckl196.zip / cklfio.c < prev    next >
C/C++ Source or Header  |  1999-12-05  |  76KB  |  2,702 lines

  1. #include "ckcsym.h"
  2. char *ckzv = "Stratus VOS File support, 6.1.005, 15 January 1998";
  3.  
  4. /* C K L F I O  --  Kermit file system support for Stratus VOS */
  5.  
  6. /*
  7.   Author: Frank da Cruz <fdc@columbia.edu>,
  8.   The Kermit Project, Columbia University
  9.   First released January 1985.
  10.   Adapted to Stratus VOS by David R. Lane, SoftCom System, Inc., Nov 1993
  11.  
  12.   Copyright (C) 1985, 1999,
  13.     Trustees of Columbia University in the City of New York.
  14.     All rights reserved.  See the C-Kermit COPYING.TXT file or the 
  15.     copyright text in the ckcmai.c module for disclaimer and permissions.
  16. */
  17.  
  18. /* Include Files */
  19.  
  20. #include "ckcdeb.h"
  21. #include <signal.h>
  22. #include <limits.h>
  23.  
  24. #define DIRSEP       '>'
  25. #define ISDIRSEP(c)  ((c)=='>'||(c)=='<')
  26.  
  27. #include <stdlib.h>
  28. #include <time.h>            /* Need this */
  29. #include <system_io_constants.h>
  30. #include <get_port_info.h>
  31. #include <file_status_info.h>
  32. #include <error_codes.h>
  33.  
  34. extern int binary;            /* We need to know this for open() */
  35.  
  36. /*
  37.   Functions (n is one of the predefined file numbers from ckcker.h):
  38.  
  39.    zopeni(n,name)   -- Opens an existing file for input.
  40.    zopeno(n,name,attr,fcb) -- Opens a new file for output.
  41.    zclose(n)        -- Closes a file.
  42.    zchin(n,&c)      -- Gets the next character from an input file.
  43.    zsinl(n,&s,x)    -- Read a line from file n, max len x, into address s.
  44.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  45.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  46.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  47.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  48.    zchki(name)      -- Check if named file exists and is readable, return size.
  49.    zchko(name)      -- Check if named file can be created.
  50.    zchkspa(name,n)  -- Check if n bytes available to create new file, name.
  51.    znewn(name,s)    -- Make a new unique file name based on the given name.
  52.    zdelet(name)     -- Delete the named file.
  53.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  54.    znext(string)    -- Returns the next file from the list in "string".
  55.    zxcmd(n,cmd)     -- Execute the command in a lower fork on file number n.
  56.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  57.    zrtol(n1,n2)     -- Convert remote filename into local form.
  58.    zltor(n1,n2)     -- Convert local filename into remote form.
  59.    zchdir(dirnam)   -- Change working directory.
  60.    zhome()          -- Return pointer to home directory name string.
  61.    zkself()         -- Kill self, log out own job.
  62.    zsattr(struct zattr *) -- Return attributes for file which is being sent.
  63.    zstime(f, struct zattr *, x) - Set file creation date from attribute packet.
  64.    zrename(old, new) -- Rename a file.
  65.    zmkdir(path)      -- create a directory
  66.    zrmdir(path)      -- delete a directory
  67.    isdir(path)       -- is this path a directory?
  68.    zfseek(offset)    -- position input file to offset (zero-based)
  69.    zfnqfp(fname, buflen, buf) -- get fully qualified absolute path
  70.  */
  71.  
  72. /* Kermit-specific includes */
  73. /*
  74.   Definitions here supersede those from system include files.
  75.   ckcdeb.h is included above.
  76. */
  77. #include "ckcker.h"            /* Kermit definitions */
  78. #include "ckucmd.h"            /* For sys-dependent keyword tables */
  79. #include "ckuver.h"            /* Version herald */
  80.  
  81. char *ckzsys = HERALD;
  82.  
  83. /* Definitions of some system commands */
  84.  
  85. char *DELCMD = "!delete_file ";        /* For file deletion */
  86. char *PWDCMD = "!display_current_dir ";    /* For saying where I am */
  87. char *TYPCMD = "!display ";        /* For typing a file */
  88. char *DIRCMD = "!list -full -files -dirs -links ";/* For directory listing */
  89.                     /* For directory listing, no args */
  90. char *DIRCM2 = "!list -full -files -dirs -links ";
  91. char *WHOCMD = "!list_users -process login ";/* Who's there? */
  92. char *SPACMD = "!display_disk_info ";    /* For space on disk */
  93. char *SPACM2 = "!display_disk_info -long ";    /* For space on disk */
  94.  
  95. #ifdef DTILDE                /* For tilde expansion */
  96. _PROTOTYP( char * tilde_expand, (char *) );
  97. #endif /* DTILDE */
  98.  
  99. _PROTOTYP( vostty, (int) );
  100.  
  101. /* Prototypes for VOS s$calls */
  102.  
  103. _PROTOTYP( extern VOID s$attach_default_output, (CV(256) *path,
  104.     short *append, short *status) );
  105.  
  106. _PROTOTYP( extern VOID s$attach_port, (CV(32) *port_name, CV(256)* path,
  107.     short *hold, short *port_id, short *status) );
  108.  
  109. _PROTOTYP( extern VOID s$change_current_dir, (CV(256) *dir, short *status) );
  110.  
  111. _PROTOTYP( extern VOID s$clone, (short *status, CV(256) *err_msg) );
  112.  
  113. _PROTOTYP( extern VOID s$close, (short *port_id, short *status) );
  114.  
  115. _PROTOTYP( extern void s$copy_file, (CV(256) *source, CV(256) *dest,
  116.     CV(32) *caller, short *switches, short *status) );
  117.  
  118. _PROTOTYP( extern VOID s$delete_file_on_close, (CV(256) *path,
  119.     short *status) );
  120.  
  121. _PROTOTYP( extern VOID s$create_dir, (CV(256) *path, short *status) );
  122.  
  123. _PROTOTYP( extern VOID s$cv_to_int_date_time, (CV(32) *src_time,
  124.     time_t *time, short *status) );
  125.  
  126. _PROTOTYP( extern VOID s$delete_dir, (CV(256) *path, CV(32) *dummy,
  127.     short *flags, short *status) );
  128.  
  129. _PROTOTYP( extern VOID s$detach_default_output, (short *status) );
  130.  
  131. _PROTOTYP( extern VOID s$detach_port, (short *port_id, short *status) );
  132.  
  133. _PROTOTYP( extern VOID s$get_current_dir, (CV(256) *dir) );
  134.  
  135. _PROTOTYP( extern VOID s$get_disk_info, (CV(66) *disk_name, VOID *info,
  136.     short *status) );
  137.  
  138. _PROTOTYP( extern VOID s$get_file_status, (CV(256) *path, VOID *info,
  139.     short *status) );
  140.  
  141. _PROTOTYP( extern VOID s$get_home_dir, (CV(256) *dir) );
  142.  
  143. _PROTOTYP( extern void s$get_object_type, (CV(256) *path, short *chase,
  144.     short *type, short *status) );
  145.  
  146. _PROTOTYP( extern VOID s$get_port_info, (short *port,
  147.     struct get_port_info *pinfo, short *status) );
  148.  
  149. _PROTOTYP( extern VOID s$get_temp_file, (short *organization,
  150.     short *max_rec_len, CV(256) *path, short *status) );
  151.  
  152. _PROTOTYP( extern VOID s$get_user_name, (CV(65) *name) ); 
  153.  
  154. _PROTOTYP( extern VOID s$expand_path, (CV(256) *src_path, CV(32) *suffix,
  155.     CV(256) *dest_path, short *status) ); 
  156.  
  157. _PROTOTYP( extern VOID s$expand_star, (CV(256) *star_name, short *flags,
  158.     short *max_count, short *count, VOID *dest, short *status) );
  159.  
  160. _PROTOTYP( extern VOID s$open, (short *port_id, short *organization,
  161.     short *max_rec_len, short *io_type, short *locking_type,
  162.     short *access_mode, CV(32) *index_name, short *status) );
  163.  
  164. _PROTOTYP( extern VOID s$read_raw, (short *port_id, short *buff_size,
  165.     short *rec_len, void *buffer, short *status) );
  166.  
  167. _PROTOTYP( extern VOID s$seq_open, (CV(256) *path, short *io_type,
  168.     short *port_id, short *status) );
  169.  
  170. _PROTOTYP( extern VOID s$seq_position,  (short *port_id, short *relation,
  171.     long *rec_number, short *status) );
  172.  
  173. _PROTOTYP( extern VOID s$seq_read, (short *port_id, short *buff_size,
  174.     short *rec_len, VOID *buffer, short *status) );
  175.  
  176. _PROTOTYP( extern VOID s$seq_write, (short *port_id, short *rec_length,
  177.     VOID *buffer, short *status) );
  178.  
  179. _PROTOTYP( extern VOID s$seq_write_partial, (short *port_id,
  180.     short *rec_length, VOID *buffer, short *status) );
  181.  
  182. _PROTOTYP( extern VOID s$set_expiration_date, (CV(256) *path_name,
  183.     time_t *date_time, short *status) );
  184.  
  185. _PROTOTYP( extern VOID s$set_object_times, (CV(256) *path_name,
  186.     time_t *time_array, short *status) ); /* four times in array */
  187.  
  188. _PROTOTYP( extern VOID s$stop_program, (CV(256) *command_line,
  189.         short *status ) );
  190.  
  191. _PROTOTYP( extern VOID s$write_raw, (short *port_id, short *length,
  192.     void *buffer, short *status) );
  193.  
  194. /* Define maximum length for a file name if not already defined */
  195.  
  196. #ifndef MAXNAMLEN
  197. #define MAXNAMLEN 32
  198. #endif /* MAXNAMLEN */
  199.  
  200. /* Longest pathname */
  201.  
  202. #ifdef MAXPATHLEN
  203. #ifdef MAXPATH
  204. #undef MAXPATH
  205. #endif /* MAXPATH */
  206. #define MAXPATH MAXPATHLEN
  207. #else
  208. #ifdef PATH_MAX
  209. #define MAXPATH PATH_MAX
  210. #else
  211. #define MAXPATH 256
  212. #endif /* PATH_MAX */
  213. #endif /* MAXPATHLEN */
  214.  
  215. /* Maximum number of filenames for wildcard expansion */
  216.  
  217. /* VOS directories can be up to 256 data blocks.  This is about 5000 entries */
  218. /* but it can vary a lot, depending on a number of factors, such as the      */
  219. /* lengths of the names, whether or not Access Control Lists are present,    */
  220. /* and so on.  This is about the most you get, in practice, though.          */
  221.  
  222. #define MAXWLD 5000
  223.  
  224. struct path {
  225.     char npart[MAXNAMLEN+4];        /* name part of path segment */
  226.     struct path *fwd;            /* forward ptr */
  227. };
  228.  
  229. _PROTOTYP( int shxpand, (char *, char *[], int ) );
  230.  
  231.  
  232. /* Declarations */
  233.  
  234. int maxnam = MAXNAMLEN;            /* Available to the outside */
  235. int maxpath = MAXPATH;
  236.  
  237. typedef struct {
  238.     short port;
  239.     short io_type;
  240.     short binary;
  241.     short organization;
  242.     short max_record_len;
  243.     short record_len;
  244.     short buffer_size;
  245.     short buffer_pointer;
  246.     short count;
  247.     short count_invalid;
  248.     char  *buffer;
  249. } VFILE;
  250.  
  251. VFILE *fp[ZNFILS] = {             /* File pointers */
  252.     NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  253.  
  254. /* There is not a Stratus-provided include for this.  I typed it in from
  255.  * the VOS C subroutines manual, R068, under s$get_disk_info.
  256.  */
  257.  
  258. struct disk_info {
  259.     short     version;
  260.     CV(32)     module_name;
  261.     long    file_partition_size;
  262.     long    file_partition_used;
  263.     long    page_partition_size;
  264.     long    page_partition_used;
  265.     CV(32)    disk_type;
  266.     long    disk_size;
  267.     short    reserved1[19];
  268.     long    disk_reads;
  269.     long    disk_writes;
  270.     short    reserved2;
  271.     short    recovery_switches;
  272.     long    reserved3;
  273.     short    number_of_members;
  274.     long    fatal_errors;
  275.     long    reserved4;
  276. };
  277.  
  278. /* Buffers and pointers used in buffered file input and output. */
  279. #ifdef DYNAMIC
  280. extern char *zinbuffer, *zoutbuffer;
  281. #else
  282. extern char zinbuffer[], zoutbuffer[];
  283. #endif /* DYNAMIC */
  284. extern char *zinptr, *zoutptr;
  285. extern int zincnt, zoutcnt;
  286. extern int wildxpand;
  287.  
  288. extern UID_T real_uid();
  289.  
  290. static long iflen = -1L;        /* Input file length */
  291.  
  292. static PID_T pid = 0;            /* pid of child fork */
  293. static int fcount;            /* Number of files in wild group */
  294. static char nambuf[MAXPATH+4];         /* Buffer for a filename */
  295. #ifndef NOFRILLS
  296. static char zmbuf[200];            /* For mail, remote print strings */
  297. #endif /* NOFRILLS */
  298.  
  299. /* static */                /* Not static, must be global now. */
  300. int  oldmtchs;                /* number of matches in last expand */
  301. char *mtchs[MAXWLD],            /* Matches found for filename */
  302.      **mtchptr;                /* Pointer to current match */
  303.  
  304. /*  Z K S E L F  --  Kill Self: log out own job, if possible.  */
  305.  
  306. int
  307. zkself() {                /* For "bye", but no guarantee! */
  308.     /* as of 6.1.alpha.11, this really does what it's supposed to do... */
  309.     CV(256) command_line;
  310.     short status;
  311.  
  312.     debug(F100,"zkself","",0);
  313.  
  314.     strcpy(&command_line,"logout");
  315.     status = 0;
  316.     s$stop_program(&command_line,&status);
  317.  
  318.     debug(F101,"zkself did not...","",status);
  319.     exit(0);
  320.     /*NOTREACHED*/
  321. }
  322.  
  323. /*  Z O P E N I  --  Open an existing file for input. */
  324.  
  325. int
  326. zopeni(n,name) int n; char *name; {
  327.     CV(256) path;
  328.     CV(256) src_path;
  329.     CV(32) suffix;
  330.     FILE_STATUS_STRUCT finfo;
  331.     short status;
  332.  
  333.     debug(F111," zopeni",name,n);
  334.     debug(F101,"  fp","", fp[n]);
  335.     if (chkfn(n) != 0) return(0);
  336.     zincnt = 0;                /* Reset input buffer */
  337.  
  338.     if (fp[n])                /* make sure it's closed */
  339.     zclose(n);
  340.  
  341.  if (n == ZSYSFN) {            /* Input from a system function? */
  342. /*** Note, this function should not be called with ZSYSFN ***/
  343. /*** Always call zxcmd() directly, and give it the real file number ***/
  344. /*** you want to use.  ***/
  345.         debug(F110,"zopeni called with ZSYSFN, failing!",name,0);
  346.     *nambuf = '\0';            /* No filename. */
  347.     return(0);            /* fail. */
  348.     }
  349.     if (n == ZSTDIO) {            /* Standard input? */
  350.     if (vostty(DEFAULT_INPUT_PORT_ID)) {
  351.         fprintf(stderr,"Terminal input not allowed");
  352.         debug(F100,"zopeni: attempts input from unredirected stdin","",0);
  353.         *nambuf = '\0';        /* No filename. */
  354.         return(0);
  355.     }
  356.  
  357.     return(1);
  358.     }
  359.  
  360. #ifdef COMMENT
  361.     *nambuf = '\0';            /* Forget filename */
  362. #endif /* COMMENT */
  363.  
  364.     strcpy (&src_path, name);
  365.     strcpy (&suffix, "");
  366.     s$expand_path (&src_path, &suffix, &path, &status);
  367.     if (status) {
  368.     debug(F101,"  zopeni s$expand_path status","",status);
  369.     return 0;
  370.     }
  371.  
  372.     finfo.version = FILE_STAT_VERSION_5;
  373.     s$get_file_status (&path, &finfo, &status);
  374.     if (status) {
  375.     debug(F101,"zopeni s$get_file_status status","",status);
  376.     return 0;
  377.     }
  378.  
  379.     switch (finfo.file_organization) {
  380.     case SEQUENTIAL_FILE:
  381.     case STREAM_FILE:
  382.         iflen = finfo.last_record_number;
  383.         break;
  384.  
  385.     case FIXED_FILE:
  386.         iflen = finfo.last_record_number * 
  387.         finfo.flags_struct.flags_bits_overlay.max_record_size;
  388.         break;
  389.  
  390.     case RELATIVE_FILE:
  391.         iflen = finfo.last_record_number * 
  392.         (finfo.flags_struct.flags_bits_overlay.max_record_size + 2);
  393.         break;
  394.  
  395.     default:    /* uhhhh.... */
  396.         iflen = -1L;
  397.         break;
  398.     }
  399.  
  400.     fp[n] = calloc (1, sizeof (**fp));
  401.     debug(F111," zopeni allocated", name, fp[n]);
  402.     if (fp[n] == NULL) {
  403.     debug(F101,"  zopeni could not allocate fp","",sizeof **fp);
  404.     perror("zopeni");
  405.     return 0;
  406.     }
  407.  
  408.     /* CANNOT RETURN AFTER HERE WITHOUT FREEING MEMORY */
  409.  
  410.     fp[n]->organization = finfo.file_organization;
  411.     fp[n]->max_record_len
  412.     = finfo.flags_struct.flags_bits_overlay.max_record_size;
  413.  
  414.     fp[n]->buffer_size = fp[n]->max_record_len ? fp[n]->max_record_len : 4096;
  415.     fp[n]->buffer = calloc (1, fp[n]->buffer_size);
  416.  
  417.     if (NULL == fp[n]->buffer) {
  418.     debug(F101,"zopeni cannot allocate buffer","",fp[n]->buffer_size);
  419.     free (fp[n]);
  420.     fp[n] = NULL;
  421.     return 0;
  422.     }
  423.  
  424.     fp[n]->io_type = INPUT_TYPE;
  425.     s$seq_open (&path, &fp[n]->io_type, &fp[n]->port, &status);
  426.     if (status) {
  427.     debug(F101,"zopeni s$seq_open status","",status);
  428.     free (fp[n]);
  429.     fp[n] = NULL;
  430.     free (fp[n]->buffer);
  431.     return 0;
  432.     }
  433.  
  434. #ifdef COMMENT
  435.     strcpy (nambuf, name);        /* save filename */
  436. #endif /* COMMENT */
  437.     fp[n]->binary = binary;         /* remember this */
  438.     fp[n]->count_invalid = 1;        /* have no buffered data yet */
  439.     return(1);
  440. }
  441.  
  442. /*  Z O P E N O  --  Open a new file for output.  */
  443.  
  444. int
  445. zopeno(n,name,zz,fcb)
  446. /* zopeno */  int n; char *name; struct zattr *zz; struct filinfo *fcb; {
  447.     short io_type;
  448.     short org;
  449.     short record_len;
  450.     short port = 0;
  451.     CV(256) path;
  452.     CV(256) src_path;
  453.     CV(32) suffix;
  454.     CV(32) index;
  455.     CV(32) portnam;
  456.     short lockmod;
  457.     short access;
  458.     short hold;
  459.     FILE_STATUS_STRUCT finfo;
  460.     short status;
  461.     int  is_binary = 0;
  462.  
  463. /* As of Version 5A, the attribute structure and the file information */
  464. /* structure are included in the arglist. */
  465.  
  466.     if (n != ZDFILE) {
  467.     debug(F111," zopeno",name,n);
  468.     if (fcb) {
  469.         debug(F101,"zopeno fcb disp","",fcb->dsp);
  470.         debug(F101,"zopeno fcb type","",fcb->typ);
  471.         debug(F101,"zopeno fcb char","",fcb->cs);
  472.     } else {
  473.         debug(F100,"zopeno fcb is NULL","",0);
  474.     }
  475.     }
  476.  
  477.     if (chkfn(n) != 0)
  478.     return(0);
  479.  
  480.     io_type = OUTPUT_TYPE;
  481.  
  482.     if (fcb) {                /* If called with an FCB... */
  483.     if (fcb->typ == XYFT_T)        /* Does it say Text? */
  484.         is_binary = 0;        /* Yes, not binary. */
  485.     else
  486.         is_binary = 1;        /* No, it is binary. */
  487.     }
  488.     else {
  489.     switch (n) {
  490.         case ZOFILE:
  491.         case ZWFILE:
  492.         case ZMFILE:
  493.         is_binary = binary;    /* Use current file mode */
  494.         break;
  495.  
  496.         case ZDFILE:
  497.         case ZTFILE:
  498.         case ZSYSFN:
  499.         case ZCTERM:
  500.         case ZSTDIO:
  501.         is_binary = 0;        /* Always text */
  502.         break;
  503.  
  504.         case ZSFILE:
  505.         is_binary = 1;        /* Always binary */
  506.         break;
  507.  
  508.     }
  509.     }
  510.  
  511.     if ((n != ZCTERM) && (n != ZSTDIO)) {  /* Terminal or standard output */
  512.  
  513.     /* A real file.  Open it in desired mode (create or append). */
  514.  
  515.     if (fcb) {                /* If called with an FCB... */
  516.         if (fcb->dsp == XYFZ_A)        /* Does it say Append? */
  517.           io_type = APPEND_TYPE;    /* Yes. */
  518.     }
  519.  
  520.     strcpy (&src_path, name);
  521.     strcpy (&suffix, "");
  522.     s$expand_path (&src_path, &suffix, &path, &status);
  523.     if (status) {
  524.         if (n != ZDFILE)
  525.         debug(F101,"  zopeno s$expand_path status","",status);
  526.         return 0;
  527.     }
  528.  
  529.     finfo.version = FILE_STAT_VERSION_5;
  530.     s$get_file_status (&path, &finfo, &status);
  531.     if (status) {
  532.         if (status != e$object_not_found) {
  533.         if (n != ZDFILE)
  534.             debug(F101,"zopeno s$get_file_status status","",status);
  535.         return 0;
  536.         }
  537.  
  538.         /* the session log is a stream file, because that reflects */
  539.         /* the "log of data sent back and forth" better            */
  540.  
  541.         org = (is_binary) ? STREAM_FILE : SEQUENTIAL_FILE;
  542.         record_len = 4096;
  543.     }
  544.     else {
  545.         org = finfo.file_organization;
  546.         record_len = finfo.flags_struct.flags_bits_overlay.max_record_size;
  547.     }
  548.     }
  549.     else {    /* it is a "terminal" */
  550.     n = ZOFILE;        /* change to output file */
  551.     org = TERMINAL_TYPE;
  552.     record_len = 4096;
  553.     port = DEFAULT_OUTPUT_PORT_ID;
  554.         debug(F101," zopen terminal, ZOFILE", "", n);
  555.     }
  556.  
  557.     fp[n] = calloc (1, sizeof (**fp));
  558.     if (n != ZDFILE)
  559.     debug(F111," zopeno allocated", name, fp[n]);
  560.     if (fp[n] == NULL) {
  561.     if (n != ZDFILE)
  562.         debug(F101,"  zopeno could not allocate fp","",sizeof **fp);
  563.     perror("zopeno");
  564.     return 0;
  565.     }
  566.  
  567.     /* CANNOT RETURN AFTER HERE WITHOUT FREEING MEMORY */
  568.  
  569.     fp[n]->max_record_len = record_len;
  570.     fp[n]->buffer_size = fp[n]->max_record_len ? fp[n]->max_record_len : 4096;
  571.     fp[n]->buffer = calloc (1, fp[n]->buffer_size);
  572.  
  573.     if (NULL == fp[n]->buffer) {
  574.     if (n != ZDFILE)
  575.         debug(F101,"zopeno cannot allocate buffer","",fp[n]->buffer_size);
  576.     free (fp[n]);
  577.     fp[n] = NULL;
  578.     return 0;
  579.     }
  580.  
  581.     /* save pre-calculated information */
  582.  
  583.     fp[n]->io_type = io_type;
  584.     fp[n]->organization = org;
  585.     fp[n]->port = port;
  586.     fp[n]->binary = is_binary;        /* save this! */
  587.  
  588.     if (0 == fp[n]->port) {  /* not set yet */
  589.     strcpy (&portnam, "");
  590.     hold = DONT_HOLD;
  591.     s$attach_port (&portnam, &path, &hold, &fp[n]->port, &status);
  592.     if (status) {
  593.         if (n != ZDFILE)
  594.         debug(F101,"zopeno s$attach_port status","",status);
  595.         free (fp[n]->buffer);
  596.         free (fp[n]);
  597.         fp[n] = NULL;
  598.         return 0;
  599.     }
  600.  
  601.     lockmod = IMPLICIT_LOCKING;
  602.     access = SEQUENTIAL_MODE;
  603.     strcpy (&index, "");
  604.     s$open (&fp[n]->port, &fp[n]->organization, &fp[n]->max_record_len,
  605.         &fp[n]->io_type, &lockmod, &access, &index, &status);
  606.     if (status) {
  607.         if (n != ZDFILE)
  608.         debug(F101,"zopeno s$open status","",status);
  609.         free (fp[n]->buffer);
  610.         free (fp[n]);
  611.         fp[n] = NULL;
  612.         return 0;
  613.     }
  614.     }
  615.  
  616.     zoutcnt = 0;            /* (PWP) reset output buffer */
  617.     zoutptr = zoutbuffer;
  618.     return((fp[n] != NULL) ? 1 : 0);
  619. }
  620.  
  621. /*  Z C L O S E  --  Close the given file.  */
  622.  
  623. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  624.  
  625. int
  626. zclose(n) int n; {
  627.     int x, x2;
  628.     short status;
  629.  
  630.     if (chkfn(n) < 1) return(0);    /* Check range of n */
  631.  
  632.     debug(F101,"zclose file","",n);
  633.  
  634.     if ((n == ZOFILE) && (zoutcnt > 0))    /* (PWP) output leftovers */
  635.       x2 = zoutdump();
  636.     else
  637.       x2 = 0;
  638.  
  639.     x = 0;                /* Initialize return code */
  640.     if (fp[ZSYSFN]) {            /* If file is really pipe */
  641.         x = zclosf(n);            /* do it specially */
  642.     } else {
  643.     if (fp[n]->port > MAX_RESERVED_PORT_ID) { /* only close user ports */
  644.         s$close (&fp[n]->port, &status);
  645.         x = status ? EOF : 0;
  646.         s$detach_port (&fp[n]->port, &status);
  647.         x = (status || x) ? EOF : 0;
  648.     }
  649.     else
  650.         x = 0;
  651.     free (fp[n]->buffer);
  652.     free (fp[n]);
  653.     fp[n] = NULL;
  654.     }
  655.  
  656.     iflen = -1L;            /* Invalidate file length */
  657.  
  658.     if (x == EOF)            /* if we got a close error */
  659.       return(-1);
  660.     else if (x2 < 0)        /* or an error flushing the last buffer */
  661.       return(-1);        /* then return an error */
  662.     else
  663.       return(1);
  664. }
  665.  
  666. /*  Z C H I N  --  Get a character from the input file.  */
  667.  
  668. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  669.  
  670. int
  671. zchin(n,c) int n; int *c; {
  672.     int a, x;
  673.     register VFILE *vp;
  674.     short status;
  675.     short rbuffsize;
  676.  
  677.     /* (PWP) Just in case this gets called when it shouldn't. */
  678.     if (n == ZIFILE) {
  679.     x = zminchar();
  680.     *c = x;
  681.     return((x < 0) ? -1 : 0);
  682.     }
  683.  
  684.     if (chkfn(n) < 1) return(-1);
  685.     vp = fp[n];
  686.  
  687.     if (vp->count_invalid) {    /* need to read more data */
  688.  
  689.     debug(F101,"zchin reading file","",n);
  690.  
  691.     if (!vp->binary) { /* text mode, add appropriate line separators */
  692.  
  693.         rbuffsize = vp->buffer_size - 1;
  694.         /* leave room for line terminator character(s) */
  695.  
  696. #ifndef NLCHAR
  697.         rbuffsize--;
  698. #endif /* NLCHAR */
  699.  
  700.         s$seq_read (&vp->port, &rbuffsize, &vp->record_len,
  701.         vp->buffer, &status);
  702.  
  703.         if (0 != status) {
  704.         debug(F101,"zchin text s$seq_read status","",status);
  705.         return (-1); /* hope it's end of file... */
  706.         }
  707. #ifdef NLCHAR
  708.         vp->buffer[vp->record_len++] = NLCHAR;
  709. #else
  710.         vp->buffer[vp->record_len++] = '\r';
  711.         vp->buffer[vp->record_len++] = '\n';
  712. #endif /* NLCHAR */
  713.     }
  714.     else {        /* binary */
  715.         if (vp->organization == STREAM_FILE) {
  716.         s$read_raw (&vp->port, &vp->buffer_size, &vp->record_len,
  717.             vp->buffer, &status);
  718.         if (status == e$short_record) /* not buffer_size bytes left */
  719.             status = 0;
  720.         }
  721.         else
  722.         s$seq_read (&vp->port, &vp->buffer_size, &vp->record_len,
  723.             vp->buffer, &status);
  724.  
  725.         if (0 != status) {
  726.         debug(F101,"zchin binary s$...read... status","",status);
  727.         return (-1); /* hope it's end of file... */
  728.         }
  729.         }
  730.     /* set up for character stuff... */
  731.     vp->count = vp->record_len;
  732.     vp->buffer_pointer = 0;
  733.     vp->count_invalid = 0;
  734.     }
  735.     /* count is valid */
  736.  
  737.     x = vp->buffer[vp->buffer_pointer++];
  738.     if (0 >= --(vp->count))
  739.     vp->count_invalid = 1;
  740.  
  741.     *c = x;
  742.     return((x < 0) ? -1 : 0);
  743. }
  744.  
  745. /*  Z S I N L  --  Read a line from a file  */
  746.  
  747. /*
  748.   Writes the line into the address provided by the caller.
  749.   n is the Kermit "channel number".
  750.   Writing terminates when newline is encountered, newline is not copied.
  751.   Writing also terminates upon EOF or if length x is exhausted.
  752.   Returns 0 on success, -1 on EOF or error.
  753. */
  754. int
  755. zsinl(n,s,x) int n, x; char *s; {
  756.     int a, z = 0;            /* z is return code. */
  757.  
  758.     if (chkfn(n) < 1) {            /* Make sure file is open */
  759.     return(-1);
  760.     }
  761.     a = -1;                /* Current character, none yet. */
  762.     while (x--) {            /* Up to given length */
  763. #ifndef NLCHAR
  764.     int old;
  765.     old = a;            /* Previous character */
  766. #endif
  767.     if (zchin(n,&a) < 0) {        /* Read a character from the file */
  768.         debug(F101,"zsinl","",a);
  769.         z = -1;            /* EOF or other error */
  770.         break;
  771.     }
  772. #ifdef NLCHAR
  773.     if (a == (char) NLCHAR) break;    /* Single-character line terminator */
  774. #else
  775.     if (a == '\015') continue;    /* CR, get next character */
  776.     if (old == '\015') {        /* Previous character was CR */
  777.         if (a == '\012') break;    /* This one is LF, so we have a line */
  778.         else *s++ = '\015';        /* Not LF, deposit CR */
  779.     }
  780. #endif /* NLCHAR */
  781.     *s = a;                /* Deposit character */
  782.     s++;
  783.     }
  784.     *s = '\0';                /* Terminate the string */
  785.     return(z);
  786. }
  787.  
  788. /*
  789.  * (PWP) (re)fill the buffered input buffer with data.  All file input
  790.  * should go through this routine, usually by calling the zminchar()
  791.  * macro (in ckcker.h).
  792.  */
  793.  
  794. /*
  795.  * Suggestion: if fread() returns 0, call ferror to find out what the
  796.  * problem was.  If it was not EOF, then return -2 instead of -1.
  797.  * Upper layers (getpkt function in ckcfns.c) should set cxseen flag
  798.  * if it gets -2 return from zminchar macro.
  799.  *
  800.  * This always uses zin* buffering for io, not fp->buffer.
  801.  */
  802. int
  803. zinfill() {
  804.     int x;
  805.     short status;
  806.     short buff_len;
  807.     VFILE *vp = fp[ZIFILE];
  808.  
  809.     errno = 0;
  810.     buff_len = INBUFSIZE;
  811.     
  812.     /* leave room for line terminators in text mode */
  813.     if (0 == vp->binary)
  814. #ifdef NLCHAR
  815.     buff_len -= 1;
  816. #else
  817.     buff_len -= 2;
  818. #endif /* NLCHAR */
  819.  
  820.     if (vp->binary && vp->organization == STREAM_FILE)
  821.     s$read_raw (&vp->port, &buff_len, &vp->record_len, zinbuffer, &status);
  822.     else
  823.     s$seq_read (&vp->port, &buff_len, &vp->record_len, zinbuffer, &status);
  824.  
  825.     zincnt = vp->record_len;
  826.  
  827.     debug(F101,"zinfill zincnt","",zincnt);
  828.     if (status) {
  829.     debug(F101,"zinfill s$...read... status","",status);
  830.     return (status == e$end_of_file) ? (-1) : (-2);
  831.     }
  832.  
  833.     if (0 == vp->binary) { /* text mode, add a NLCHAR to it */
  834. #ifdef NLCHAR
  835.     zinbuffer[zincnt++] = NLCHAR;
  836. #else
  837.     zinbuffer[zincnt++] = '\r';
  838.     zinbuffer[zincnt++] = '\n';
  839. #endif
  840.     }
  841.  
  842.     zinptr = zinbuffer;    /* set pointer to beginning, (== &zinbuffer[0]) */
  843.     zincnt--;        /* one less char in buffer */
  844.     return((int)(*zinptr++) & 0377); /* because we return the first */
  845. }
  846.  
  847. /*  Z S O U T  --  Write a string out to the given file, buffered.  */
  848.  
  849. int
  850. zsout(n,s) int n; char *s; {
  851.     short status;
  852.     int len;
  853.  
  854.     if (chkfn(n) < 1) return(-1); /* Keep this here, prevents memory faults */
  855.     if (n != ZDFILE)
  856.     debug(F111,"zsout writing to file",s,n);
  857.  
  858.     len = strlen (s);
  859.     if ('\n' == s[len-1]) {
  860.     s[--len] = '\0';
  861.     return (zsoutl (n,s));
  862.     }
  863.  
  864.     fp[n]->record_len = (short) strlen(s);
  865.     /* since this never writes an end-of-line, always write partial */
  866.     if (fp[n]->organization == STREAM_FILE && fp[n]->binary)
  867.     s$write_raw (&fp[n]->port, &fp[n]->record_len, s, &status);
  868.     else
  869.     s$seq_write_partial (&fp[n]->port, &fp[n]->record_len, s, &status);
  870.  
  871.     return status ? (-1) : 0;
  872. }
  873.  
  874. /*  Z S O U T L  --  Write string to file, with line terminator, buffered  */
  875.  
  876. int
  877. zsoutl(n,s) int n; char *s; {
  878.     short status;
  879.     int len;
  880.  
  881.     if (chkfn(n) < 1) return(-1);
  882.     if (n != ZDFILE)
  883.     debug(F111,"zsoutl writing to file",s,n);
  884.  
  885.     len = strlen (s);
  886.     if ('\n' == s[len-1])
  887.     len--;
  888.  
  889.     fp[n]->record_len = len;
  890.     /* adding a record terminator, means always use seq_write... */
  891.  
  892.     s$seq_write (&fp[n]->port, &fp[n]->record_len, s, &status);
  893.     return status ? (-1) : 0;
  894. }
  895.  
  896. /*  Z S O U T X  --  Write x characters to file, unbuffered.  
  897.  *  returns -1 on error, or the number of characters written.
  898.  */
  899.  
  900. int
  901. zsoutx(n,s,x) int n, x; char *s; {
  902.     short status;
  903.  
  904.     if (chkfn(n) < 1) return(-1);
  905.     if (n != ZDFILE)
  906.     debug(F101,"zsoutx writing to file","",n);
  907.     fp[n]->record_len = x;
  908.  
  909.     if (fp[n]->binary) {
  910.     if (fp[n]->organization == STREAM_FILE)
  911.         s$write_raw (&fp[n]->port, &fp[n]->record_len, s, &status);
  912.     else
  913.         s$seq_write_partial (&fp[n]->port, &fp[n]->record_len, s, &status);
  914.     }
  915.     else
  916.     s$seq_write (&fp[n]->port, &fp[n]->record_len, s, &status);
  917.  
  918.     return((status == 0) ? x : -1);
  919. }
  920.  
  921.  
  922. /*  Z C H O U T  --  Add a character to the given file.  */
  923.  
  924. /*  Should return 0 or greater on success, -1 on failure (e.g. disk full)  */
  925.  
  926. int
  927. #ifdef CK_ANSIC
  928. zchout(register int n, char c)
  929. #else
  930. zchout(n,c) register int n; char c;
  931. #endif /* CK_ANSIC */
  932. /* zchout() */ {
  933.     short status;
  934.     char  buff[2];
  935.  
  936.     if (chkfn(n) < 1) return(-1);
  937.  
  938.     buff[0] = c;
  939.     buff[1] = '\0';
  940.     if (n != ZDFILE)
  941.     debug(F111,"zchout writing to file",buff,n);
  942.  
  943.     fp[n]->record_len = 1;
  944.  
  945.     if (fp[n]->binary) {
  946.     if (fp[n]->organization == STREAM_FILE)
  947.         s$write_raw (&fp[n]->port, &fp[n]->record_len, &c, &status);
  948.     else
  949.         s$seq_write_partial (&fp[n]->port, &fp[n]->record_len,
  950.         &c, &status);
  951.     }
  952.     else
  953.     s$seq_write (&fp[n]->port, &fp[n]->record_len, &c, &status);
  954.  
  955.     return((status == 0) ? 1 : -1);
  956. }
  957.  
  958. /* (PWP) buffered character output routine to speed up file IO */
  959.  
  960. int
  961. zoutdump() {
  962.     int x;
  963.     VFILE *vp;
  964.     short status = 0;
  965.     int   len;
  966.     register char  *ep;
  967. #ifdef NLCHAR
  968.     char  *cp;
  969. #else
  970.     register int   i;
  971.     static   char  last;
  972. #endif /* NLCHAR */
  973.  
  974.     zoutptr = zoutbuffer;        /* Reset buffer pointer in all cases */
  975.     debug(F101,"zoutdump chars","",zoutcnt);
  976.     if (zoutcnt == 0) {            /* Nothing to output */
  977.     return(0);
  978.     } else if (zoutcnt < 0) {        /* Unexpected negative argument */
  979.     zoutcnt = 0;            /* Reset output buffer count */
  980.     return(-1);            /* and fail. */
  981.     }
  982.  
  983.     vp = fp[ZOFILE];
  984.  
  985.     if (vp->binary) {
  986.     vp->record_len = zoutcnt;
  987.     debug(F101,"zoutdump binary, port","",vp->port);
  988.     if (vp->port == DEFAULT_OUTPUT_PORT_ID) { /* terminal */
  989.         conxo (vp->record_len, zoutbuffer);
  990.     }
  991.     else {
  992.         if (vp->organization == STREAM_FILE)
  993.         s$write_raw (&vp->port, &vp->record_len,
  994.             zoutbuffer, &status);
  995.         else
  996.         s$seq_write_partial (&vp->port, &vp->record_len,
  997.             zoutbuffer, &status);
  998.     }
  999.     }
  1000.     else {
  1001.     /* need to deblock the data in the output buffer */
  1002.  
  1003.     debug(F101,"zoutdump text, port","",vp->port);
  1004. #ifdef NLCHAR
  1005.     cp = zoutbuffer;
  1006.  
  1007.     do {
  1008.         for (ep = cp; *ep != NLCHAR; ep++)
  1009.         if (ep > &zoutbuffer[zoutcnt-1]) {
  1010.             ep = NULL;
  1011.             break;
  1012.         }
  1013.         if (NULL != ep) {
  1014.         len = (int) (ep - cp); /* NOT +1 */
  1015.         vp->record_len = len;
  1016.         if (vp->port == DEFAULT_OUTPUT_PORT_ID) { /* terminal */
  1017.             *ep = '\0'; /* conoll wants ASCIIZ string */
  1018.             conoll (cp);
  1019.         }
  1020.         else
  1021.             s$seq_write (&vp->port, &vp->record_len, cp, &status);
  1022.         ep++;    /* move past the NLCHAR or NULL */
  1023.         }
  1024.         else if (cp < &zoutbuffer[zoutcnt-1]) {    /* not done it all */
  1025.         /* it's flushing in the middle of a record */
  1026.         len = zoutcnt - (int) (cp - (&zoutbuffer[0]));
  1027.         vp->record_len = len;
  1028.         if (vp->port == DEFAULT_OUTPUT_PORT_ID) { /* terminal */
  1029.             *(cp+len) = '\0'; /* conol wants ASCIIZ string */
  1030.             conol (cp);
  1031.         }
  1032.         else {
  1033.             if (vp->organization == STREAM_FILE)
  1034.             s$write_raw (&vp->port, &vp->record_len,
  1035.                 cp, &status);
  1036.             else
  1037.             s$seq_write_partial (&vp->port, &vp->record_len,
  1038.                 cp, &status);
  1039.         }
  1040.         }
  1041.         cp = ep;
  1042.     } while ((status == 0) && (NULL != cp) &&
  1043.          ((cp < &zoutbuffer[zoutcnt-1])));
  1044. #else
  1045.     /* this is not tested, but it is identical to log code in dpt.pm */
  1046.     ep = zoutbuffer;
  1047.     for (i = 0; i < zoutcnt; i++)
  1048.     {
  1049.         if ((zoutbuffer[i] == '\r') || (zoutbuffer[i] == '\n'))
  1050.         {
  1051.         len = (zoutbuffer + i) - ep;
  1052.         if ((last != '\r') || (buff[i] != '\n')) {
  1053.             if (vp->port == DEFAULT_OUTPUT_PORT_ID) { /* terminal */
  1054.             *(ep + len) = '\0'; /* conoll wants ASCIIZ string */
  1055.             conoll (ep);
  1056.             }
  1057.             else
  1058.             s$seq_write (&vp->port, &len, ep, &status);
  1059.         }
  1060.         ep = &zoutbuffer[i+1];
  1061.         }
  1062.         last = zoutbuffer[i];
  1063.     }
  1064.  
  1065.     if (ep < (zoutbuffer + len)) /* write out the last bit */
  1066.     {
  1067.         len = (zoutbuff + len) - ep;
  1068.         if (vp->port == DEFAULT_OUTPUT_PORT_ID) { /* terminal */
  1069.         *(ep + len) = '\0'; /* conol wants ASCIIZ string */
  1070.         conol (ep);
  1071.         }
  1072.         else {
  1073.         if (vp->organization == STREAM_FILE)
  1074.             s$write_raw (&vp->port, &len, ep, &status);
  1075.         else
  1076.             s$seq_write_partial (&vp->port, &len, ep, &status);
  1077.         }
  1078.     }
  1079.  
  1080. #endif /* NLCHAR */
  1081.  
  1082.     }
  1083.     if (0 == status) {
  1084.     debug(F101,"zoutdump s$...write... ok","",zoutcnt);
  1085.     zoutcnt = 0;            /* Reset output buffer count */
  1086.     return(0);            /* write() worked OK */
  1087.     } else {
  1088.     debug(F101,"zoutdump s$...write... status","",status);
  1089.     zoutcnt = 0;            /* Reset output buffer count */
  1090.     return(-1);            /* s$write_* failed */
  1091.     }
  1092. }
  1093.  
  1094. /*  C H K F N  --  Internal function to verify file number is ok  */
  1095.  
  1096. /*
  1097.  Returns:
  1098.   -1: File number n is out of range
  1099.    0: n is in range, but file is not open
  1100.    1: n in range and file is open
  1101. */
  1102. int
  1103. chkfn(n) int n; {
  1104.     if (n < 0 || n >= ZNFILS) {
  1105.     debug(F101,"chkfn: file number out of range","",n);
  1106.     return(-1);
  1107.     }
  1108.     else return( (fp[n] == NULL) ? 0 : 1 );
  1109. }
  1110.  
  1111. /*  Z C H K I  --  Check if input file exists and is readable  */
  1112.  
  1113. /*
  1114.   Returns:
  1115.    >= 0 if the file can be read (returns the size).
  1116.      -1 if file doesn't exist or can't be accessed,
  1117.      -2 if file exists but is not readable (e.g. a directory file).
  1118.      -3 if file exists but protected against read access.
  1119.  
  1120.  Directory files, special files, and symbolic links are not readable.
  1121. */
  1122. long
  1123. zchki(name) char *name; {
  1124.     CV(256) path;
  1125.     CV(256) src_path;
  1126.     CV(32) suffix;
  1127.     FILE_STATUS_STRUCT finfo;
  1128.     short status;
  1129.     int x;
  1130.     long size;
  1131.  
  1132.     debug(F110,"zchki checking",name,0);
  1133.  
  1134.     if (!name) return (-1);
  1135.  
  1136.     strcpy (&src_path, name);
  1137.     strcpy (&suffix, "");
  1138.     s$expand_path (&src_path, &suffix, &path, &status);
  1139.     if (status) {
  1140.     debug(F101,"zchki s$expand_path status","",status);
  1141.     return -1; /* doesn't exist or cannot be accessed */
  1142.     }
  1143.  
  1144.     finfo.version = FILE_STAT_VERSION_5;
  1145.     s$get_file_status (&path, &finfo, &status);
  1146.     if (status) {
  1147.     debug(F101,"zchki s$get_file_status status","",status);
  1148.     return -1;
  1149.     }
  1150.  
  1151.     switch ((int) finfo.callers_mode) {
  1152.     case 'r':
  1153.     case 'w':
  1154.         break;        /* exists, and we can read it */
  1155.  
  1156.     case 'e':
  1157.     case 'n':
  1158.     case 'u':
  1159.         return (-3);    /* exists, but protected from reading */
  1160.  
  1161.     default:
  1162.         return (-2);    /* not a file */
  1163.     }
  1164.  
  1165.     switch (finfo.file_organization) {
  1166.     case SEQUENTIAL_FILE:
  1167.     case STREAM_FILE:
  1168.         size = finfo.last_record_number;
  1169.         break;
  1170.  
  1171.     case FIXED_FILE:
  1172.         size = finfo.last_record_number * 
  1173.         finfo.flags_struct.flags_bits_overlay.max_record_size;
  1174.         break;
  1175.  
  1176.     case RELATIVE_FILE:
  1177.         size = finfo.last_record_number * 
  1178.         (finfo.flags_struct.flags_bits_overlay.max_record_size + 2);
  1179.         break;
  1180.  
  1181.     default:    /* uhhhh.... */
  1182.         size = 0;
  1183.         break;
  1184.     }
  1185.  
  1186.     strcpy (nambuf,name);        /* remember the name of the file */
  1187.  
  1188.     if (size > (finfo.blocks_used * 4096))
  1189.     size = (finfo.blocks_used * 4096);
  1190.  
  1191.     if (size < 0)
  1192.     size = 0;
  1193.  
  1194.     return (size);
  1195. }
  1196.  
  1197. /*  Z C H K O  --  Check if output file can be created  */
  1198.  
  1199. /*
  1200.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  1201. */
  1202. int
  1203. zchko(name) char *name; {
  1204.     int i, x;
  1205.     char *s;
  1206.  
  1207.     if (!name) return(-1);        /* Watch out for null pointer. */
  1208.     x = (int)strlen(name);        /* Get length of filename */
  1209.     debug(F111,"zchko length",name,x);
  1210.  
  1211.     return (0);
  1212. }
  1213.  
  1214. /*  Z D E L E T  --  Delete the named file.  */
  1215.  
  1216. int
  1217. zdelet(name) char *name; {
  1218.     debug(F110,"zdelet",name,0);
  1219.     return(remove(name));
  1220. }
  1221.  
  1222. /*  Z R T O L  --  Convert remote filename into local form  */
  1223.  
  1224. /*  For VOS as in UNIX, this means changing uppercase letters to lowercase. */
  1225.  
  1226. VOID
  1227. zrtol(name,name2) char *name, *name2; {
  1228.     char *p; int flag = 0;
  1229.     debug(F101,"zrtol name","",name);
  1230.     debug(F101,"zrtol name2","",name2);
  1231.     if (!name || !name2) return;
  1232.     debug(F101,"zrtol input","",name);
  1233.     p = name2;
  1234.     for ( ; *name != '\0'; name++) {
  1235.     if (*name > ' ') flag = 1;    /* Strip leading blanks and controls */
  1236.     if (flag == 0 && *name < '!') continue;
  1237.         *p++ = isupper(*name) ? tolower(*name) : *name;
  1238.     }
  1239.     *p-- = '\0';            /* Terminate */
  1240.     while (*p < '!' && p > name2)    /* Strip trailing blanks & contronls */
  1241.       *p-- = '\0';
  1242.  
  1243.     debug(F110,"zrtol result",name2,0);
  1244. }
  1245.  
  1246. VOID
  1247. nzrtol(name,name2,fncnv,fnrpath,max)
  1248.     char *name, *name2; int fncnv, fnrpath, max;
  1249. { /* nzrtol */
  1250.     char *p; int flag = 0, n = 0;
  1251.     char tmpbuf[CKMAXPATH+1], *tmp;
  1252.     int devnull = 0;
  1253.     int acase = 0;
  1254.     if (!name2) return;
  1255.     if (!name) name = "";
  1256.     debug(F110,"zrtol original name",name,0);
  1257.  
  1258.     tmpbuf[0] = '\0';
  1259.     tmp = tmpbuf;
  1260.     devnull = !strcmp(name,"/dev/null");
  1261.  
  1262.     if (!devnull && fnrpath == PATH_OFF) { /* RECEIVE PATHNAMES OFF */
  1263.     zstrip(name,&p);
  1264.     strncpy(tmpbuf,p,CKMAXPATH);
  1265.     } else if (!devnull && fnrpath == PATH_ABS) { /* REC PATHNAMES ABSOLUTE */
  1266.     strncpy(tmpbuf,name,CKMAXPATH); 
  1267.     } else if (!devnull && isabsolute(name)) { /* RECEIVE PATHNAMES RELATIVE */
  1268.     sprintf(tmpbuf,".%s",name);
  1269.     } else {                /* Ditto */
  1270.     strncpy(tmpbuf,name,CKMAXPATH); 
  1271.     }
  1272.     tmpbuf[CKMAXPATH] = 0;
  1273.     debug(F110,"zrtol tmpbuf",tmpbuf,0);
  1274.  
  1275.     if (!fncnv || devnull) {        /* Not converting */
  1276.     strncpy(name2,tmpbuf,max);    /* We're done. */
  1277.     return;
  1278.     }
  1279.     name = tmpbuf;            /* Converting */
  1280.  
  1281.     p = name2;
  1282.     for (; *name != '\0' && n < maxnam; name++) {
  1283.     if (*name > ' ') flag = 1;    /* Strip leading blanks and controls */
  1284.     if (flag == 0 && *name < '!')
  1285.       continue;
  1286.     if (isupper(*name))        /* Check for mixed case */
  1287.       acase |= 1;
  1288.     else if (islower(*name))
  1289.       acase |= 2;
  1290.     *p++ = *name;
  1291.     n++;
  1292.     }
  1293.     *p-- = '\0';            /* Terminate */
  1294.     while (*p < '!' && p > name2)    /* Strip trailing blanks & controls */
  1295.       *p-- = '\0';
  1296.  
  1297.     if (*name2 == '\0') {        /* Nothing left? */
  1298.     strcpy(name2,"NONAME");        /* do this... */
  1299.     } else if (acase == 1) {        /* All uppercase? */
  1300.     p = name2;            /* So convert all letters to lower */
  1301.     while (*p) {
  1302.         if (isupper(*p))
  1303.           *p = tolower(*p);
  1304.         p++;
  1305.     }
  1306.     }      
  1307.     debug(F110,"zrtol new name",name2,0);
  1308. }
  1309.  
  1310. /*  Z S T R I P  --  Strip device & directory name from file specification */
  1311.  
  1312. /*  Strip pathname from filename "name", return pointer to result in name2 */
  1313.  
  1314. static char work[257];
  1315.  
  1316. VOID
  1317. zstrip(name,name2) char *name, **name2; {
  1318.     char *cp, *pp;
  1319.     debug(F110,"zstrip before",name,0);
  1320.     pp = work;
  1321. #ifdef DTILDE
  1322.     if (*name == '~') name++;
  1323. #endif /* DTILDE */
  1324.     for (cp = name; *cp != '\0'; cp++) {
  1325.         if (ISDIRSEP(*cp))
  1326.       pp = work;
  1327.     else
  1328.       *pp++ = *cp;
  1329.     }
  1330.     *pp = '\0';                /* Terminate the string */
  1331.     *name2 = work;
  1332.     debug(F110,"zstrip after",*name2,0);
  1333. }
  1334.  
  1335. /*  Z L T O R  --  Local TO Remote */
  1336.  
  1337. VOID
  1338. zltor(name,name2) char *name, *name2; {
  1339.     nzltor(name,name2,1,0,CKMAXPATH);
  1340. }
  1341.  
  1342. /*  N Z L T O R  --  New Local TO Remote */
  1343.  
  1344. VOID
  1345. nzltor(name,name2,fncnv,fnspath,max)
  1346.     char *name, *name2; int fncnv, fnspath, max;
  1347. { /* nzltor */
  1348.     char *cp, *pp;
  1349. #ifdef COMMENT
  1350.     int dc = 0;
  1351. #endif /* COMMENT */
  1352.     int n = 0;
  1353.     char *dotp = NULL;
  1354.     char tmpbuf[CKMAXPATH+1];
  1355.     char *p;
  1356.  
  1357.     debug(F110,"nzltor name",name,0);
  1358.  
  1359.     /* Handle pathname */
  1360.     
  1361.     tmpbuf[0] = 0;
  1362.     if (fnspath == PATH_OFF) {        /* PATHNAMES OFF */
  1363.     zstrip(name,&p);
  1364.     strncpy(tmpbuf,p,CKMAXPATH);
  1365.     tmpbuf[CKMAXPATH] = 0;
  1366.     } else {
  1367.     int ndx;
  1368.  
  1369.     if (fnspath == PATH_ABS) {    /* ABSOLUTE */
  1370.         zfnqfp(name,CKMAXPATH,tmpbuf);
  1371.     } else {            /* RELATIVE */
  1372.         strncpy(tmpbuf,name,CKMAXPATH); 
  1373.     }
  1374.  
  1375.     for (p = tmpbuf; !ISDIRSEP(*p); p++)
  1376.         ;                 /* Look for first '>' */
  1377.     ndx = 0;               /* strip off %system#module */
  1378.     while (*p) {
  1379.         tmpbuf[ndx++] = *p++;
  1380.     }
  1381.  
  1382.     }
  1383.     debug(F110,"zltor tmpbuf",tmpbuf,0);
  1384.  
  1385.     if (!fncnv) {            /* Not converting */
  1386.     strncpy(name2,tmpbuf,max);    /* We're done. */
  1387.     return;
  1388.     }
  1389.     name = tmpbuf;            /* Converting */
  1390.  
  1391.     pp = work;                /* Output buffer */
  1392.  
  1393.     for (cp = name, n = 0; *cp && n < max; cp++,n++) { /* Strip path name */
  1394.  
  1395.     if (islower(*cp))        /* Uppercase letters */
  1396.       *pp++ = toupper(*cp);        /* Change tilde to 'X' */
  1397.     else if (*cp == '~')
  1398.       *pp++ = 'X';
  1399.     else if (*cp == '#')        /* Change number sign to 'X' */
  1400.       *pp++ = 'X';
  1401.     else if (*cp == '*' || *cp == '?')
  1402.       *pp++ = 'X';
  1403.     else if (*cp <= ' ')        /* Change space and controls to 'X' */
  1404.       *pp++ = 'X';
  1405.     else if (*cp == '>' || *cp == '<')    /* Change any dirseps to '/' */
  1406.       *pp++ = '/';                  /* <'s should really be parsed out */
  1407.     else if (*cp == '.') {        /* Change dot to underscore */
  1408.         dotp = pp;            /* Remember where we last did this */
  1409.         *pp++ = '_';
  1410.     } else
  1411.       *pp++ = *cp;
  1412.     }
  1413.     *pp = 0;                /* Tie it off. */
  1414.     if (dotp) *dotp = '.';        /* Restore last dot (if any) */
  1415.     cp = name2;                /* If nothing before dot, */
  1416.     if (*work == '.') *cp++ = 'X';    /* insert 'X' */
  1417.     strncpy(cp,work,max);
  1418.     debug(F110,"zltor name2",name2,0);
  1419. }
  1420.  
  1421.  
  1422.  
  1423. /*  Z C H D I R  --  Change directory  */
  1424. /*
  1425.   Call with:
  1426.     dirnam = pointer to name of directory to change to,
  1427.       which may be "" or NULL to indicate user's home directory.
  1428.   Returns:
  1429.     0 on failure
  1430.     1 on success
  1431. */
  1432. int
  1433. zchdir(dirnam) char *dirnam; {
  1434.     char *hd, *sp, *p;
  1435.     CV(256) rel;
  1436.     CV(256) abs;
  1437.     CV(32) suffix;
  1438.     short status;
  1439.  
  1440.     debug(F110,"zchdir",dirnam,0);
  1441.     if (dirnam == NULL || dirnam == "" || *dirnam == '\0') /* If arg is null */
  1442.       dirnam = zhome();            /* use user's home directory. */
  1443.     sp = dirnam;
  1444.     debug(F110,"zchdir 2",dirnam,0);
  1445.  
  1446. #ifdef DTILDE
  1447.     hd = tilde_expand(dirnam);        /* Attempt to expand tilde */
  1448.     if (*hd == '\0') hd = dirnam;    /* in directory name. */
  1449. #else
  1450.     hd = dirnam;
  1451. #endif /* DTILDE */
  1452.     debug(F110,"zchdir 3",hd,0);
  1453.  
  1454.  
  1455.     strcpy(&rel, hd);
  1456.     strcpy(&abs, "");
  1457.     strcpy(&suffix, "");
  1458.     status = 0;
  1459.  
  1460.     s$expand_path(&rel, &suffix, &abs, &status);
  1461.     debug(F111,"zchdir4",hd,status);
  1462.     if(status)
  1463.     return(0);
  1464.  
  1465.     s$change_current_dir(&abs, &status);
  1466.     debug(F111,"zchdir5",hd,status);
  1467.     if(status)
  1468.      return(0);
  1469.     else
  1470.     return(1);
  1471. }
  1472.  
  1473. /*  Z H O M E  --  Return pointer to user's home directory  */
  1474.  
  1475. /* This is a royal pain under some operating systems, involving reading    */
  1476. /* the password file, etc.  Under VOS we just ask, since the user is    */
  1477. /* allowed to specify a different home dir at login time, they make it  */
  1478. /* easy to get the answer.                        */
  1479.  
  1480. char *
  1481. zhome() {
  1482.     CV(256) cv_home;
  1483.     static char homebuf[257];
  1484.     char *home;
  1485.  
  1486.     s$get_home_dir (&cv_home);
  1487.     strcpy (homebuf, &cv_home);
  1488.     home = homebuf;
  1489.  
  1490.     return(home ? home : ".");
  1491. }
  1492.  
  1493. /*  Z G T D I R  --  Return pointer to user's current directory  */
  1494.  
  1495. char *
  1496. zgtdir() {
  1497.     char *buf;
  1498.     static char currbuf[257];
  1499.     CV(256) cv_curr;
  1500.  
  1501.     s$get_current_dir (&cv_curr);
  1502.     strcpy (currbuf, &cv_curr);
  1503.     buf = currbuf;
  1504.  
  1505.     return buf;
  1506. }
  1507.  
  1508. /*  Z X C M D -- Run a system command so its output can be read like a file */
  1509.  
  1510. /* return 0 on error, 1 on success, -1 if file number is bad. */
  1511.  
  1512. int
  1513. zxcmd(filnum,comand) int filnum; char *comand; {
  1514.     int out;
  1515.     short organization = SEQUENTIAL_FILE;
  1516.     short max_record_len = 4096;
  1517.     short status;
  1518.     short append_switch = 0;
  1519.     CV(256) temp_path;
  1520.     char c_temp_path[257];
  1521.     int save_binary;
  1522.  
  1523.     debug(F111,"zxcmd called",comand,filnum);
  1524.     if (chkfn(filnum) < 0) return(-1);    /* Need a valid Kermit file number. */
  1525.  
  1526.     if (filnum == ZSTDIO || filnum == ZCTERM) /* But not one of these. */
  1527.       return(0);
  1528.  
  1529.     out = (filnum == ZIFILE || filnum == ZRFILE) ? 0 : 1 ;
  1530.     if (out) {
  1531.       debug(F101,"zxcmd cannot open for output","",filnum);
  1532.       return (0); /* we can't write to the file */
  1533.     }
  1534.  
  1535.     s$get_temp_file (&organization, &max_record_len, &temp_path, &status);
  1536.     if (status) {
  1537.     debug(F101,"zxcmd s$get_temp_file, status","",status);
  1538.     return (0); /* error */
  1539.     }
  1540.     strcpy (c_temp_path, &temp_path);
  1541.     s$attach_default_output (&temp_path, &append_switch, &status);
  1542.     if (status) {
  1543.     debug(F111,"zxcmd s$attach_default_output, status",c_temp_path,status);
  1544.     return (0); /* error */
  1545.     }
  1546.     *nambuf = '\0';        /* forget old name */
  1547.     status = zsyscmd(comand); /* Hope this works. get VOS status back. */ 
  1548.     debug(F111,"zxcmd zsyscmd status",c_temp_path,status);
  1549.     if (status) { /* error */
  1550.     s$detach_default_output (&status); /* ignore this status */
  1551.     s$delete_file_on_close (&temp_path, &status); /* ignore this status */
  1552.     return (0); /* error */
  1553.     }
  1554.     s$detach_default_output (&status);
  1555.     if (status) {
  1556.     debug(F111,"zxcmd s$detach_default_output, status",c_temp_path,status);
  1557.     s$delete_file_on_close (&temp_path, &status); /* ignore this status */
  1558.     return (0); /* error */
  1559.     }
  1560.     save_binary = binary;
  1561.     binary = 0; /* it's a text file */
  1562.     if (0 == zopeni (filnum, c_temp_path)) { /* error? */
  1563.     binary = save_binary;
  1564.     debug(F100,"zxcmd zopeni failed","",0);
  1565.     s$delete_file_on_close (&temp_path, &status); /* ignore this status */
  1566.     return (0); /* error */
  1567.     }
  1568.     binary = save_binary;
  1569.     s$delete_file_on_close (&temp_path, &status); /* will poof at zclosf */
  1570.     if (status) {
  1571.     debug(F111,"zxcmd s$delete_file_on_close, status",c_temp_path,status);
  1572.     /* continue, it will still read, just leaving a loose file around */
  1573.     }
  1574.  
  1575.     return(1); /* Success! */
  1576. }
  1577.  
  1578. /*  Z C L O S F  - wait for the child fork to terminate and close the pipe. */
  1579.  
  1580. int
  1581. zclosf(filnum) int filnum; {
  1582.     debug(F101,"zclosf called","",filnum);
  1583.     return(1);
  1584. }
  1585.  
  1586. /*  Z X P A N D  --  Expand a wildcard string into an array of strings  */
  1587. /*
  1588.   Returns the number of files that match fn1, with data structures set up
  1589.   so  that first file (if any) will be returned by the next znext() call.
  1590.   Depends on external variable wildxpand:  0 means  we  expand  wildcards
  1591.   internally, nonzero means we call the shell to do it. In VOS, we always
  1592.   do it.
  1593. */
  1594.  
  1595. int
  1596. zxpand(fn) char *fn; {
  1597.     char *p;
  1598.  
  1599. #ifdef DTILDE                /* Built with tilde-expansion? */
  1600.     char *tnam;
  1601. #endif /* DTILDE */
  1602.     debug(F111,"zxpand entry",fn,wildxpand);
  1603. #ifdef DTILDE                /* Built with tilde-expansion? */
  1604.     if (*fn == '~') {            /* Starts with tilde? */
  1605.     tnam = tilde_expand(fn);    /* Try to expand it. */
  1606.     if (tnam) fn = tnam;
  1607.     }
  1608.     debug(F110,"zxpand after tilde_x",fn,0);
  1609. #endif /* DTILDE */
  1610.  
  1611.     fcount = shxpand(fn,mtchs,MAXWLD); /* Shell [JUST KIDDING!] */
  1612.  
  1613.     if (fcount > 0) {
  1614.     mtchptr = mtchs;        /* Save pointer for next. */
  1615.     }
  1616.     if (fcount > 0) {
  1617.     debug(F111,"zxpand ok",mtchs[0],fcount);
  1618.     return(fcount);
  1619.     }
  1620.     debug(F111,"zxpand fgen1",fn,fcount); /* Didn't get one, or got too many */
  1621.     p = malloc((int)strlen(fn) + 10);    /* Make space */
  1622.     if (!p) return(0);
  1623.     zrtol(fn,p);            /* Try again, maybe lowercase */
  1624.  
  1625.     fcount = shxpand(p,mtchs,MAXWLD); /* Shell */
  1626.  
  1627.     if (fcount > 0) {            /* Got one? */
  1628.     mtchptr = mtchs;        /* Save pointer for next. */
  1629.     debug(F111,"zxpand fgen2 ok",mtchs[0],fcount);
  1630.     } else debug(F111,"zxpand 2 not ok",p,fcount);
  1631.     free(p);
  1632.     return(fcount);
  1633. }
  1634.  
  1635.  
  1636. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  1637. /*
  1638.  Returns >0 if there's another file, with its name copied into the arg string,
  1639.  or 0 if no more files in list.
  1640. */
  1641. int
  1642. znext(fn) char *fn; {
  1643.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  1644.     else *fn = '\0';
  1645.     debug(F111,"znext",fn,fcount+1);
  1646.     return(fcount+1);
  1647. }
  1648.  
  1649.  
  1650. /*  Z C H K S P A  --  Check if there is enough space to store the file  */
  1651.  
  1652. /*
  1653.  Call with file specification f, size n in bytes.
  1654.  Returns -1 on error, 0 if not enough space, 1 if enough space.
  1655.  
  1656.  On  VOS, we pick up the volume name from first part of the path name, then
  1657.  check to make sure it's valid, then check the amount of space available on
  1658.  that volume.  Also, VOS won't let you use the last 200 blocks of the  file
  1659.  partition  on  the  volume, so we count off for that.  Also, the number of
  1660.  indirect blocks is only  partially  accounted  for.  This  isn't  perfect,
  1661.  because  some  of  the different file organizations can cause the reported
  1662.  size of the file not to match the actual size of the  file,  but  this  is
  1663.  only  a problem with files with a large number of records and certain file
  1664.  orgs.  We don't try to figure this out, instead we let the  transfer  fail
  1665.  when  the  disk gets full, which is what a number of implementations do in
  1666.  all cases.
  1667.  
  1668. */
  1669. int
  1670. #ifdef CK_ANSIC
  1671. zchkspa(char *f, long n)
  1672. #else
  1673. zchkspa(f,n) char *f; long n;
  1674. #endif /* CK_ANSIC */
  1675. /* zchkspa() */ {
  1676.     CV(256) path;
  1677.     CV(256) src_path;
  1678.     char    cpath[257];
  1679.     CV(32) suffix;
  1680.     CV(66) disk_name;
  1681.     short status;
  1682.     int x;
  1683.     struct disk_info dinfo;
  1684.     char *cp;
  1685.     long blocks_left;
  1686.  
  1687.     debug(F111,"zchkspa checking",f,n);
  1688.  
  1689.     if (!f) return (-1);
  1690.  
  1691.     strcpy (&src_path, f);
  1692.     strcpy (&suffix, "");
  1693.     s$expand_path (&src_path, &suffix, &path, &status);
  1694.     if (status) {
  1695.     debug(F101,"zchkspa s$expand_path status","",status);
  1696.     return -1; /* an error occurred */
  1697.     }
  1698.  
  1699.     strcpy (cpath, &path);
  1700.     cp = strchr (cpath, DIRSEP);
  1701.     if (cp)
  1702.     *cp = '\0'; /* cut off the directory name from the disk name */
  1703.  
  1704.     strcpy (&disk_name, cpath);
  1705.     dinfo.version = 1;
  1706.     s$get_disk_info (&disk_name, &dinfo, &status);
  1707.     if (status) {
  1708.     debug(F111,"zchkspa s$get_disk_info status",cpath,status);
  1709.     return -1; /* an error occurred */
  1710.     }
  1711.  
  1712.     blocks_left = dinfo.file_partition_size - dinfo.file_partition_used;
  1713.     blocks_left -= 200; /* can't use last 200 blocks */
  1714.  
  1715.     n /= 4096;
  1716.     n++;
  1717.  
  1718.     /* count some of the indirect blocks */
  1719.     /* I used to know how to count these really... */
  1720.  
  1721.     if (n > 16)
  1722.     n++;
  1723.  
  1724.     if (n > 256)
  1725.     n++;
  1726.  
  1727.     if (n >= blocks_left)
  1728.     return(0);                /* not enough space */
  1729.     else
  1730.     return(1);                /* Okay! */
  1731. }
  1732.  
  1733.  
  1734. /*  Z N E W N  --  Make a new name for the given file  */
  1735.  
  1736. /*
  1737.   Given  the  name,  fn, of a file that already exists, this function builds a
  1738.   new name of the form "<oldname>.~<n>~", where  <oldname>  is  argument  name
  1739.   (fn),  and  <n>  is  a  version number, one higher than any existing version
  1740.   number for that file, up to 9999.  This format is consistent with that  used
  1741.   by GNU EMACS.  If the constructed name is too long for the system's maximum,
  1742.   enough  characters  are  truncated from the end of <fn> to allow the version
  1743.   number to fit.  If no free version numbers  exist  between  1  and  9999,  a
  1744.   version  number  of  "xxxx"  is  used.  Returns a pointer to the new name in
  1745.   argument s.
  1746. */
  1747.  
  1748. VOID
  1749. znewn(fn,s) char *fn, **s; {
  1750. #define ZNEWNBL 255
  1751. #define ZNEWNMD 4
  1752.  
  1753.     static char buf[ZNEWNBL+1];
  1754.     char *bp, *xp, *yp;
  1755. #ifdef OS2
  1756.     char *zp, ch, temp[14];
  1757. #endif /* OS2 */
  1758.     int len = 0, d = 0, n, t, i, j, k, power = 1;
  1759.  
  1760.     int max = MAXNAMLEN;        /* Maximum name length */
  1761.  
  1762.     if (max < 14) max = 14;        /* Make it reasonable */
  1763.     if (max > ZNEWNBL) max = ZNEWNBL;
  1764.     bp = buf;                /* Buffer for building new name */
  1765.     yp = fn;
  1766.     while (*yp) {            /* Copy old name into buffer */
  1767.     *bp++ = *yp++;
  1768.     if (len++ > ZNEWNBL) break;    /* ...up to buffer length */
  1769.     }
  1770.     *s = NULL;
  1771.     for (i = 1; i < ZNEWNMD + 1; i++) {    /* Version numbers up to 10**i - 1 */
  1772.     power *= 10;            /* Next power of 10 */
  1773.     j = max - len;            /* Space left for version number */
  1774.     k = 3 + i;            /* Space needed for it */
  1775.     if (j < k) {            /* Make room if necessary */
  1776.         len -= (k - j);        /* Adjust length of filename */
  1777.         bp = buf + len;        /* Point to new end */
  1778.     }
  1779.     *bp++ = '*';            /* Put a star on the end (UNIX) */
  1780.     *bp-- = '\0';            /* Terminate with null */
  1781.  
  1782.     n = zxpand(buf);        /* Expand the resulting wild name */
  1783.                     /* n is the number of matches */
  1784.     while (n-- > 0) {        /* Find any existing name.~n~ files */
  1785.         xp = *mtchptr++;        /* Point at matching name */
  1786.         xp += len;            /* Look for .~<n>~ at the end of it */
  1787.         if (*xp == '.' && *(xp+1) == '~') {    /* Has a version number */
  1788.         t = atoi(xp+2);                /* Get it */
  1789.         if (t > d) d = t;    /* Save d = highest version number */
  1790.         }
  1791.     }
  1792.     if (d < power-1) {        /* Less than maximum possible? */
  1793.         sprintf(bp,".~%d~",d+1);    /* Yes, make "name.~<d+1>~" */
  1794.         *s = buf;            /* Point to new name */
  1795.         break;            /* Done, return it */
  1796.     }
  1797.     }
  1798.     if (*s == NULL) {
  1799.     sprintf(bp,".~xxxx~");        /* Too many, use xxxx. */
  1800.     *s = buf;
  1801.     }
  1802.  
  1803.     return;
  1804. }
  1805.  
  1806.  
  1807. /*  Z R E N A M E  --  Rename a file  */
  1808.  
  1809. /*  Call with old and new names */
  1810. /*  Returns 0 on success, -1 on failure. */
  1811.  
  1812. int
  1813. zrename(old,new) char *old, *new; {
  1814.    debug(F110,"zrename old",old,0);
  1815.    debug(F110,"        new",new,0);
  1816.    return rename(old, new);
  1817. }
  1818.  
  1819.  
  1820. /*  Z C O P Y  --  Copy a file  */
  1821. /*
  1822.    Call with source and destination names.
  1823.    If destination name is the name of a directory, the source file is
  1824.    copied to that directory with the original name.
  1825.    Returns 0 on success, -1 on failure.
  1826. */
  1827. int
  1828. zcopy(source,destination) char *source, *destination; {
  1829.     CV(256) cvsrc;
  1830.     CV(256) cvdest;
  1831.     CV(32)  suffix;
  1832.     CV(256) cvpath;
  1833.     CV(32)  caller;
  1834.     short   switches;
  1835.     short   status;
  1836.     char p[257], *s = destination;
  1837.     int x;
  1838.     
  1839.     debug(F110,"zcopy source",source,0);
  1840.     debug(F110,"zcopy destination",s,0);
  1841.     /* it's easier to code this way, not faster... */
  1842.     if (isdir(destination)) {
  1843.     char *q = NULL;
  1844.     x = strlen(destination);
  1845.  
  1846.     strcpy(p,destination);        /* Directory part */
  1847.     if (!ISDIRSEP(*(destination+x-1))) /* Separator, if needed */
  1848.       strcat(p,">");
  1849.     zstrip(source,&q);        /* Strip path part from old name */
  1850.     strcat(p,q);            /* Concatenate to new directory */
  1851.     s = p;
  1852.     debug(F110,"zcopy dir",s,0);
  1853.     } else
  1854.         debug(F110,"zcopy no dir",s,0);
  1855.  
  1856.     strcpy(&cvpath,source);
  1857.     strcpy(&suffix,"");
  1858.     status = 0;
  1859.     s$expand_path(&cvpath,&suffix,&cvsrc,&status);
  1860.     if (status != 0) {
  1861.         debug(F101,"zcopy failed expand src","",status);
  1862.     return(-1);
  1863.     }
  1864.  
  1865.     strcpy(&cvpath,s);
  1866.     s$expand_path(&cvpath,&suffix,&cvdest,&status);
  1867.     if (status != 0) {
  1868.         debug(F101,"zcopy failed expand dest","",status);
  1869.     return(-1);
  1870.     }
  1871.  
  1872.     switches = 2; /* OVERWRITE */
  1873.     strcpy(&caller,"kermit");
  1874.     s$copy_file(&cvsrc,&cvdest,&caller,&switches,&status);
  1875.     if (status != 0) {
  1876.         debug(F101,"zcopy failed s$copy_file","",status);
  1877.     return(-1);
  1878.     }
  1879.  
  1880.     return(0);
  1881. }
  1882.  
  1883.  
  1884. /*  Z S A T T R */
  1885. /*
  1886.  Fills in a Kermit file attribute structure for the file which is to be sent.
  1887.  Returns 0 on success with the structure filled in, or -1 on failure.
  1888.  If any string member is null, then it should be ignored.
  1889.  If any numeric member is -1, then it should be ignored.
  1890. */
  1891. int
  1892. zsattr(xx) struct zattr *xx; {
  1893.     long k;
  1894.     CV(256) path;
  1895.     CV(256) src_path;
  1896.     CV(32) suffix;
  1897.     FILE_STATUS_STRUCT finfo;
  1898.     short status;
  1899.     int x;
  1900.     char *cp;
  1901.     static char  creator[67];
  1902.     static char  cdate[20];
  1903.     struct tm *tp;
  1904.  
  1905.     k = iflen % 1024L;            /* File length in K */
  1906.     if (k != 0L) k = 1L;
  1907.     xx->lengthk = (iflen / 1024L) + k;
  1908.     xx->type.len = 0;            /* File type can't be filled in here */
  1909.     xx->type.val = "";
  1910.  
  1911.     if (*nambuf) {
  1912.         debug(F110,"zsattr checking",nambuf,0);
  1913.  
  1914.     strcpy (&src_path, nambuf);
  1915.     strcpy (&suffix, "");
  1916.     s$expand_path (&src_path, &suffix, &path, &status);
  1917.     if (status) {
  1918.         debug(F101,"zsattr s$expand_path status","",status);
  1919.         return -1; /* doesn't exist or cannot be accessed */
  1920.     }
  1921.  
  1922.     finfo.version = FILE_STAT_VERSION_5;
  1923.     s$get_file_status (&path, &finfo, &status);
  1924.     if (status) {
  1925.         debug(F101,"zsattr s$get_file_status status","",status);
  1926.         return -1;
  1927.     }
  1928.  
  1929.     tp = localtime ((const time_t *) &finfo.date_time_modified);
  1930.     if (NULL != tp) {
  1931.         sprintf(cdate, "%04d%02d%02d %02d:%02d:%02d", tp->tm_year + 1900,
  1932.         tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min,
  1933.         tp->tm_sec);
  1934.         xx->date.val = cdate;    /* File creation date */
  1935.         xx->date.len = (int)strlen(xx->date.val);
  1936.     }
  1937.     else {
  1938.         xx->date.val = "";    /* File creation date */
  1939.         xx->date.len = 0;
  1940.     }
  1941.  
  1942.     strcpy (creator, &finfo.author);
  1943.     cp = strchr (creator, '.');
  1944.     *cp++ = '\0';
  1945.  
  1946.     /* (person_name (file_info author)) */
  1947.     xx->creator.val = creator;        /* File creator */
  1948.     xx->creator.len = strlen (creator);
  1949.  
  1950.     /* (group_name (file_info author)) */
  1951.     xx->account.len = strlen (cp);        /* File account */
  1952.     xx->account.val = cp;
  1953.  
  1954.     xx->recfm.len = 0;            /* Record format */
  1955.     xx->recfm.val = "";
  1956.  
  1957.     } else {
  1958.     xx->date.len = 0;
  1959.     xx->date.val = "";
  1960.  
  1961.     xx->creator.len = 0;        /* File creator */
  1962.     xx->creator.val = "";
  1963.  
  1964.     xx->recfm.len = 0;            /* Record format */
  1965.     xx->recfm.val = "";
  1966.  
  1967.     xx->account.len = 0;        /* File account */
  1968.     xx->account.val = "";
  1969.     }
  1970.     xx->area.len = 0;            /* File area */
  1971.     xx->area.val = "";
  1972.     xx->password.len = 0;        /* Area password */
  1973.     xx->password.val = "";
  1974.     xx->blksize = -1L;            /* File blocksize */
  1975.     xx->xaccess.len = 0;        /* File access */
  1976.     xx->xaccess.val = "";
  1977.     xx->encoding.len = 0;        /* Transfer syntax */
  1978.     xx->encoding.val = 0;
  1979.     xx->disp.len = 0;            /* Disposition upon arrival */
  1980.     xx->disp.val = "";
  1981.     xx->lprotect.len = 0;        /* Local protection */
  1982.     xx->lprotect.val = "";
  1983.     xx->gprotect.len = 0;        /* Generic protection */
  1984.     xx->gprotect.val = "";
  1985.     xx->systemid.len = 2;        /* System ID */
  1986.     xx->systemid.val = "MV";        /* MISC, VOS */
  1987.     xx->sysparam.len = 0;        /* System-dependent parameters */
  1988.     xx->sysparam.val = "";
  1989.     xx->length = iflen;            /* Length */
  1990.     return(0);
  1991. }
  1992.  
  1993. /* Z F C D A T  --  Get file creation date */
  1994. /*
  1995.   Call with pointer to filename.
  1996.   On success, returns pointer to creation date in yyyymmdd hh:mm:ss format.
  1997.   On failure, returns pointer to null string.
  1998. */
  1999. char *
  2000. zfcdat(name) char *name; {
  2001.     static char  cdate[20];
  2002.     CV(256) path;
  2003.     CV(256) src_path;
  2004.     CV(32) suffix;
  2005.     FILE_STATUS_STRUCT finfo;
  2006.     short status;
  2007.     struct tm  *tp;
  2008.  
  2009.     debug(F110,"zfcdate called",name,0);
  2010.     strcpy (&src_path, name);
  2011.     strcpy (&suffix, "");
  2012.     s$expand_path (&src_path, &suffix, &path, &status);
  2013.     if (status) {
  2014.     debug(F101,"zfcdate s$expand_path status","",status);
  2015.     return ""; /* error */
  2016.     }
  2017.  
  2018.     finfo.version = FILE_STAT_VERSION_5;
  2019.     s$get_file_status (&path, &finfo, &status);
  2020.     if (status) {
  2021.     debug(F101,"zfcdate s$get_file_status status","",status);
  2022.     return "";
  2023.     }
  2024.  
  2025.     tp = localtime ((const time_t *) &finfo.date_time_modified);
  2026.     if (NULL == tp)
  2027.     return("");
  2028.  
  2029.     sprintf(cdate, "%04d%02d%02d %02d:%02d:%02d", tp->tm_year + 1900,
  2030.     tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min,
  2031.     tp->tm_sec);
  2032.  
  2033.     return cdate;
  2034. }
  2035.  
  2036. /* Z S T I M E  --  Set creation date for incoming file */
  2037. /*
  2038.  Call with:
  2039.  f  = pointer to name of existing file.
  2040.  yy = pointer to a Kermit file attribute structure in which yy->date.val
  2041.       is a date of the form yyyymmdd hh:mm:ss, e.g. 19900208 13:00:00.
  2042.  x  = is a function code: 0 means to set the file's creation date as given.
  2043.       1 means compare the given date with the file creation date.
  2044.  Returns:
  2045.  -1 on any kind of error.
  2046.   0 if x is 0 and the file date was set successfully.
  2047.   0 if x is 1 and date from attribute structure <= file creation date.
  2048.   1 if x is 1 and date from attribute structure > file creation date.
  2049. */
  2050.  
  2051. int
  2052. zstime(f,yy,x) char *f; struct zattr *yy; int x; {
  2053.     int r = -1;                /* return code */
  2054.     CV(256) path;
  2055.     CV(256) src_path;
  2056.     CV(32) suffix;
  2057.     FILE_STATUS_STRUCT finfo;
  2058.     short status;
  2059.     struct tm  *tp;
  2060.     struct tm  mytm;
  2061.     char fdate[20];
  2062.     char adate[32];
  2063.     CV(32) cvtime;
  2064.     int dlen; /* length of date part */
  2065.     time_t file_times[4]; /* created, used, modified, saved */
  2066.     time_t idate;
  2067.     char *cp;
  2068.     int scanval;
  2069.     time_t now;
  2070.  
  2071.     now = time(NULL);
  2072.     debug(F111,"zstime called, x",f,x);
  2073.     if (NULL == yy) {
  2074.     debug(F100,"zstime called with NULL attr struct","",0);
  2075.     return r;
  2076.     }
  2077.     if (yy->date.len > 17) { /* too long */
  2078.     debug(F111,"zstime bad date",yy->date.val,yy->date.len);
  2079.     return r;
  2080.     }
  2081.     strncpy (adate, yy->date.val, yy->date.len);
  2082.     adate[yy->date.len] = '\0';
  2083.     debug(F111,"zstime attr date",adate,yy->date.len);
  2084.  
  2085.     memset (&mytm, 0, sizeof mytm); /* clear it out initially */
  2086.     
  2087.     /* "normalize" attribute date string */
  2088.     if (NULL != (cp = strchr (adate, ' '))) {    /* is time present? */
  2089.       dlen = (int)(cp - adate);
  2090.     cp++; /* now get time, let sscanf deal with missing seconds */
  2091.     scanval = sscanf(cp,"%2d:%2d:%2d",
  2092.         &mytm.tm_hour, &mytm.tm_min, &mytm.tm_sec);
  2093.         debug(F111,"   sscan time",cp,scanval);
  2094.     if (scanval != 2 && scanval != 3)
  2095.         return (-1);        /* this is an error */
  2096.     }
  2097.     else
  2098.     dlen = strlen (adate);
  2099.  
  2100.     if (dlen == 8) {         /* yyyymmdd */
  2101.     scanval = sscanf(adate,"%4d%2d%2d",
  2102.         &mytm.tm_year, &mytm.tm_mon, &mytm.tm_mday);
  2103.     debug(F111,"   sscan long date",adate,scanval);
  2104.     if (scanval != 3)
  2105.         return (-1);
  2106.     mytm.tm_year -= 1900;
  2107.     }
  2108.     else if (dlen == 6) {    /* yymmdd */
  2109.     sscanf(adate,"%2d%2d%2d",
  2110.         &mytm.tm_year, &mytm.tm_mon, &mytm.tm_mday);
  2111.     debug(F111,"   sscan short date",adate,scanval);
  2112.     if (scanval != 3)
  2113.         return (-1);
  2114.     if (mytm.tm_year < 80)             /* VOS dates start at 1980 */
  2115.         mytm.tm_year += 100;        /* UNIX could be 1975...   */
  2116.     }
  2117.     else {                    /* error */
  2118.     debug(F110,"zstime bad date",adate,0);
  2119.     return (-1);
  2120.     }
  2121.  
  2122.     mytm.tm_mon -= 1;
  2123.  
  2124.     strcpy (&src_path, f);
  2125.     strcpy (&suffix, "");
  2126.     s$expand_path (&src_path, &suffix, &path, &status);
  2127.     if (status) {
  2128.     debug(F101,"zstime s$expand_path status","",status);
  2129.     return (-1); /* error */
  2130.     }
  2131.  
  2132.     finfo.version = FILE_STAT_VERSION_5;
  2133.     s$get_file_status (&path, &finfo, &status);
  2134.     if (status) {
  2135.     debug(F101,"zstime s$get_file_status status","",status);
  2136.     return (-1);
  2137.     }
  2138.  
  2139.     if (1 == x) { /* compare times only */
  2140.     tp = localtime ((const time_t *) &finfo.date_time_modified);
  2141.     if (NULL == tp)
  2142.         return (-1);
  2143.  
  2144.     sprintf(fdate, "%04d%02d%02d %02d:%02d:%02d", tp->tm_year + 1900,
  2145.         tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min,
  2146.         tp->tm_sec);
  2147.  
  2148.         debug(F111,"zstime file mod date",fdate,finfo.date_time_modified);
  2149.  
  2150.     sprintf(adate, "%04d%02d%02d %02d:%02d:%02d", mytm.tm_year + 1900,
  2151.         mytm.tm_mon + 1, mytm.tm_mday, mytm.tm_hour, mytm.tm_min,
  2152.         mytm.tm_sec);
  2153.  
  2154.         debug(F110,"zstime adate",adate,0);
  2155.  
  2156.     if (strcmp (adate, fdate) > 0)
  2157.         r = 1;
  2158.     else
  2159.         r = 0;
  2160.      debug(F101,"zstime compare","",r);
  2161.     return r;
  2162.     }
  2163.  
  2164.     /* set date on existing file */
  2165.     sprintf (adate,"%02.2ld-%02.2ld-%02.2ld %02.2ld:%02.2ld:%02.2ld",
  2166.     mytm.tm_year, mytm.tm_mon + 1, mytm.tm_mday,
  2167.     mytm.tm_hour, mytm.tm_min, mytm.tm_sec);
  2168.  
  2169.     strcpy (&cvtime, adate);
  2170.     s$cv_to_int_date_time (&cvtime, &idate, &status);
  2171.     if (status) {
  2172.     debug(F111,"zstime s$cv_to_int_date_time status",adate,status);
  2173.     return (-1);
  2174.     }
  2175.  
  2176.     file_times[0] = idate;            /* time created */
  2177.     file_times[1] = now;            /* time used */
  2178.     file_times[2] = idate;            /* time modified */
  2179.     file_times[3] = 0;                /* time saved */
  2180.  
  2181.     s$set_object_times (&path, &file_times[0], &status);
  2182.     if (status) {
  2183.     debug(F111,"zstime s$set_object_times status",adate,status);
  2184.     return (-1);
  2185.     }
  2186.  
  2187.     s$set_expiration_date (&path, &now, &status);
  2188.     if (status) {
  2189.     debug(F111,"zstime s$set_expiration_date status",adate,status);
  2190.     return (-1);
  2191.     }
  2192.  
  2193.     return(0);
  2194. }
  2195.  
  2196. /* Find initialization file. */
  2197.  
  2198. int
  2199. zkermini() {
  2200. /*  nothing here for VOS.  This function added for benefit of VMS Kermit.  */
  2201. /*  VOS always uses "(home_dir)>start_up.kermit"                */
  2202.     return(0);
  2203. }
  2204.  
  2205. #ifndef NOFRILLS
  2206. int
  2207. zmail(p,f) char *p; char *f; {        /* Send file f as mail to address p */
  2208. /*
  2209.   Returns 0 on success
  2210.    2 if mail delivered but temp file should not be deleted
  2211.   -2 if mail can't be delivered
  2212. */
  2213.     char buff[300];
  2214.     int r;
  2215.     int len;
  2216.  
  2217.     len = sprintf (buff, 
  2218.     "!send_mail '%s' 'Kermit:%s' -priority '%s' -letter_path '%s'",
  2219.     p, f, "normal", f);
  2220.     debug(F110,"zmail",buff,len);
  2221.     r = zsyscmd (buff);
  2222.     if (r != 0) {
  2223.     debug(F101,"zmail call to zsyscmd","",r);
  2224.     return(-2);
  2225.     }
  2226.  
  2227.     return 0;
  2228. }
  2229. #endif /* NOFRILLS */
  2230.  
  2231. #ifndef NOFRILLS
  2232. int
  2233. zprint(p,f) char *p; char *f; {        /* Print file f with options p */
  2234. /*
  2235.   Returns 0 on success
  2236.    3 if file is printed but temp file can't be deleted
  2237.   -3 if file can't be printed
  2238. */
  2239.     char buff[300];
  2240.     int r;
  2241.     int len;
  2242.  
  2243.     len = sprintf (buff, "!print -delete %s %s", f, p);
  2244.     debug(F110,"zprint",buff,len);
  2245.     r = zsyscmd (buff);
  2246.     if (r != 0)
  2247.     return(-3);
  2248.  
  2249.     /* the spooler will delete the file after it is printed */
  2250.     /**/ /* should this be 3? check caller */
  2251.     return 0;
  2252. }
  2253. #endif /* NOFRILLS */
  2254.  
  2255. /* S H X P A N D  -- expand star names appropriate to the user's shell.
  2256.    In VOS, use the s$expand_star command, which is the same as the command
  2257.    processor uses.  It is also much easier than doing it our self.
  2258. */
  2259. int
  2260. shxpand(pat,namlst,len) char *pat, *namlst[]; int len; {
  2261.     char *p, *q;            /* Workers */
  2262.     int i, x, retcode; char c;        /* ... */
  2263.     CV(256) relpath;
  2264.     CV(32)  suffix;
  2265.     CV(256) fullpath;
  2266.     char dirnam[256];
  2267.     short status;
  2268.     struct {
  2269.     short version;
  2270.     struct {
  2271.         CV(32) objnam;
  2272.         short         objtyp;
  2273.     } entries[1];
  2274.     } *expinfo = NULL;
  2275.     short switches;
  2276.     short maxnbr;
  2277.     short nbrmatch;
  2278.     short dummy;
  2279.  
  2280.     for (i = 0; i < oldmtchs; i++)    /* Free previous file list */
  2281.       free(namlst[i]);
  2282.     
  2283.     oldmtchs = 0;
  2284.     strcpy (&relpath, pat);
  2285.     strcpy (&suffix, "");        /* don't append a suffix */
  2286.     s$expand_path (&relpath, &suffix, &fullpath, &status);
  2287.     if (status) {
  2288.     debug(F111,"shxpand expand_path failed",pat,status);
  2289.     return 0;
  2290.     }
  2291.  
  2292.     strcpy (dirnam, &fullpath);
  2293.     *(strrchr (dirnam, DIRSEP) + 1) = '\0'; /* cut it off after last dir */
  2294.  
  2295.     switches = 1; /* FILES, no dirs, no links, don't sort */
  2296.     maxnbr = 0;   /* don't want names, just how many */
  2297.     dummy = 1;      /* looks like expinfo->version, with no data part */
  2298.     s$expand_star (&fullpath, &switches, &maxnbr, &nbrmatch, &dummy, &status);
  2299.     if (status) {
  2300.     debug(F111,"shxpand starmatch 1 failed",pat,status);
  2301.     return 0;
  2302.     }
  2303.  
  2304.     debug(F111,"shxpand found matches",pat,nbrmatch);
  2305.     if (0 == nbrmatch) /* aren't any matches anyway */
  2306.     return 0;
  2307.  
  2308.     nbrmatch += 10; /* add a little bit */
  2309.     if (nbrmatch > MAXWLD)
  2310.       nbrmatch = MAXWLD;
  2311.  
  2312.     expinfo = malloc (sizeof (*expinfo->entries) * (nbrmatch - 1)
  2313.               + sizeof (*expinfo));
  2314.     if (NULL == expinfo) {
  2315.     debug(F101,"shxpand expinfo malloc","",(int)expinfo);
  2316.     return 0;
  2317.     }
  2318.     switches = 9; /* FILES, no dirs, no links, SORT */
  2319.     maxnbr = nbrmatch;
  2320.     expinfo->version = 1;
  2321.     s$expand_star (&fullpath, &switches, &maxnbr, &nbrmatch, expinfo, &status);
  2322.     if (status) {
  2323.     debug(F111,"shxpand starmatch 2 failed",pat,status);
  2324.     free(expinfo);
  2325.     return 0;
  2326.     }
  2327.  
  2328.     strcpy (&relpath, dirnam);    /* set up relnam */
  2329.     for (i = 0; i < nbrmatch; i++) {
  2330.     /* if no dirsep chars, don't put full path in expansion */
  2331.         if ((NULL == strchr(pat,'>')) && (NULL == strchr(pat,'<')))
  2332.         strcpy (&fullpath, &expinfo->entries[i].objnam);
  2333.     else {
  2334.         strcpy (&fullpath, &relpath); /* faster with CV's */
  2335.         strcat (&fullpath, &expinfo->entries[i].objnam);
  2336.     }
  2337.  
  2338.     x = (int)strlen(&fullpath);    /* Yes, get length of name */
  2339.     q = malloc(x+1);        /* Allocate space for it */
  2340.     if (!q) goto shxfin;        /* Fail if space can't be obtained */
  2341.     strcpy(q,&fullpath);        /* Copy name to space */
  2342.  
  2343.     namlst[i] = q;        /* Copy pointer to name into array */
  2344.     if (i >= len) goto shxfin;    /* Fail if too many */
  2345.     }
  2346.     retcode = i;            /* Return number of matching files */
  2347.  
  2348. shxfin:                    /* Common exit point */
  2349.  
  2350.     oldmtchs = i;            /* Remember how many files */
  2351.     free(expinfo);
  2352.     return(retcode);
  2353. }
  2354.  
  2355. /*  W H O A M I  --  Get user's username.  */
  2356.  
  2357. /*
  2358.   Under UNIX, this is hard to do, and different methods are used on
  2359.   different implementations (ATT/BSD/MINIX/etc).  On VOS, it's a gimme.
  2360. */
  2361. static char *
  2362. whoami () {
  2363.     CV(65) uname;
  2364.     char realname[66];    
  2365.  
  2366.     s$get_user_name (&uname);
  2367.     strcpy (realname, &uname);
  2368.     return(realname);
  2369. }
  2370.  
  2371. /*  T I L D E _ E X P A N D  --  expand ~user to the user's home directory. */
  2372.  
  2373. /*  This is not implemented correctly.  If DTILDE is defined, you will get  */
  2374. /*  compilation errors.  Tilde expansion is not a VOS concept.              */
  2375.  
  2376. char *
  2377. tilde_expand(dirname) char *dirname; {
  2378. #ifdef DTILDE
  2379.  
  2380. #define BUFLEN 257
  2381.  
  2382.     struct passwd *user;
  2383.     static char olddir[BUFLEN];
  2384.     static char oldrealdir[BUFLEN];
  2385.     static char temp[BUFLEN];
  2386.     int i, j;
  2387.  
  2388.     debug(F111,"tilde_expand",dirname,dirname[0]);
  2389.  
  2390.     if (dirname[0] != '~')        /* Not a tilde...return param */
  2391.       return(dirname);
  2392.     if (!strcmp(olddir,dirname)) {    /* Same as last time */
  2393.       return(oldrealdir);        /* so return old answer. */
  2394.     } else {
  2395.     j = (int)strlen(dirname);
  2396.     for (i = 0; i < j; i++)        /* find username part of string */
  2397.       if (!ISDIRSEP(dirname[i]))
  2398.         temp[i] = dirname[i];
  2399.       else break;
  2400.     temp[i] = '\0';            /* tie off with a NULL */
  2401.     if (i == 1) {            /* if just a "~" */
  2402.         user = getpwnam(whoami());    /*  get info on current user */
  2403.     } else {
  2404.         user = getpwnam(&temp[1]);    /* otherwise on the specified user */
  2405.     }
  2406.     }
  2407.     if (user != NULL) {            /* valid user? */
  2408.     strcpy(olddir, dirname);    /* remember the directory */
  2409.     strcpy(oldrealdir,user->pw_dir); /* and their home directory */
  2410.     strcat(oldrealdir,&dirname[i]);
  2411.     return(oldrealdir);
  2412.     } else {                /* invalid? */
  2413.     strcpy(olddir, dirname);    /* remember for next time */
  2414.     strcpy(oldrealdir, dirname);
  2415.     return(oldrealdir);
  2416.     }
  2417. #else
  2418.     return(NULL);
  2419. #endif /* DTILDE */
  2420. }
  2421.  
  2422. /*
  2423.   Functions for executing system commands.
  2424.  
  2425.   zsyscmd() executes the system command in the normal, default way for
  2426.   the system.  In UNIX, it does what system() does.  Thus, its results
  2427.   are  always  predictable;  in  VOS, it uses system(), so it is NEVER
  2428.   predictable, because system() is one of the weirdest  parts  of  the
  2429.   c-runtime.
  2430.  
  2431.   zshcmd()  executes  the  command "using the user's preferred shell,"
  2432.   which is a meaningless concept under VOS,  so  we  use  zsyscmd  for
  2433.   consistency.
  2434. */
  2435. int
  2436. zsyscmd(s) char *s; {
  2437.     int r;
  2438.     short status;
  2439.     CV(256) msgtext;
  2440.     char msg[257];
  2441.  
  2442.     debug(F110,"zsyscmd",s,0);
  2443.     if (strcmp (s, "login") && 0 != strlen (s)) {
  2444.     r = system(s);
  2445.     if (0 != r) {
  2446.         debug(F101,"system() returned status","",r);
  2447.     }
  2448.     }
  2449.     else {
  2450.     strcpy (&msgtext, "");
  2451.     s$clone (&status, &msgtext);
  2452.     r = status;
  2453.         if (0 != r) {
  2454.         strcpy (msg, &msgtext);
  2455.         debug(F111,"s$clone status",msg,status);
  2456.     }
  2457.     }
  2458.  
  2459. #ifdef COMMENT
  2460.     /* changed in edit 002 */
  2461.     /* 0 == failure, 1 == success */
  2462.     return (0 == r);
  2463. #else
  2464.     return r;
  2465. #endif /* COMMENT */
  2466. }
  2467.  
  2468. int
  2469. zshcmd(s) char *s; {
  2470.     debug(F110,"zshcmd",s,0);
  2471.     return(zsyscmd(s));
  2472. }
  2473.  
  2474. /*  I S W I L D  --  Check if filespec is "wild"  */
  2475.  
  2476. /*
  2477.   Returns 0 if it is a single file, 1 if it contains wildcard characters.
  2478.   Note: must match the algorithm used by match(), hence no [a-z], etc.
  2479. */
  2480. int
  2481. iswild(filespec) char *filespec; {
  2482.     char c; int x; char *p;
  2483.     if (wildxpand) {
  2484.     if ((x = zxpand(filespec)) > 1) return(1);
  2485.     if (x == 0) return(0);        /* File does not exist */
  2486.     p = malloc(MAXNAMLEN + 20);
  2487.     znext(p);
  2488.     x = (strcmp(filespec,p) != 0);
  2489.     free(p);
  2490.     return(x);
  2491.     } else {
  2492.     while ((c = *filespec++) != '\0')
  2493.       if (c == '*') return(1); /* only '*' allowed in VOS star-names */
  2494.     return(0);
  2495.     }
  2496. }
  2497.  
  2498. /*
  2499.    Tell if string pointer s is the name of a directory.
  2500.    Returns 1 if directory, 0 if not a directory.
  2501. */
  2502. int
  2503. isdir(s) char *s; {
  2504.     int x;
  2505.     CV(256) relpath;
  2506.     CV(32)  suffix;
  2507.     CV(256) fullpath;
  2508.     short   status;
  2509.     short   type;
  2510.     short   chase = 1;
  2511.  
  2512.     if (!s) return(0);
  2513.     if (!*s) return(0);
  2514.  
  2515.     strcpy (&relpath, s);
  2516.     strcpy (&suffix, "");        /* don't append a suffix */
  2517.     s$expand_path (&relpath, &suffix, &fullpath, &status);
  2518.     if (status) {
  2519.     debug(F111,"isdir expand_path failed",s,status);
  2520.     return 0;
  2521.     }
  2522.  
  2523.     s$get_object_type (&fullpath, &chase, &type, &status);
  2524.     
  2525.     debug(F111,"isdir type",s,type);
  2526.     return( (type == 2) ? 1 : 0 );  /* 1=File, 2=Dir, 3=Link */
  2527. }
  2528.  
  2529. #ifdef CK_MKDIR
  2530. /* Some systems don't have mkdir(), e.g. Tandy Xenix 3.2.. */
  2531.  
  2532. /* Z M K D I R  --  Create directory(s) if necessary */
  2533. /*
  2534.    Call with:
  2535.     A pointer to a file specification that might contain directory
  2536.     information.  The filename is expected to be included.
  2537.     If the file specification does not include any directory separators,
  2538.     then it is assumed to be a plain file.
  2539.     If one or more directories are included in the file specification,
  2540.     this routine tries to create them if they don't already exist.
  2541.    Returns:
  2542.     0 on success, i.e. the directory was created
  2543.    -1 on failure to create the directory
  2544. */
  2545. int
  2546. zmkdir(path) char *path; {
  2547.     char *xp, *tp, c;
  2548.     int x;
  2549.     CV(256) cv_path;
  2550.     short   status;
  2551.  
  2552.     x = strlen(path);
  2553.     debug(F111,"zmkdir",path,x);
  2554.     if (x < 1 || x > MAXPATH)        /* Check length */
  2555.       return(-1);
  2556.     if (!(tp = malloc(x+1)))        /* Make a temporary copy */
  2557.       return(-1);
  2558.     strcpy(tp,path);
  2559.  
  2560.     xp = tp;
  2561.     if (ISDIRSEP(*xp))            /* Don't create root directory! */
  2562.       xp++;
  2563.  
  2564.     /* Go thru filespec from left to right... */
  2565.  
  2566.     for (; *xp; xp++) {            /* Create parts that don't exist */
  2567.     if (!ISDIRSEP(*xp))        /* Find next directory separator */
  2568.       continue;
  2569.     c = *xp;            /* Got one. */
  2570.     *xp = '\0';            /* Make this the end of the string. */
  2571.     if (!isdir(tp)) {        /* This directory exists already? */
  2572.         debug(F110,"zmkdir making",tp,0);
  2573.         strcpy (&cv_path, tp);
  2574.         s$create_dir (&cv_path, &status);
  2575.     
  2576.         if (status != 0) {
  2577.         debug(F101,"zmkdir failed, status","",status);
  2578.         free(tp);        /* Free temporary buffer. */
  2579.         return(-1);        /* Freturn failure code. */
  2580.         }
  2581.     }
  2582.     *xp = c;            /* Replace the separator. */
  2583.     }
  2584.     free(tp);                /* Free temporary buffer. */
  2585.     return(0);                /* Return success code. */
  2586. }
  2587. #endif /* CK_MKDIR */
  2588.  
  2589. int
  2590. zrmdir(path) char *path; {
  2591.     CV(256) cv_path;
  2592.     CV(32)  idunno;
  2593.     short status;
  2594.     short flags;
  2595.  
  2596.     flags = 0;
  2597.     status = 0;
  2598.     strcpy(&idunno, "");
  2599.     strcpy(&cv_path, path);
  2600.     s$delete_dir(&cv_path, &idunno, &flags, &status);    
  2601.     debug(F111,"zrmdir", path, status);
  2602.  
  2603.     return(status == 0);
  2604. }
  2605.  
  2606.  
  2607. /* Z F S E E K  --  Position input file pointer */
  2608. /*
  2609.    Call with:
  2610.     Long int, 0-based, indicating desired position.
  2611.    Returns:
  2612.     0 on success.
  2613.    -1 on failure.
  2614. */
  2615. #ifndef NORESEND
  2616. int
  2617. #ifdef CK_ANSIC
  2618. zfseek(long pos)
  2619. #else
  2620. zfseek(pos) long pos;
  2621. #endif /* CK_ANSIC */
  2622. /* zfseek */ {
  2623.     short port;
  2624.     short position_control;
  2625.     short status;
  2626.     long  nbr_records;
  2627.     int   rv; /* return value */
  2628.     VFILE *vp = fp[ZIFILE];
  2629.  
  2630.     debug(F101,"zfseek","",pos);
  2631.     if (NULL != vp) {
  2632.     port = vp->port;
  2633.     position_control = POS_BEGINNING_OF_FILE;
  2634.     nbr_records = 0;
  2635.     status = 0;
  2636.  
  2637.     s$seq_position (&port, &position_control, &nbr_records, &status);
  2638.     if (status == 0) {
  2639.         port = vp->port;
  2640.         position_control = POS_NUM_BYTES_FORWARD;
  2641.         nbr_records = pos;
  2642.         status = 0;
  2643.  
  2644.         s$seq_position (&port, &position_control, &nbr_records, &status);
  2645.     }
  2646.     } else {
  2647.         status = 1;
  2648.     }
  2649.  
  2650.     rv = (status == 0) ? 0 : -1;
  2651.     return(rv);
  2652. }
  2653. #endif /* NORESEND */
  2654.  
  2655. struct zfnfp *
  2656. zfnqfp(fname, buflen, buf)  char * fname; int buflen; char * buf; {
  2657.     int x = 0, y = 0;
  2658.     char * xp;
  2659.     static struct zfnfp fnfp;
  2660.     CV(256) rel;
  2661.     CV(256) abs;
  2662.     CV(32) suffix = "";
  2663.     short status;
  2664.  
  2665.     if (!fname)
  2666.       return(NULL);
  2667.  
  2668.     /* initialize the data structure */
  2669.     fnfp.len = buflen;
  2670.     fnfp.fpath = buf;
  2671.     fnfp.fname = NULL;
  2672.  
  2673. #ifdef DTILDE
  2674.         if (*fname == '~') {
  2675.         xp = tilde_expand(fname);
  2676.     if (*xp)
  2677.         fname = xp;
  2678.     }
  2679. #endif /* DTILDE */
  2680.  
  2681.     strcpy(&rel, fname);
  2682.     strcpy(&abs, "");
  2683.     strcpy(&suffix, "");
  2684.     status = 0;
  2685.  
  2686.     s$expand_path(&rel, &suffix, &abs, &status);
  2687.     debug(F111,"zfnqfp",fname,status);
  2688.     if(status)
  2689.     return(NULL);
  2690.  
  2691.     if(strlen(&abs)<buflen) {
  2692.     strncpy(buf,&abs,buflen);
  2693.     x = strlen(buf);
  2694.     while(buf[x]!='>')        /* absolute path must have one */
  2695.         x--;
  2696.     fnfp.fname = &buf[x];
  2697.     return(&fnfp);
  2698.     }
  2699.  
  2700.     return(NULL);
  2701. }
  2702.