home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d026 / c-kermit.lha / C-kermit / src / ckifio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-16  |  9.9 KB  |  393 lines

  1. char *ckzv = "AMIGA file support, 1.0(000)+0, 30 Mar 1986";
  2. char *ckzsys = " Commodore AMIGA";
  3.  
  4. /* C K I F I O  --  Kermit file system support for Commodore AMIGA */
  5.  
  6. /*
  7.  * Copyright (C) 1986, Trustees of Columbia University in the City of
  8.  * New York.  Permission is granted to any individual or institution to
  9.  * use, copy, or redistribute this software, so long as it is not sold
  10.  * for profit, provided this copyright notice is retained.
  11.  */
  12.  
  13. /* Edit history
  14.  * 28 Feb 1986 - Davide P. Cervone - wrote it.  Based on CKVFIO.C
  15.  *  still have to work out wildcard file names
  16.  */
  17.  
  18. /* Definitions of some AMIGA system commands */
  19.  
  20. char *DIRCMD = "DIRECTORY ";      /* For directory listing */
  21. char *DELCMD = "DELETE ";         /* For file deletion */
  22. char *TYPCMD = "TYPE ";           /* For typing a file */
  23. char *SPACMD = "INFO ";           /* Space/quota of current directory */
  24. char *SPACM2 = "LIST ";           /* Space/quota of current directory */
  25. char *WHOCMD = "STATUS ";         /* For seeing who's logged in */
  26.  
  27.  
  28.  
  29. /*
  30.   Functions (n is one of the predefined file numbers from ckermi.h):
  31.  
  32.    zopeni(n,name)   -- Opens an existing file for input.
  33.    zopeno(n,name)   -- Opens a new file for output.
  34.    zclose(n)        -- Closes a file.
  35.    zchin(n)         -- Gets the next character from an input file.
  36.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  37.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  38.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  39.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  40.    zchki(name)      -- Check if named file exists and is readable, return size.
  41.    zchko(name)      -- Check if named file can be created.
  42.    znewn(name,s)    -- Make a new unique file name based on the given name.
  43.    zdelet(name)     -- Delete the named file.
  44.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  45.    znext(string)    -- Returns the next file from the list in "string".
  46.    zxcmd(cmd)       -- Execute the command in a lower fork.
  47.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  48.    zrtol(n1,n2)     -- Convert remote filename into local form.
  49.    zltor(n1,n2)     -- Convert local filename into remote form.
  50.    zchdir(dirnam)   -- Change working directory.
  51.    zhome()          -- Return pointer to home directory name string.
  52.    zkself()         -- Log self out
  53.  */
  54.  
  55.  
  56.  
  57. /* Includes */
  58.  
  59. #include <stdio.h>
  60. #include <ctype.h>
  61. #include "ckcker.h"
  62. #include "ckcdeb.h"
  63.  
  64. #define MAXWLD 50         /* Maximum wildcard filenames */
  65.  
  66.  
  67. /* Declarations */
  68.  
  69. FILE *fp[ZNFILS] = {            /* File pointers */
  70.     NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  71.  
  72. static int fcount;              /* Number of files in wild group */
  73. char *getenv(), *strcpy();      /* For finding home directory */
  74.  
  75. static char *mtchs[MAXWLD],     /* Matches found for filename */
  76.      **mtchptr;                 /* Pointer to current match */
  77.  
  78.  
  79.  
  80. /***  Z K S E L F --  Log self out  ***/
  81.  
  82. zkself() {
  83.    doexit(0);
  84. }
  85.  
  86.  
  87. /*  Z O P E N I  --  Open an existing file for input. */
  88.  
  89. zopeni(n,name) int n; char *name; {
  90.     debug(F111," zopeni",name,n);
  91.     debug(F101,"  fp","",(int) fp[n]);
  92.     if (chkfn(n) != 0) return(0);
  93.     if (n == ZSYSFN) {           /* Input from a system function? */
  94.        return(zxcmd(name));      /* Try to fork the command */
  95.     }
  96.     if (n == ZSTDIO) {           /* Standard input? */
  97.        fp[ZIFILE] = stdin;
  98.        return(1);
  99.     }
  100.     fp[n] = fopen(name,"r");      /* Real file. */
  101.     if (fp[n] == NULL) perror(name);
  102.     debug(F111," zopeni", name, (int) fp[n]);
  103.     return((fp[n] != NULL) ? 1 : 0);
  104. }
  105.  
  106. /*  Z O P E N O  --  Open a new file for output.  */
  107.  
  108. zopeno(n,name) int n; char *name; {
  109.     debug(F111," zopeno",name,n);
  110.     if (chkfn(n) != 0) return(0);
  111.     if ((n == ZCTERM) || (n == ZSTDIO)) {   /* Terminal or standard output */
  112.        fp[ZOFILE] = stdout;
  113.        debug(F101," fp[]=stdout", "", (int) fp[n]);
  114.        return(1);
  115.     }
  116.     fp[n] = fopen(name, "w");
  117.     if (fp[n] == NULL) perror(name);
  118.     if (n == ZDFILE)
  119.        setbuf(fp[n], (char *)NULL);    /* Make debugging file unbuffered */
  120.     debug(F101, " fp[n]", "", (int) fp[n]);
  121.     return((fp[n] != NULL) ? 1 : 0);
  122. }
  123.  
  124. /*  Z C L O S E  --  Close the given file.  */
  125.  
  126. zclose(n) int n; {
  127.     if (chkfn(n) < 1) return(0);
  128.     if ((fp[n] != stdout) && (fp[n] != stdin)) fclose(fp[n]);
  129.     fp[n] = NULL;
  130.     return(1);
  131. }
  132.  
  133. /*  Z C H I N  --  Get a character from the input file.  */
  134.  
  135. zchin(n,c) int n; char *c; {
  136.     int a;
  137.     if (chkfn(n) < 1) return(-1);
  138.     a = getc(fp[n]);
  139.     if (a == EOF) return(-1);
  140.     *c = (a & 0377);
  141.     return(0);
  142. }
  143.  
  144.  
  145.  
  146. /*  Z S O U T  --  Write a string to the given file, buffered.  */
  147.  
  148. zsout(n,s) int n; char *s; {
  149.     if (chkfn(n) < 1) return(-1);
  150.     fputs(s, fp[n]);         /* Don't use fprintf here MM */
  151.     return(0);
  152. }
  153.  
  154. /*  Z S O U T L  --  Write string to file, with line terminator, buffered  */
  155.  
  156. zsoutl(n,s) int n; char *s; {
  157.     if (chkfn(n) < 1) return(-1);
  158.     fputs(s, fp[n]);         /* Don't use fprintf MM */
  159.     putc('\n', fp[n]);
  160.     return(0);
  161. }
  162.  
  163. /*  Z S O U T X  --  Write x characters to file, unbuffered.  */
  164.  
  165. zsoutx(n,s,x) int n, x; char *s; {
  166.     if (chkfn(n) < 1) return(-1);
  167.     return(write(fileno(fp[n]),s,x));    /* fnf */
  168. }
  169.  
  170.  
  171. /*  Z C H O U T  --  Add a character to the given file.  */
  172.  
  173. zchout(n,c) int n; char c; {
  174.     if (chkfn(n) < 1) return(-1);
  175.     if (n == ZSFILE)
  176.        return(write(fileno(fp[n]),&c,1)); /* Use unbuffered for session log */
  177.     else {
  178.        if (putc(c,fp[n]) == EOF)   /* If true, maybe there was an error */
  179.           return(ferror(fp[n]));   /* Check to make sure */
  180.        else                        /* Otherwise... */
  181.           return(0);               /* There was no error. */
  182.     }
  183. }
  184.  
  185.  
  186.  
  187. /*  C H K F N  --  Internal function to verify file number is ok  */
  188.  
  189. /*
  190.  Returns:
  191.   -1: File number n is out of range
  192.    0: n is in range, but file is not open
  193.    1: n in range and file is open
  194. */
  195. chkfn(n) int n; {
  196.     switch (n) {
  197.    case ZCTERM:
  198.    case ZSTDIO:
  199.    case ZIFILE:
  200.    case ZOFILE:
  201.    case ZDFILE:
  202.    case ZTFILE:
  203.    case ZPFILE:
  204.    case ZSFILE: break;
  205.    default:
  206.        debug(F101,"chkfn: file number out of range","",n);
  207.        printf2("?File number out of range - %d\n",n);
  208.        return(-1);
  209.     }
  210.     return( (fp[n] == NULL) ? 0 : 1 );
  211. }
  212.  
  213.  
  214.  
  215. /*  Z C H K I  --  Check if input file exists and is readable  */
  216.  
  217. /*
  218.   Returns:
  219.    >= 0 if the file can be read (returns the size).
  220.      -1 if file doesn't exist or can't be accessed,
  221.      -2 if file exists but is not readable (e.g. a directory file).
  222.      -3 if file exists but protected against read access.
  223. */
  224. /*
  225.  For Berkeley Unix, a file must be of type "regular" to be readable.
  226.  Directory files, special files, and symbolic links are not readable.
  227. */
  228. long
  229. zchki(name) char *name; {
  230.     int x; long pos;
  231.  
  232.     x = open(name, 0);
  233.     if (x < 0) {
  234.        debug(F111,"zchki stat fails",name,errno);
  235.        return(-1);
  236.     }
  237.     pos = lseek(x, 0L, 2);
  238.     close(x);
  239.     return(pos);
  240. }
  241.  
  242.  
  243.  
  244. /*  Z C H K O  --  Check if output file can be created  */
  245.  
  246. /*
  247.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  248. */
  249. zchko(name) char *name; {
  250.     return(0);       /* don't know how to check */
  251. }
  252.  
  253.  
  254.  
  255. /*  Z D E L E T  --  Delete the named file.  */
  256.  
  257. zdelet(name) char *name; {
  258.     return(unlink(name));
  259. }
  260.  
  261.  
  262. /*  Z R T O L  --  Convert remote filename into local form  */
  263.  
  264. /*  For AMIGA, we convert : and / to .  */
  265.  
  266. zrtol(name,name2) char *name, *name2; {
  267.  
  268.    for (; *name != '\0'; name++, name2++) *name2 = (*name == ':' || *name == '/') ? '.' : *name;
  269.    *name2 = '\0';
  270. }
  271.  
  272.  
  273. /*  Z L T O R  --  Convert filename from local format to common form.   */
  274.  
  275. /* remove all special characters, and any path names */
  276.  
  277. zltor(name,name2) char *name, *name2; {
  278.    char *oldname2;
  279.  
  280.    oldname2 = name2;
  281.    for (; *name != '\0'; name++)
  282.    {
  283.       if (*name == '/' || *name == ':')
  284.          name2 = oldname2;
  285.       else
  286.       {
  287.          *name2 = (isalnum(*name)) ? toupper(*name) : 'X';
  288.          name2++;
  289.       }
  290.    }
  291.    *name2 = '\0';
  292. }
  293.  
  294.  
  295. /*  Z C H D I R  --  Change directory  */
  296.  
  297. zchdir(dirnam) char *dirnam; {
  298.     char *hd;
  299.     if (*dirnam == '\0') hd = ":";
  300.     else hd = dirnam;
  301.     return((chdir(hd) == 0) ? 1 : 0);
  302. }
  303.  
  304.  
  305. /*  Z H O M E  --  Return pointer to user's home directory  */
  306.  
  307. char *
  308. zhome() {
  309.     return(":");
  310. }
  311.  
  312.  
  313.  
  314. /*  Z X C M D -- Run a system command so its output can be read like a file */
  315.  
  316. zxcmd(comand) char *comand; {
  317.     return(0);    /* for now, say we can't do it */
  318. }
  319.  
  320. /*  Z C L O S F  - close the subprocess output file.  */
  321.  
  322. zclosf() {
  323. }
  324.  
  325. /*  Z K I L L F  - kill the subprocess used for host commands  */
  326. /*  The return value is 1 if the subprocess was killed successfully. */
  327. /*         -1 if there was no subprocess to kill. */
  328.  
  329. zkillf() {
  330.    return(1);    /* always successful, since never any subprocesses, yet */
  331. }
  332.  
  333.  
  334.  
  335. /*  Z X P A N D  --  Expand a wildcard string into an array of strings  */
  336. /*
  337.   Returns the number of files that match fn1, with data structures set up
  338.   so that first file (if any) will be returned by the next znext() call.
  339. */
  340. zxpand(fn) char *fn; {
  341.     fcount = fgen(fn,mtchs,MAXWLD);   /* Look up the file. */
  342.     if (fcount > 0) {
  343.        mtchptr = mtchs;      /* Save pointer for next. */
  344.     }
  345.     debug(F111,"zxpand",mtchs[0],fcount);
  346.     return(fcount);
  347. }
  348.  
  349.  
  350. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  351. /*
  352.  Returns >0 if there's another file, with its name copied into the arg string,
  353.  or 0 if no more files in list.
  354. */
  355. znext(fn) char *fn; {
  356.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  357.     else *fn = '\0';
  358.     debug(F111,"znext",fn,fcount+1);
  359.     return(fcount+1);
  360. }
  361.  
  362.  
  363. /*  Z N E W N  --  Make a new name for the given file  */
  364.  
  365. znewn(fn,s) char *fn, **s; {
  366.     static char buf[100];
  367.     int i;
  368.  
  369.     strcpy(buf, fn);
  370.     i = strlen(buf);
  371.     buf[i+1] = '.';
  372.     buf [i+2] = '\0';
  373.     *s = buf;
  374. }
  375.  
  376.  
  377.  
  378. fgen(pat,resarry,len)
  379. char *pat,*resarry[];
  380. int len;
  381. {
  382.     extern char *malloc();
  383.  
  384.     resarry[0] = malloc(strlen(pat)+1);
  385.     strcpy(resarry[0], pat);
  386.     return(1);
  387. }
  388.  
  389. system(s)  char *s;  {
  390.  
  391.     zxcmd(s);
  392. }
  393.