home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d995 / xprkermit.lha / XprKermit / source.lha / ckxfio.c next >
C/C++ Source or Header  |  1993-03-31  |  15KB  |  633 lines

  1. /*
  2.  * CKXFIO.C
  3.  *
  4.  * File I/O routines for XPR C Kermit.
  5.  *
  6.  */
  7.  
  8. char *ckzv = "Amiga XPR file support, 5A(001), 6 August 1992";
  9.  
  10. #include "ckcdeb.h"
  11. #include "ckcker.h"
  12. #include "ckxker.h"
  13.  
  14. char *DELCMD = "delete ";
  15. char *DIRCMD = "list ";
  16. char *DIRCM2 = "list ";
  17. char *PWDCMD = "cd ";
  18. char *SPACMD = "info ";
  19. char *SPACM2 = "info ";
  20. char *TYPCMD = "type ";
  21. char *WHOCMD = "status ";
  22. int backgrd = 1;
  23. int ckxech = 0;
  24. char *ckxsys = "Amiga External Protocol";
  25. char *ckzsys = " Amiga XPR";
  26. int maxnam = 128;
  27. int maxpath = 128;
  28. int success = 1;
  29. int fdispla = XYFD_C;        /* We always do screen displays */
  30. char optbuf[100];        /* Option buffer for MAIL or PRINT */
  31.  
  32. /* Declarations */
  33.  
  34. long fp[ZNFILS] = {             /* File pointers */
  35.     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
  36.  
  37. /* (PWP) external def. of things used in buffered file input and output */
  38. #ifdef DYNAMIC
  39. extern CHAR *zinbuffer, *zoutbuffer;
  40. #else
  41. extern CHAR zinbuffer[], zoutbuffer[];
  42. #endif /* DYNAMIC */
  43. extern CHAR *zinptr, *zoutptr;
  44. extern int zincnt, zoutcnt;
  45.  
  46. extern long (*xupdate) (), (*xswrite) (), (*xfopen) (), (*xfclose) (),
  47.   (*xfread) (), (*xsread) (), (*xchkabort) (), (*xfnext) (), (*xffirst) (),
  48.   (*xsflush) (), (*xfwrite) (), (*xgets) (), (*xfinfo) (), (*xunlink)() ,
  49.   (*xsquery) (), (*xchkmisc) ();
  50.  
  51. /*  C H K F N  --  Internal function to verify file number is ok  */
  52.  
  53. /*
  54.  Returns:
  55.   -1: File number n is out of range
  56.    0: n is in range, but file is not open
  57.    1: n in range and file is open
  58. */
  59. int
  60. chkfn(int n) {
  61.     switch (n) {
  62.     case ZIFILE:
  63.     case ZOFILE:
  64.     case ZDFILE:
  65.     case ZTFILE:
  66.     case ZPFILE:
  67.     case ZSFILE:
  68.         case ZRFILE:
  69.         case ZWFILE: break;
  70.     default:
  71.         debug(F101,"chkfn: file number out of range","",n);
  72.         return(-1);
  73.     }
  74.     return( (fp[n] == 0L) ? 0 : 1 );
  75. }
  76.  
  77. int
  78. iswild(char *filespec) {
  79.    if (strchr(filespec, '?') >= 0)
  80.       return(1);
  81.    else
  82.       return(0);
  83. }
  84.  
  85. #ifndef MAXPATHLEN
  86. #define MAXPATHLEN 256
  87. #endif
  88.  
  89. static char zcurrent[MAXPATHLEN] = "";
  90.  
  91. int
  92. zchdir(char *dirnam) {
  93.    strncpy(zcurrent, dirnam, MAXPATHLEN - 1);
  94.    return 1;
  95. }
  96. /*  Z C H K I  --  Check if input file exists and is readable  */
  97.  
  98. /*
  99.   Returns:
  100.    >= 0 if the file can be read (returns the size).
  101.      -1 if file doesn't exist or can't be accessed,
  102.      -2 if file exists but is not readable (e.g. a directory file).
  103.      -3 if file exists but protected against read access.
  104. */
  105.  
  106. static long iflen;
  107. static long iftype;
  108.  
  109. long
  110. zchki(char *fn) {
  111.  
  112.    iflen = callad(xfinfo, fn, 1L);
  113.    if (iflen <= 0)
  114.       return(-1);
  115.    else {
  116.       iftype = callad(xfinfo, fn, 2L);
  117.       return iflen;
  118.    }
  119. }
  120. /*  Z C H K O  --  Check if output file can be created  */
  121.  
  122. /*
  123.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  124.  Since we can't really do this from XPR Kermit, always return zero and
  125.  let the zopeno() call fail.
  126. */
  127.  
  128. int
  129. zchko(char *fn) {
  130.    return 0;
  131.    
  132. }
  133.  
  134. /*  Z C H K S P A -- Check to see if there is enough space for the file. */
  135.  
  136. int
  137. zchkspa(char *f, long len) {
  138.    return 1;
  139. }
  140.  
  141. /*  Z C H I N  --  Get a character from the input file.  */
  142.  
  143. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  144. int
  145. zchin(int n, int *c) {
  146.     int a, x;
  147.  
  148.     /* (PWP) Just in case this gets called when it shoudn't */
  149.     if (n == ZIFILE) {
  150.     x = zminchar();
  151.     *c = x;
  152.     return (x);
  153.     }
  154.  
  155.     if (calladda(xfread, &a, sizeof(char), 1, fp[n]) != 1) return(-1);
  156.     *c = (CHAR) a & 0377;
  157.     return(0);
  158. }
  159.  
  160. /*  Z C H O U T  --  Add a character to the given file.  */
  161.  
  162. /*  Should return 0 or greater on success, -1 on failure (e.g. disk full)  */
  163.  
  164. int
  165. zchout(int n, char c) {
  166.    if (chkfn(n) < 1) return(-1);
  167.    if (calladda(xfwrite, &c, sizeof(char), 1, fp[n]) != 1)
  168.       return(-1);
  169.    else                 /* Otherwise... */
  170.       return(0);            /* There was no error. */
  171. }
  172.  
  173. /*  Z C L O S E  --  Close the given file.  */
  174.  
  175. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  176.  
  177. int
  178. zclose(int n) {
  179.     int x, x2;
  180.  
  181.     if (chkfn(n) < 1) return(0);    /* Check range of n */
  182.  
  183.     if ((n == ZOFILE) && (zoutcnt > 0))    /* (PWP) output leftovers */
  184.       x2 = zoutdump();
  185.     else
  186.       x2 = 0;
  187.  
  188.     x = calla(xfclose, fp[n]);
  189.     iflen = -1;                /* Invalidate file length */
  190.     fp[n] = NULL;                       /* And file pointer */
  191.     if (x < 0)                /* if we got a close error */
  192.     return (-1);
  193.     else if (x2 < 0)        /* or an error flushing the last buffer */
  194.     return (-1);        /* then return an error */
  195.     else
  196.     return (1);
  197. }
  198.  
  199. /*  Z D E L E T  --  Delete the named file.  */
  200.  
  201. int
  202. zdelet(char *fn) {
  203.    if (xunlink != NULL) {
  204.       if (calla(xunlink, fn) < 0)
  205.     return -1;
  206.    }
  207.    return 0;
  208. }
  209.  
  210. /* Z G T D I R --- Return pointer to name of current directory */
  211.  
  212. char *
  213. zgtdir(void) {
  214.    return NULL;
  215. }
  216.  
  217. /* Z H O M E --- Return pointer to user's home directory */
  218.  
  219. /* we return "S:", which is where startup scripts are found */
  220. char *
  221. zhome(void) {
  222.    return("S:");                  /* approximately */
  223. }
  224.  
  225. /*
  226.  * (PWP) (re)fill the buffered input buffer with data.  All file input
  227.  * should go through this routine, usually by calling the zminchar()
  228.  * macro (in ckcker.h).
  229.  */
  230. int
  231. zinfill(void) {
  232.     zincnt = calladda(xfread, zinbuffer, sizeof (char), INBUFSIZE, fp[ZIFILE]);
  233.     debug(F101,"zinfill zincnt","",zincnt);
  234.     if (zincnt == 0) return (-1); /* end of file */
  235.     zinptr = zinbuffer;       /* set pointer to beginning, (== &zinbuffer[0]) */
  236.     zincnt--;            /* one less char in buffer */
  237.     return((int)(*zinptr++) & 0377); /* because we return the first */
  238. }
  239.  
  240. int
  241. zkself(void) {
  242.    return -1;
  243. }
  244. /*  Z S T R I P  --  Strip device & directory name from file specification */
  245.  
  246. /*  Strip pathname from filename "name", return pointer to result in name2 */
  247.  
  248. static char work[256];    /* buffer for use by zstrip and zltor and others */
  249.  
  250. void
  251. zstrip(char *name, char **name2) {
  252.     char *cp, *pp;
  253.     debug(F110,"zstrip before",name,0);
  254.     pp = work;
  255.     if ((cp = strrchr(name, ':')) == NULL)
  256.         cp = name;
  257.     else
  258.         ++cp;
  259.     for (; *cp != '\0'; cp++) {
  260.         if (*cp == '/')
  261.       pp = work;
  262.     else
  263.       *pp++ = *cp;
  264.     }
  265.     *pp = '\0';                /* Terminate the string */
  266.     *name2 = work;
  267.     debug(F110,"zstrip after",*name2,0);
  268. }
  269. /*  Z L T O R  --  Local TO Remote */
  270.  
  271. /*  Convert filename from local format to common (remote) form.  */
  272.  
  273. void
  274. zltor(char *name, char *name2) {
  275.     char *cp, *pp;
  276.     int dc = 0;
  277.  
  278.     debug(F110,"zltor",name,0);
  279.     pp = work;
  280.     if ((cp = strrchr(name, ':')) == NULL)
  281.         cp = name;
  282.     else
  283.         ++cp;
  284.  
  285.     for (; *cp != '\0'; cp++) {    /* strip path name */
  286.         if (*cp == '/') {
  287.         dc = 0;
  288.         pp = work;
  289.     }
  290.     else if (islower(*cp)) *pp++ = toupper(*cp); /* Uppercase letters */
  291.     else if (*cp == '~') *pp++ = 'X';    /* Change tilde to 'X' */
  292.     else if (*cp == '#') *pp++ = 'X';    /* Change number sign to 'X' */
  293.     else if ((*cp == '.') && (++dc > 1)) *pp++ = 'X'; /* & extra dots */
  294.     else *pp++ = *cp;
  295.     }
  296.     *pp = '\0';                /* Tie it off. */
  297.     cp = name2;                /* If nothing before dot, */
  298.     if (*work == '.') *cp++ = 'X';    /* insert 'X' */
  299.     strcpy(cp,work);
  300.     debug(F110," name2",name2,0);
  301. }    
  302.  
  303. /* Z M A I L --- Send file as mail to user */
  304.  
  305. /* Not implementable on a personal computer. */
  306. int
  307. zmail(char *addr, char *fn) {
  308.    return -2;
  309. }
  310. /*  Z N E W N  --  Make a new name for the given file  */
  311.  
  312. void
  313. znewn(char *fn,char **s) {
  314.     static char buf[100];
  315.     char *bp;
  316.     int len = 0, d;
  317. #ifdef MAXNAMLEN    
  318.     int maxlen = MAXNAMLEN;
  319. #else
  320.     int maxlen = 14;
  321. #endif
  322.  
  323.     bp = buf;
  324.     while (*fn) {            /* Copy name into buf */
  325.     *bp++ = *fn++;
  326.     len++;
  327.     }
  328.     if (len > maxlen-3) bp -= 3;    /* Don't let it get too long */
  329.  
  330.     /* 
  331.      * On the Amiga, it takes much less time to determine
  332.      * if a given file exists than to read all the file names in
  333.      * a directory (or even just names with a certain prefix).
  334.      */
  335.     d = 0;
  336.     do {
  337.     sprintf(bp, "~%d", ++d);
  338.     } while (zchki(buf) != -1 && d < 100);
  339.  
  340.     *s = buf;
  341. }
  342.  
  343. /* Z X P A N D --- Expand wild card file name list */
  344.  
  345. #define MAXWLD 300
  346. static char *mtchs[MAXWLD],        /* Matches found for filename */
  347.   **mtchptr;                    /* Pointer to current match */
  348. static int fcount;                      /* Number of matches */
  349. #define SSPACE 4000
  350. static char sspace[SSPACE];             /* buffer to generate names in */
  351.  
  352. int
  353. zxpand(char *fn) {
  354.    char *sptr = sspace;
  355.    long state;
  356.  
  357.    if ((state = callaa(xffirst, work, fn)) == 0)
  358.       return 0;
  359.    if (strlen(work) >= SSPACE)          /* Unlikely */
  360.       return -1;
  361.    strcpy(sptr, work);
  362.    mtchs[0] = sptr;
  363.    mtchptr = mtchs;
  364.    fcount = 1;
  365.    sptr += (strlen(sptr) + 1);
  366.    while ((state = calldaa(xfnext, state, work, fn)) != 0) {
  367.       if (sptr - sspace + strlen(work) >= SSPACE)
  368.      return -1;
  369.       strcpy(sptr, work);
  370.       mtchs[fcount++] = sptr;
  371.       sptr += (strlen(sptr) + 1);
  372.    }
  373.    return(fcount);
  374. }
  375.  
  376. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  377. /*
  378.  Returns >0 if there's another file, with its name copied into the arg string,
  379.  or 0 if no more files in list.
  380. */
  381. int
  382. znext(char *fn) {
  383.     if (fcount-- > 0)
  384.        strcpy(fn,*mtchptr++);
  385.     else
  386.        *fn = '\0';
  387.     debug(F111,"znext",fn,fcount+1);
  388.     return(fcount+1);
  389. }
  390.  
  391. /*  Z O P E N I  --  Open an existing file for input. */
  392.  
  393. int
  394. zopeni(int n, char *fn) {
  395.    if (chkfn(n) != 0)
  396.       return 0;
  397.    fp[n] = callaa(xfopen, fn, "r");        /* Real file. */
  398.    debug(F111," zopeni", name, (int) fp[n]);
  399.    return((fp[n] != 0L) ? 1 : 0);
  400. }
  401.  
  402. /*  Z O P E N O  --  Open a new file for output.  */
  403. int
  404. zopeno(int n, char *name, struct zattr *zz, struct filinfo *fcb) {
  405.  
  406.     char *p;        /* Local use pointer */
  407.  
  408.     if (fcb) {    
  409.     debug(F101,"zopeno fcb disp","",fcb->dsp);
  410.     debug(F101,"zopeno fcb type","",fcb->typ);
  411.     debug(F101,"zopeno fcb char","",fcb->cs);
  412.     } else {
  413.     debug(F100,"zopeno fcb is NULL","",0);
  414.     }
  415.     if (n != ZDFILE)
  416.       debug(F111," zopeno",name,n);
  417.     if (chkfn(n) != 0) return(0);
  418.     p = "w";                /* Assume write/create mode */
  419.     if (fcb) {                /* If called with an FCB... */
  420.     if (fcb->dsp == XYFZ_A)        /* Does it say Append? */
  421.       p = "a";            /* Yes. */
  422.     }
  423.     fp[n] = callaa(xfopen, name, p);    /* Open the file */
  424.  
  425.     if (fp[n] == 0L)
  426.        perror("zopeno can't open");
  427.     zoutcnt = 0;        /* (PWP) reset output buffer */
  428.     zoutptr = zoutbuffer;
  429.     if (n != ZDFILE)
  430.        debug(F101, " fp[n]", "", (int) fp[n]);
  431.     return((fp[n] != 0L) ? 1 : 0);
  432. }
  433.  
  434. /* (PWP) buffered character output routine to speed up file IO */
  435. int
  436. zoutdump(void) {
  437.     int x;
  438.     zoutptr = zoutbuffer;        /* reset buffer pointer in all cases */
  439.     debug(F101,"zoutdump chars","",zoutcnt);
  440.     if (zoutcnt == 0) {            /* nothing to output */
  441.     return(0);
  442.     } else if (zoutcnt < 0) {        /* unexpected negative value */
  443.     zoutcnt = 0;            /* reset output buffer count */
  444.     return(-1);            /* and fail. */
  445.     }
  446.     if (x = calladda(xfwrite, zoutbuffer, 1, zoutcnt, fp[ZOFILE])) {
  447.     debug(F101,"zoutdump fwrite wrote","",x);
  448.     zoutcnt = 0;            /* reset output buffer count */
  449.     return(0);            /* things worked OK */
  450.     } else {
  451.     zoutcnt = 0;            /* reset output buffer count */
  452.     debug(F101,"zoutdump fwrite error","",x);
  453.     return(x ? -1 : 0);        /* return failure if error */
  454.     }
  455. }
  456.  
  457. int
  458. zprint(char *p, char *f) {
  459.    return 0;
  460. }
  461.  
  462. int
  463. zrename(char *fn, char *fn2) {
  464.    return 0;
  465. }
  466.  
  467. /*  Z R T O L  --  Convert remote filename into local form  */
  468.  
  469. /*  For AMIGA, this means changing uppercase letters to lowercase.  */
  470.  
  471. void
  472. zrtol(char *name, char *name2) {
  473.     for ( ; *name != '\0'; name++) {
  474.         *name2++ = isupper(*name) ? tolower(*name) : *name;
  475.     }
  476.     *name2 = '\0';
  477.     debug(F110,"zrtol:",name2,0);
  478. }
  479.  
  480. int
  481. zsattr(struct zattr *xx) {
  482.    long k;
  483.  
  484.    k = iflen % 1024L;                   /* File length in K */
  485.    if (k != 0L) k = 1L;
  486.    xx->lengthk = (iflen / 1024L) + k;
  487.    if (iftype == 1L)
  488.       xx->type.val = "text";
  489.    else
  490.       xx->type.val = "binary";
  491.    xx->type.len = strlen(xx->type.val);
  492.    xx->date.len = 0;
  493.    xx->date.val = "";
  494.     xx->creator.len = 0;        /* File creator */
  495.     xx->creator.val = "";
  496.     xx->account.len = 0;        /* File account */
  497.     xx->account.val = "";
  498.     xx->area.len = 0;            /* File area */
  499.     xx->area.val = "";
  500.     xx->passwd.len = 0;            /* Area password */
  501.     xx->passwd.val = "";
  502.     xx->blksize = -1L;            /* File blocksize */
  503.     xx->access.len = 0;            /* File access */
  504.     xx->access.val = "";
  505.     xx->encoding.len = 0;        /* Transfer syntax */
  506.     xx->encoding.val = 0;
  507.     xx->disp.len = 0;            /* Disposition upon arrival */
  508.     xx->disp.val = "";
  509.     xx->lprotect.len = 0;        /* Local protection */
  510.     xx->lprotect.val = "";
  511.     xx->gprotect.len = 0;        /* Generic protection */
  512.     xx->gprotect.val = "";
  513.     xx->systemid.len = 2;        /* System ID length */
  514.     xx->systemid.val = "L3";        /* Amiga system ID code */
  515.     xx->recfm.len = 0;            /* Record format */
  516.     xx->recfm.val = "";
  517.     xx->sysparam.len = 0;        /* System-dependent parameters */
  518.     xx->sysparam.val = "";
  519.     xx->length = iflen;            /* Length */
  520.     return(0);
  521. }
  522.  
  523. int
  524. zshcmd(char *s) {
  525.    return -1;
  526. }
  527.  
  528. int
  529. zsyscmd(char *s) {
  530.    return -1;
  531. }
  532.  
  533. /*  Z S I N L  --  Read a line from a file  */
  534.  
  535. /*
  536.   Writes the line into the address provided by the caller.
  537.   n is the Kermit "channel number".
  538.   Writing terminates when newline is encountered, newline is not copied.
  539.   Writing also terminates upon EOF or if length x is exhausted.
  540.   Returns 0 on success, -1 on EOF or error.
  541. */
  542. int
  543. zsinl(int n, char *s, int x) {
  544.     int z = 0, a;
  545.  
  546.     if (chkfn(n) < 1) {            /* Make sure file is open */
  547.     return(-1);
  548.     }
  549.     while (x--) {
  550.     if (zchin(n,&a) < 0) {        /* Read a character from the file */
  551.         z = -1;
  552.         break;
  553.     }
  554.     if (a == '\n') break;
  555.     *s = a;
  556.     s++;
  557.     }
  558.     *s = '\0';
  559.     return(z);
  560. }
  561.  
  562. /*  Z S O U T  --  Write a string to the given file, buffered.  */
  563. int
  564. zsout(int n, char *s) {
  565.    if (chkfn(n) < 1) return(-1);
  566.    if (strlen(s) == 0)
  567.       return 0;
  568.    if (calladda(xfwrite, s, sizeof(char), strlen(s), fp[n]) <= 0)
  569.       return -1;
  570.    return(strlen(s));
  571. }
  572.  
  573. /*  Z S O U T L  --  Write string to file, with line terminator, buffered  */
  574. int
  575. zsoutl(int n, char *s) {
  576.     if (chkfn(n) < 1) return(-1);
  577.     if (strlen(s) > 0)
  578.        if (calladda(xfwrite, s, sizeof(char), strlen(s), fp[n]) <= 0)
  579.       return -1;
  580.     if (calladda(xfwrite, "\n", sizeof(char), 1, fp[n]) <= 0)
  581.        return -1;;
  582.     return(0);
  583. }
  584.  
  585. int
  586. zsoutx(int n, char *s, int x) {
  587.    if (x > 0)
  588.       if (calladda(xfwrite, s, sizeof(char), x, fp[n]) != x)
  589.      return -1;
  590.    return x;
  591. }
  592.  
  593. int
  594. zstime(char *f, struct zattr *yy, int x) {
  595.    return 0;
  596. }
  597.  
  598. int
  599. zxcmd(int n, char *s) {
  600.    return 0;
  601. }
  602.  
  603. int
  604. xsystem(char *cmd) {
  605.    return -1;
  606. }
  607.  
  608. int
  609. priv_ini(void) {
  610.    return 0;
  611. }
  612.  
  613. int
  614. priv_on(void) {
  615.    return 0;
  616. }
  617.  
  618. int
  619. priv_off(void) {
  620.    return 0;
  621. }
  622.  
  623. int
  624. priv_can(void) {
  625.    return 0;
  626. }
  627.  
  628.  
  629. int
  630. priv_chk(void) {
  631.    return 0;
  632. }
  633.