home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit5a190 / ckifio.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  39KB  |  1,324 lines

  1. char *ckzv = "Amiga file support $Id: ckifio.c,v 1.9 94/07/26 16:38:59 swalton Exp Locker: swalton $";
  2.  
  3. /* C K I F I O  --  Kermit file system support for the Amiga */
  4.  
  5. /*
  6.   Author: Frank da Cruz (SY.FDC@CU20B),
  7.   Columbia University Center for Computing Activities, January 1985.
  8.   Modified for Amiga by Jack J. Rouse, The Software Distillery
  9.   Further modified for C Kermit version 4F(095) by Stephen Walton,
  10.   California State University, Northridge, ecphssrw@afws.csun.edu
  11.  
  12.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  13.   York.  Permission is granted to any individual or institution to use this
  14.   software as long as it is not sold for profit.  This copyright notice must be
  15.   retained.  This software may not be included in commercial products without
  16.   written permission of Columbia University.
  17.  
  18.  $Log:    ckifio.c,v $
  19.  * Revision 1.9  94/07/26  16:38:59  swalton
  20.  * Added zfseek() to support RESEND.  Changed spelling of item in file type
  21.  * buffer from passwd to password.
  22.  * 
  23.  * Revision 1.8  93/08/03  08:28:44  swalton
  24.  * Changed include of SAS and Aztec-specific include files to Amiga
  25.  * standard (clib/ and pragmas/).
  26.  * 
  27.  * Revision 1.7  92/10/30  16:11:42  swalton
  28.  * Added code to call SetCurrentDirName() if and only if ROM version
  29.  * is greater than 37.  Depends on the extern int v37, set in ckitio.c.
  30.  * 
  31.  * Revision 1.6  92/01/15  17:14:16  swalton
  32.  * Delete the Aztec StatToTime function, as it is now identical to the
  33.  * Lattice one.
  34.  * 
  35.  * Also used Id rather than Header in the RCS ID string.
  36.  * 
  37.  * Revision 1.5  91/07/18  16:01:59  swalton
  38.  * zxcmd() fixed to work properly.  I had accidentally copied the first part
  39.  * of the Unix version, which dealt with Kermit sending its received data
  40.  * to a command as its input.  Amiga Kermit doesn't support this, but does
  41.  * support sending the output of a command to a remote Kermit via the
  42.  * pipeopen() routine in ckiutl.c.
  43.  * 
  44.  * Revision 1.4  91/05/29  09:08:17  swalton
  45.  * 1.  Changed function definitions to prototype style.  Required adding
  46.  *     a few forward declarations.
  47.  * 2.  Removed includes of stdio.h, stdlib.h, and string.h, as they are
  48.  *     now pulled in by ckcdeb.h, provided we compile with -DCK_ANSILIBS.
  49.  * 
  50.  * Revision 1.3  90/11/19  21:46:11  swalton
  51.  * Modifications for compiling with SAS/C Version 5.10, courtesy of
  52.  * Larry Rosenman (ler@lerami.lonestar.org, ler on BIX)
  53.  * 
  54.  * Revision 1.2  90/11/07  14:40:57  swalton
  55.  * Version 1.2--released to world as first beta test version simultaneously
  56.  * with release of edit 5A(160).
  57.  * 
  58.  * Revision 1.1  90/07/12  07:56:05  swalton
  59.  * Fairly extensive modifications to bring Amiga Kermit up to Version 5A, edit
  60.  * 149.  Most of the changes can be discerned by reading ckasys.doc, the C Kermit
  61.  * interface document and looking for the items flagged *NEW*.
  62.  * 
  63.  * Revision 1.0  90/04/30  11:54:31  swalton
  64.  * Initial revision
  65.  *  
  66. */
  67.  
  68. /* Includes */
  69.  
  70. #define LONG            /* Prevent LONG definition from coming from
  71.                    ckcdeb.h, as it conflicts with exec/type.h */
  72. #include "ckcdeb.h"        /* Typedefs, formats for debug() */
  73. #undef LONG
  74. #include "ckcker.h"
  75. #include "ckcasc.h"
  76. #if AZTEC_C
  77. #include <stat.h>
  78. #endif
  79. #if __SASC
  80. #include <sys/types.h>
  81. #include <sys/stat.h>
  82. #endif
  83. #include <time.h>
  84. #include <libraries/dosextens.h>
  85. #include <exec/memory.h>
  86.  
  87. #include <clib/exec_protos.h>
  88. #include <clib/dos_protos.h>
  89. #include <pragmas/exec_pragmas.h>
  90. #include <pragmas/dos_pragmas.h>
  91.  
  92. #include <fcntl.h>        /* for prototypes for write() and read() */
  93. #define MAXNAMLEN 30
  94. #ifdef AZTEC_C
  95. char *ckzsys = " Amiga (Aztec C)";
  96. #endif
  97. #ifdef __SASC
  98. char *ckzsys = " Amiga (SAS/C)";
  99. #endif
  100.  
  101. /* Definitions of some Amiga system commands */
  102.  
  103. char *DIRCMD = "list ";            /* For directory listing */
  104. char *DIRCM2 = "list ";            /* Also for directory listing. */
  105. char *DELCMD = "delete ";        /* For file deletion */
  106. char *TYPCMD = "type ";            /* For typing a file */
  107. char *PWDCMD = "cd ";            /* For saying where I am */
  108.  
  109. char *SPACMD = "info ";
  110.  
  111. char *SPACM2 = "info ";            /* should be space in specified directory */
  112.  
  113. char *WHOCMD = "status ";        /* Check process status */
  114.  
  115. #define MAXWLD 300
  116.  
  117. /*
  118.   Functions (n is one of the predefined file numbers from ckermi.h):
  119.  
  120.    zopeni(n,name)   -- Opens an existing file for input.
  121.    zopeno(n,name,attr,fcb)  -- Opens a new file for output.
  122.    zclose(n)        -- Closes a file.
  123.    zchin(n,&c)      -- Gets the next character from an input file.
  124.    zsinl(n,s,x)      -- Reads a line from file number n.
  125.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  126.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  127.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  128.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  129.    zchki(name)      -- Check if named file exists and is readable, return size.
  130.    zchko(name)      -- Check if named file can be created.
  131.    zchkspa(fn, len) -- Check if there is enough space for file.
  132.    znewn(name,s)    -- Make a new unique file name based on the given name.
  133.    zdelet(name)     -- Delete the named file.
  134.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  135.    znext(string)    -- Returns the next file from the list in "string".
  136.    zxcmd(n,cmd)     -- Execute the command in a lower fork on file number n.
  137.    zclosf(n)        -- Close input file associated with zxcmd()'s lower fork.
  138.    zrtol(n1,n2)     -- Convert remote filename into local form.
  139.    zltor(n1,n2)     -- Convert local filename into remote form.
  140.    zstrip(n1, n2)   -- Find trailing path component of n1 and return pointer
  141.    zchdir(dirnam)   -- Change working directory.
  142.    zhome()          -- Return pointer to home directory name string.
  143.    zkself()         -- Kill self, log out own job (simply exits)
  144.    zsattr(struct zattr *) -- Return attributes for file which is being sent.
  145.    zstime(f, struct zattr *, x) - Set file creation date from attribute packet.
  146.    zrename(old, new) -- Rename a file.
  147.    zshcmd(s,local)  -- Execute a shell command and redirect its output, if
  148.                        necessary (see ckasys.doc)
  149.   */
  150.  
  151. /* Declarations */
  152.  
  153. FILE *fp[ZNFILS] = {             /* File pointers */
  154.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  155.  
  156. /* (PWP) external def. of things used in buffered file input and output */
  157. #ifdef DYNAMIC
  158. extern CHAR *zinbuffer, *zoutbuffer;
  159. #else
  160. extern CHAR zinbuffer[], zoutbuffer[];
  161. #endif /* DYNAMIC */
  162. extern CHAR *zinptr, *zoutptr;
  163. extern int zincnt, zoutcnt;
  164.  
  165. static long iflen = -1;            /* Input file length. */
  166.  
  167. static int fcount;            /* Number of files in wild group */
  168. static char  nambuf[MAXNAMLEN+2];    /* Buffer for a filename */
  169.  
  170. char *mtchs[MAXWLD],        /* Matches found for filename */
  171.      **mtchptr;            /* Pointer to current match */
  172. extern short v37;        /* Are we 2.0 or greater? */
  173.  
  174. /* utility functions from ckiutl.c */
  175. extern int existobj();
  176. struct DirHandle    /* fake structure definition */
  177. {
  178.     int _foo_;
  179. };
  180. extern struct DirHandle *opendir();
  181. extern char *readdir();
  182. void closedir();
  183.  
  184. /*
  185.  * Some new time functions.  These are defined up here to make it obvious
  186.  * that they need changing for your compiler.
  187.  */
  188.  
  189. /*
  190.  * The following uses the fact that, on the Amiga, a tm is a long value
  191.  * containing the number of seconds since midnight January 1, 1970.
  192.  * The Amiga's reference time is January 1, 1978 at midnight.
  193.  * The following conversion value is the number of seconds between
  194.  * those two dates.
  195.  */
  196. #define CONVERT (86400L*(2*366 + 6*365))
  197.  
  198. /*
  199.  * TimeToDateStamp accepts a time_t value as returned by, for instance,
  200.  * time() and converts it into an AmigaDos DateStamp.
  201.  */
  202. static void
  203. TimeToDateStamp(time_t tm, struct DateStamp *ds) {
  204.     long seconds = (long) tm - CONVERT;
  205.     ds->ds_Days = seconds / 86400;
  206.     ds->ds_Minute = (seconds - (ds->ds_Days * 86400)) / 60;
  207.     ds->ds_Tick = TICKS_PER_SECOND * (seconds%60);
  208. }
  209.  
  210. static time_t
  211. StatToTime(long mtime) {
  212.     return((time_t) (mtime + CONVERT));
  213. }
  214.  
  215. /*  Z K S E L F  --  Kill Self: log out own job, if possible.  */
  216. int 
  217. zkself(void) {                /* For "bye", but no guarantee! */
  218.     doexit(GOOD_EXIT, -1);
  219. }
  220.  
  221. /*  Z O P E N I  --  Open an existing file for input. */
  222. int 
  223. zopeni(int n, char *name) {
  224.     debug(F111," zopeni",name,n);
  225.     debug(F101,"  fp","",(int) fp[n]);
  226.     if (chkfn(n) != 0) return(0);
  227.     zincnt = 0;
  228.     if (n == ZSYSFN) {            /* Input from a system function? */
  229. /*** Note, this function should not be called with ZSYSFN ***/
  230. /*** Always call zxcmd() directly, and give it the real file number ***/
  231. /*** you want to use.  ***/
  232.         debug(F110,"zopeni called with ZSYSFN, failing!",name,0);
  233.     *nambuf = '\0';            /* No filename. */
  234.     return(0);            /* fail. */
  235. #ifdef COMMENT
  236.     return(zxcmd(n,name));        /* Try to fork the command */
  237. #endif
  238.     }
  239.     if (n == ZSTDIO) {            /* Standard input? */
  240.     if (isatty(0)) {
  241.         ermsg("Terminal input not allowed");
  242.         debug(F110,"zopeni: attempts input from unredirected stdin","",0);
  243.         return(0);
  244.     }
  245.     fp[ZIFILE] = stdin;
  246.     return(1);
  247.     }
  248.     fp[n] = fopen(name,"r");        /* Real file. */
  249.     debug(F111," zopeni", name, (int) fp[n]);
  250.     if (fp[n] == NULL) perror("zopeni");
  251.     return((fp[n] != NULL) ? 1 : 0);
  252. }
  253.  
  254. /*  Z O P E N O  --  Open a new file for output.  */
  255. int
  256. zopeno(int n, char *name, struct zattr *zz, struct filinfo *fcb) {
  257.  
  258.     char *p;        /* Local use pointer */
  259.     if (fcb) {    
  260.     debug(F101,"zopeno fcb disp","",fcb->dsp);
  261.     debug(F101,"zopeno fcb type","",fcb->typ);
  262.     debug(F101,"zopeno fcb char","",fcb->cs);
  263.     } else {
  264.     debug(F100,"zopeno fcb is NULL","",0);
  265.     }
  266.     if (n != ZDFILE)
  267.       debug(F111," zopeno",name,n);
  268.     if (chkfn(n) != 0) return(0);
  269.     if ((n == ZCTERM) || (n == ZSTDIO)) {   /* Terminal or standard output */
  270.     fp[ZOFILE] = stdout;
  271.     debug(F101," fp[]=stdout", "", (int) fp[n]);
  272.     zoutcnt = 0;
  273.     zoutptr = zoutbuffer;
  274.     return(1);
  275.     }
  276.     p = "w";                /* Assume write/create mode */
  277.     if (fcb) {                /* If called with an FCB... */
  278.     if (fcb->dsp == XYFZ_A)        /* Does it say Append? */
  279.       p = "a";            /* Yes. */
  280.     }
  281.     fp[n] = fopen(name,p);        /* Open the file */
  282.  
  283.     if (fp[n] == NULL) {
  284.         perror("zopeno can't open");
  285.     } else {
  286.         if (n == ZDFILE) setbuf(fp[n],NULL); /* Debugging file unbuffered */
  287.     }
  288.     zoutcnt = 0;        /* (PWP) reset output buffer */
  289.     zoutptr = zoutbuffer;
  290.     if (n != ZDFILE)
  291.       debug(F101, " fp[n]", "", (int) fp[n]);
  292.     return((fp[n] != NULL) ? 1 : 0);
  293. }
  294.  
  295. /*  Z C L O S E  --  Close the given file.  */
  296.  
  297. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  298. int 
  299. zclose(int n) {
  300.     int x, x2;
  301.  
  302.     if (chkfn(n) < 1) return(0);    /* Check range of n */
  303.  
  304.     if ((n == ZOFILE) && (zoutcnt > 0))    /* (PWP) output leftovers */
  305.       x2 = zoutdump();
  306.     else
  307.       x2 = 0;
  308.  
  309.     x = 0;                /* Initialize return code */
  310.     if (fp[ZSYSFN]) {            /* If file is realy a pipe */
  311.         x = zclosf(n);            /* do it specially */
  312.     } else {
  313.         if ((fp[n] != stdout) && (fp[n] != stdin)) x = fclose(fp[n]);
  314.     fp[n] = NULL;
  315.     }
  316.     iflen = -1;                /* Invalidate file length */
  317.     if (x == EOF)            /* if we got a close error */
  318.     return (-1);
  319.     else if (x2 < 0)        /* or an error flushing the last buffer */
  320.     return (-1);        /* then return an error */
  321.     else
  322.     return (1);
  323. }
  324.  
  325. /*  Z C H I N  --  Get a character from the input file.  */
  326.  
  327. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  328. int
  329. zchin(int n, int *c) {
  330.     int a, x;
  331.  
  332.     /* (PWP) Just in case this gets called when it shoudn't */
  333.     if (n == ZIFILE) {
  334.     x = zminchar();
  335.     *c = x;
  336.     return (x);
  337.     }
  338.  
  339.     /* if (chkfn(n) < 1) return(-1); */
  340.     a = getc(fp[n]);
  341.     if (a == EOF) return(-1);
  342.     *c = (CHAR) a & 0377;
  343.     return(0);
  344. }
  345.  
  346. /*  Z S I N L  --  Read a line from a file  */
  347.  
  348. /*
  349.   Writes the line into the address provided by the caller.
  350.   n is the Kermit "channel number".
  351.   Writing terminates when newline is encountered, newline is not copied.
  352.   Writing also terminates upon EOF or if length x is exhausted.
  353.   Returns 0 on success, -1 on EOF or error.
  354. */
  355. int
  356. zsinl(int n, char *s, int x) {
  357.     int z = 0, a;
  358.  
  359.     if (chkfn(n) < 1) {            /* Make sure file is open */
  360.     return(-1);
  361.     }
  362.     while (x--) {
  363.     if (zchin(n,&a) < 0) {        /* Read a character from the file */
  364.         z = -1;
  365.         break;
  366.     }
  367.     if (a == '\n') break;
  368.     *s = a;
  369.     s++;
  370.     }
  371.     *s = '\0';
  372.     return(z);
  373. }
  374.  
  375. /*
  376.  * (PWP) (re)fill the buffered input buffer with data.  All file input
  377.  * should go through this routine, usually by calling the zminchar()
  378.  * macro (in ckcker.h).
  379.  */
  380. int
  381. zinfill(void) {
  382.     zincnt = fread(zinbuffer, sizeof (char), INBUFSIZE, fp[ZIFILE]);
  383.     debug(F101,"zinfill zincnt","",zincnt);
  384.     if (zincnt == 0) return (-1); /* end of file */
  385.     zinptr = zinbuffer;       /* set pointer to beginning, (== &zinbuffer[0]) */
  386.     zincnt--;            /* one less char in buffer */
  387.     return((int)(*zinptr++) & 0377); /* because we return the first */
  388. }
  389.  
  390. /*  Z S O U T  --  Write a string to the given file, buffered.  */
  391. int
  392. zsout(int n, char *s) {
  393.     if (chkfn(n) < 1) return(-1);
  394.     fputs(s,fp[n]);
  395.     return(0);
  396. }
  397.  
  398. /*  Z S O U T L  --  Write string to file, with line terminator, buffered  */
  399. int
  400. zsoutl(int n, char *s) {
  401.     if (chkfn(n) < 1) return(-1);
  402.     fputs(s,fp[n]);
  403.     fputs("\n",fp[n]);
  404.     return(0);
  405. }
  406.  
  407. /*  Z S O U T X  --  Write x characters to file, unbuffered.  */
  408. int
  409. zsoutx(int n, char *s, int x) {
  410.     if (chkfn(n) < 1) return(-1);
  411.     return(write(fileno(fp[n]),s,x));
  412. }
  413.  
  414.  
  415. /*  Z C H O U T  --  Add a character to the given file.  */
  416.  
  417. /*  Should return 0 or greater on success, -1 on failure (e.g. disk full)  */
  418. int 
  419. zchout(int n, char c) {
  420.     if (chkfn(n) < 1) return(-1);
  421.     if (n == ZSFILE)
  422.         return(write(fileno(fp[n]),&c,1)); /* Use unbuffered for session log */
  423.     else {                /* Buffered for everything else */
  424.     if (putc(c,fp[n]) == EOF)    /* If true, maybe there was an error */
  425.         return(ferror(fp[n])?-1:0);    /* Check to make sure */
  426.     else                /* Otherwise... */
  427.         return(0);            /* There was no error. */
  428.     }
  429. }
  430.  
  431. /* (PWP) buffered character output routine to speed up file IO */
  432. int
  433. zoutdump(void) {
  434.     int x;
  435.     zoutptr = zoutbuffer;        /* reset buffer pointer in all cases */
  436.     debug(F101,"zoutdump chars","",zoutcnt);
  437.     if (zoutcnt == 0) {            /* nothing to output */
  438.     return(0);
  439.     } else if (zoutcnt < 0) {        /* unexpected negative value */
  440.     zoutcnt = 0;            /* reset output buffer count */
  441.     return(-1);            /* and fail. */
  442.     }
  443.     
  444. /* Frank Prindle suggested that replacing this fwrite() by an fflush() */
  445. /* followed by a write() would improve the efficiency, especially when */
  446. /* writing to stdout.  Subsequent tests showed a 5-fold improvement!   */
  447. /*  if (x = fwrite(zoutbuffer, 1, zoutcnt, fp[ZOFILE])) {              */
  448.  
  449.     fflush(fp[ZOFILE]);
  450.     if (x = write(fileno(fp[ZOFILE]),zoutbuffer,zoutcnt)) {
  451.     debug(F101,"zoutdump fwrite wrote","",x);
  452.     zoutcnt = 0;            /* reset output buffer count */
  453.     return(0);            /* things worked OK */
  454.     } else {
  455.     zoutcnt = 0;            /* reset output buffer count */
  456.     x = ferror(fp[ZOFILE]);        /* get error code */
  457.     debug(F101,"zoutdump fwrite error","",x);
  458.     return(x ? -1 : 0);        /* return failure if error */
  459.     }
  460. }
  461.  
  462. /*  C H K F N  --  Internal function to verify file number is ok  */
  463.  
  464. /*
  465.  Returns:
  466.   -1: File number n is out of range
  467.    0: n is in range, but file is not open
  468.    1: n in range and file is open
  469. */
  470. int
  471. chkfn(int n) {
  472.     switch (n) {
  473.     case ZCTERM:
  474.     case ZSTDIO:
  475.     case ZIFILE:
  476.     case ZOFILE:
  477.     case ZDFILE:
  478.     case ZTFILE:
  479.     case ZPFILE:
  480.     case ZSFILE:
  481.     case ZSYSFN:
  482.         case ZRFILE:
  483.         case ZWFILE: break;
  484.     default:
  485.         debug(F101,"chkfn: file number out of range","",n);
  486.         fprintf(stderr,"?File number out of range - %d\n",n);
  487.         return(-1);
  488.     }
  489.     return( (fp[n] == NULL) ? 0 : 1 );
  490. }
  491.  
  492. /*  Z C H K I  --  Check if input file exists and is readable  */
  493.  
  494. /*
  495.   Returns:
  496.    >= 0 if the file can be read (returns the size).
  497.      -1 if file doesn't exist or can't be accessed,
  498.      -2 if file exists but is not readable (e.g. a directory file).
  499.      -3 if file exists but protected against read access.
  500. */
  501. long
  502. zchki(char *name) {
  503.     long size, readstat();
  504.  
  505.     size = readstat(name);
  506.     debug(F111,"zchki file size",name,(int)size);
  507.     iflen = size;
  508.     strcpy(nambuf, name);        /* Remember file name globally. */
  509.     return(size);
  510. }
  511.  
  512. /*  Z C H K O  --  Check if output file can be created  */
  513.  
  514. /*
  515.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  516. */
  517. int
  518. zchko(char *name) {
  519.     int rc = writestat(name);
  520.  
  521. #ifdef DEBUG
  522.     if (rc < 0)
  523.     debug(F111,"zchko access failed:",name,rc);
  524.     else
  525.     debug(F111,"zchko access ok:",name,rc);
  526. #endif
  527.     return(rc);
  528. }
  529.  
  530. /*  Z D E L E T  --  Delete the named file.  */
  531. int
  532. zdelet(char *name) {
  533.     return(unlink(name));
  534. }
  535.  
  536.  
  537. /*  Z R T O L  --  Convert remote filename into local form  */
  538.  
  539. /*  For AMIGA, this means changing uppercase letters to lowercase.  */
  540.  
  541. void
  542. zrtol(char *name, char *name2) {
  543.     for ( ; *name != '\0'; name++) {
  544.         *name2++ = isupper(*name) ? tolower(*name) : *name;
  545.     }
  546.     *name2 = '\0';
  547.     debug(F110,"zrtol:",name2,0);
  548. }
  549.  
  550. /*  Z S T R I P  --  Strip device & directory name from file specification */
  551.  
  552. /*  Strip pathname from filename "name", return pointer to result in name2 */
  553.  
  554. static char work[100];    /* buffer for use by zstrip and zltor */
  555.  
  556. void
  557. zstrip(char *name, char **name2) {
  558.     char *cp, *pp;
  559.     debug(F110,"zstrip before",name,0);
  560.     pp = work;
  561.     if ((cp = strrchr(name, ':')) == NULL)
  562.         cp = name;
  563.     else
  564.         ++cp;
  565.     for (; *cp != '\0'; cp++) {
  566.         if (*cp == '/')
  567.       pp = work;
  568.     else
  569.       *pp++ = *cp;
  570.     }
  571.     *pp = '\0';                /* Terminate the string */
  572.     *name2 = work;
  573.     debug(F110,"zstrip after",*name2,0);
  574. }
  575.  
  576. /*  Z L T O R  --  Local TO Remote */
  577.  
  578. /*  Convert filename from local format to common (remote) form.  */
  579.  
  580. void
  581. zltor(char *name, char *name2) {
  582.     char *cp, *pp;
  583.     int dc = 0;
  584.  
  585.     debug(F110,"zltor",name,0);
  586.     pp = work;
  587.     if ((cp = strrchr(name, ':')) == NULL)
  588.         cp = name;
  589.     else
  590.         ++cp;
  591.  
  592.     for (; *cp != '\0'; cp++) {    /* strip path name */
  593.         if (*cp == '/') {
  594.         dc = 0;
  595.         pp = work;
  596.     }
  597.     else if (islower(*cp)) *pp++ = toupper(*cp); /* Uppercase letters */
  598.     else if (*cp == '~') *pp++ = 'X';    /* Change tilde to 'X' */
  599.     else if (*cp == '#') *pp++ = 'X';    /* Change number sign to 'X' */
  600.     else if ((*cp == '.') && (++dc > 1)) *pp++ = 'X'; /* & extra dots */
  601.     else *pp++ = *cp;
  602.     }
  603.     *pp = '\0';                /* Tie it off. */
  604.     cp = name2;                /* If nothing before dot, */
  605.     if (*work == '.') *cp++ = 'X';    /* insert 'X' */
  606.     strcpy(cp,work);
  607.     debug(F110," name2",name2,0);
  608. }    
  609.  
  610. /*  Z H O M E  --  Return pointer to user's home directory  */
  611.  
  612. /* we return "s:", which is where startup scripts are found */
  613. char *
  614. zhome(void) {
  615.     return("s:");        /* very approximately */
  616. }
  617.  
  618. /*  Z C H D I R  --  Change directory  */
  619. int
  620. zchdir(char *dirnam) {
  621.     if (chdir(dirnam) != 0)        /* failed */
  622.         return 0;
  623.     if (v37)                /* This only if V37 or more */
  624.         if (!SetCurrentDirName(dirnam))
  625.             return 0;
  626.     return 1;
  627. }
  628.  
  629. /*  Z G T D I R  --  Return pointer to user's current directory  */
  630.  
  631. char *
  632. zgtdir(void) {
  633.  
  634. #ifdef MAXPATHLEN
  635. #define CWDBL MAXPATHLEN
  636. #else
  637. #define CWDBL 100
  638. #endif
  639.  
  640.     static char cwdbuf[CWDBL+1];
  641.     char *buf, *getcwd();
  642.     buf = cwdbuf;
  643.     return(getcwd(buf, CWDBL));
  644. }
  645.  
  646. /*  Z X C M D -- Run a system command so its output can be read like a file */
  647. int
  648. zxcmd(int filnum, char *comand) {
  649.     FILE *pipeopen();
  650.     int out;
  651.  
  652.     if (chkfn(filnum) < 0) return(-1);    /* Need a valid Kermit file number. */
  653.     if (filnum == ZSTDIO || filnum == ZCTERM) /* But not one of these. */
  654.       return(0);
  655.  
  656.     out = (filnum == ZIFILE || filnum == ZRFILE) ? 0 : 1 ;
  657.  
  658. /* Output to a command */
  659.  
  660.     if (out) {                /* Need popen() to do this. */
  661.         return -1;            /* Not yet supported. */
  662.     }
  663.  
  664. /* Input from a command */
  665.     else {
  666.     if ((fp[filnum] = pipeopen(comand)) == NULL) return -1;
  667.     fp[ZSYSFN] = fp[filnum];    /* Remember */ 
  668.     zincnt = 0;            /* (PWP) reset input buffer */
  669.     zinptr = zinbuffer;
  670.     }
  671.     return 1;
  672. }
  673.  
  674. /*  Z C L O S F  - wait for the child fork to terminate and close the pipe. */
  675. int
  676. zclosf(int filnum) {
  677.     pipeclose(fp[filnum]);
  678.     fp[filnum] = fp[ZSYSFN] = NULL;
  679.     return(1);
  680. }
  681.  
  682. /*  Z X P A N D  --  Expand a wildcard string into an array of strings  */
  683. /*
  684.   Returns the number of files that match fn1, with data structures set up
  685.   so that first file (if any) will be returned by the next znext() call.
  686. */
  687. int
  688. zxpand(char *fn) {
  689.     fcount = fgen(fn,mtchs,MAXWLD);    /* Look up the file. */
  690.     if (fcount > 0) {
  691.     mtchptr = mtchs;        /* Save pointer for next. */
  692.     }
  693.     debug(F111,"zxpand",mtchs[0],fcount);
  694.     return(fcount);
  695. }
  696.  
  697.  
  698. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  699. /*
  700.  Returns >0 if there's another file, with its name copied into the arg string,
  701.  or 0 if no more files in list.
  702. */
  703. int
  704. znext(char *fn) {
  705.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  706.     else *fn = '\0';
  707.     debug(F111,"znext",fn,fcount+1);
  708.     return(fcount+1);
  709. }
  710.  
  711. /*  Z C H K S P A -- Check to see if there is enough space for the file. */
  712.  
  713. /*
  714.  * Uses Manx-supplied dospacket() routine. (Source supplied for SAS/C
  715.  *                                            by Larry Rosenman)
  716.  */
  717. int
  718. zchkspa(char *fn, long len) {
  719.     struct MsgPort *MyPort;
  720.     struct InfoData *MyInfo;
  721.     long space;
  722.  
  723.     if ((MyPort = (struct MsgPort *)DeviceProc(fn)) == NULL)
  724.     return -1;
  725.     if ((MyInfo = AllocMem(sizeof(struct InfoData), MEMF_PUBLIC)) == NULL)
  726.     return -1;
  727.     if (dos_packet(MyPort, ACTION_DISK_INFO, ((BPTR) MyInfo) >> 2,
  728.                    0L, 0L, 0L, 0L, 0L) != 0) {
  729.     if (MyInfo->id_UnitNumber == -1)    /* Imperfect check for RAM: */
  730.         space = len + 1;            /* Always enough space */
  731.     else
  732.         space = (MyInfo->id_NumBlocks - MyInfo->id_NumBlocksUsed)*
  733.                     MyInfo->id_BytesPerBlock;
  734.     } else
  735.     space = -1;        /* To mark error return after FreeMem(). */
  736.     FreeMem(MyInfo, sizeof(struct InfoData));
  737.     if (space <= 0)
  738.     return -1;
  739.     else if (len < space)
  740.     return 1;
  741.     else
  742.     return 0;
  743. }
  744.  
  745. /*  Z N E W N  --  Make a new name for the given file  */
  746.  
  747. void
  748. znewn(char *fn,char **s) {
  749.     static char buf[100];
  750.     char *bp;
  751.     int len = 0, d;
  752. #ifdef MAXNAMLEN    
  753.     int maxlen = MAXNAMLEN;
  754. #else
  755.     int maxlen = 14;
  756. #endif
  757.  
  758.     bp = buf;
  759.     while (*fn) {            /* Copy name into buf */
  760.     *bp++ = *fn++;
  761.     len++;
  762.     }
  763.     if (len > maxlen-3) bp -= 3;    /* Don't let it get too long */
  764.  
  765.     /* 
  766.      * On the Amiga, it takes much less time to determine
  767.      * if a given file exists than to read all the file names in
  768.      * a directory (or even just names with a certain prefix).
  769.      */
  770.     d = 0;
  771.     do {
  772.     sprintf(bp, "~%d", ++d);
  773.     } while (zchki(buf) != -1 && d < 100);
  774.  
  775.     *s = buf;
  776. }
  777.  
  778. /*  Z S A T T R */
  779. /*
  780.  Fills in a Kermit file attribute structure for the file which is to be sent.
  781.  Returns 0 on success with the structure filled in, or -1 on failure.
  782.  If any string member is null, then it should be ignored.
  783.  If any numeric member is -1, then it should be ignored.
  784. */
  785. int
  786. zsattr(struct zattr *xx) {
  787.     long k;
  788.     char *zfcdat();
  789.  
  790.     k = iflen % 1024L;            /* File length in K */
  791.     if (k != 0L) k = 1L;
  792.     xx->lengthk = (iflen / 1024L) + k;
  793.     xx->type.len = 0;            /* File type can't be filled in here */
  794.     xx->type.val = "";
  795.     if (*nambuf) {
  796.     xx->date.val = zfcdat(nambuf);    /* File creation date */
  797.     xx->date.len = strlen(xx->date.val);
  798.     } else {
  799.     xx->date.len = 0;
  800.     xx->date.val = "";
  801.     }
  802.     xx->creator.len = 0;        /* File creator */
  803.     xx->creator.val = "";
  804.     xx->account.len = 0;        /* File account */
  805.     xx->account.val = "";
  806.     xx->area.len = 0;            /* File area */
  807.     xx->area.val = "";
  808.     xx->password.len = 0;            /* Area password */
  809.     xx->password.val = "";
  810.     xx->blksize = -1L;            /* File blocksize */
  811.     xx->access.len = 0;            /* File access */
  812.     xx->access.val = "";
  813.     xx->encoding.len = 0;        /* Transfer syntax */
  814.     xx->encoding.val = 0;
  815.     xx->disp.len = 0;            /* Disposition upon arrival */
  816.     xx->disp.val = "";
  817.     xx->lprotect.len = 0;        /* Local protection */
  818.     xx->lprotect.val = "";
  819.     xx->gprotect.len = 0;        /* Generic protection */
  820.     xx->gprotect.val = "";
  821.     xx->systemid.len = 2;        /* System ID length */
  822.     xx->systemid.val = "L3";        /* Amiga system ID code */
  823.     xx->recfm.len = 0;            /* Record format */
  824.     xx->recfm.val = "";
  825.     xx->sysparam.len = 0;        /* System-dependent parameters */
  826.     xx->sysparam.val = "";
  827.     xx->length = iflen;            /* Length */
  828.     return(0);
  829. }
  830.  
  831. /* Z F C D A T -- Return a string containing the time stamp for a file */
  832.  
  833. char *
  834. zfcdat(char *name) {
  835.  
  836.     struct stat buffer;
  837.     struct tm *time_stamp, *localtime();
  838.     time_t filetime;
  839.     static char datbuf[20];
  840.  
  841.     datbuf[0] = '\0';
  842.     if(stat(name,&buffer) != 0) {
  843.     debug(F110,"zcfdat stat failed",name,0);
  844.     return("");
  845.     }
  846.     filetime = StatToTime(buffer.st_mtime);
  847.     time_stamp = localtime(&filetime);
  848.     if (time_stamp->tm_year < 1900) time_stamp->tm_year += 1900;
  849.     sprintf(datbuf,"%-4.4d%02.2d%02.2d %002.2d:%002.2d:%002.2d",
  850.         time_stamp->tm_year,
  851.         time_stamp->tm_mon + 1,
  852.         time_stamp->tm_mday,
  853.         time_stamp->tm_hour,
  854.         time_stamp->tm_min,
  855.         time_stamp->tm_sec);
  856.     debug(F111,"zcfdat",datbuf,strlen(datbuf));
  857.     return(datbuf);
  858. }
  859.  
  860. /* Z S T I M E  --  Set creation date for incoming file */
  861. /*
  862.  Call with:
  863.  f  = pointer to name of existing file.
  864.  yy = pointer to a Kermit file attribute structure in which yy->date.val
  865.       is a date of the form yyyymmdd hh:mm:ss, e.g. 19900208 13:00:00.
  866.  x  = is a function code: 0 means to set the file's creation date as given.
  867.       1 means compare the given date with the file creation date.      
  868.  Returns:
  869.  -1 on any kind of error.
  870.   0 if x is 0 and the file date was set successfully.
  871.   0 if x is 1 and date from attribute structure <= file creation date.
  872.   1 if x is 1 and date from attribute structure > file creation date.
  873. */
  874. int
  875. zstime(char *f, struct zattr *yy, int x) {
  876.  
  877. /*
  878.  * This code takes advantage of the ANSI time functions.  Once UNIX has
  879.  * an ANSI compiler, maybe UNIX can use this one instead...
  880.  */
  881.  
  882.     struct tm InTime;
  883.     time_t CompareTime;
  884.     struct stat MyStat;
  885.     
  886.     int isleapyear, i;
  887.     static int monthdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  888.  
  889.     debug(F110,"zstime",f,0);
  890.  
  891.     if (sscanf(yy->date.val, "%4d%2d%2d %2d:%2d:%2d", &InTime.tm_year,
  892.            &InTime.tm_mon, &InTime.tm_mday, &InTime.tm_hour,
  893.            &InTime.tm_min, &InTime.tm_sec) != 6) {
  894.     debug(F111, "Bad creation date ", yy->date.val, yy->date.len);
  895.     return -1;
  896.     }
  897.     InTime.tm_mon--;        /* a struct tm contains months with 0 = Jan */
  898.  
  899.     isleapyear = (( InTime.tm_year % 4 == 0 && InTime.tm_year % 100 !=0) ||
  900.                   InTime.tm_year % 400 == 0);
  901.     InTime.tm_year -= 1900;
  902. /*
  903.  * Find number of days since start-of-year.
  904.  */
  905.     InTime.tm_yday = (isleapyear ? 0 : 1);
  906.     for (i = 0; i <= InTime.tm_mon; i++)
  907. /*    InTime.tm_yday += monthdays[0]; */ /*ler*/
  908.     InTime.tm_yday += monthdays[i]; 
  909.     InTime.tm_yday += InTime.tm_mday;
  910.     InTime.tm_isdst = 0;        /* No daylight savings on Amiga. */
  911. /*
  912.  * We don't set day-of-week in this code, and simply hope mktime ignores it.
  913.  */
  914.     CompareTime = mktime(&InTime);
  915.     if (x == 1) {            /* Compare time with file */
  916.     if (stat(f, &MyStat) < 0) {
  917.         debug(F110,"zstime: stat failed", f, 0);
  918.         return -1;
  919.     }
  920.     if (StatToTime(MyStat.st_mtime) <= CompareTime)
  921.         return 0;
  922.     else
  923.         return 1;
  924.     } else if (x == 0) {        /* Set file to time */
  925.     struct DateStamp NewTime;
  926.  
  927.     TimeToDateStamp(CompareTime, &NewTime);
  928.     return(touch(f, &NewTime));
  929.     }
  930.     return -1;                /* Illegal value for x */
  931. }
  932. /*
  933.  * Set modification date of file fn to NewTime.
  934.  */
  935. static int
  936. touch(char *fn, struct DateStamp *NewTime) {
  937.     struct MsgPort *task;
  938.     BPTR lock, plock;
  939.     UBYTE *pointer;
  940.  
  941.     if(!(pointer = (UBYTE *)AllocMem(256L,MEMF_PUBLIC)))
  942.     return -1;
  943.     if(!(task=(struct MsgPort *)DeviceProc(fn))) {
  944.     FreeMem((void *) pointer, 256L);
  945.     return;
  946.     }
  947.     if(!(lock = Lock(fn,SHARED_LOCK))) {
  948.     FreeMem((void *) pointer, 256L);
  949.     return -1;
  950.     }
  951.     plock = ParentDir(lock);
  952.     UnLock(lock);
  953.  
  954.     strcpy((char *)(pointer + 1),fn);
  955.     *pointer = strlen(fn);
  956.  
  957.     dos_packet(task, ACTION_SET_DATE, NULL, plock, (ULONG) &pointer[0] >> 2,
  958.     (ULONG) NewTime, 0L, 0L, 0L);
  959.  
  960.     UnLock(plock);
  961.     FreeMem((void *) pointer, 256L);
  962.     return 0;
  963. }
  964. /*
  965.  * Dummy functions for the Amiga.  Sending mail cannot be done;  I haven't
  966.  * decided how to handle print requests yet.
  967.  */
  968. int
  969. zmail(char *p, char *f) {        /* Send file f as mail to address p */
  970.     return(0);
  971. }
  972.  
  973. int
  974. zprint(char *p, char *f) {        /* Print file f with flags p */
  975.     return(0);
  976. }
  977.  
  978. int
  979. zrename(char *old, char *new) {
  980.     return(rename(old, new) == 0 ? 0 : -1);
  981. }
  982.  
  983. /* Z S H C M D -- Issue shell command and redirect output, if necessary. */
  984.  
  985. /*
  986.  * This function is to pass the command contained in the string s to the
  987.  * local command parser.  If the command does not begin with the character
  988.  * ">", then give the command to the system and display its results on the
  989.  * screen.  If the command does not begin with ">" and local != 0, then send
  990.  * the results of the command out the currently open communications device.
  991.  *
  992.  * In this version, we simply punt.
  993.  */
  994. int
  995. zshcmd(char *s) {
  996.     if (*s == '>') s++;
  997.     return(system(s));
  998. }
  999.  
  1000. /* Directory Functions for Unix, written by Jeff Damens, CUCCA, 1984. */
  1001.  
  1002. /*
  1003.  * The path structure is used to represent the name to match.
  1004.  * Each slash-separated segment of the name is kept in one
  1005.  * such structure, and they are linked together, to make
  1006.  * traversing the name easier.
  1007.  */
  1008.  
  1009. struct path {
  1010.               char npart[MAXNAMLEN];    /* name part of path segment */
  1011.               struct path *fwd;        /* forward ptr */
  1012.             };
  1013.  
  1014. #define SSPACE 4000            /* size of string-generating buffer */
  1015.  
  1016. static char sspace[SSPACE];             /* buffer to generate names in */
  1017. static char *freeptr,**resptr;             /* copies of caller's arguments */
  1018. static int remlen;                      /* remaining length in caller's array*/
  1019. static int numfnd;                      /* number of matches found */
  1020.  
  1021. /*
  1022.  * splitpath:
  1023.  *  takes a string and splits the slash-separated portions into
  1024.  *  a list of path structures.  Returns the head of the list.  The
  1025.  *  structures are allocated by malloc, so they must be freed.
  1026.  *  Splitpath is used internally by the filename generator.
  1027.  *
  1028.  * Input: A string.
  1029.  * Returns: A linked list of the slash-separated segments of the input.
  1030.  */
  1031.  
  1032. struct path *
  1033. splitpath(char *p) {
  1034.  struct path *head,*cur,*prv;
  1035.  int i;
  1036.  head = prv = NULL;
  1037.  if (*p == '/') p++;            /* skip leading slash */
  1038.  while (*p != '\0')
  1039.  {
  1040.    cur = (struct path *) malloc(sizeof (struct path));
  1041.    debug(F101,"splitpath malloc","",(cur == NULL ? 0 : 1));
  1042.    if (cur == NULL) fatal("malloc fails in splitpath()");
  1043.    cur -> fwd = NULL;
  1044.    if (head == NULL) head = cur;
  1045.    else prv -> fwd = cur;       /* link into chain */
  1046.    prv = cur;
  1047.    for (i=0; i < MAXNAMLEN && *p != '/' && *p != '\0'; i++)
  1048.      cur -> npart[i] = *p++;
  1049.    cur -> npart[i] = '\0';      /* end this segment */
  1050.    if (i >= MAXNAMLEN) while (*p != '/' && *p != '\0') p++;
  1051.    if (*p == '/') p++;
  1052.  }
  1053.  return(head);
  1054. }
  1055.  
  1056. /*
  1057.  * fgen:
  1058.  *  This is the actual name generator.  It is passed a string,
  1059.  *  possibly containing wildcards, and an array of character pointers.
  1060.  *  It finds all the matching filenames and stores them into the array.
  1061.  *  The returned strings are allocated from a static buffer local to
  1062.  *  this module (so the caller doesn't have to worry about deallocating
  1063.  *  them); this means that successive calls to fgen will wipe out
  1064.  *  the results of previous calls.  This isn't a problem here
  1065.  *  because we process one wildcard string at a time.
  1066.  *
  1067.  * Input: a wildcard string, an array to write names to, the
  1068.  *        length of the array.
  1069.  * Returns: the number of matches.  The array is filled with filenames
  1070.  *          that matched the pattern.  If there wasn't enough room in the
  1071.  *        array, -1 is returned.
  1072.  * By: Jeff Damens, CUCCA, 1984.
  1073.  */
  1074. int
  1075. fgen(char *pat, char *resarry[], int len) {
  1076.  struct path *head;
  1077.  char scratch[100],*sptr;
  1078.  char *tail;
  1079.  void traverse(struct path *pl, char *sofar, char *endcur);
  1080.  
  1081.  if ((tail = strrchr(pat, ':')) == NULL) /* locate unit name */
  1082.   tail = pat;                /* no unit name */
  1083.  else
  1084.   ++tail;                /* eat ':' */
  1085.  while (*tail == '/')            /* eat parent path slashes */
  1086.   ++tail;
  1087.  sptr = scratch;            /* init buffer correctly */
  1088.  while (pat < tail)
  1089.   *sptr++ = *pat++;
  1090.  head = splitpath(pat);
  1091.  numfnd = 0;                            /* none found yet */
  1092.  freeptr = sspace;            /* this is where matches are copied */
  1093.  resptr = resarry;            /* static copies of these so*/
  1094.  remlen = len;                /* recursive calls can alter them */
  1095.  traverse(head,scratch,sptr);        /* go walk the directory tree */
  1096.  while (head != NULL) {
  1097.    struct path *next = head -> fwd;
  1098.    free(head);                /* return the path segments */
  1099.    head = next;
  1100.  }
  1101.  return(numfnd);            /* and return the number of matches */
  1102. }
  1103.  
  1104. /* traverse:
  1105.  *  Walks the directory tree looking for matches to its arguments.
  1106.  *  The algorithm is, briefly:
  1107.  *   If the current pattern segment contains no wildcards, that
  1108.  *   segment is added to what we already have.  If the name so far
  1109.  *   exists, we call ourselves recursively with the next segment
  1110.  *   in the pattern string; otherwise, we just return.
  1111.  *
  1112.  *   If the current pattern segment contains wildcards, we open the name
  1113.  *   we've accumulated so far (assuming it is really a directory), then read 
  1114.  *   each filename in it, and, if it matches the wildcard pattern segment, add
  1115.  *   that filename to what we have so far and call ourselves recursively on the
  1116.  *   next segment.
  1117.  *
  1118.  *   Finally, when no more pattern segments remain, we add what's accumulated
  1119.  *   so far to the result array and increment the number of matches.
  1120.  *
  1121.  * Input: a pattern path list (as generated by splitpath), a string
  1122.  *      pointer that points to what we've traversed so far (this
  1123.  *      can be initialized to "/" to start the search at the root
  1124.  *      directory, or to "./" to start the search at the current
  1125.  *      directory), and a string pointer to the end of the string
  1126.  *      in the previous argument.
  1127.  * Returns: nothing.
  1128.  */
  1129. void
  1130. traverse(struct path *pl, char *sofar, char *endcur) {
  1131.  struct DirHandle *fd;
  1132.  char *fname;
  1133.  
  1134.  if (pl == NULL)
  1135.  {
  1136.   *--endcur = '\0';                    /* end string, overwrite trailing / */
  1137.   addresult(sofar);
  1138.   return;
  1139.  }
  1140.  if (!iswild(pl -> npart))
  1141.  {
  1142.   strcpy(endcur,pl -> npart);
  1143.   endcur += strlen(pl -> npart);
  1144.   *endcur = '\0';                         /* end current string */
  1145.   if (existobj(sofar))            /* if current piece exists */
  1146.   {
  1147.       *endcur++ = '/';            /* add slash to end */
  1148.       *endcur = '\0';            /* and end the string */
  1149.       traverse(pl -> fwd,sofar,endcur);
  1150.   }
  1151.   return;
  1152.  }
  1153. /* cont'd... */
  1154.  
  1155. /*...traverse, cont'd */
  1156.  
  1157. /* segment contains wildcards, have to search directory */
  1158.  *endcur = '\0';                            /* end current string */
  1159.  if ((fd = opendir(sofar)) == NULL) return;      /* can't open, forget it */
  1160.  while (fname = readdir(fd))
  1161. {
  1162.   if (match(pl -> npart,fname)) {
  1163.     char *eos;
  1164.     strcpy(endcur,fname);
  1165.     eos = endcur + strlen(fname);
  1166.     *eos = '/';                    /* end this segment */
  1167.     traverse(pl -> fwd,sofar,eos+1);
  1168.   }
  1169. }
  1170.  closedir(fd);
  1171. }
  1172.  
  1173. /*
  1174.  * addresult:
  1175.  *  Adds a result string to the result array.  Increments the number
  1176.  *  of matches found, copies the found string into our string
  1177.  *  buffer, and puts a pointer to the buffer into the caller's result
  1178.  *  array.  Our free buffer pointer is updated.  If there is no
  1179.  *  more room in the caller's array, the number of matches is set to -1.
  1180.  * Input: a result string.
  1181.  * Returns: nothing.
  1182.  */
  1183. int
  1184. addresult(char *str) {
  1185.  int l;
  1186.  if (--remlen < 0) {
  1187.   numfnd = -1;
  1188.   return;
  1189.  }
  1190.  l = strlen(str) + 1;            /* size this will take up */
  1191.  if ((freeptr + l) > &sspace[SSPACE]) {
  1192.     numfnd = -1;            /* do not record if not enough space */
  1193.     return;
  1194.   }
  1195.  strcpy(freeptr,str);
  1196.  *resptr++ = freeptr;
  1197.  freeptr += l;
  1198.  numfnd++;
  1199. }
  1200.  
  1201. int 
  1202. iswild(char *str) {
  1203.  char c;
  1204.  while ((c = *str++) != '\0')
  1205.    if (c == '*' || c == '?') return(1);
  1206.  return(0);
  1207. }
  1208.  
  1209. #ifdef OLDMATCH
  1210. /*
  1211.  * match:
  1212.  *  pattern matcher.  Takes a string and a pattern possibly containing
  1213.  *  the wildcard characters '*' and '?'.  Returns true if the pattern
  1214.  *  matches the string, false otherwise.
  1215.  * by: Jeff Damens, CUCCA
  1216.  *
  1217.  * Input: a string and a wildcard pattern.
  1218.  * Returns: 1 if match, 0 if no match.
  1219.  */
  1220. int
  1221. match(char *pattern, char *string) {
  1222.     char *psave,*ssave;            /* back up pointers for failure */
  1223.     psave = ssave = NULL;
  1224.     while (1) {
  1225.     for (;
  1226.          tolower(*pattern) == tolower(*string);
  1227.          pattern++,string++)  /* skip first */
  1228.         if (*string == '\0') return(1);    /* end of strings, succeed */
  1229.     if (*string != '\0' && *pattern == '?') {
  1230.         pattern++;            /* '?', let it match */
  1231.         string++;
  1232.     } else if (*pattern == '*') {    /* '*' ... */
  1233.         psave = ++pattern;        /* remember where we saw it */
  1234.         ssave = string;        /* let it match 0 chars */
  1235.     } else if (ssave != NULL && *ssave != '\0') {    /* if not at end  */
  1236.                       /* ...have seen a star */
  1237.         string = ++ssave;        /* skip 1 char from string */
  1238.         pattern = psave;        /* and back up pattern */
  1239.     } else return(0);        /* otherwise just fail */
  1240.     }
  1241. }
  1242. #else
  1243. /*
  1244.  * match -- match wildcard pattern to string
  1245.  *    allows multiple '*'s and works without backtracking
  1246.  *    upper and lower case considered equivalent
  1247.  *    written by Jack Rouse
  1248.  *    working without backtracking is cute, but is this usually going
  1249.  *       to be the most efficient method?
  1250.  */
  1251. int
  1252. match(char *pattern, char *target) {
  1253.     int link[MAXNAMLEN];        /* list of matches to try in pattern */
  1254.     register int first, last;    /* first and last items in list */
  1255.     register int here, next;    /* current and next list items */
  1256.     char lowch;            /* current target character */
  1257.  
  1258.     /* start out trying to match at first position */
  1259.     first = last = 0;
  1260.     link[0] = -1;
  1261.  
  1262.     /* go through the target */
  1263.     for (; *target; ++target)
  1264.     {
  1265.         /* get lowercase target character */
  1266.         lowch = tolower(*target);
  1267.  
  1268.         /* go through all positions this round and build next round */
  1269.         last = next = -1;
  1270.         for (here = first; here >= 0; here = next)
  1271.         {
  1272.             next = link[here];
  1273.             switch (pattern[here])
  1274.             {
  1275.             case '*':
  1276.                 /* try match at here+1 this round */
  1277.                 /*!!!check needed only if "**" allowed? */
  1278.                 if (next != here + 1)
  1279.                 {
  1280.                     link[here + 1] = next;
  1281.                     next = here + 1;
  1282.                 }
  1283.                 /* retry match at here next round */
  1284.                 break;
  1285.             default:
  1286.                 if (tolower(pattern[here]) != lowch)
  1287.                     continue;
  1288.                 /* matched, fall through */
  1289.             case '?':
  1290.                 /* try match at here+1 next round */
  1291.                 ++here;
  1292.                 break;
  1293.             }
  1294.             /* try match at here value next round */
  1295.             if (last < 0)
  1296.                 first = here;
  1297.             else
  1298.                 link[last] = here;
  1299.             last = here;
  1300.         }
  1301.         /* if no positions left, match failed */
  1302.         if (last == -1) return(0);
  1303.         /* terminate list */
  1304.         link[last] = -1;
  1305.     }
  1306.  
  1307.     /* at end of target, skip empty matches */
  1308.     while (pattern[last] == '*')
  1309.         ++last;
  1310.  
  1311.     return(pattern[last] == '\0');
  1312. }
  1313. #endif
  1314.  
  1315. #ifndef NORESEND
  1316. int
  1317. zfseek(long pos)
  1318. /* zfseek */ {
  1319.     debug(F101,"zfseek","",pos);
  1320.     return(fseek(fp[ZIFILE], pos, 0));
  1321. }
  1322. #endif /* NORESEND */
  1323.  
  1324.