home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit5a188 / ckufio.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  85KB  |  3,061 lines

  1. #ifdef OS2
  2. char *ckzv = "OS/2 File support, 5A(070) 8 Mar 93";
  3. #else
  4. #ifdef aegis
  5. char *ckzv = "Aegis File support, 5A(070) 8 Mar 93";
  6. #else
  7. char *ckzv = "UNIX File support, 5A(070) 8 Mar 93";
  8. #endif /* aegis */
  9. #endif /* OS2 */
  10.  
  11. /* C K U F I O  --  Kermit file system support for UNIX, OS/2, and Aegis */
  12.  
  13. /*
  14.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  15.   Columbia University Center for Computing Activities.
  16.   First released January 1985.
  17.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  18.   York.  Permission is granted to any individual or institution to use this
  19.   software as long as it is not sold for profit.  This copyright notice must be
  20.   retained.  This software may not be included in commercial products without
  21.   written permission of Columbia University.
  22. */
  23.  
  24. /* Include Files */
  25.  
  26. #include "ckcdeb.h"
  27.  
  28. #include <signal.h>
  29.  
  30. #ifdef MINIX
  31. #include <limits.h>
  32. #endif /* MINIX */
  33. #ifdef POSIX
  34. #include <limits.h>
  35. #endif /* POSIX */
  36.  
  37. /* Directory structure header file */
  38.  
  39. #ifdef OS2
  40. /*
  41.  
  42.   C-Kermit's OS/2 support originally by Chris Adie <C.Adie@uk.ac.edinburgh>
  43.   Edinburgh University Computing Service, Scotland, for C-Kermit 4F.  Adapted
  44.   to C-Kermit 5A and integrated into the UNIX support module by Kai Uwe Rommel
  45.   <rommel@informatik.tu-muenchen.de>, Muenchen, Germany, December 1991.
  46. */
  47.  
  48. /*
  49.   Directory Separator macros, to allow this module to work with both UNIX and
  50.   OS/2: Because of ambiguity with the command line editor escape \ character,
  51.   the directory separator is currently left as / for OS/2 too, because the
  52.   OS/2 kernel also accepts / as directory separator.  But this is subject to
  53.   change in future versions to conform to the normal OS/2 style.
  54. */
  55. #define DIRSEP       '/'
  56. /* #define DIRSEP       '\\' */
  57. #define ISDIRSEP(c)  ((c)=='/'||(c)=='\\')
  58. #else /* not OS2 */
  59. #define DIRSEP       '/'
  60. #define ISDIRSEP(c)  ((c)=='/')
  61. #endif /* OS2 */
  62.  
  63. #ifdef SDIRENT
  64. #define DIRENT
  65. #endif /* SDIRENT */
  66.  
  67. #ifdef XNDIR
  68. #include <sys/ndir.h>
  69. #else /* !XNDIR */
  70. #ifdef NDIR
  71. #include <ndir.h>
  72. #else /* !NDIR, !XNDIR */
  73. #ifdef RTU
  74. #include "/usr/lib/ndir.h"
  75. #else /* !RTU, !NDIR, !XNDIR */
  76. #ifdef DIRENT
  77. #ifdef SDIRENT
  78. #include <sys/dirent.h>
  79. #else
  80. #include <dirent.h>
  81. #endif /* SDIRENT */
  82. #else
  83. #ifdef OS2
  84. #define OPENDIR
  85. #define DIRENT
  86. #include "ckodir.h"
  87. #else/* !RTU, !NDIR, !XNDIR, !DIRENT, !OS2, i.e. all others */
  88. #include <sys/dir.h>
  89. #endif /* OS2 */
  90. #endif /* DIRENT */
  91. #endif /* RTU */
  92. #endif /* NDIR */
  93. #endif /* XNDIR */
  94.  
  95. #ifdef OS2                /* OS/2 file system interface */
  96. #define BSD4                /* is like Berkeley UNIX */
  97. #define NOFILEH                /* with no <file.h> */
  98. #include <sys/utime.h>
  99. #include <stdlib.h>
  100. #include <process.h>
  101. extern int binary;            /* We need to know this for open() */
  102. #ifdef __IBMC__
  103. extern FILE *popen(char *, char *);
  104. extern int pclose(FILE *);
  105. #else
  106. #ifndef __EMX__
  107. #define popen    _popen
  108. #define pclose   _pclose
  109. #include <share.h>
  110. #define fopen(n, m)  _fsopen(n, m, SH_DENYWR)
  111. #endif /* __EMX__ */
  112. #endif /* __IBMC__ */
  113. #else
  114. #include <pwd.h>            /* Password file for shell name */
  115. #endif /* OS2 */
  116.  
  117. #ifndef OS2
  118. #ifdef SYSUTIMEH            /* <sys/utime.h> if requested,  */
  119. #include <sys/utime.h>            /* for extra fields required by */
  120. #endif /* SYSUTIMEH */            /* 88Open spec. */
  121. #endif /* OS2 */
  122.  
  123. #ifdef BSD44                /* BSD 4.4 */
  124. #define TIMESTAMP            /* Can do file dates */
  125. #include <sys/time.h>
  126. #include <sys/timeb.h>
  127.  
  128. #else
  129.  
  130. #ifdef BSD4                /* BSD 4.3 and below */
  131. #define TIMESTAMP            /* Can do file dates */
  132. #include <time.h>            /* Need this */
  133. #include <sys/timeb.h>            /* Need this if really BSD */
  134.  
  135. #else
  136.  
  137. #ifdef ATTSV
  138. /*
  139.   4.4 BSD uses utimes() instead of utime() for this, which accepts a...
  140. */
  141. #define TIMESTAMP
  142. #include <time.h>
  143. /* void tzset(); (the "void" type upsets some compilers) */
  144. #ifndef ultrix
  145. extern long timezone;
  146. #endif /* ultrix */
  147. #endif /* ATTSV */
  148. #endif /* BSD4 */
  149. #endif /* BSD44 */
  150.  
  151. /* Is `y' a leap year? */
  152. #define leap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  153.  
  154. /* Number of leap years from 1970 to `y' (not including `y' itself). */
  155. #define nleap(y) (((y) - 1969) / 4 - ((y) - 1901) / 100 + ((y) - 1601) / 400)
  156.  
  157. #ifdef COMMENT /* not used */
  158. /* Number of days in each month of the year. */
  159. static char monlens[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  160. #endif /* COMMENT */
  161.  
  162. #ifdef CIE
  163. #include <stat.h>            /* File status */
  164. #else
  165. #include <sys/stat.h>
  166. #ifdef OS2
  167. #include <sys/types.h>
  168. /* because standard stat has trouble with trailing /'s we have to wrap it */
  169. int os2stat(char *, struct stat *);
  170. #ifdef __IBMC__
  171. #ifdef system
  172. #undef system
  173. #endif
  174. #ifdef stat
  175. #undef stat
  176. #define __IBMC__STAT
  177. #endif
  178. #endif
  179. #define stat(path, buf) os2stat(path, buf)
  180. #endif /* OS2 */
  181. #endif /* CIE */
  182.  
  183. /*
  184.   Functions (n is one of the predefined file numbers from ckcker.h):
  185.  
  186.    zopeni(n,name)   -- Opens an existing file for input.
  187.    zopeno(n,name,attr,fcb) -- Opens a new file for output.
  188.    zclose(n)        -- Closes a file.
  189.    zchin(n,&c)      -- Gets the next character from an input file.
  190.    zsinl(n,&s,x)    -- Read a line from file n, max len x, into address s.
  191.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  192.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  193.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  194.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  195.    zchki(name)      -- Check if named file exists and is readable, return size.
  196.    zchko(name)      -- Check if named file can be created.
  197.    zchkspa(name,n)  -- Check if n bytes available to create new file, name.
  198.    znewn(name,s)    -- Make a new unique file name based on the given name.
  199.    zdelet(name)     -- Delete the named file.
  200.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  201.    znext(string)    -- Returns the next file from the list in "string".
  202.    zxcmd(n,cmd)     -- Execute the command in a lower fork on file number n.
  203.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  204.    zrtol(n1,n2)     -- Convert remote filename into local form.
  205.    zltor(n1,n2)     -- Convert local filename into remote form.
  206.    zchdir(dirnam)   -- Change working directory.
  207.    zhome()          -- Return pointer to home directory name string.
  208.    zkself()         -- Kill self, log out own job.
  209.    zsattr(struct zattr *) -- Return attributes for file which is being sent.
  210.    zstime(f, struct zattr *, x) - Set file creation date from attribute packet.
  211.    zrename(old, new) -- Rename a file.
  212.  */
  213.  
  214. /* Kermit-specific includes */
  215. /*
  216.   Definitions here supersede those from system include files.
  217.   ckcdeb.h is included above.
  218. */
  219. #include "ckcker.h"            /* Kermit definitions */
  220. #include "ckucmd.h"            /* For sys-dependent keyword tables */
  221. #include "ckuver.h"            /* Version herald */
  222.  
  223. char *ckzsys = HERALD;
  224.  
  225. /*
  226.   File access checking ...  There are two calls to access() in this module.
  227.   If this program is installed setuid or setgid on a Berkeley-based UNIX
  228.   system that does NOT incorporate the saved-original-effective-uid/gid
  229.   feature, then, when we have swapped the effective and original uid/gid,
  230.   access() fails because it uses what it thinks are the REAL ids, but we have
  231.   swapped them.  This occurs on systems where ANYBSD is defined, NOSETREU
  232.   is NOT defined, and SAVEDUID is NOT defined.  So, in theory, we should take
  233.   care of this situation like so:
  234.  
  235.     ifdef ANYBSD
  236.     ifndef NOSETREU
  237.     ifndef SAVEDUID
  238.     define SW_ACC_ID
  239.     endif
  240.     endif
  241.     endif
  242.  
  243.   But we can't test such a general scheme everywhere, so let's only do this
  244.   when we know we have to...
  245. */
  246. #ifdef NEXT                /* NeXTSTEP 1.0-3.0 */
  247. #define SW_ACC_ID
  248. #endif /* NEXT */
  249.  
  250. /* Support for tilde-expansion in file and directory names */
  251.  
  252. #ifdef POSIX
  253. #define NAMEENV "LOGNAME"
  254. #endif /* POSIX */
  255.  
  256. #ifdef BSD4
  257. #define NAMEENV "USER"
  258. #endif /* BSD4 */
  259.  
  260. #ifdef ATTSV
  261. #define NAMEENV "LOGNAME"
  262. #endif /* ATTSV */
  263.  
  264. /* Berkeley Unix Version 4.x */
  265. /* 4.1bsd support from Charles E Brooks, EDN-VAX */
  266.  
  267. #ifdef BSD4
  268. #ifdef MAXNAMLEN
  269. #define BSD42
  270. #endif /* MAXNAMLEN */
  271. #endif /* BSD4 */
  272.  
  273. /* Definitions of some system commands */
  274.  
  275. #ifdef OS2
  276. char *DELCMD = "del ";            /* For file deletion */
  277. char *PWDCMD = "chdir ";        /* For saying where I am */
  278. char *TYPCMD = "type ";            /* For typing a file */
  279. char *DIRCMD = "dir ";            /* For directory listing */
  280. char *DIRCM2 = "dir ";            /* For directory listing, no args */
  281. char *WHOCMD = "";            /* Who's there? */
  282. char *SPACMD = "dir | find \"bytes free\""; /* For space on disk */
  283. char *SPACM2 = "dir | find \"bytes free\""; /* For space on disk */
  284.  
  285. #else /* Not OS2, ergo UNIX */
  286.  
  287. char *DELCMD = "rm -f ";        /* For file deletion */
  288. char *PWDCMD = "pwd ";            /* For saying where I am */
  289. #ifdef COMMENT
  290. char *DIRCMD = "/bin/ls -ld ";        /* For directory listing */
  291. char *DIRCM2 = "/bin/ls -ld *";        /* For directory listing, no args */
  292. #else
  293. char *DIRCMD = "/bin/ls -l ";        /* For directory listing */
  294. char *DIRCM2 = "/bin/ls -l ";        /* For directory listing, no args */
  295. #endif /* COMMENT */
  296. char *TYPCMD = "cat ";            /* For typing a file */
  297.  
  298. #ifdef FT18                /* Fortune For:Pro 1.8 */
  299. #undef BSD4
  300. #endif /* FT18 */
  301.  
  302. #ifdef BSD4
  303. char *SPACMD = "pwd ; df .";        /* Space in current directory */
  304. #else
  305. #ifdef FT18
  306. char *SPACMD = "pwd ; du ; df .";
  307. #else
  308. char *SPACMD = "df ";
  309. #endif /* FT18 */
  310. #endif /* BSD4 */
  311.  
  312. char *SPACM2 = "df ";            /* For space in specified directory */
  313.  
  314. #ifdef FT18
  315. #define BSD4
  316. #endif /* FT18 */
  317.  
  318. #ifdef BSD4
  319. char *WHOCMD = "finger ";
  320. #else
  321. char *WHOCMD = "who ";
  322. #endif /* BSD4 */
  323.  
  324. #endif /* OS2 */
  325.  
  326. #ifdef DTILDE                /* For tilde expansion */
  327. _PROTOTYP( char * tilde_expand, (char *) );
  328. #endif /* DTILDE */
  329.  
  330. /* More system-dependent includes, which depend on symbols defined */
  331. /* in the Kermit-specific includes.  Oh what a tangled web we weave... */
  332.  
  333. #ifdef COHERENT                /* <sys/file.h> */
  334. #define NOFILEH
  335. #endif /* COHERENT */
  336.  
  337. #ifdef MINIX
  338. #define NOFILEH
  339. #endif /* MINIX */
  340.  
  341. #ifdef aegis
  342. #define NOFILEH
  343. #endif /* aegis */
  344.  
  345. #ifdef unos
  346. #define NOFILEH
  347. #endif /* unos */
  348.  
  349. #ifndef NOFILEH
  350. #include <sys/file.h>
  351. #endif /* NOFILEH */
  352.  
  353. #ifndef is68k                /* Whether to include <fcntl.h> */
  354. #ifndef BSD41                /* All but a couple UNIXes have it. */
  355. #ifndef FT18
  356. #ifndef COHERENT
  357. #include <fcntl.h>
  358. #endif /* COHERENT */
  359. #endif /* FT18  */
  360. #endif /* BSD41 */
  361. #endif /* not is68k */
  362.  
  363. #ifdef COHERENT
  364. #include <sys/fcntl.h>
  365. #endif /* COHERENT */
  366.  
  367. /*
  368.   Change argument to "(const char *)" if this causes trouble.
  369.   Or... if it causes trouble, then maybe it was already declared 
  370.   in a header file after all, so you can remove this prototype.
  371. */
  372. #ifndef _POSIX_SOURCE
  373. #ifndef NEXT
  374. #ifndef SVR4
  375. /* POSIX <pwd.h> already gave prototypes for these. */
  376. #ifdef IRIX40
  377. _PROTOTYP( struct passwd * getpwnam, (const char *) );
  378. #else
  379. _PROTOTYP( struct passwd * getpwnam, (char *) );
  380. #endif /* IRIX40 */
  381. #ifndef SUNOS4
  382. _PROTOTYP( struct passwd * getpwuid, (PWID_T) );
  383. #endif /* SUNOS4 */
  384. _PROTOTYP( struct passwd * getpwent, (void) );
  385. #endif /* SVR4 */
  386. #endif /* NEXT */
  387. #endif /* _POSIX_SOURCE */
  388.  
  389. /* Define macros for getting file type */
  390.  
  391. #ifdef OXOS
  392. /*
  393.   Olivetti X/OS 2.3 has S_ISREG and S_ISDIR defined
  394.   incorrectly, so we force their redefinition.
  395. */
  396. #undef S_ISREG
  397. #undef S_ISDIR
  398. #endif /* OXOS */
  399.  
  400. #ifdef UTSV                /* Same deal for Amdahl UTSV */
  401. #undef S_ISREG
  402. #undef S_ISDIR
  403. #endif /* UTSV */
  404.  
  405. #ifdef UNISYS52                /* And for UNISYS UTS V 5.2 */
  406. #undef S_ISREG
  407. #undef S_ISDIR
  408. #endif /* UNISYS52 */
  409.  
  410. #ifndef S_ISREG
  411. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  412. #endif /* S_ISREG */
  413. #ifndef S_ISDIR
  414. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  415. #endif /* S_ISDIR */
  416.  
  417. /* Define maximum length for a file name if not already defined */
  418.  
  419. #ifndef MAXNAMLEN
  420. #ifdef sun
  421. #define MAXNAMLEN 255
  422. #else
  423. #ifdef FILENAME_MAX
  424. #define MAXNAMLEN FILENAME_MAX
  425. #else
  426. #ifdef NAME_MAX
  427. #define MAXNAMLEN NAME_MAX
  428. #else
  429. #ifdef _POSIX_NAME_MAX
  430. #define MAXNAMLEN _POSIX_NAME_MAX
  431. #else
  432. #ifdef _D_NAME_MAX
  433. #define MAXNAMLEN _D_NAME_MAX
  434. #else
  435. #ifdef DIRSIZ
  436. #define MAXNAMLEN DIRSIZ
  437. #else
  438. #define MAXNAMLEN 14
  439. #endif /* DIRSIZ */
  440. #endif /* _D_NAME_MAX */
  441. #endif /* _POSIX_NAME_MAX */
  442. #endif /* NAME_MAX */
  443. #endif /* FILENAME_MAX */
  444. #endif /* sun */
  445. #endif /* MAXNAMLEN */
  446.  
  447. /* Longest pathname */
  448.  
  449. #ifdef MAXPATHLEN
  450. #ifdef MAXPATH
  451. #undef MAXPATH
  452. #endif /* MAXPATH */
  453. #define MAXPATH MAXPATHLEN
  454. #else
  455. #ifdef PATH_MAX
  456. #define MAXPATH PATH_MAX
  457. #else
  458. #ifdef _POSIX_PATH_MAX
  459. #define MAXPATH _POSIX_PATH_MAX
  460. #else
  461. #ifdef BSD42
  462. #define MAXPATH 1024
  463. #else
  464. #define MAXPATH 255
  465. #endif /* BSD42 */
  466. #endif /* _POSIX_PATH_MAX */
  467. #endif /* PATH_MAX */
  468. #endif /* MAXPATHLEN */
  469.  
  470. /* Maximum number of filenames for wildcard expansion */
  471.  
  472. #ifdef PROVX1
  473. #define MAXWLD 50
  474. #else
  475. #ifdef BSD29
  476. #define MAXWLD 50
  477. #else
  478. #ifdef pdp11
  479. #define MAXWLD 50
  480. #else
  481. #define MAXWLD 1000
  482. #endif /* pdp11 */
  483. #endif /* BSD29 */
  484. #endif /* PROVX1 */
  485.  
  486. /* More internal function prototypes */
  487. /*
  488.  * The path structure is used to represent the name to match.
  489.  * Each slash-separated segment of the name is kept in one
  490.  * such structure, and they are linked together, to make
  491.  * traversing the name easier.
  492.  */
  493. struct path {
  494.     char npart[MAXNAMLEN+4];        /* name part of path segment */
  495.     struct path *fwd;            /* forward ptr */
  496. };
  497. _PROTOTYP( int shxpand, (char *, char *[], int ) );
  498. _PROTOTYP( static int fgen, (char *, char *[], int ) );
  499. _PROTOTYP( static VOID traverse, (struct path *, char *, char *) );
  500. _PROTOTYP( static VOID addresult, (char *) );
  501. _PROTOTYP( static int match, (char *, char *) );
  502. _PROTOTYP( static char * whoami, (void) );
  503. _PROTOTYP( char * xindex, (char *, char) );
  504. _PROTOTYP( UID_T real_uid, (void) );
  505. _PROTOTYP( struct path *splitpath, (char *p) );
  506.  
  507. /* Some systems define these symbols in include files, others don't... */
  508.  
  509. #ifndef R_OK
  510. #define R_OK 4                /* For access */
  511. #endif
  512.  
  513. #ifndef W_OK
  514. #define W_OK 2
  515. #endif
  516.  
  517. #ifndef O_RDONLY
  518. #define O_RDONLY 000
  519. #endif
  520.  
  521. /* Declarations */
  522.  
  523. int maxnam = MAXNAMLEN;            /* Available to the outside */
  524. int maxpath = MAXPATH;
  525.  
  526. FILE *fp[ZNFILS] = {             /* File pointers */
  527.     NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  528. #ifdef OS2
  529. int ispipe[ZNFILS];            /* Flag for file is a pipe */
  530. #endif /* OS2 */
  531.  
  532. /* Buffers and pointers used in buffered file input and output. */
  533. #ifdef DYNAMIC
  534. extern char *zinbuffer, *zoutbuffer;
  535. #else
  536. extern char zinbuffer[], zoutbuffer[];
  537. #endif /* DYNAMIC */
  538. extern char *zinptr, *zoutptr;
  539. extern int zincnt, zoutcnt;
  540. extern int wildxpand;
  541.  
  542. extern UID_T real_uid();
  543.  
  544. static long iflen = -1L;        /* Input file length */
  545.  
  546. static PID_T pid = 0;            /* pid of child fork */
  547. static int fcount;            /* Number of files in wild group */
  548. static char nambuf[MAXNAMLEN+4];    /* Buffer for a filename */
  549. #ifndef NOFRILLS
  550. static char zmbuf[200];            /* For mail, remote print strings */
  551. #endif /* NOFRILLS */
  552.  
  553. /* static */                /* Not static, must be global now. */
  554. char *mtchs[MAXWLD],            /* Matches found for filename */
  555.      **mtchptr;                /* Pointer to current match */
  556.  
  557. /*  Z K S E L F  --  Kill Self: log out own job, if possible.  */
  558.  
  559. /* Note, should get current pid, but if your system doesn't have */
  560. /* getppid(), then just kill(0,9)...  */
  561.  
  562. #ifndef SVR3
  563. #ifndef POSIX
  564. /* Already declared in unistd.h for SVR3 and POSIX */
  565. #ifdef CK_ANSIC
  566. extern PID_T getppid(void);
  567. #else
  568. #ifndef PS2AIX10
  569. extern PID_T getppid();
  570. #endif /* PS2AIX10 */
  571. #endif /* CK_ANSIC */
  572. #endif /* POSIX */
  573. #endif /* SVR3 */
  574. int
  575. zkself() {                /* For "bye", but no guarantee! */
  576. #ifdef PROVX1
  577.     return(kill(0,9));
  578. #else
  579. #ifdef V7
  580.     return(kill(0,9));
  581. #else
  582. #ifdef TOWER1
  583.     return(kill(0,9));
  584. #else
  585. #ifdef FT18
  586.     return(kill(0,9));
  587. #else
  588. #ifdef aegis
  589.     return(kill(0,9));
  590. #else
  591. #ifdef COHERENT
  592.     return(kill((PID_T)getpid(),1));
  593. #else
  594. #ifdef OS2
  595.     exit(3);
  596. #else
  597. #ifdef PID_T
  598.     exit(kill((PID_T)getppid(),1));
  599. #else
  600.     exit(kill(getppid(),1));
  601. #endif
  602. #endif
  603. #endif
  604. #endif
  605. #endif
  606. #endif
  607. #endif
  608. #endif
  609. }
  610.  
  611. /*  Z O P E N I  --  Open an existing file for input. */
  612.  
  613. int
  614. zopeni(n,name) int n; char *name; {
  615.     debug(F111," zopeni",name,n);
  616.     debug(F101,"  fp","", fp[n]);
  617.     if (chkfn(n) != 0) return(0);
  618.     zincnt = 0;                /* Reset input buffer */
  619.     if (n == ZSYSFN) {            /* Input from a system function? */
  620. /*** Note, this function should not be called with ZSYSFN ***/
  621. /*** Always call zxcmd() directly, and give it the real file number ***/
  622. /*** you want to use.  ***/
  623.         debug(F110,"zopeni called with ZSYSFN, failing!",name,0);
  624.     *nambuf = '\0';            /* No filename. */
  625.     return(0);            /* fail. */
  626. #ifdef COMMENT
  627.     return(zxcmd(n,name));        /* Try to fork the command */
  628. #endif
  629.     }
  630.     if (n == ZSTDIO) {            /* Standard input? */
  631.     if (isatty(0)) {
  632.         fprintf(stderr,"Terminal input not allowed");
  633.         debug(F110,"zopeni: attempts input from unredirected stdin","",0);
  634.         return(0);
  635.     }
  636.     fp[ZIFILE] = stdin;
  637. #ifdef OS2
  638.     setmode(fileno(stdin),O_BINARY);
  639. #endif /* OS2 */
  640.     return(1);
  641.     }
  642. #ifdef OS2
  643.     if (n == ZIFILE || n == ZRFILE)
  644.       fp[n] = fopen(name,"rb");        /* Binary mode */
  645.     else
  646. #endif /* OS2 */
  647.       fp[n] = fopen(name,"r");        /* Real file, open it. */
  648.     debug(F111," zopeni", name, fp[n]);
  649.     if (fp[n] == NULL) perror("zopeni");
  650.     return((fp[n] != NULL) ? 1 : 0);
  651. }
  652.  
  653. /*  Z O P E N O  --  Open a new file for output.  */
  654.  
  655. int
  656. zopeno(n,name,zz,fcb)
  657. /* zopeno */  int n; char *name; struct zattr *zz; struct filinfo *fcb; {
  658.  
  659.     char p[8];                /* (===OS2 change===) */
  660.  
  661. /* As of Version 5A, the attribute structure and the file information */
  662. /* structure are included in the arglist. */
  663.  
  664. #ifdef DEBUG
  665.     debug(F111,"zopeno",name,n);
  666.     if (fcb) {
  667.     debug(F101,"zopeno fcb disp","",fcb->dsp);
  668.     debug(F101,"zopeno fcb type","",fcb->typ);
  669.     debug(F101,"zopeno fcb char","",fcb->cs);
  670.     } else {
  671.     debug(F100,"zopeno fcb is NULL","",0);
  672.     }
  673.     if (n != ZDFILE)
  674.       debug(F111," zopeno",name,n);
  675. #endif /* DEBUG */
  676.     if (chkfn(n) != 0) return(0);
  677.     if ((n == ZCTERM) || (n == ZSTDIO)) {   /* Terminal or standard output */
  678.     fp[ZOFILE] = stdout;
  679. #ifdef DEBUG
  680.     if (n != ZDFILE)
  681.       debug(F101," fp[]=stdout", "", fp[n]);
  682. #endif /* DEBUG */
  683.     zoutcnt = 0;
  684.     zoutptr = zoutbuffer;
  685.     return(1);
  686.     }
  687.  
  688. /* A real file.  Open it in desired mode (create or append). */
  689.  
  690.     strcpy(p,"w");            /* Assume write/create mode */
  691.     if (fcb) {                /* If called with an FCB... */
  692.     if (fcb->dsp == XYFZ_A)        /* Does it say Append? */
  693.       strcpy(p,"a");        /* Yes. */
  694.     }
  695. #ifdef OS2
  696.     if (n == ZOFILE || n == ZSFILE)    /* OS/2 binary mode */
  697.       strcat(p,"b");
  698. #endif /* OS2 */
  699.     fp[n] = fopen(name,p);        /* Try to open the file */
  700.  
  701.     if (fp[n] == NULL) {        /* Failed */
  702.     debug(F101,"zopeno failed errno","",errno);
  703. #ifdef COMMENT                /* Let upper levels print message. */
  704.         perror("Can't open output file");
  705. #endif /* COMMENT */
  706.     } else {                /* Succeeded */
  707.         if (n == ZDFILE)        /* If it's the debug log */
  708.       setbuf(fp[n],NULL);        /* make it unbuffered */
  709.     else
  710.       debug(F100, "zopeno ok", "", 0);
  711.     }
  712.     zoutcnt = 0;            /* (PWP) reset output buffer */
  713.     zoutptr = zoutbuffer;
  714.     return((fp[n] != NULL) ? 1 : 0);
  715. }
  716.  
  717. /*  Z C L O S E  --  Close the given file.  */
  718.  
  719. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  720.  
  721. int
  722. zclose(n) int n; {
  723.     int x, x2;
  724.     if (chkfn(n) < 1) return(0);    /* Check range of n */
  725.  
  726.     if ((n == ZOFILE) && (zoutcnt > 0))    /* (PWP) output leftovers */
  727.       x2 = zoutdump();
  728.     else
  729.       x2 = 0;
  730.  
  731.     x = 0;                /* Initialize return code */
  732.     if (fp[ZSYSFN]) {            /* If file is really pipe */
  733.         x = zclosf(n);            /* do it specially */
  734.     } else {
  735.         if ((fp[n] != stdout) && (fp[n] != stdin)) x = fclose(fp[n]);
  736.     fp[n] = NULL;
  737.     }
  738.     iflen = -1L;            /* Invalidate file length */
  739.     if (x == EOF)            /* if we got a close error */
  740.       return(-1);
  741.     else if (x2 < 0)        /* or an error flushing the last buffer */
  742.       return(-1);        /* then return an error */
  743.     else
  744.       return(1);
  745. }
  746.  
  747. /*  Z C H I N  --  Get a character from the input file.  */
  748.  
  749. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  750.  
  751. int
  752. zchin(n,c) int n; int *c; {
  753.     int a, x;
  754.  
  755.     /* (PWP) Just in case this gets called when it shouldn't. */
  756.     if (n == ZIFILE) {
  757.     x = zminchar();
  758.     *c = x;
  759.     return(x);
  760.     }
  761.     /* if (chkfn(n) < 1) return(-1); */
  762.     a = getc(fp[n]);
  763.     if (a == EOF) return(-1);
  764. #ifdef OS2
  765.     if (!binary && a == 0x1A)        /* Ctrl-Z marks EOF for text mode*/
  766.       return(-1);
  767. #endif
  768.     *c = (CHAR) a & 0377;
  769.     return(0);
  770. }
  771.  
  772. /*  Z S I N L  --  Read a line from a file  */
  773.  
  774. /*
  775.   Writes the line into the address provided by the caller.
  776.   n is the Kermit "channel number".
  777.   Writing terminates when newline is encountered, newline is not copied.
  778.   Writing also terminates upon EOF or if length x is exhausted.
  779.   Returns 0 on success, -1 on EOF or error.
  780. */
  781. int
  782. zsinl(n,s,x) int n, x; char *s; {
  783.     int a, z = 0;            /* z is return code. */
  784.  
  785.     if (chkfn(n) < 1) {            /* Make sure file is open */
  786.     return(-1);
  787.     }
  788.     a = -1;                /* Current character, none yet. */
  789.     while (x--) {            /* Up to given length */
  790. #ifndef NLCHAR
  791.     int old;
  792.     old = a;            /* Previous character */
  793. #endif
  794.     if (zchin(n,&a) < 0) {        /* Read a character from the file */
  795.         debug(F101,"zsinl","",a);
  796.         z = -1;            /* EOF or other error */
  797.         break;
  798.     }
  799. #ifdef NLCHAR
  800.     if (a == (char) NLCHAR) break;    /* Single-character line terminator */
  801. #else                    /* CRLF line terminator */
  802.     if (a == '\015') continue;    /* CR, get next character */
  803.     if (old == '\015') {        /* Previous character was CR */
  804.         if (a == '\012') break;    /* This one is LF, so we have a line */
  805.         else *s++ = '\015';        /* Not LF, deposit CR */
  806.     }
  807. #ifdef OS2
  808. /*
  809.   I'm not sure I understand this one, so let's keep it only for OS/2 for now.
  810. */
  811.     if (a == '\012') break;        /* Break on single LF too */
  812. #endif /* OS2 */
  813. #endif /* NLCHAR */
  814.     *s = a;                /* Deposit character */
  815.     s++;
  816.     }
  817.     *s = '\0';                /* Terminate the string */
  818.     return(z);
  819. }
  820.  
  821. /*
  822.  * (PWP) (re)fill the buffered input buffer with data.  All file input
  823.  * should go through this routine, usually by calling the zminchar()
  824.  * macro (in ckcker.h).
  825.  */
  826.  
  827. /*
  828.  * Suggestion: if fread() returns 0, call ferror to find out what the
  829.  * problem was.  If it was not EOF, then return -2 instead of -1.
  830.  * Upper layers (getpkt function in ckcfns.c) should set cxseen flag
  831.  * if it gets -2 return from zminchar macro.
  832.  */
  833. int
  834. zinfill() {
  835.     int x;
  836.  
  837.     errno = 0;
  838.     zincnt = fread(zinbuffer, sizeof (char), INBUFSIZE, fp[ZIFILE]);
  839. #ifdef COMMENT
  840.     debug(F101,"zinfill fp","",fp[ZIFILE]);
  841.     debug(F101,"zinfill zincnt","",zincnt);
  842. #endif
  843.     if (zincnt == 0) {
  844. #ifndef UTEK
  845. #ifdef ferror
  846.     x = ferror(fp[ZIFILE]);
  847.     debug(F101,"zinfill errno","",errno);
  848.     debug(F101,"zinfill ferror","",x);
  849.     if (x) return(-2);
  850. #endif /* ferror */
  851. #else
  852.      x = feof(fp[ZIFILE]);
  853.      debug(F101,"zinfill errno","",errno);
  854.      debug(F101,"zinfill feof","",x);
  855.      if (!x && ferror(fp[ZIFILE])) return(-2);
  856. #endif /* UTEK */
  857.     return(-1);
  858.     }
  859.     zinptr = zinbuffer;       /* set pointer to beginning, (== &zinbuffer[0]) */
  860.     zincnt--;            /* one less char in buffer */
  861.     return((int)(*zinptr++) & 0377); /* because we return the first */
  862. }
  863.  
  864. /*  Z S O U T  --  Write a string out to the given file, buffered.  */
  865.  
  866. int
  867. zsout(n,s) int n; char *s; {
  868.     if (chkfn(n) < 1) return(-1); /* Keep this here, prevents memory faults */
  869. #ifndef OS2
  870.     if (n == ZSFILE)
  871.     return(write(fileno(fp[n]),s,(int)strlen(s)));
  872.     else
  873. #endif /* OS2 */
  874.         return(fputs(s,fp[n]) == EOF ? -1 : 0);
  875. }
  876.  
  877. /*  Z S O U T L  --  Write string to file, with line terminator, buffered  */
  878.  
  879. int
  880. zsoutl(n,s) int n; char *s; {
  881.     /* if (chkfn(n) < 1) return(-1); */
  882.     if (fputs(s,fp[n]) == EOF) return(-1);
  883.     if (fputs("\n",fp[n]) == EOF) return(-1); /* (===OS2 ? \r\n) */
  884.     return(0);
  885. }
  886.  
  887. /*  Z S O U T X  --  Write x characters to file, unbuffered.  */
  888.  
  889. int
  890. zsoutx(n,s,x) int n, x; char *s; {
  891. #ifdef COMMENT
  892.     if (chkfn(n) < 1) return(-1);
  893.     return(write(fp[n]->_file,s,x));
  894. #endif
  895.     return(write(fileno(fp[n]),s,x) == x ? x : -1);
  896. }
  897.  
  898.  
  899. /*  Z C H O U T  --  Add a character to the given file.  */
  900.  
  901. /*  Should return 0 or greater on success, -1 on failure (e.g. disk full)  */
  902.  
  903. int
  904. #ifdef CK_ANSIC
  905. zchout(register int n, char c)
  906. #else
  907. zchout(n,c) register int n; char c;
  908. #endif /* CK_ANSIC */
  909. /* zchout() */ {
  910.     /* if (chkfn(n) < 1) return(-1); */
  911. #ifndef OS2
  912.     if (n == ZSFILE) {            /* Use unbuffered for session log */
  913.         return(write(fileno(fp[n]),&c,1) == 1 ? 0 : -1);
  914.     } else {                /* Buffered for everything else */
  915. #endif /* OS2 */
  916.     if (putc(c,fp[n]) == EOF)    /* If true, maybe there was an error */
  917.       return(ferror(fp[n])?-1:0);    /* Check to make sure */
  918.     else                /* Otherwise... */
  919.       return(0);            /* There was no error. */
  920. #ifndef OS2
  921.     }
  922. #endif /* OS2 */
  923. }
  924.  
  925. /* (PWP) buffered character output routine to speed up file IO */
  926.  
  927. int
  928. zoutdump() {
  929.     int x;
  930.     zoutptr = zoutbuffer;        /* Reset buffer pointer in all cases */
  931.     debug(F101,"zoutdump chars","",zoutcnt);
  932.     if (zoutcnt == 0) {            /* Nothing to output */
  933.     return(0);
  934.     } else if (zoutcnt < 0) {        /* Unexpected negative argument */
  935.     zoutcnt = 0;            /* Reset output buffer count */
  936.     return(-1);            /* and fail. */
  937.     }
  938.  
  939. /* Frank Prindle suggested that replacing this fwrite() by an fflush() */
  940. /* followed by a write() would improve the efficiency, especially when */
  941. /* writing to stdout.  Subsequent tests showed a 5-fold improvement!   */
  942. /* if (x = fwrite(zoutbuffer, 1, zoutcnt, fp[ZOFILE])) {              */
  943.  
  944.     fflush(fp[ZOFILE]);
  945.     if ((x = write(fileno(fp[ZOFILE]),zoutbuffer,zoutcnt)) == zoutcnt) {
  946.     debug(F101,"zoutdump write ok","",zoutcnt);
  947.     zoutcnt = 0;            /* Reset output buffer count */
  948.     return(0);            /* write() worked OK */
  949.     } else {
  950.     debug(F101,"zoutdump write error","",errno);
  951.     debug(F101,"zoutdump write returns","",x);
  952.     zoutcnt = 0;            /* Reset output buffer count */
  953.     return(-1);            /* write() failed */
  954.     }
  955. }
  956.  
  957. /*  C H K F N  --  Internal function to verify file number is ok  */
  958.  
  959. /*
  960.  Returns:
  961.   -1: File number n is out of range
  962.    0: n is in range, but file is not open
  963.    1: n in range and file is open
  964. */
  965. int
  966. chkfn(n) int n; {
  967. #ifdef COMMENT                /* Save some stack space */
  968.     switch (n) {
  969.     case ZCTERM:
  970.     case ZSTDIO:
  971.     case ZIFILE:
  972.     case ZOFILE:
  973.     case ZDFILE:
  974.     case ZTFILE:
  975.     case ZPFILE:
  976.     case ZSFILE:
  977.     case ZSYSFN:
  978.         case ZRFILE:
  979.         case ZWFILE: break;
  980.     default:
  981.         debug(F101,"chkfn: file number out of range","",n);
  982.         fprintf(stderr,"?File number out of range - %d\n",n);
  983.         return(-1);
  984.     }
  985.     return( (fp[n] == NULL) ? 0 : 1 );
  986. #else
  987.     if (n < 0 || n >= ZNFILS)
  988.       return(-1);
  989.     else return( (fp[n] == NULL) ? 0 : 1 );
  990. #endif /* COMMENT */
  991. }
  992.  
  993. /*  Z C H K I  --  Check if input file exists and is readable  */
  994.  
  995. /*
  996.   Returns:
  997.    >= 0 if the file can be read (returns the size).
  998.      -1 if file doesn't exist or can't be accessed,
  999.      -2 if file exists but is not readable (e.g. a directory file).
  1000.      -3 if file exists but protected against read access.
  1001. */
  1002. /*
  1003.  For Berkeley Unix, a file must be of type "regular" to be readable.
  1004.  Directory files, special files, and symbolic links are not readable.
  1005. */
  1006. long
  1007. zchki(name) char *name; {
  1008.     struct stat buf;
  1009.     int x;
  1010.  
  1011. #ifdef UNIX
  1012.     x = strlen(name);
  1013.     if (x == 9 && !strcmp(name,"/dev/null"))
  1014.       return(0);
  1015. #endif /* UNIX */
  1016.  
  1017.     x = stat(name,&buf);
  1018.     if (x < 0) {
  1019.     debug(F111,"zchki stat fails",name,errno);
  1020.     return(-1);
  1021.     }
  1022.     if (!S_ISREG (buf.st_mode)) {    /* Must be regular file */
  1023.     debug(F111,"zchki skipping:",name,x);
  1024.     return(-2);
  1025.     }
  1026.     debug(F111,"zchki stat ok:",name,x);
  1027.  
  1028. #ifdef SW_ACC_ID
  1029.     debug(F100,"zchki swapping ids for access()","",0);
  1030.     priv_on();
  1031. #endif /* SW_ACC_ID */
  1032.     x = access(name,R_OK);
  1033. #ifdef SW_ACC_ID
  1034.     priv_off();
  1035.     debug(F100,"zchki swapped ids restored","",0);
  1036. #endif /* SW_ACC_ID */
  1037.     if (x < 0) {     /* Is the file accessible? */
  1038.     debug(F111," access failed:",name,x); /* No */
  1039.         return(-3);
  1040.     } else {
  1041.     iflen = buf.st_size;              /* Yes, remember size */
  1042.     strncpy(nambuf,name,MAXNAMLEN);          /* and name globally. */
  1043.     debug(F111," access ok:",name,(int) iflen);
  1044.     return( (iflen > -1L) ? iflen : 0L );
  1045.     }
  1046. }
  1047.  
  1048. /*  Z C H K O  --  Check if output file can be created  */
  1049.  
  1050. /*
  1051.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  1052. */
  1053. int
  1054. zchko(name) char *name; {
  1055.     int i, x;
  1056.     char *s;
  1057.  
  1058.     if (!name) return(-1);        /* Watch out for null pointer. */
  1059.     x = (int)strlen(name);        /* Get length of filename */
  1060.     debug(F101," length","",x);
  1061.  
  1062. #ifdef UNIX
  1063. /*
  1064.   Writing to null device is OK.
  1065. */
  1066.     if (x == 9 && !strcmp(name,"/dev/null"))
  1067.       return(0);
  1068. #endif /* UNIX */
  1069.  
  1070.     s = malloc(x+3);            /* Must copy because we can't */
  1071.     if (!s) {                /* write into our argument. */
  1072.     fprintf(stderr,"Malloc error 46\n");
  1073.     return(-1);
  1074.     }
  1075.     strcpy(s,name);
  1076.  
  1077.     for (i = x; i > 0; i--)        /* Strip filename from right. */
  1078.       if (ISDIRSEP(s[i-1])) break;
  1079.     debug(F101," i","",i);
  1080.  
  1081. #ifdef COMMENT
  1082. /* X/OPEN XPG3-compliant systems fail if argument ends with "/"...  */
  1083.     if (i == 0)                /* If no path, use current directory */
  1084.       strcpy(s,"./");
  1085.     else                /* Otherwise, use given one. */
  1086.       s[i] = '\0';
  1087. #else
  1088. /* So now we use "path/." if path given, or "." if no path given. */
  1089.     s[i++] = '.';            /* Append "." to path. */
  1090.     s[i] = '\0';
  1091. #endif /* COMMENT */
  1092.  
  1093. #ifdef SW_ACC_ID
  1094.     debug(F100,"zchko swapping ids for access()","",0);
  1095.     priv_on();
  1096. #endif /* SW_ACC_ID */
  1097. #ifdef OS2                /* No unwritable directories in OS/2 */
  1098.     x = 0;
  1099. #else
  1100.     x = access(s,W_OK);            /* Check access of path. */
  1101. #endif /* OS2 */
  1102. #ifdef SW_ACC_ID
  1103.     priv_off();
  1104.     debug(F100,"zchko swapped ids restored","",0);
  1105. #endif /* SW_ACC_ID */
  1106.     if (x < 0)
  1107.       debug(F111,"zchko access failed:",s,errno);
  1108.     else
  1109.       debug(F111,"zchko access ok:",s,x);
  1110.     free(s);                /* Free temporary storage */
  1111.     return((x < 0) ? -1 : 0);        /* and return. */
  1112. }
  1113.  
  1114. /*  Z D E L E T  --  Delete the named file.  */
  1115.  
  1116. int
  1117. zdelet(name) char *name; {
  1118.     return(unlink(name));
  1119. }
  1120.  
  1121.  
  1122. /*  Z R T O L  --  Convert remote filename into local form  */
  1123.  
  1124. /*  For UNIX, this means changing uppercase letters to lowercase.  */
  1125.  
  1126. VOID
  1127. zrtol(name,name2) char *name, *name2; {
  1128.     char *p; int flag = 0;
  1129.     debug(F101,"zrtol name","",name);
  1130.     debug(F101,"zrtol name2","",name2);
  1131.     if (!name || !name2) return;
  1132.     debug(F101,"zrtol input","",name);
  1133.     p = name2;
  1134.     for ( ; *name != '\0'; name++) {
  1135.     if (*name > ' ') flag = 1;    /* Strip leading blanks and controls */
  1136.     if (flag == 0 && *name < '!') continue;
  1137.         *p++ = isupper(*name) ? tolower(*name) : *name;
  1138.     }
  1139.     *p-- = '\0';            /* Terminate */
  1140.     while (*p < '!' && p > name2)    /* Strip trailing blanks & contronls */
  1141.       *p-- = '\0';
  1142. #ifdef OS2
  1143.     if (!IsFileNameValid(name2))
  1144.       ChangeNameForFAT(name2);
  1145. #endif /* OS2 */
  1146.     debug(F110,"zrtol result",name2,0);
  1147. }
  1148.  
  1149.  
  1150. /*  Z S T R I P  --  Strip device & directory name from file specification */
  1151.  
  1152. /*  Strip pathname from filename "name", return pointer to result in name2 */
  1153.  
  1154. #ifdef pdp11
  1155. static char work[100];        /* buffer for use by zstrip and zltor */
  1156. #else
  1157. static char work[257];
  1158. #endif /* pdp11 */
  1159.  
  1160. VOID
  1161. zstrip(name,name2) char *name, **name2; {
  1162.     char *cp, *pp;
  1163.     debug(F110,"zstrip before",name,0);
  1164.     pp = work;
  1165. #ifdef DTILDE
  1166.     if (*name == '~') name++;
  1167. #endif /* DTILDE */
  1168.     for (cp = name; *cp != '\0'; cp++) {
  1169.         if (ISDIRSEP(*cp))
  1170.       pp = work;
  1171.     else
  1172.       *pp++ = *cp;
  1173.     }
  1174.     *pp = '\0';                /* Terminate the string */
  1175.     *name2 = work;
  1176.     debug(F110,"zstrip after",*name2,0);
  1177. }
  1178.  
  1179. /*  Z L T O R  --  Local TO Remote */
  1180.  
  1181. VOID
  1182. zltor(name,name2) char *name, *name2; {
  1183.     char *cp, *pp;
  1184.     int dc = 0;
  1185. #ifdef aegis
  1186.     char *namechars;
  1187.     int tilde = 0, bslash = 0;
  1188.  
  1189.     if ((namechars = getenv("NAMECHARS")) != NULL) {
  1190.         if (xindex(namechars, '~' ) != NULL) tilde  = '~';
  1191.         if (xindex(namechars, '\\') != NULL) bslash = '\\';
  1192.     } else {
  1193.         tilde = '~';
  1194.         bslash = '\\';
  1195.     }
  1196. #endif /* aegis */
  1197.  
  1198.     debug(F110,"zltor",name,0);
  1199.     pp = work;
  1200. #ifdef aegis
  1201.     cp = name;
  1202.     if (tilde && *cp == tilde)
  1203.         ++cp;
  1204.     for (; *cp != '\0'; cp++) {
  1205.         if (*cp == '/' || *cp == bslash) {    /* strip path name */
  1206. #else
  1207.     for (cp = name; *cp != '\0'; cp++) {    /* strip path name */
  1208.         if (ISDIRSEP(*cp)) {
  1209. #endif /* aegis */
  1210.         dc = 0;
  1211.         pp = work;
  1212.     }
  1213.     else if (islower(*cp)) *pp++ = toupper(*cp); /* Uppercase letters */
  1214.     else if (*cp == '~') *pp++ = 'X';    /* Change tilde to 'X' */
  1215.     else if (*cp == '#') *pp++ = 'X';    /* Change number sign to 'X' */
  1216.     else if ((*cp == '.') && (++dc > 1)) *pp++ = 'X'; /* & extra dots */
  1217.     else *pp++ = *cp;
  1218.     }
  1219.     *pp = '\0';                /* Tie it off. */
  1220.     cp = name2;                /* If nothing before dot, */
  1221.     if (*work == '.') *cp++ = 'X';    /* insert 'X' */
  1222.     strcpy(cp,work);
  1223.     debug(F110," name2",name2,0);
  1224. }
  1225.  
  1226.  
  1227. /*  Z C H D I R  --  Change directory  */
  1228. /*
  1229.   Call with:
  1230.     dirnam = pointer to name of directory to change to,
  1231.       which may be "" or NULL to indicate user's home directory.
  1232.   Returns:
  1233.     0 on failure
  1234.     1 on success
  1235. */
  1236. int
  1237. zchdir(dirnam) char *dirnam; {
  1238.     char *hd, *sp, *p;
  1239.  
  1240.     debug(F110,"zchdir",dirnam,0);
  1241.     if (dirnam == NULL || dirnam == "" || *dirnam == '\0') /* If arg is null */
  1242.       dirnam = zhome();            /* use user's home directory. */
  1243.     sp = dirnam;
  1244.     debug(F110,"zchdir 2",dirnam,0);
  1245.  
  1246. #ifdef DTILDE
  1247.     hd = tilde_expand(dirnam);        /* Attempt to expand tilde */
  1248.     if (*hd == '\0') hd = dirnam;    /* in directory name. */
  1249. #else
  1250.     hd = dirnam;
  1251. #endif /* DTILDE */
  1252.     debug(F110,"zchdir 3",hd,0);
  1253. #ifdef pdp11
  1254.     /* Just to save some space */
  1255.     return((chdir(hd) == 0) ? 1 : 0);
  1256. #else
  1257. #ifdef OS2
  1258.     if (isalpha(hd[0]) && hd[1] == ':') {
  1259.         if (zchdsk(hd[0]))
  1260.       return(0);
  1261.     if (hd[2] == 0)
  1262.       return(1);            /* Handle drive-only case */
  1263.     }
  1264. #endif /* OS2 */
  1265.     if (chdir(hd) == 0) return(1);    /* Try to cd */ /* (===OS2===) */
  1266.     p = sp;                /* Failed, lowercase it. */
  1267.     while (*p) {
  1268.     if (isupper(*p)) *p = tolower(*p);
  1269.     p++;
  1270.     }
  1271.     debug(F110,"zchdir 4",hd,0);
  1272. #ifdef DTILDE
  1273.     hd = tilde_expand(sp);        /* Try again to expand tilde */
  1274.     if (*hd == '\0') hd = sp;
  1275. #else
  1276.     hd = sp;                /* Point to result */
  1277. #endif /* DTILDE */
  1278.     debug(F110,"zchdir 5",hd,0);
  1279.     return((chdir(hd) == 0) ? 1 : 0);
  1280. #endif /* pdp11 */
  1281. }
  1282.  
  1283. /*  Z H O M E  --  Return pointer to user's home directory  */
  1284.  
  1285. char *
  1286. zhome() {
  1287.     char *home = getenv("HOME");
  1288. #ifdef OS2
  1289.     extern char startupdir[];
  1290.     return(home ? home : startupdir);
  1291. #else
  1292.     return(home ? home : ".");
  1293. #endif
  1294. }
  1295.  
  1296. /*  Z G T D I R  --  Return pointer to user's current directory  */
  1297.  
  1298. #ifdef pdp11
  1299. #define CWDBL 80            /* Save every byte we can... */
  1300. #else
  1301. #ifdef MAXPATHLEN
  1302. #define CWDBL MAXPATHLEN
  1303. #else
  1304. #define CWDBL 100
  1305. #endif /* MAXPATHLEN */
  1306. #endif /* pdp11 */
  1307. static char cwdbuf[CWDBL+1];
  1308.  
  1309. char *
  1310. zgtdir() {
  1311.     char *buf;
  1312.  
  1313. #ifdef BSD44
  1314.     extern char *getwd();
  1315.     buf = cwdbuf;
  1316.     return(getwd(buf));
  1317. #else
  1318. #ifdef SVORPOSIX
  1319.     extern char *getcwd();
  1320.     buf = cwdbuf;
  1321.     return(getcwd(buf,CWDBL));
  1322. #else
  1323. #ifdef OS2
  1324. #ifndef __IBMC__ /* which has a macro for this */
  1325.     extern char *getcwd();
  1326. #endif /* __IBMC__ */
  1327.     buf = cwdbuf;
  1328.     return(getcwd(buf,CWDBL));
  1329. #else
  1330. #ifdef BSD4
  1331.     extern char *getwd();
  1332.     buf = cwdbuf;
  1333.     return(getwd(buf));
  1334. #else
  1335.     return("directory unknown");
  1336. #endif /* BSD4 */
  1337. #endif /* OS2 */
  1338. #endif /* SYSVORPOSIX */
  1339. #endif /* BSD44 */
  1340. }
  1341.  
  1342. /*  Z X C M D -- Run a system command so its output can be read like a file */
  1343.  
  1344. int
  1345. zxcmd(filnum,comand) int filnum; char *comand; {
  1346. #ifdef OS2
  1347.     if (chkfn(filnum) < 0) return(-1);    /* Need a valid Kermit file number. */
  1348.     if (filnum == ZSTDIO || filnum == ZCTERM) /* But not one of these. */
  1349.       return(0);
  1350.     if (filnum == ZIFILE || filnum == ZRFILE) { /* Input from a command */
  1351.     if (priv_chk() || ((fp[filnum] = popen(comand,"r")) == NULL))
  1352.       return(0);
  1353.     } else { /* Output to a command */
  1354.     if (priv_chk() || ((fp[filnum] = popen(comand,"w")) == NULL))
  1355.       return(0);
  1356.     }
  1357.     ispipe[filnum] = 1;
  1358.     return(1);
  1359. #else /* Not OS2 */
  1360.     int pipes[2];
  1361.     int out;
  1362.  
  1363.     if (chkfn(filnum) < 0) return(-1);    /* Need a valid Kermit file number. */
  1364.     if (filnum == ZSTDIO || filnum == ZCTERM) /* But not one of these. */
  1365.       return(0);
  1366.  
  1367.     out = (filnum == ZIFILE || filnum == ZRFILE) ? 0 : 1 ;
  1368.  
  1369. /* Output to a command */
  1370.  
  1371.     if (out) {                /* Need popen() to do this. */
  1372. #ifdef NOPOPEN
  1373.     return(0);            /* no popen(), fail. */
  1374. #else
  1375. /* Use popen() to run the command. */
  1376.  
  1377. #ifdef _POSIX_SOURCE
  1378. /* Strictly speaking, popen() is not available in POSIX.1 */
  1379. #define DCLPOPEN
  1380. #endif /* _POSIX_SOURCE */
  1381.  
  1382. #ifdef DCLPOPEN
  1383. /* popen() needs declaring because it's not declared in <stdio.h> */
  1384.     FILE *popen();
  1385. #endif /* DCLPOPEN */
  1386.  
  1387.     if (priv_chk() || ((fp[filnum] = popen(comand,"w")) == NULL))
  1388.       return(0);
  1389.     else return(1);
  1390. #endif /* NOPOPEN */
  1391.     }
  1392.  
  1393. /* Input from a command */
  1394.  
  1395.     if (pipe(pipes) != 0) {
  1396.     debug(F100,"zxcmd pipe failure","",0);
  1397.     return(0);            /* can't make pipe, fail */
  1398.     }
  1399.  
  1400. /* Create a fork in which to run the named process */
  1401.  
  1402. #ifdef aegis
  1403.     if ((pid = vfork()) == 0) {        /* child */
  1404. #else
  1405.     if ((pid = fork()) == 0) {        /* child */
  1406. #endif
  1407.  
  1408. /* We're in the fork. */
  1409.  
  1410.     char *shpath, *shname, *shptr;    /* Find user's preferred shell */
  1411. #ifndef aegis
  1412.     struct passwd *p;
  1413.     char *defshell = "/bin/sh";    /* default shell */
  1414. #endif /* aegis */
  1415.     if (priv_can()) exit(1);    /* Turn off any privileges! */
  1416.     debug(F101,"zxcmd pid","",pid);
  1417.     close(pipes[0]);        /* close input side of pipe */
  1418.     close(0);            /* close stdin */
  1419.     if (open("/dev/null",0) < 0) return(0); /* replace input by null */
  1420. #ifndef OXOS
  1421. #ifndef SVORPOSIX
  1422.     dup2(pipes[1],1);        /* BSD: replace stdout & stderr */
  1423.     dup2(pipes[1],2);        /* by the pipe */
  1424. #else
  1425.     close(1);            /* AT&T: close stdout */
  1426.     if ( dup(pipes[1]) != 1 )    /* Send stdout to the pipe */
  1427.       return(0);
  1428.     close(2);            /* Send stderr to the pipe */
  1429.     if ( dup(pipes[1]) != 2 )
  1430.       return(0);
  1431. #endif /* SVORPOSIX */
  1432. #else /* OXOS */
  1433.     dup2(pipes[1],1);
  1434.     dup2(pipes[1],2);
  1435. #endif /* OXOS */
  1436.     close(pipes[1]);        /* Don't need this any more. */
  1437.  
  1438. #ifdef aegis
  1439.     if ((shpath = getenv("SERVERSHELL")) == NULL) shpath = "/bin/sh";
  1440. #else
  1441.     shpath = getenv("SHELL");    /* What shell? */
  1442.     if (shpath == NULL) {
  1443.         p = getpwuid( real_uid() );    /* Get login data */
  1444.         if (p == (struct passwd *)NULL || !*(p->pw_shell))
  1445.           shpath = defshell;
  1446.         else shpath = p->pw_shell;
  1447.         }
  1448. #endif /* aegis */
  1449.     shptr = shname = shpath;
  1450.     while (*shptr != '\0')
  1451.       if (*shptr++ == '/')
  1452.         shname = shptr;
  1453.     debug(F100,"zxcmd...","",0);
  1454.     debug(F110,shpath,shname,0);
  1455.  
  1456.     execl(shpath,shname,"-c",comand,(char *)NULL); /* Execute the cmd */
  1457.     exit(0);            /* just punt if it failed. */
  1458.     } else if (pid == (PID_T) -1) {
  1459.     debug(F100,"zxcmd fork failure","",0);
  1460.     return(0);
  1461.     }
  1462.     debug(F101,"zxcmd pid","",pid);
  1463.     if (out) {
  1464.     close(pipes[0]);        /* Don't need the input side */
  1465.     fp[filnum] = fdopen(pipes[1],"w"); /* Open a stream for output. */
  1466.     fp[ZSYSFN] = fp[filnum];    /* Remember. */
  1467.     zoutcnt = 0;            /* (PWP) reset input buffer */
  1468.     zoutptr = zoutbuffer;
  1469.     } else {
  1470.     close(pipes[1]);        /* Don't need the output side */
  1471.     fp[filnum] = fdopen(pipes[0],"r"); /* Open a stream for input. */
  1472.     fp[ZSYSFN] = fp[filnum];    /* Remember. */
  1473.     zincnt = 0;            /* (PWP) reset input buffer */
  1474.     zinptr = zinbuffer;
  1475.     }
  1476.     return(1);
  1477. #endif /* OS2 */
  1478. }
  1479.  
  1480. /*  Z C L O S F  - wait for the child fork to terminate and close the pipe. */
  1481.  
  1482. int
  1483. zclosf(filnum) int filnum; {
  1484.     int wstat;
  1485.     debug(F101,"zclosf filnum","",filnum);
  1486. #ifndef NOPOPEN
  1487. #ifdef OS2
  1488.     if (ispipe[filnum]) {
  1489.     int x;
  1490.     x = pclose(fp[filnum]);
  1491.     fp[filnum] = NULL;
  1492.     ispipe[filnum] = 0;
  1493. #else
  1494.     if (filnum == ZWFILE) {
  1495.     int x;
  1496.     x = pclose(fp[filnum]);
  1497.     fp[filnum] = fp[ZSYSFN] = NULL;
  1498. #endif /* OS2 */
  1499.     return((x < 0) ? 0 : 1);
  1500.     }
  1501. #endif /* NOPOPEN */
  1502.     debug(F101,"zclosf fp[filnum]","", fp[filnum]);
  1503.     debug(F101,"zclosf fp[ZSYSFN]","", fp[ZSYSFN]);
  1504. #ifdef OS2
  1505.     fclose(fp[filnum]);
  1506.     fp[filnum] = NULL;
  1507. #else
  1508.     if (pid != (PID_T) 0) {
  1509.     debug(F101,"zclosf killing pid","",pid);
  1510.     kill(pid,9);
  1511.         while ((wstat = wait((WAIT_T *)0)) != pid && wstat != -1) ;
  1512.         pid = 0;
  1513.     }
  1514.     fclose(fp[filnum]);
  1515.     fp[filnum] = fp[ZSYSFN] = NULL;
  1516. #endif /* OS2 */
  1517.     return(1);
  1518. }
  1519.  
  1520. /*  Z X P A N D  --  Expand a wildcard string into an array of strings  */
  1521. /*
  1522.   Returns the number of files that match fn1, with data structures set up
  1523.   so that first file (if any) will be returned by the next znext() call.
  1524.   Depends on external variable wildxpand: 0 means we expand wildcards
  1525.   internally, nonzero means we call the shell to do it.
  1526. */
  1527.  
  1528. int
  1529. zxpand(fn) char *fn; {
  1530.     char *p;
  1531.  
  1532. #ifdef DTILDE                /* Built with tilde-expansion? */
  1533.     char *tnam;
  1534. #endif /* DTILDE */
  1535.     debug(F111,"zxpand entry",fn,wildxpand);
  1536. #ifdef DTILDE                /* Built with tilde-expansion? */
  1537.     if (*fn == '~') {            /* Starts with tilde? */
  1538.     tnam = tilde_expand(fn);    /* Try to expand it. */
  1539.     if (tnam) fn = tnam;
  1540.     }
  1541.     debug(F110,"zxpand after tilde_x",fn,0);
  1542. #endif /* DTILDE */
  1543. #ifndef OS2
  1544.     if (wildxpand)            /* Who is expanding wildcards? */
  1545.       fcount = shxpand(fn,mtchs,MAXWLD); /* Shell */
  1546.     else
  1547. #endif /* OS2 */
  1548.       fcount = fgen(fn,mtchs,MAXWLD);    /* Kermit */
  1549.     if (fcount > 0) {
  1550.     mtchptr = mtchs;        /* Save pointer for next. */
  1551.     }
  1552.     if (fcount > 0) {
  1553.     debug(F111,"zxpand ok",mtchs[0],fcount);
  1554.     return(fcount);
  1555.     }
  1556.     debug(F111,"zxpand fgen1",fn,fcount); /* Didn't get one, or got too many */
  1557.     p = malloc((int)strlen(fn) + 10);    /* Make space */
  1558.     if (!p) return(0);
  1559.     zrtol(fn,p);            /* Try again, maybe lowercase */
  1560. #ifndef OS2
  1561.     if (wildxpand)
  1562.       fcount = shxpand(p,mtchs,MAXWLD); /* Shell */
  1563.     else
  1564. #endif /* OS2 */
  1565.       fcount = fgen(p,mtchs,MAXWLD);    /* Kermit */
  1566.     if (fcount > 0) {            /* Got one? */
  1567.     mtchptr = mtchs;        /* Save pointer for next. */
  1568.     debug(F111,"zxpand fgen2 ok",mtchs[0],fcount);
  1569.     } else debug(F111,"zxpand 2 not ok",p,fcount);
  1570.     free(p);
  1571.     return(fcount);
  1572. }
  1573.  
  1574.  
  1575. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  1576. /*
  1577.  Returns >0 if there's another file, with its name copied into the arg string,
  1578.  or 0 if no more files in list.
  1579. */
  1580. int
  1581. znext(fn) char *fn; {
  1582.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  1583.     else *fn = '\0';
  1584.     debug(F111,"znext",fn,fcount+1);
  1585.     return(fcount+1);
  1586. }
  1587.  
  1588.  
  1589. /*  Z C H K S P A  --  Check if there is enough space to store the file  */
  1590.  
  1591. /*
  1592.  Call with file specification f, size n in bytes.
  1593.  Returns -1 on error, 0 if not enough space, 1 if enough space.
  1594. */
  1595. int
  1596. #ifdef CK_ANSIC
  1597. zchkspa(char *f, long n)
  1598. #else
  1599. zchkspa(f,n) char *f; long n;
  1600. #endif /* CK_ANSIC */
  1601. /* zchkspa() */ {
  1602. #ifdef OS2
  1603. /* OS/2 gives us an easy way to do this. */
  1604.     if (isalpha(f[0]) && f[1] == ':')
  1605.       return(zdskspace(toupper(f[0]) - 'A' + 1) >= n);
  1606.     else
  1607.       return(zdskspace(0) >= n);
  1608. #else
  1609. /* In UNIX there is no good (and portable) way. */
  1610.     return(1);                /* Always say OK. */
  1611. #endif /* OS2 */
  1612. }
  1613.  
  1614.  
  1615. /*  Z N E W N  --  Make a new name for the given file  */
  1616.  
  1617. /*
  1618.   Given the name, fn, of a file that already exists, this function builds a
  1619.   new name of the form "<oldname>.~<n>~", where <oldname> is argument name
  1620.   (fn), and <n> is a version number, one higher than any existing version
  1621.   number for that file, up to 9999.  This format is consistent with that used
  1622.   by GNU EMACS.  If the constructed name is too long for the system's maximum,
  1623.   enough characters are truncated from the end of <fn> to allow the version
  1624.   number to fit.  If no free version numbers exist between 1 and 9999, a
  1625.   version number of "xxxx" is used.  Returns a pointer to the new name in
  1626.   argument s.
  1627. */
  1628.  
  1629. VOID
  1630. znewn(fn,s) char *fn, **s; {
  1631. #ifdef pdp11
  1632. #define ZNEWNBL 63            /* Name buffer length */
  1633. #define ZNEWNMD 3            /* Max digits for version number */
  1634. #else
  1635. #define ZNEWNBL 255
  1636. #define ZNEWNMD 4
  1637. #endif /* pdp11 */
  1638.  
  1639.     static char buf[ZNEWNBL+1];
  1640.     char *bp, *xp, *yp;
  1641. #ifdef OS2
  1642.     char *zp, ch, temp[14];
  1643. #endif /* OS2 */
  1644.     int len = 0, d = 0, n, t, i, j, k, power = 1;
  1645.  
  1646.     int max = MAXNAMLEN;        /* Maximum name length */
  1647.  
  1648.     if (max < 14) max = 14;        /* Make it reasonable */
  1649.     if (max > ZNEWNBL) max = ZNEWNBL;
  1650.     bp = buf;                /* Buffer for building new name */
  1651.     yp = fn;
  1652.     while (*yp) {            /* Copy old name into buffer */
  1653.     *bp++ = *yp++;
  1654.     if (len++ > ZNEWNBL) break;    /* ...up to buffer length */
  1655.     }
  1656.     *s = NULL;
  1657.     for (i = 1; i < ZNEWNMD + 1; i++) {    /* Version numbers up to 10**i - 1 */
  1658.     power *= 10;            /* Next power of 10 */
  1659.     j = max - len;            /* Space left for version number */
  1660.     k = 3 + i;            /* Space needed for it */
  1661.     if (j < k) {            /* Make room if necessary */
  1662.         len -= (k - j);        /* Adjust length of filename */
  1663.         bp = buf + len;        /* Point to new end */
  1664.     }
  1665.     *bp++ = '*';            /* Put a star on the end (UNIX) */
  1666.     *bp-- = '\0';            /* Terminate with null */
  1667.  
  1668.     n = zxpand(buf);        /* Expand the resulting wild name */
  1669.                     /* n is the number of matches */
  1670.     while (n-- > 0) {        /* Find any existing name.~n~ files */
  1671.         xp = *mtchptr++;        /* Point at matching name */
  1672.         xp += len;            /* Look for .~<n>~ at the end of it */
  1673.         if (*xp == '.' && *(xp+1) == '~') {    /* Has a version number */
  1674.         t = atoi(xp+2);                /* Get it */
  1675.         if (t > d) d = t;    /* Save d = highest version number */
  1676.         }
  1677.     }
  1678.     if (d < power-1) {        /* Less than maximum possible? */
  1679.         sprintf(bp,".~%d~",d+1);    /* Yes, make "name.~<d+1>~" */
  1680.         *s = buf;            /* Point to new name */
  1681.         break;            /* Done, return it */
  1682.     }
  1683.     }
  1684.     if (*s == NULL) {
  1685.     sprintf(bp,".~xxxx~");        /* Too many, use xxxx. */
  1686.     *s = buf;
  1687.     }
  1688. #ifdef OS2
  1689.     if (IsFileNameValid(buf))
  1690.         return; /* HPFS */
  1691.     /* otherwise make FAT 8.3 name */
  1692.     xp = bp = buf;
  1693.     yp = fn;
  1694.     while (*yp) {            /* Copy name into buf */
  1695.     ch = *bp++ = *yp++;
  1696.     if (ISDIRSEP(ch) || (ch == ':')) xp=bp;
  1697.     }
  1698.     *bp = '\0';
  1699.     yp = xp;
  1700.     i = 1;
  1701.     while (*yp && (*yp != '.')) {
  1702.     yp++;
  1703.     if (++i<=6) zp=yp;
  1704.     }
  1705.     /* zp points to 6th character in name, or yp, whichever occurs first. */
  1706.     strcpy(temp,yp);            /* Copy extension, if any */
  1707.     while (zp != xp+8) {
  1708.         if ( zp < xp+5 ) *zp++='0';
  1709.         else *zp++='?';            /* Pad out with wild cards */
  1710.     }
  1711.     strcpy(zp,temp);            /* Get the extension back */
  1712.     n = zxpand(buf);            /* Expand the resulting wild name */
  1713.     d = 0;                /* Index number */
  1714.     while (znext(temp)) {
  1715.         i = atoi(temp+5);
  1716.         if (i > d) d = i;
  1717.     }
  1718.     sprintf(temp,"%03d",d+1);        /* get the number into a string */
  1719.     memcpy(xp+5, temp, 3);
  1720. #endif /* OS2 */
  1721.     return;
  1722. }
  1723.  
  1724.  
  1725. /*  Z R E N A M E  --  Rename a file  */
  1726.  
  1727. /*  Note, link() and unlink() are used because rename() is not available  */
  1728. /*  in some versions of UNIX.   */
  1729. /*  Call with old and new names */
  1730. /*  Returns 0 on success, -1 on failure. */
  1731.  
  1732. int
  1733. zrename(old,new) char *old, *new; {
  1734. #ifdef OS2
  1735.    return rename(old, new);
  1736. #else
  1737.    if (link(old,new) < 0) {        /* Make a link with the new name. */
  1738.     debug(F111,"zrename link fails, errno",old,errno);
  1739.     return(-1);
  1740.     }
  1741.     if (unlink(old) < 0) {        /* Unlink the old name. */
  1742.     debug(F111,"zrename unlink fails, errno",old,errno);
  1743.     return(-1);
  1744.     }
  1745.     return(0);
  1746. #endif /* OS2 */
  1747. }
  1748.  
  1749. /*  Z S A T T R */
  1750. /*
  1751.  Fills in a Kermit file attribute structure for the file which is to be sent.
  1752.  Returns 0 on success with the structure filled in, or -1 on failure.
  1753.  If any string member is null, then it should be ignored.
  1754.  If any numeric member is -1, then it should be ignored.
  1755. */
  1756. int
  1757. zsattr(xx) struct zattr *xx; {
  1758.     long k;
  1759.  
  1760.     k = iflen % 1024L;            /* File length in K */
  1761.     if (k != 0L) k = 1L;
  1762.     xx->lengthk = (iflen / 1024L) + k;
  1763.     xx->type.len = 0;            /* File type can't be filled in here */
  1764.     xx->type.val = "";
  1765.     if (*nambuf) {
  1766.     xx->date.val = zfcdat(nambuf);    /* File creation date */
  1767.     xx->date.len = (int)strlen(xx->date.val);
  1768.     } else {
  1769.     xx->date.len = 0;
  1770.     xx->date.val = "";
  1771.     }
  1772.     xx->creator.len = 0;        /* File creator */
  1773.     xx->creator.val = "";
  1774.     xx->account.len = 0;        /* File account */
  1775.     xx->account.val = "";
  1776.     xx->area.len = 0;            /* File area */
  1777.     xx->area.val = "";
  1778.     xx->passwd.len = 0;            /* Area password */
  1779.     xx->passwd.val = "";
  1780.     xx->blksize = -1L;            /* File blocksize */
  1781.     xx->access.len = 0;            /* File access */
  1782.     xx->access.val = "";
  1783.     xx->encoding.len = 0;        /* Transfer syntax */
  1784.     xx->encoding.val = 0;
  1785.     xx->disp.len = 0;            /* Disposition upon arrival */
  1786.     xx->disp.val = "";
  1787.     xx->lprotect.len = 0;        /* Local protection */
  1788.     xx->lprotect.val = "";
  1789.     xx->gprotect.len = 0;        /* Generic protection */
  1790.     xx->gprotect.val = "";
  1791.     xx->systemid.len = 2;        /* System ID */
  1792.     xx->systemid.val = "U1";
  1793.     xx->recfm.len = 0;            /* Record format */
  1794.     xx->recfm.val = "";
  1795.     xx->sysparam.len = 0;        /* System-dependent parameters */
  1796.     xx->sysparam.val = "";
  1797.     xx->length = iflen;            /* Length */
  1798.     return(0);
  1799. }
  1800.  
  1801. /* Z F C D A T  --  Get file creation date */
  1802. /*
  1803.   Call with pointer to filename.
  1804.   On success, returns pointer to creation date in yyyymmdd hh:mm:ss format.
  1805.   On failure, returns pointer to null string.
  1806. */
  1807. static char datbuf[40];
  1808.  
  1809. /* static */                /* (===OS2 change===) */
  1810. char *
  1811. zfcdat(name) char *name; {
  1812.  
  1813. #ifdef TIMESTAMP
  1814.     struct stat buffer;
  1815.     struct tm *time_stamp, *localtime();
  1816.     int yy, ss;
  1817.  
  1818.     datbuf[0] = '\0';
  1819.     if(stat(name,&buffer) != 0) {
  1820.     debug(F110,"zfcdat stat failed",name,0);
  1821.     return("");
  1822.     }
  1823.     time_stamp = localtime(&(buffer.st_mtime));
  1824.     yy = time_stamp->tm_year;
  1825.     if (yy < 100)            /* In case it returns 2-digit year? */
  1826.       yy += 1900;
  1827.     if (yy < 0 || yy > 9999) {        /* Make sure year is ok */
  1828.     debug(F110,"zfcdat date failed",name,0);
  1829.     return("");
  1830.     }
  1831.     if (time_stamp->tm_mon  < 0 || time_stamp->tm_mon  > 11)
  1832.       return("");
  1833.     if (time_stamp->tm_mday < 0 || time_stamp->tm_mday > 31)
  1834.       return("");
  1835.     if (time_stamp->tm_hour < 0 || time_stamp->tm_hour > 23)
  1836.       return("");
  1837.     if (time_stamp->tm_min  < 0 || time_stamp->tm_min  > 59)
  1838.       return("");
  1839.     ss = time_stamp->tm_sec;        /* Seconds */
  1840.     if (ss < 0 || ss  > 59)        /* Some systems give a BIG number */
  1841.       ss = 0;
  1842.     sprintf(datbuf,
  1843. #ifdef pdp11
  1844. /* For some reason, 2.1x BSD sprintf gets the last field wrong. */
  1845.         "%04d%02d%02d %02d:%02d:00",
  1846. #else
  1847.         "%04d%02d%02d %02d:%02d:%02d",
  1848. #endif /* pdp11 */
  1849.         yy,
  1850.         time_stamp->tm_mon + 1,
  1851.         time_stamp->tm_mday,
  1852.         time_stamp->tm_hour,
  1853.         time_stamp->tm_min
  1854. #ifndef pdp11
  1855.         , ss
  1856. #endif /* pdp11 */
  1857.         );
  1858.     yy = (int)strlen(datbuf);
  1859.     debug(F111,"zfcdat",datbuf,yy);
  1860.     if (yy > 17) datbuf[17] = '\0';
  1861.     return(datbuf);
  1862. #else
  1863.     return("");
  1864. #endif /* TIMESTAMP */
  1865. }
  1866.  
  1867. /* Z S T I M E  --  Set creation date for incoming file */
  1868. /*
  1869.  Call with:
  1870.  f  = pointer to name of existing file.
  1871.  yy = pointer to a Kermit file attribute structure in which yy->date.val
  1872.       is a date of the form yyyymmdd hh:mm:ss, e.g. 19900208 13:00:00.
  1873.  x  = is a function code: 0 means to set the file's creation date as given.
  1874.       1 means compare the given date with the file creation date.
  1875.  Returns:
  1876.  -1 on any kind of error.
  1877.   0 if x is 0 and the file date was set successfully.
  1878.   0 if x is 1 and date from attribute structure <= file creation date.
  1879.   1 if x is 1 and date from attribute structure > file creation date.
  1880. */
  1881. int
  1882. zstime(f,yy,x) char *f; struct zattr *yy; int x; {
  1883.     int r = -1;                /* return code */
  1884. /*
  1885.   It is ifdef'd TIMESTAMP because it might not work on V7. bk@kullmar.se.
  1886. */
  1887. #ifdef TIMESTAMP
  1888. /*
  1889.   To do: adapt code from OS-9 Kermit's ck9fio.c zstime function, which
  1890.   is more flexible, allowing [yy]yymmdd[ hh:mm[:ss]].
  1891. */
  1892. #ifndef OS2
  1893.     extern int ftime(), stat();
  1894. #ifdef BSD44
  1895.     extern int utimes();
  1896. #else
  1897.     extern int utime();
  1898. #endif /* BSD44 */
  1899.     /* at least, the declarations for int functions are not needed anyway */
  1900.     extern struct tm *localtime();
  1901.     /* and this should have been declared always through a header file */
  1902. #endif /* OS2 */
  1903.     long tm, days;
  1904.     int i, n, isleapyear;
  1905.                    /*       J  F  M  A   M   J   J   A   S   O   N   D   */
  1906.                    /*      31 28 31 30  31  30  31  31  30  31  30  31   */
  1907.     static
  1908.     int monthdays [13] = {  0,0,31,59,90,120,151,181,212,243,273,304,334 };
  1909.     char s[5];
  1910.     struct stat sb;
  1911. #ifdef BSD44
  1912.     struct timeval tp[2];
  1913. #else
  1914. #ifdef OS2
  1915.     struct utimbuf tp;
  1916. #ifdef __EMX__
  1917.     long timezone;
  1918.     struct timeb tbp;
  1919. #endif /* __EMX__ */
  1920. #else
  1921. #ifdef V7
  1922.     struct utimbuf {
  1923.       time_t timep[2];        /* New access and modificaton time */
  1924.     } tp;
  1925.     char *tz;
  1926.     long timezone;        /* In case timezone not defined in .h file */
  1927. #else
  1928. #ifdef SYSUTIMEH
  1929.     struct utimbuf tp;
  1930. #else
  1931.     struct utimbuf {
  1932.       time_t atime;        /* New access time */
  1933.       time_t mtime;        /* New modification time */
  1934.     } tp;
  1935. #endif /* SYSUTIMEH */
  1936. #endif /* V7 */
  1937. #endif /* OS2 */
  1938. #endif /* BSD44 */
  1939.  
  1940. #ifdef ANYBSD
  1941.     long timezone;
  1942.     static struct timeb tbp;
  1943. #endif /* ANYBSD */
  1944.  
  1945.     debug(F110,"zstime",f,0);
  1946.  
  1947.     if ((yy->date.len == 0)
  1948.         || (yy->date.len != 17)
  1949.         || (yy->date.val[8] != ' ')
  1950.         || (yy->date.val[11] != ':')
  1951.         || (yy->date.val[14] != ':') ) {
  1952.         debug(F111,"Bad creation date ",yy->date.val,yy->date.len);
  1953.         return(-1);
  1954.     }
  1955.     debug(F111,"zstime date check 1",yy->date.val,yy->date.len);
  1956.     for(i = 0; i < 8; i++) {
  1957.     if (!isdigit(yy->date.val[i])) {
  1958.         debug(F111,"Bad creation date ",yy->date.val,yy->date.len);
  1959.         return(-1);
  1960.     }
  1961.     }
  1962.     debug(F111,"zstime date check 2",yy->date.val,yy->date.len);
  1963.     i++;
  1964.  
  1965.     for (; i < 16; i += 3) {
  1966.     if ((!isdigit(yy->date.val[i])) || (!isdigit(yy->date.val[i + 1]))) {
  1967.         debug(F111,"Bad creation date ",yy->date.val,yy->date.len);
  1968.         return(-1);
  1969.     }
  1970.     }
  1971.     debug(F111,"zstime date check 3",yy->date.val,yy->date.len);
  1972.  
  1973. #ifdef ANYBSD
  1974.     debug(F100,"ztime BSD calling ftime","",0);
  1975.     ftime(&tbp);
  1976.     debug(F100,"ztime BSD back from ftime","",0);
  1977.     timezone = tbp.timezone * 60L;
  1978.     debug(F101,"ztime BSD timezone","",timezone);
  1979. #endif
  1980.  
  1981. #ifdef OS2
  1982. #ifdef __EMX__
  1983.     ftime(&tbp);
  1984.     timezone = tbp.timezone * 60L;
  1985. #endif /* __EMX__ */
  1986. #endif /* OS2 */
  1987.  
  1988. #ifdef ATTSV
  1989.     tzset();                /* Set `timezone'. */
  1990. #endif /* ATTSV */
  1991.  
  1992. #ifdef V7
  1993.     if ((tz = getenv("TZ")) == NULL)
  1994.       timezone = 0;        /* UTC/GMT */
  1995.     else
  1996.       timezone = atoi(&tz[3]);    /* Set 'timezone'. */
  1997.     timezone *= 60L;
  1998. #endif
  1999.  
  2000.     debug(F100,"zstime so far so good","",0);
  2001.  
  2002.     s[4] = '\0';
  2003.     for (i = 0; i < 4; i++)    /* Fix the year */
  2004.       s[i] = yy->date.val[i];
  2005.  
  2006.     debug(F110,"zstime year",s,0);
  2007.  
  2008.     n = atoi(s);
  2009.  
  2010.     debug(F111,"zstime year",s,n);
  2011.  
  2012. /* Previous year's leap days. This won't work after year 2100. */
  2013.  
  2014.     isleapyear = (( n % 4 == 0 && n % 100 !=0) || n % 400 == 0);
  2015.     days = (long) (n - 1970) * 365;
  2016.     days += (n - 1968 - 1) / 4 - (n - 1900 - 1) / 100 + (n - 1600 - 1) / 400;
  2017.  
  2018.     s[2] = '\0';
  2019.  
  2020.     for (i = 4 ; i < 16; i += 2) {
  2021.     s[0] = yy->date.val[i];
  2022.     s[1] = yy->date.val[i + 1];
  2023.     n = atoi(s);
  2024.     switch (i) {
  2025.       case 4:            /* MM: month */
  2026.         if ((n < 1 ) || ( n > 12)) {
  2027.         debug(F111,"zstime 4 bad date ",yy->date.val,yy->date.len);
  2028.         return(-1);
  2029.         }
  2030.         days += monthdays [n];
  2031.         if (isleapyear && n > 2)
  2032.           ++days;
  2033.         continue;
  2034.  
  2035.       case 6:            /* DD: day */
  2036.         if ((n < 1 ) || ( n > 31)) {
  2037.         debug(F111,"zstime 6 bad date ",yy->date.val,yy->date.len);
  2038.         return(-1);
  2039.         }
  2040.         tm = (days + n - 1) * 24L * 60L * 60L;
  2041.         i++;            /* Skip the space */
  2042.         continue;
  2043.  
  2044.       case 9:            /* hh: hour */
  2045.         if ((n < 0 ) || ( n > 23)) {
  2046.         debug(F111,"zstime 9 bad date ",yy->date.val,yy->date.len);
  2047.         return(-1);
  2048.         }
  2049.         tm += n * 60L * 60L;
  2050.         i++;            /* Skip the colon */
  2051.         continue;
  2052.  
  2053.       case 12:            /* mm: minute */
  2054.         if ((n < 0 ) || ( n > 59)) {
  2055.         debug(F111,"zstime 12 bad date ",yy->date.val,yy->date.len);
  2056.         return(-1);
  2057.         }
  2058. #ifdef ANYBSD                /* Correct for time zone */
  2059.         tm += timezone;
  2060. #else
  2061. #ifndef BSD44                /* For now... */
  2062. #ifdef ultrix
  2063.         tm += (long) timezone;
  2064. #else
  2065.         tm += timezone;
  2066. #endif /* ultrix */
  2067. #endif /* BSD44 */
  2068. #endif /* ANYBSD */
  2069.         tm += n * 60L;
  2070.         i++;            /* Skip the colon */
  2071.         continue;
  2072.  
  2073.       case 15:            /* ss: second */
  2074.         if ((n < 0 ) || ( n > 59)) {
  2075.         debug(F111,"zstime 15 bad date ",yy->date.val,yy->date.len);
  2076.         return(-1);
  2077.         }
  2078.         tm += n;
  2079.     }
  2080.     if (localtime(&tm)->tm_isdst)
  2081.       tm -= 60L * 60L;        /* Adjust for daylight savings time */
  2082.     }
  2083.     debug(F111,"A-pkt date ok ",yy->date.val,yy->date.len);
  2084.  
  2085.     if (stat(f,&sb)) {            /* Get the time for the file */
  2086.     debug(F110,"Can't stat file:",f,0);
  2087.     return(-1);
  2088.     }
  2089. #ifdef OS2
  2090.     tp.modtime = tm;            /* Set modif. time to creation date */
  2091.     tp.actime = sb.st_atime;        /* Don't change the access time */
  2092. #else
  2093. #ifdef SYSUTIMEH
  2094.     tp.modtime = tm;            /* Set modif. time to creation date */
  2095.     tp.actime = sb.st_atime;        /* Don't change the access time */
  2096. #else
  2097. #ifdef V7
  2098.     tp.timep[0] = tm;            /* Set modif. time to creation date */
  2099.     tp.timep[1] = sb.st_atime;        /* Don't change the access time */
  2100. #else
  2101. #ifdef BSD44
  2102.     tp[0].tv_sec = sb.st_atime;        /* Access time first */
  2103.     tp[1].tv_sec = tm;            /* Update time second */
  2104. #else
  2105.     tp.mtime = tm;            /* Set modif. time to creation date */
  2106.     tp.atime = sb.st_atime;        /* Don't change the access time */
  2107. #endif /* BSD44 */
  2108. #endif /* V7 */
  2109. #endif /* SYSUTIMEH */
  2110. #endif /* OS2 */
  2111.  
  2112.     switch (x) {            /* Execute desired function */
  2113.       case 0:                /* Set the creation date of the file */
  2114.     if (
  2115. #ifdef BSD44
  2116.         utimes(f,tp)
  2117. #else
  2118.         utime(f,&tp)
  2119. #endif /* BSD44 */
  2120.         ) {        /* Fix modification time */
  2121.         debug(F110,"Can't set modification time for file: ",f,0);
  2122.         r = -1;
  2123.     } else  {
  2124.         debug(F110,"Modification time is set for file: ",f,0);
  2125.         r = 0;
  2126.     }
  2127.     break;
  2128.       case 1:                /* Compare the dates */
  2129.     debug(F111,"zstime compare",f,sb.st_atime);
  2130.     debug(F111,"zstime compare","packet",tm);
  2131.     r = (sb.st_atime < tm) ? 0 : 1;
  2132.     break;
  2133.       default:                /* Error */
  2134.     r = -1;
  2135.     }
  2136. #endif /* TIMESTAMP */
  2137.     return(r);
  2138. }
  2139.  
  2140. /* Find initialization file. */
  2141.  
  2142. #ifdef NOTUSED
  2143. int
  2144. zkermini() {
  2145. /*  nothing here for Unix.  This function added for benefit of VMS Kermit.  */
  2146.     return(0);
  2147. }
  2148. #endif /* NOTUSED */
  2149.  
  2150. #ifndef NOFRILLS
  2151. int
  2152. zmail(p,f) char *p; char *f; {        /* Send file f as mail to address p */
  2153. /*
  2154.   Returns 0 on success
  2155.    2 if mail delivered but temp file can't be deleted
  2156.   -2 if mail can't be delivered
  2157.   The UNIX version always returns 0 because it can't get a good return
  2158.   code from zsyscmd.
  2159. */
  2160. #ifdef BSD4
  2161. /* The idea is to use /usr/ucb/mail, rather than regular mail, so that   */
  2162. /* a subject line can be included with -s.  Since we can't depend on the */
  2163. /* user's path, we use the convention that /usr/ucb/Mail = /usr/ucb/mail */
  2164. /* and even if Mail has been moved to somewhere else, this should still  */
  2165. /* find it...  The search could be made more reliable by actually using  */
  2166. /* access() to see if /usr/ucb/Mail exists. */
  2167.  
  2168. /* Should also make some check on zmbuf overflow... */
  2169.  
  2170. #ifdef DGUX540
  2171.     sprintf(zmbuf,"mailx -s %c%s%c %s < %s", '"', f, '"', p, f);
  2172. #else
  2173.     sprintf(zmbuf,"Mail -s %c%s%c %s < %s", '"', f, '"', p, f);
  2174. #endif /* DGUX540 */
  2175.     zsyscmd(zmbuf);
  2176. #else
  2177. #ifdef SVORPOSIX
  2178. #ifndef OXOS
  2179.     sprintf(zmbuf,"mail %s < %s", p, f);
  2180. #else /* OXOS */
  2181.     sprintf(zmbuf,"mailx -s %c%s%c %s < %s", '"', f, '"', p, f);
  2182. #endif /* OXOS */
  2183.     zsyscmd(zmbuf);
  2184. #else
  2185.     *zmbuf = '\0';
  2186. #endif
  2187. #endif
  2188.     return(0);
  2189. }
  2190. #endif /* NOFRILLS */
  2191.  
  2192. #ifndef NOFRILLS
  2193. int
  2194. zprint(p,f) char *p; char *f; {        /* Print file f with options p */
  2195.  
  2196. #ifdef OS2
  2197.     sprintf(zmbuf,"print %s %s", p, f); /* Construct print command */
  2198.     zsyscmd(zmbuf);
  2199. #else
  2200. #ifdef UNIX
  2201. #ifdef ANYBSD                /* BSD uses lpr to spool */
  2202. #ifdef DGUX540                /* And DG/UX */
  2203. #define SPOOLER "lp"
  2204. #else
  2205. #define SPOOLER "lpr"
  2206. #endif /* DGUX540 */
  2207. #else                    /* Sys V uses lp */
  2208. #ifdef TRS16                /* except for Tandy-16/6000... */
  2209. #define SPOOLER "lpr"
  2210. #else
  2211. #define SPOOLER "lp"
  2212. #endif
  2213. #endif
  2214. /*
  2215.   Note use of standard input redirection.  In some systems, lp[r] runs
  2216.   setuid to lp (or ...?), so if user has sent a file into a directory
  2217.   that lp does not have read access to, it can't be printed unless it is
  2218.   feed to lp[r] as standard input.
  2219. */
  2220.     sprintf(zmbuf,"%s %s < %s", SPOOLER, p, f); /* Construct print command */
  2221.     zsyscmd(zmbuf);
  2222. #else /* Not UNIX */
  2223.     *zmbuf = '\0';
  2224. #endif /* UNIX */
  2225. #endif /* OS2 */
  2226.     return(0);
  2227. }
  2228. #endif /* NOFRILLS */
  2229.  
  2230. /*
  2231.   Wildcard expansion functions.  C-Kermit used to insist on doing this itself
  2232.   New code (version 5A, 1990-91) gives user option to ask UNIX to do it.
  2233.   This lets users use the wildcard expansion features of their favorite shell.
  2234.   Operation is slower because of the forking & piping, but flexibility is
  2235.   greater and program is smaller.  For OS/2, C-Kermit still does this itself.
  2236. */
  2237. static char scratch[MAXPATH+4];        /* Used by both methods */
  2238.  
  2239. #ifndef OS2
  2240. static int oldmtchs = 0;        /* Let shell (ls) expand them. */
  2241. #ifdef COMMENT
  2242. static char *lscmd = "/bin/ls -d";     /* Command to use. */
  2243. #else
  2244. static char *lscmd = "echo";        /* Command to use. */
  2245. #endif /* COMMENT */
  2246.  
  2247. int
  2248. shxpand(pat,namlst,len) char *pat, *namlst[]; int len; {
  2249.     char *fgbuf = NULL;            /* Buffer for forming ls command */
  2250.     char *p, *q;            /* Workers */
  2251.     int i, x, retcode; char c;        /* ... */
  2252.  
  2253.     x = (int)strlen(pat) + (int)strlen(lscmd) + 3; /* Length of ls command */
  2254.     for (i = 0; i < oldmtchs; i++)    /* Free previous file list */
  2255.       free(namlst[i]);
  2256.     fgbuf = malloc(x);            /* Get buffer for command */
  2257.     if (!fgbuf) return(-1);        /* Fail if cannot */
  2258.     sprintf(fgbuf,"%s %s",lscmd,pat);    /* Form the command */
  2259.     zxcmd(ZIFILE,fgbuf);        /* Start the command */
  2260.     i = 0;                /* File counter */
  2261.     p = scratch;            /* Point to scratch area */
  2262.     retcode = -1;            /* Assume failure */
  2263.     while ((x = zminchar()) != -1) {    /* Read characters from command */
  2264.     c = (char) x;
  2265.     if (c == ' ' || c == '\n') {    /* Got newline or space? */
  2266.         *p = '\0';            /* Yes, terminate string */
  2267.         p = scratch;        /* Point back to beginning */
  2268.         if (zchki(p) == -1)        /* Does file exist? */
  2269.           continue;            /* No, continue */
  2270.         x = (int)strlen(p);        /* Yes, get length of name */
  2271.         q = malloc(x+1);        /* Allocate space for it */
  2272.         if (!q) goto shxfin;    /* Fail if space can't be obtained */
  2273.         strcpy(q,scratch);        /* Copy name to space */
  2274.         namlst[i++] = q;        /* Copy pointer to name into array */
  2275.         if (i > len) goto shxfin;    /* Fail if too many */
  2276.     } else {            /* Regular character */
  2277.         *p++ = c;            /* Copy it into scratch area */
  2278.     }
  2279.     }
  2280.     retcode = i;            /* Return number of matching files */
  2281. shxfin:                    /* Common exit point */
  2282.     free(fgbuf);            /* Free command buffer */
  2283.     zclosf(ZIFILE);            /* Delete the command fork. */
  2284.     oldmtchs = i;            /* Remember how many files */
  2285.     return(retcode);
  2286. }
  2287. #endif /* OS2 */
  2288.  
  2289. /* Directory Functions for Unix, written by Jeff Damens, CUCCA, 1984. */
  2290.  
  2291. /* Define the size of the string space for filename expansion. */
  2292.  
  2293. #ifndef DYNAMIC
  2294. #ifdef PROVX1
  2295. #define SSPACE 500
  2296. #else
  2297. #ifdef BSD29
  2298. #define SSPACE 500
  2299. #else
  2300. #ifdef pdp11
  2301. #define SSPACE 500
  2302. #else
  2303. #ifdef aegis
  2304. #define SSPACE 10000            /* size of string-generating buffer */
  2305. static char bslash;            /* backslash character if active */
  2306. #else                    /* Default static buffer size */
  2307. #define SSPACE 2000            /* size of string-generating buffer */
  2308. #endif /* aegis */
  2309. #endif /* pdp11 */
  2310. #endif /* BSD29 */
  2311. #endif /* PROVX1 */
  2312. static char sspace[SSPACE];             /* buffer for generating filenames */
  2313. #else /* DYNAMIC */
  2314. #define SSPACE 10000
  2315. static char *sspace = (char *)0;
  2316. #endif /* DYNAMIC */
  2317. static int ssplen = SSPACE;        /* length of string space buffer */
  2318.  
  2319. static char *freeptr,**resptr;             /* copies of caller's arguments */
  2320. static int remlen;                      /* remaining length in caller's array*/
  2321. static int numfnd;                      /* number of matches found */
  2322.  
  2323. /*
  2324.  * splitpath:
  2325.  *  takes a string and splits the slash-separated portions into
  2326.  *  a list of path structures.  Returns the head of the list.  The
  2327.  *  structures are allocated by malloc, so they must be freed.
  2328.  *  Splitpath is used internally by the filename generator.
  2329.  *
  2330.  * Input: A string.
  2331.  * Returns: A linked list of the slash-separated segments of the input.
  2332.  */
  2333.  
  2334. struct path *
  2335. splitpath(p) char *p; {
  2336.     struct path *head,*cur,*prv;
  2337.     int i;
  2338.  
  2339.     debug(F110,"splitpath",p,0);
  2340.  
  2341.     head = prv = NULL;
  2342.     if (ISDIRSEP(*p)) p++;            /* skip leading slash */
  2343.     while (*p != '\0') {
  2344.     cur = (struct path *) malloc(sizeof (struct path));
  2345.     debug(F101,"splitpath malloc","",cur);
  2346.     if (cur == NULL) {
  2347.         debug(F100,"splitpath malloc failure","",0);
  2348.         return((struct path *)NULL);
  2349.     }
  2350.     cur -> fwd = NULL;
  2351.     if (head == NULL)
  2352.       head = cur;
  2353.     else
  2354.       prv -> fwd = cur;        /* link into chain */
  2355.     prv = cur;
  2356. #ifdef aegis
  2357.     /* treat backslash as "../" */
  2358.     if (bslash && *p == bslash) {
  2359.         strcpy(cur->npart, "..");
  2360.         ++p;
  2361.     } else {
  2362.         for (i=0; i < MAXNAMLEN && *p && *p != '/' && *p != bslash; i++)
  2363.           cur -> npart[i] = *p++;
  2364.         cur -> npart[i] = '\0';    /* end this segment */
  2365.         if (i >= MAXNAMLEN)
  2366.           while (*p && *p != '/' && *p != bslash)
  2367.         p++;
  2368.     }
  2369.     if (*p == '/') p++;
  2370. #else
  2371. #ifdef OS2
  2372.     for (i = 0;
  2373.          i < MAXNAMLEN && !ISDIRSEP(*p) && *p != ':' && *p != '\0';
  2374.          i++ )
  2375.         cur -> npart[i] = *p++;
  2376.         if ( *p == ':' ) {
  2377.             cur -> npart[i++] = *p++;
  2378.             if ( !ISDIRSEP(*p) )
  2379.                 cur -> npart[i++] = '.';
  2380.         }
  2381. #else
  2382.     for (i=0; i < MAXNAMLEN && !ISDIRSEP(*p) && *p != '\0'; i++) {
  2383.         cur -> npart[i] = *p++;
  2384.     }
  2385. #endif /* OS2 */
  2386.     cur -> npart[i] = '\0';        /* end this segment */
  2387.     if (i >= MAXNAMLEN)
  2388.       while (!ISDIRSEP(*p) && *p != '\0') p++;
  2389.     if (ISDIRSEP(*p))
  2390.       p++;
  2391.  
  2392. #endif /* aegis */
  2393.     }
  2394.     return(head);
  2395. }
  2396.  
  2397. /*
  2398.  * fgen:
  2399.  *  This is the actual name generator.  It is passed a string,
  2400.  *  possibly containing wildcards, and an array of character pointers.
  2401.  *  It finds all the matching filenames and stores them into the array.
  2402.  *  The returned strings are allocated from a static buffer local to
  2403.  *  this module (so the caller doesn't have to worry about deallocating
  2404.  *  them); this means that successive calls to fgen will wipe out
  2405.  *  the results of previous calls.  This isn't a problem here
  2406.  *  because we process one wildcard string at a time.
  2407.  *
  2408.  * Input: a wildcard string, an array to write names to, the
  2409.  *        length of the array.
  2410.  * Returns: the number of matches.  The array is filled with filenames
  2411.  *          that matched the pattern.  If there wasn't enough room in the
  2412.  *        array, -1 is returned.
  2413.  * Originally by: Jeff Damens, CUCCA, 1984.  Many changes since then.
  2414.  */
  2415. static int
  2416. fgen(pat,resarry,len) char *pat,*resarry[]; int len; {
  2417.     struct path *head;
  2418.     char *sptr;
  2419. #ifdef aegis
  2420.     char *namechars;
  2421.     int tilde = 0, bquote = 0;
  2422.  
  2423.     if ((namechars = getenv("NAMECHARS")) != NULL) {
  2424.     if (xindex(namechars, '~' ) != NULL) tilde  = '~';
  2425.     if (xindex(namechars, '\\') != NULL) bslash = '\\';
  2426.     if (xindex(namechars, '`' ) != NULL) bquote = '`';
  2427.     } else {
  2428.     tilde = '~'; bslash = '\\'; bquote = '`';
  2429.     }
  2430.  
  2431.     sptr = scratch;
  2432.  
  2433.     /* copy "`node_data", etc. anchors */
  2434.     if (bquote && *pat == bquote)
  2435.       while (*pat && *pat != '/' && *pat != bslash)
  2436.     *sptr++ = *pat++;
  2437.     else if (tilde && *pat == tilde)
  2438.       *sptr++ = *pat++;
  2439.     while (*pat == '/')
  2440.       *sptr++ = *pat++;
  2441.     if (sptr == scratch) {
  2442.     strcpy(scratch,"./");
  2443.     sptr = scratch+2;
  2444.     }                    /* init buffer correctly */
  2445.     if (!(head = splitpath(pat))) return(-1);
  2446. #else /* not aegis */
  2447.     debug(F110,"fgen pat",pat,0);
  2448.     if (!(head = splitpath(pat))) return(-1);
  2449.     sptr = scratch;
  2450.     if (!ISDIRSEP(*pat))
  2451.     *sptr++ = '.';                  /* init buffer correctly */
  2452.     *sptr++ = DIRSEP;
  2453. #ifdef OS2
  2454.     if (isalpha(pat[0]) && pat[1] == ':')
  2455.         sptr = scratch;                 /* reset in case of leading drive: */
  2456. #endif /* OS2 */
  2457. #endif /* aegis */
  2458.     numfnd = 0;                /* none found yet */
  2459. #ifdef DYNAMIC
  2460.     if (!sspace) {            /* Need to allocate string space? */
  2461.     while (ssplen > 50) {
  2462.         if ((sspace = malloc(ssplen+2))) { /* Got it. */
  2463.         debug(F101,"fgen string space","",ssplen);
  2464.         break;
  2465.         }
  2466.         ssplen = (ssplen / 2) + (ssplen / 4); /* Didn't, reduce by 3/4 */
  2467.     }
  2468.     if (ssplen <= 50) {        /* Did we get it? */
  2469.         fprintf(stderr,"fgen can't malloc string space\n");
  2470.         return(-1);
  2471.     }
  2472.     }
  2473. #endif /* DYNAMIC */
  2474.     freeptr = sspace;            /* this is where matches are copied */
  2475.     resptr = resarry;            /* static copies of these so */
  2476.     remlen = len;            /* recursive calls can alter them */
  2477.     traverse(head,scratch,sptr);    /* go walk the directory tree */
  2478. #ifdef COMMENT
  2479. /*
  2480.   This code, circa 1984, has never worked right - it references the head
  2481.   pointer after it has already been freed.  Lord knows what might have been
  2482.   happening because of this.  Thanks to Steve Walton for finding & fixing
  2483.   this bug.
  2484. */
  2485.     for (; head != NULL; head = head -> fwd)
  2486.       free(head);            /* return the path segments */
  2487. #else
  2488.     while (head != NULL) {
  2489.     struct path *next = head -> fwd;
  2490.     free(head);
  2491.     head = next;
  2492.     }
  2493. #endif /* COMMENT */
  2494.     return(numfnd);            /* and return the number of matches */
  2495. }
  2496.  
  2497. /* traverse:
  2498.  *  Walks the directory tree looking for matches to its arguments.
  2499.  *  The algorithm is, briefly:
  2500.  *   If the current pattern segment contains no wildcards, that
  2501.  *   segment is added to what we already have.  If the name so far
  2502.  *   exists, we call ourselves recursively with the next segment
  2503.  *   in the pattern string; otherwise, we just return.
  2504.  *
  2505.  *   If the current pattern segment contains wildcards, we open the name
  2506.  *   we've accumulated so far (assuming it is really a directory), then read
  2507.  *   each filename in it, and, if it matches the wildcard pattern segment, add
  2508.  *   that filename to what we have so far and call ourselves recursively on the
  2509.  *   next segment.
  2510.  *
  2511.  *   Finally, when no more pattern segments remain, we add what's accumulated
  2512.  *   so far to the result array and increment the number of matches.
  2513.  *
  2514.  * Input: a pattern path list (as generated by splitpath), a string
  2515.  *      pointer that points to what we've traversed so far (this
  2516.  *      can be initialized to "/" to start the search at the root
  2517.  *      directory, or to "./" to start the search at the current
  2518.  *      directory), and a string pointer to the end of the string
  2519.  *      in the previous argument.
  2520.  * Returns: nothing.
  2521.  */
  2522. static VOID
  2523. traverse(pl,sofar,endcur) struct path *pl; char *sofar, *endcur; {
  2524.  
  2525. /* Define LONGFN (long file names) automatically for BSD 2.9 and 4.2 */
  2526. /* LONGFN can also be defined on the cc command line. */
  2527.  
  2528. #ifdef BSD29
  2529. #ifndef LONGFN
  2530. #define LONGFN
  2531. #endif
  2532. #endif
  2533.  
  2534. #ifdef BSD42
  2535. #ifndef LONGFN
  2536. #define LONGFN
  2537. #endif
  2538. #endif
  2539.  
  2540. /* Appropriate declarations for directory routines and structures */
  2541. /* #define OPENDIR means to use opendir(), readdir(), closedir()  */
  2542. /* If OPENDIR not defined, we use open(), read(), close() */
  2543.  
  2544. #ifdef DIRENT                /* New way, <dirent.h> */
  2545. #define OPENDIR
  2546.     DIR *fd, *opendir();
  2547.     struct dirent *dirbuf;
  2548.     struct dirent *readdir();
  2549. #else /* !DIRENT */
  2550. #ifdef LONGFN                /* Old way, <dir.h> with opendir() */
  2551. #define OPENDIR
  2552.     DIR *fd, *opendir();
  2553.     struct direct *dirbuf;
  2554. #else /* !LONGFN */
  2555.     int fd;                /* Old way, <dir.h> with open() */
  2556.     struct direct dir_entry;
  2557.     struct direct *dirbuf = &dir_entry;
  2558. #endif /* LONGFN */
  2559. #endif /* DIRENT */
  2560.  
  2561.     struct stat statbuf;        /* for file info */
  2562.  
  2563.     if (pl == NULL) {
  2564.     *--endcur = '\0';        /* end string, overwrite trailing / */
  2565.     addresult(sofar);
  2566.     return;
  2567.     }
  2568.     if (!iswild(pl -> npart)) {
  2569.     strcpy(endcur,pl -> npart);
  2570.     endcur += (int)strlen(pl -> npart);
  2571.     *endcur = '\0';            /* end current string */
  2572.     if (stat(sofar,&statbuf) == 0) { /* if current piece exists */
  2573. #ifdef OS2
  2574.         if (endcur - sofar == 3 && endcur[-1] == '.' && endcur[-2] == ':')
  2575.           endcur--;
  2576.         else
  2577. #endif /* OS2 */
  2578.           *endcur++ = DIRSEP;    /* add slash to end */
  2579.         *endcur = '\0';        /* and end the string */
  2580.         traverse(pl -> fwd,sofar,endcur);
  2581.     }
  2582.     return;
  2583.     }
  2584.  
  2585.     /* Segment contains wildcards, have to search directory */
  2586.  
  2587.     *endcur = '\0';                            /* end current string */
  2588.     if (stat(sofar,&statbuf) == -1) return;       /* doesn't exist, forget it */
  2589.     if (!S_ISDIR (statbuf.st_mode)) return;     /* not a directory, skip */
  2590.  
  2591. #ifdef OPENDIR
  2592.     if ((fd = opendir(sofar)) == NULL) return; /* Can't open, fail. */
  2593.     while (dirbuf = readdir(fd))
  2594. #else /* !OPENDIR */
  2595.     if ((fd = open(sofar,O_RDONLY)) < 0) return; /* Can't open, fail. */
  2596.     while (read(fd, (char *)dirbuf, sizeof dir_entry))
  2597. #endif /* OPENDIR */
  2598.       {
  2599.       /* Get null-terminated copy!!! */
  2600.       strncpy(nambuf,dirbuf->d_name,MAXNAMLEN);
  2601.       nambuf[MAXNAMLEN] = '\0';
  2602. #ifdef unos
  2603.       if (dirbuf->d_ino != -1 && match(pl -> npart,nambuf))
  2604. #else
  2605. /* #ifdef _POSIX_SOURCE */
  2606. /*
  2607.   Directory reading is not specified in POSIX.1.  POSIX.2 gives us glob() and
  2608.   fnmatch(), which are not yet supported by C-Kermit.  Meanwhile, maybe POSIX
  2609.   implementations should force "set wildcard shell" and remove all of this
  2610.   code.
  2611. */
  2612. #ifdef QNX
  2613.       if (dirbuf->d_stat.st_ino != 0 && match(pl -> npart,nambuf))
  2614. #else
  2615. #ifdef SOLARIS
  2616.       if (dirbuf->d_ino != 0 && match(pl -> npart,nambuf))
  2617. #else
  2618. #ifdef sun
  2619.       if (dirbuf->d_fileno != 0 && match(pl -> npart,nambuf))
  2620. #else
  2621. #ifdef bsdi
  2622.       if (dirbuf->d_fileno != 0 && match(pl -> npart,nambuf))
  2623. #else
  2624. #ifdef __386BSD__
  2625.       if (dirbuf->d_fileno != 0 && match(pl -> npart,nambuf))
  2626. #else
  2627. #ifdef ultrix
  2628.       if (dirbuf->gd_ino != 0 && match(pl -> npart,nambuf))
  2629. #else
  2630.       if (dirbuf->d_ino != 0 && match(pl -> npart,nambuf))
  2631. #endif /* ultrix */
  2632. #endif /* __386BSD__ */
  2633. #endif /* bsdi */
  2634. #endif /* sun */
  2635. #endif /* SOLARIS */
  2636. #endif /* QNX */
  2637.  
  2638. /* #else */ /* not _POSIX_SOURCE */
  2639. /*      if (dirbuf->d_ino != 0 && match(pl -> npart,nambuf)) */
  2640. /* #endif */ /* _POSIX_SOURCE */
  2641.  
  2642. #endif /* unos */
  2643.       {
  2644.           char *eos;
  2645.           strcpy(endcur,nambuf);
  2646.           eos = endcur + (int)strlen(nambuf);
  2647.           *eos++ = DIRSEP;        /* end this segment */
  2648.           traverse(pl -> fwd,sofar,eos);
  2649.       }
  2650.       }
  2651. #ifdef OPENDIR
  2652.     closedir(fd);
  2653. #else /* !OPENDIR */
  2654.     close(fd);
  2655. #endif /* OPENDIR */
  2656. }
  2657.  
  2658. /*
  2659.  * addresult:
  2660.  *  Adds a result string to the result array.  Increments the number
  2661.  *  of matches found, copies the found string into our string
  2662.  *  buffer, and puts a pointer to the buffer into the caller's result
  2663.  *  array.  Our free buffer pointer is updated.  If there is no
  2664.  *  more room in the caller's array, the number of matches is set to -1.
  2665.  * Input: a result string.
  2666.  * Returns: nothing.
  2667.  */
  2668. static VOID
  2669. addresult(str) char *str; {
  2670.     int l;
  2671.     debug(F111,"addresult",str,remlen);
  2672.     if (str[0] == '.' && ISDIRSEP(str[1])) str += 2; /* (===OS2 change===) */
  2673.     if (--remlen < 0) {
  2674.     numfnd = -1;
  2675.     return;
  2676.     }
  2677.     l = (int)strlen(str) + 1;        /* size this will take up */
  2678.     if ((freeptr + l) > (sspace + ssplen)) {
  2679.     numfnd = -1;            /* do not record if not enough space */
  2680.     return;
  2681.     }
  2682.     strcpy(freeptr,str);
  2683.     *resptr++ = freeptr;
  2684.     freeptr += l;
  2685.     numfnd++;
  2686. }
  2687.  
  2688. /*
  2689.  * match:
  2690.  *  pattern matcher.  Takes a string and a pattern possibly containing
  2691.  *  the wildcard characters '*' and '?'.  Returns true if the pattern
  2692.  *  matches the string, false otherwise.
  2693.  * by: Jeff Damens, CUCCA, 1984
  2694.  * skipping over dot files and backslash quoting added by fdc, 1990.
  2695.  *
  2696.  * Input: a string and a wildcard pattern.
  2697.  * Returns: 1 if match, 0 if no match.
  2698.  */
  2699. static int
  2700. match(pattern,string) char *pattern,*string; {
  2701.     char *psave,*ssave;            /* back up pointers for failure */
  2702.     int q = 0;                /* quote flag */
  2703.  
  2704.     debug(F110,"match str",string,0);
  2705.     psave = ssave = NULL;
  2706. #ifndef MATCHDOT
  2707.     if (*string == '.' && *pattern != '.') {
  2708.     debug(F110,"match skip",string,0);
  2709.     return(0);
  2710.     }
  2711. #endif
  2712.     while (1) {
  2713.     for (; *pattern == *string; pattern++,string++)  /* skip first */
  2714.         if (*string == '\0') return(1);    /* end of strings, succeed */
  2715.  
  2716.     if (*pattern == '\\' && q == 0) { /* Watch out for quoted */
  2717.         q = 1;            /* metacharacters */
  2718.         pattern++;            /* advance past quote */
  2719.         if (*pattern != *string) return(0);
  2720.         continue;
  2721.     } else q = 0;
  2722.  
  2723.     if (q) {
  2724.         return(0);
  2725.     } else {
  2726.         if (*string != '\0' && *pattern == '?') {
  2727.         pattern++;        /* '?', let it match */
  2728.         string++;
  2729.         } else if (*pattern == '*') { /* '*' ... */
  2730.         psave = ++pattern;    /* remember where we saw it */
  2731.         ssave = string;        /* let it match 0 chars */
  2732.         } else if (ssave != NULL && *ssave != '\0') { /* if not at end  */
  2733.                     /* ...have seen a star */
  2734.         string = ++ssave;    /* skip 1 char from string */
  2735.         pattern = psave;    /* and back up pattern */
  2736.         } else return(0);        /* otherwise just fail */
  2737.     }
  2738.     }
  2739. }
  2740.  
  2741. /*
  2742.   The following two functions are for expanding tilde in filenames
  2743.   Contributed by Howie Kaye, CUCCA, developed for CCMD package.
  2744. */
  2745.  
  2746. /*  W H O A M I  --  Get user's username.  */
  2747.  
  2748. /*
  2749.   1) Get real uid
  2750.   2) See if the $USER environment variable is set ($LOGNAME on AT&T)
  2751.   3) If $USER's uid is the same as ruid, realname is $USER
  2752.   4) Otherwise get logged in user's name
  2753.   5) If that name has the same uid as the real uid realname is loginname
  2754.   6) Otherwise, get a name for ruid from /etc/passwd
  2755. */
  2756. static char *
  2757. whoami () {
  2758. #ifdef DTILDE
  2759. #ifdef pdp11
  2760. #define WHOLEN 100
  2761. #else
  2762. #define WHOLEN 257
  2763. #endif /* pdp11 */
  2764.     static char realname[256];        /* user's name */
  2765.     static int ruid = -1;        /* user's real uid */
  2766.     char loginname[256], envname[256];    /* temp storage */
  2767.     char *c;
  2768.     struct passwd *p;
  2769.     _PROTOTYP(extern char * getlogin, (void) );
  2770.  
  2771.     if (ruid != -1)
  2772.       return(realname);
  2773.  
  2774.     ruid = real_uid();            /* get our uid */
  2775.  
  2776.   /* how about $USER or $LOGNAME? */
  2777.     if ((c = getenv(NAMEENV)) != NULL) { /* check the env variable */
  2778.     strcpy (envname, c);
  2779.     if ((p = getpwnam(envname)) != NULL) {
  2780.         if (p->pw_uid == ruid) {    /* get passwd entry for envname */
  2781.         strcpy (realname, envname); /* if the uid's are the same */
  2782.         return(realname);
  2783.         }
  2784.     }
  2785.     }
  2786.  
  2787.   /* can we use loginname() ? */
  2788.  
  2789.     if ((c =  getlogin()) != NULL) {    /* name from utmp file */
  2790.     strcpy (loginname, c);
  2791.     if ((p = getpwnam(loginname)) != NULL) /* get passwd entry */
  2792.       if (p->pw_uid == ruid) {    /* for loginname */
  2793.           strcpy (realname, loginname); /* if the uid's are the same */
  2794.           return(realname);
  2795.       }
  2796.     }
  2797.  
  2798.   /* Use first name we get for ruid */
  2799.  
  2800.     if ((p = getpwuid(ruid)) == NULL) { /* name for uid */
  2801.     realname[0] = '\0';        /* no user name */
  2802.     ruid = -1;
  2803.     return(NULL);
  2804.     }
  2805.     strcpy (realname, p->pw_name);
  2806.     return(realname);
  2807. #else
  2808.     return(NULL);
  2809. #endif /* DTILDE */
  2810. }
  2811.  
  2812. /*  T I L D E _ E X P A N D  --  expand ~user to the user's home directory. */
  2813.  
  2814. char *
  2815. tilde_expand(dirname) char *dirname; {
  2816. #ifdef DTILDE
  2817. #ifdef pdp11
  2818. #define BUFLEN 100
  2819. #else
  2820. #define BUFLEN 257
  2821. #endif /* pdp11 */
  2822.     struct passwd *user;
  2823.     static char olddir[BUFLEN];
  2824.     static char oldrealdir[BUFLEN];
  2825.     static char temp[BUFLEN];
  2826.     int i, j;
  2827.  
  2828.     debug(F111,"tilde_expand",dirname,dirname[0]);
  2829.  
  2830.     if (dirname[0] != '~')        /* Not a tilde...return param */
  2831.       return(dirname);
  2832.     if (!strcmp(olddir,dirname)) {    /* Same as last time */
  2833.       return(oldrealdir);        /* so return old answer. */
  2834.     } else {
  2835.     j = (int)strlen(dirname);
  2836.     for (i = 0; i < j; i++)        /* find username part of string */
  2837.       if (!ISDIRSEP(dirname[i]))
  2838.         temp[i] = dirname[i];
  2839.       else break;
  2840.     temp[i] = '\0';            /* tie off with a NULL */
  2841.     if (i == 1) {            /* if just a "~" */
  2842.         user = getpwnam(whoami());    /*  get info on current user */
  2843.     } else {
  2844.         user = getpwnam(&temp[1]);    /* otherwise on the specified user */
  2845.     }
  2846.     }
  2847.     if (user != NULL) {            /* valid user? */
  2848.     strcpy(olddir, dirname);    /* remember the directory */
  2849.     strcpy(oldrealdir,user->pw_dir); /* and their home directory */
  2850.     strcat(oldrealdir,&dirname[i]);
  2851.     return(oldrealdir);
  2852.     } else {                /* invalid? */
  2853.     strcpy(olddir, dirname);    /* remember for next time */
  2854.     strcpy(oldrealdir, dirname);
  2855.     return(oldrealdir);
  2856.     }
  2857. #else
  2858.     return(NULL);
  2859. #endif /* DTILDE */
  2860. }
  2861.  
  2862. /*
  2863.   Functions for executing system commands.
  2864.   zsyscmd() executes the system command in the normal, default way for
  2865.   the system.  In UNIX, it does what system() does.  Thus, its results
  2866.   are always predictable.
  2867.   zshcmd() executes the command using the user's preferred shell.
  2868. */
  2869. int
  2870. zsyscmd(s) char *s; {
  2871. #ifdef OS2
  2872.     if (!priv_chk()) system(s);
  2873. #else
  2874.     PID_T shpid;
  2875. #ifdef COMMENT
  2876. /* This doesn't work... */
  2877.     WAIT_T status;
  2878. #else
  2879.     int status;
  2880. #endif /* COMMENT */
  2881.  
  2882.     if (shpid = fork()) {
  2883.     if (shpid < (PID_T)0) return(-1); /* Parent */
  2884.     while (shpid != (PID_T) wait(&status))
  2885.       ;
  2886.     return(status);
  2887.     }
  2888.     if (priv_can()) {            /* Child: cancel any priv's */
  2889.     printf("?Privilege cancellation failure\n");
  2890.     _exit(255);
  2891.     }
  2892.     execl("/bin/sh","sh","-c",s,NULL);
  2893.     perror("/bin/sh");
  2894.     _exit(255);
  2895.     return(0);                /* Shut up ANSI compilers. */
  2896. #endif /* OS2 */
  2897. }
  2898.  
  2899. /*
  2900.   UNIX code by H. Fischer; copyright rights assigned to Columbia Univ.
  2901.   Adapted to use getpwuid to find login shell because many systems do not
  2902.   have SHELL in environment, and to use direct calling of shell rather
  2903.   than intermediate system() call. -- H. Fischer
  2904.   Call with s pointing to command to execute.
  2905. */
  2906.  
  2907. int
  2908. zshcmd(s) char *s; {
  2909.     PID_T pid;
  2910.  
  2911. #ifdef OS2
  2912.     char *shell = getenv("COMSPEC");
  2913.     if (!priv_chk())
  2914.       if (*s == '\0')
  2915.         spawnl(P_WAIT, shell, shell, NULL);
  2916.       else
  2917.         system(s);
  2918. #else
  2919. #ifdef AMIGA
  2920.     if (!priv_chk()) system(s);
  2921. #else
  2922. #ifdef datageneral
  2923.     if (priv_chk) return(1);
  2924.     if (*s == '\0')            /* Interactive shell requested? */
  2925. #ifdef mvux
  2926.     system("/bin/sh ");
  2927. #else
  2928.         system("x :cli prefix Kermit_Baby:");
  2929. #endif /* mvux */
  2930.     else                /* Otherwise, */
  2931.         system(s);            /* Best for aos/vs?? */
  2932.  
  2933. #else
  2934. #ifdef aegis
  2935.     if ((pid = vfork()) == 0) {        /* Make child quickly */
  2936.     char *shpath, *shname, *shptr;    /* For finding desired shell */
  2937.  
  2938.     if (priv_can()) exit(1);    /* Turn off privs. */
  2939.         if ((shpath = getenv("SHELL")) == NULL) shpath = "/com/sh";
  2940.  
  2941. #else                    /* All Unix systems */
  2942.     if ((pid = fork()) == 0) {        /* Make child */
  2943.     char *shpath, *shname, *shptr;    /* For finding desired shell */
  2944.     struct passwd *p;
  2945.     char *defshell = "/bin/sh";    /* Default */
  2946.  
  2947.     if (priv_can()) exit(1);    /* Turn off privs. */
  2948.     p = getpwuid(real_uid());    /* Get login data */
  2949.     if (p == (struct passwd *) NULL || !*(p->pw_shell))
  2950.       shpath = defshell;
  2951.     else
  2952.       shpath = p->pw_shell;
  2953. #endif /* aegis */
  2954.     shptr = shname = shpath;
  2955.     while (*shptr != '\0')
  2956.       if (*shptr++ == DIRSEP)
  2957.         shname = shptr;
  2958.     if (s == NULL || *s == '\0') {    /* Interactive shell requested? */
  2959.         execl(shpath,shname,"-i",NULL); /* Yes, do that */
  2960.     } else {            /* Otherwise, */
  2961.         execl(shpath,shname,"-c",s,NULL); /* exec the given command */
  2962.     }                /* If execl() failed, */
  2963.     exit(BAD_EXIT);            /* return bad return code. */
  2964.  
  2965.     } else {                /* Parent */
  2966.  
  2967.         int wstat;            /* ... must wait for child */
  2968.     SIGTYP (*istat)(), (*qstat)();
  2969.  
  2970.     if (pid == (PID_T) -1) return(0); /* fork() failed? */
  2971.  
  2972.     istat = signal(SIGINT,SIG_IGN);    /* Let the fork handle keyboard */
  2973.     qstat = signal(SIGQUIT,SIG_IGN); /* interrupts itself... */
  2974.  
  2975.         while (((wstat = wait((WAIT_T *)0)) != pid) && (wstat != -1))
  2976.       ;                /* Wait for fork */
  2977.     signal(SIGINT,istat);        /* Restore interrupts */
  2978.     signal(SIGQUIT,qstat);
  2979.     }
  2980. #endif
  2981. #endif
  2982. #endif
  2983.     return(1);
  2984. }
  2985.  
  2986. #ifdef aegis
  2987. /*
  2988.  Replacement for strchr() and index(), neither of which seem to be universal.
  2989. */
  2990.  
  2991. static char *
  2992. #ifdef CK_ANSIC
  2993. xindex(char * s, char c)
  2994. #else
  2995. xindex(s,c) char *s, c;
  2996. #endif /* CK_ANSIC */
  2997. /* xindex */ {
  2998.     while (*s != '\0' && *s != c) s++;
  2999.     if (*s == c) return(s); else return(NULL);
  3000. }
  3001. #endif /* aegis */
  3002.  
  3003. /*  I S W I L D  --  Check if filespec is "wild"  */
  3004.  
  3005. /*
  3006.   Returns 0 if it is a single file, 1 if it contains wildcard characters.
  3007.   Note: must match the algorithm used by match(), hence no [a-z], etc.
  3008. */
  3009. int
  3010. iswild(filespec) char *filespec; {
  3011.     char c; int x; char *p;
  3012.     if (wildxpand) {
  3013.     if ((x = zxpand(filespec)) > 1) return(1);
  3014.     if (x == 0) return(0);        /* File does not exist */
  3015.     p = malloc(MAXNAMLEN + 20);
  3016.     znext(p);
  3017.     x = (strcmp(filespec,p) != 0);
  3018.     free(p);
  3019.     return(x);
  3020.     } else {
  3021.     while ((c = *filespec++) != '\0')
  3022.       if (c == '*' || c == '?') return(1);
  3023.     return(0);
  3024.     }
  3025. }
  3026.  
  3027. #ifdef OS2
  3028.  
  3029. /*  Z C H D S K  --  Change currently selected disk device */
  3030.  
  3031. /* Returns -1 if error, otherwise 0 */
  3032.  
  3033. zchdsk(c) int c; {
  3034.     int i = toupper(c) - 64;
  3035.     return( _chdrive(i));
  3036. }
  3037.  
  3038. #undef stat
  3039. #ifdef __IBMC__STAT
  3040. #define stat(p, s) _stat(p, s)
  3041. #endif /* __IBMC__STAT */
  3042.  
  3043. os2stat(char *path, struct stat *st) {
  3044.     char local[MAXPATHLEN];
  3045.     int len;
  3046.  
  3047.     strcpy(local, path);
  3048.     len = strlen(local);
  3049.  
  3050.     if ( len == 2 && local[1] == ':' )
  3051.         local[2] = DIRSEP, local[3] = 0; /* if drive only, append / */
  3052.     else if ( len == 0 )
  3053.         local[0] = DIRSEP, local[1] = 0; /* if empty path, take / instead */
  3054.     else if ( len > 1 && ISDIRSEP(local[len - 1]) && local[len - 2] != ':' )
  3055.         local[len - 1] = 0; /* strip trailing / except after d: */
  3056.  
  3057.     return stat(local, st);
  3058. }
  3059.  
  3060. #endif /* OS2 */
  3061.