home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / convergent / ctzcto.c < prev   
C/C++ Source or Header  |  2020-01-01  |  11KB  |  395 lines

  1. char *ckzv = "File access CTOS/BTOS Version-2.00, Sep 1991";
  2.  
  3. /* Joel Dunn, UNC-CH Administrative Data Processing */
  4. /* Modification    Joel Dunn, 9/21/87
  5. zchki returns an integer filesize, thus
  6. it had problems for >32K.  Converted size to Kbytes */
  7.  
  8. /* Modification Doug Drury ITT-Federal Services Corp  6/91
  9. ZCLOSE checked file pointer to see if it is pointing to origional 
  10. vidio file pointer.  If it is, the file is not closed.  This fixes
  11. a problem that existed with the remote and remote host commands.  After
  12. a remote or remote host was executed, the file pointer to the local
  13. vidio was closed */
  14.  
  15. char *ckzsys = " CTOS 10.3 Standard Software";
  16. /* Definitions of some Unix system commands, retain during conversion */
  17.  
  18. char *DIRCMD = "";
  19. char *DELCMD = "";
  20. char *TYPCMD = "";
  21. char *SPACMD = "";
  22. char *SPACM2 = "";
  23. char *WHOCMD = "";
  24.  
  25. /*
  26.   Functions (n is one of the predefined file numbers from ckermi.h):
  27.  
  28.    zopeni(n,name)   -- Opens an existing file for input.
  29.    zopeno(n,name)   -- Opens a new file for output.
  30.    zclose(n)        -- Closes a file.
  31.    zchin(n)         -- Gets the next character from an input file.
  32.    zsout(n,s)       -- Write a null-terminated string to output file.
  33.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  34.    zsoutx(n,s,x)    -- Write x characters to output file.
  35.    zchout(n,c)      -- Add a character to an output file.
  36.    zchki(name)      -- Check if named file exists and is readable, ret size.
  37.    zchko(name)      -- Check if named file can be created.
  38.    znewn(name,s)    -- Make a new unique file name based on the given name.
  39.    zdelet(name)     -- Delete the named file.
  40.    zxpand(string)   -- NOT CURRENTLY SUPPORTED, returns file name unchanged.
  41.    znext(string)    -- Returns the next file from list in string.
  42.    zxcmd(cmd)       -- NOT CURRENTLY SUPPORTED
  43.    zrtol(n1,n2)     -- Convert remote filename into local form.
  44.    zltor(n1,n2)     -- Convert local filename into remote form.
  45.    zchdir(dirnam)   -- Change working directory.
  46.    zhome()          -- Return pointer to home directory name string.
  47.  */
  48.  
  49. /* Includes */
  50.  
  51. #include "ctermi.h"            /* Kermit definitions, ctype, stdio */
  52.  
  53. #ifndef MAXNAMLEN
  54. #define MAXNAMLEN 50            /* If still not defined... */
  55. #endif
  56.  
  57. #define MAXWLD 2 /* maximum wildcard filenames */
  58.  
  59. /* Declarations */
  60.  
  61. char *fp[ZNFILS];  /* array of pointers to BSWA's */
  62. char *fbuff[ZNFILS];  /* array of pointers to file buffers */
  63. static int erc;
  64. extern char bsvid[];
  65. static int fcount; /* number of files in wild group */
  66. static char *mtchs[MAXWLD], /* matches found for filename */
  67.             **mtchptr; /* pointer to current match */
  68.  
  69. /*  Z O P E N I  --  Open an existing file for input. */
  70.  
  71. zopeni(n,name) int n; char *name; {
  72.     debug(F111," zopeni",name,n);
  73.     debug(F101,"  fp","", n);
  74.     if (chkfn(n) != 0) return(0);
  75.     if (n == ZSTDIO) {            /* Standard input? */
  76.     fprintf(stderr, "?Terminal input not allowed\n");
  77.     debug(F110, "zopeni: attempts input from stdin","",0);
  78.     return(0);
  79.     }
  80.     erc = allocmemorysl(130, &fp[n]);
  81.     if (erc) { debug(F101,"zopeni - allocmemorysl of fp","", erc);
  82.                     perror("zopeni - allocmemorysl of fp");
  83.                 return(0);}
  84.     erc = allocmemorysl(2048, &fbuff[n]);
  85.     if (erc) { debug(F101,"zopeni - allocmemorysl of fbuff","", erc);
  86.                     perror("zopeni - allocmemorysl of fbuff");
  87.                 return(0);}
  88.     erc = openbytestream(fp[n], name, strlen(name), 0l, 0, 0x6d72,
  89.                         fbuff[n], 2048);
  90.     debug(F111," zopeni", name, n);
  91.     if (erc != 0) perror("zopeni");
  92.     return((erc == 0) ? 1 : 0);
  93. }
  94.  
  95. /*  Z O P E N O  --  Open a new file for output.  */
  96.  
  97. zopeno(n,name) int n; char *name; {
  98.     debug(F111," zopeno",name,n);
  99.     debug(F101,"  fp","", n);
  100.     if (chkfn(n) != 0) return(0);
  101.     if ((n == ZCTERM) || (n == ZSTDIO)) {   /* Terminal or standard output */
  102.     fp[ZOFILE] = &bsvid[0];
  103.     debug(F101," fp[]=stdout", "", n);
  104.     return(1);
  105.     }
  106.     erc = allocmemorysl(130, &fp[n]);
  107.     if (erc) { debug(F101,"zopeno - allocmemorysl of fp","", erc);
  108.                     perror("zopeno - allocmemorysl of fp");
  109.                 return(0);}
  110.     erc = allocmemorysl(2048, &fbuff[n]);
  111.     if (erc) { debug(F101,"zopeno - allocmemorysl of fbuff","", erc);
  112.                     perror("zopeno - allocmemorysl of fbuff");
  113.                 return(0);}
  114.     erc = openbytestream(fp[n], name, strlen(name), 0l, 0, 0x6d77,
  115.                         fbuff[n], 2048);
  116.     debug(F111," zopeno", name, n);
  117.     if (erc != 0) perror("zopeno");
  118.     return((erc == 0) ? 1 : 0);
  119. }
  120.  
  121. /*  Z C L O S E  --  Close the given file.  */
  122.  
  123. zclose (n) int n; {
  124.     if (chkfn(n) < 1) return(0);
  125. /*    return(1); temp test code, receive works with this line in */
  126.     erc = 0;
  127.         if (fp[n] != &bsvid[0]) {  
  128.       if ((n != ZCTERM ) && (n != ZSTDIO)) {
  129.                erc = closebytestream(fp[n]);
  130.           }                        
  131.         }
  132.                fp[n] = NULL;
  133.           if (erc == 0) return(1);
  134.     else return(0);
  135. }
  136.  
  137. /*  Z C H I N  --  Get a character from the input file.  */
  138.  
  139. zchin(n) int n; {
  140.     int a;
  141.     if (chkfn(n) < 1) return(-1);
  142.     erc = readbyte(fp[n], &a);
  143.     return((erc != 0) ? -1 : a & 0377);
  144. }
  145.  
  146. /*  Z S O U T  --  Write a string to the given file.  */
  147.  
  148. zsout(n,s) int n; char *s; {
  149.     int cbwritten;
  150.     if (chkfn(n) < 1) return(-1);
  151.     erc = writebsrecord(fp[n], s, strlen(s), &cbwritten);
  152.     return(0);
  153. }
  154.  
  155. /*  Z S O U T L  --  Write string to file, with line terminator  */
  156.  
  157. zsoutl(n,s) int n; char *s; {
  158.     int cbwritten;
  159.     if (chkfn(n) < 1) return(-1);
  160.     erc = writebsrecord(fp[n], s, strlen(s), &cbwritten);
  161.     erc = writebyte(fp[n], 0x0a);
  162.     return(0);
  163. }
  164.  
  165. /*  Z S O U T X  --  Write x characters to file */
  166.  
  167. zsoutx(n,s,x) int n, x; char *s; {
  168.     int i;
  169.     if (chkfn(n) < 1) return(-1);
  170.     for (i = 0; i < x; i++) erc = writebyte(fp[n], *s++);
  171.     return(0);
  172. }
  173.  
  174.  
  175. /*  Z C H O U T  --  Add a character to the given file.  */
  176.  
  177. zchout(n,c) int n; char c; {
  178.     if (chkfn(n) < 1) return(-1);
  179.     erc = writebyte(fp[n], c);
  180.     return(0);
  181. }
  182.  
  183. /*  C H K F N  --  Internal function to verify file number is ok  */
  184.  
  185. /*
  186.  Returns:
  187.   -1: File number n is out of range
  188.    0: n is in range, but file is not open
  189.    1: n in range and file is open
  190. */
  191. chkfn(n) int n; {
  192.     switch (n) {
  193.     case ZCTERM:
  194.     case ZSTDIO:
  195.     case ZIFILE:
  196.     case ZOFILE:
  197.     case ZDFILE:
  198.     case ZTFILE:
  199.     case ZPFILE:
  200.     case ZSFILE: break;
  201.     default:
  202.         debug(F101,"chkfn: file number out of range","",n);
  203.         fprintf(stderr,"?File number out of range - %d\n",n);
  204.         return(-1);
  205.     }
  206.     return( (fp[n] == NULL) ? 0 : 1 );
  207. }
  208.  
  209. /*  Z C H K I  --  Check if input file exists and is readable  */
  210.  
  211. /*
  212.   Returns:
  213.    >= 0 if the file can be read (returns the size in Kbytes).
  214.      -1 if file doesn't exist or can't be accessed,
  215.      -2 if file exists but is not readable (e.g. a directory file).
  216.      -3 if file exists but protected against read access.
  217. */
  218. zchki(name) char *name; {
  219.     int fh;
  220.     int erc;
  221.     int erc2;
  222.     long int filelenret;
  223.     int filelen;
  224.     erc = openfile(&fh, name, strlen(name), name, 0, 0x6d72);
  225.     if (erc == 203) return(-1);
  226.     if (erc == 219) return(-3);
  227.     if (erc != 0) return(-2);
  228.     erc = getfilestatus(fh, 0, &filelenret, 4);
  229.     erc2 = closefile(fh);
  230.     if (erc == 0)
  231.         {
  232.         filelenret /= 1024l;
  233.         filelen = filelenret;
  234.         if (!filelen) filelen = 1;
  235.         return(filelen);
  236.         }
  237.     else return(-2);
  238.     
  239. }
  240.  
  241. /*  Z C H K O  --  Check if output file can be created  */
  242.  
  243. /*
  244.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  245. */
  246. zchko(name) char *name; {
  247.     return(0);
  248. }
  249.  
  250. /*  Z D E L E T  --  Delete the named file.  */
  251.  
  252. zdelet(name) char *name; {
  253.     int fh;
  254.     erc = openfile(&fh, name, strlen(name), name, 0, 0x6d6d);
  255.     if (erc == 0) deletefile(fh);
  256.     return(erc);
  257. }
  258.  
  259.  
  260. /*  Z R T O L  --  Convert remote filename into local form  */
  261.  
  262. /*  For UNIX, this means changing uppercase letters to lowercase.  */
  263.  
  264. zrtol(name,name2) char *name, *name2; {
  265.     for ( ; *name != '\0'; name++) {
  266.         *name2++ = isupper(*name) ? tolower(*name) : *name;
  267.     }
  268.     *name2 = '\0';
  269. }
  270.  
  271.  
  272. /*  Z L T O R  --  Convert filename from local format to common form.   */
  273.  
  274. zltor(name,name2) char *name, *name2; {
  275.     char work[100], *cp, *pp;
  276.     int dc = 0;
  277.  
  278.     strcpy(work,name);
  279.     for (cp = pp = work; *cp != '\0'; cp++) {    /* strip path name */
  280.         if (*cp == '/') {
  281.         pp = cp;
  282.         pp++;
  283.     }
  284.     else if (islower(*cp)) *cp = toupper(*cp); /* Uppercase letters */
  285.     else if (*cp == '~') *cp = 'X';    /* Change tilde to 'X' */
  286.     else if ((*cp == '.') && (++dc > 1)) *cp = 'X'; /* & extra dots */
  287.     }
  288.     cp = name2;                /* If nothing before dot, */
  289.     if (*pp == '.') *cp++ = 'X';    /* insert 'X' */
  290.     strcpy(cp,pp);
  291. }    
  292.  
  293.  
  294. /*  Z X C M D -- Run a system command so its output can be read like a file
  295.                 ----- not implemented under CTOS */
  296.  
  297. zxcmd(comand) char *comand; {
  298.     return(0); /* return of any value less than 1 is considered a failure */
  299. }
  300.  
  301. /* P E R R O R -- print error string */
  302. perror(s) char *s;
  303. {
  304.     printf(s);
  305.     putchar('\n');
  306.     return(0);
  307. }
  308.  
  309. /* zhome -- return a null pointer for CTOS */
  310. char *zhome() { return(NULL); }
  311.  
  312. /* Z X P A N D -- Expand a wildcard string into an array of strings --
  313.                   WILDCARDS NOT YET SUPPORTED UNDER CTOS */
  314. zxpand(fn) char *fn;
  315. {
  316.     static char fnbuff[50];
  317.     strcpy(fnbuff, fn);
  318.     mtchs[0] = &fnbuff[0];
  319.     if (fnbuff[0] != '\0') fcount = 1;
  320.     else fcount = 0;
  321.     if (fcount > 0) mtchptr = mtchs; /* save pointer for next */
  322.     debug(F111, "zxpand",mtchs[0],fcount);
  323.     return(fcount);
  324. }
  325.  
  326. /* Z N E W N -- Make a new name for the given file */
  327. znewn(fn,s) char *fn, **s;
  328. {
  329.     static char buf[100];
  330.     char *timeptr;
  331.     char *rootname="CTOS-kermit";
  332.     strcpy(buf, rootname);
  333.     ztime(&timeptr);
  334.     strcat(buf, timeptr);
  335.     *s = buf;
  336. }
  337.  
  338. /* Z N E X T -- Get the name of next file from list created by zxpand().
  339.   Returns > 0 if there's another file, with its name copied
  340.   into the arg string, or 0 if no more files in the list.
  341. */
  342. znext(fn) char *fn;
  343. {
  344.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  345.     else *fn  = '\0';
  346.     debug(F111, "znext",fn,fcount+1);
  347.     return(fcount+1);
  348. }
  349.  
  350. /* Z C H D I R -- Change directory, string of form [vol]<directory> */
  351. zchdir(dirnam)
  352.  
  353. char *dirnam;
  354.  
  355. {
  356.     char ch;
  357.     int namln = strlen(dirnam);
  358.     int i;
  359.     char *v, *d, *p;
  360.     int vl=0, dl=0, pl=0;
  361.     struct ucbtype
  362.         {
  363.         int reserved;
  364.         char sizevol;
  365.         char volname[12];
  366.         char sizedir;
  367.         char dirname[12];
  368.         char sizepw;
  369.         char password[12];
  370.         } ucb;
  371.  
  372.     if ((ch = *dirnam++) != 0x5b) return(-1);
  373.     v = dirnam;
  374.     for (vl = 0; vl < namln; vl++)
  375.         if ((ch = *dirnam++) == 0x5d) break;
  376.     if ((ch = *dirnam++) != 0x3c) return(-1);
  377.     d = dirnam;
  378.     for (dl = 0; dl < namln-(vl+2); dl++)
  379.         if ((ch = *dirnam++) == 0x3e) break;
  380.     if (getucb(&ucb, sizeof(ucb))) return(-1);
  381.     p = &ucb.password[0];
  382.     pl = ucb.sizepw;
  383.     if ((ch = *dirnam++) == 0x5e)
  384.         {
  385.         p = dirnam;
  386.         for (pl = 0; pl < namln-(vl+dl+4); pl++)
  387.             if ((ch = *dirnam++) == 0x00) break;
  388.         }
  389.     if (erc = setpath(v, vl, d, dl, p, pl)) return(-1);
  390.     return(0);
  391. }
  392.  
  393. /* zclosf -- close fork; return 0 for CTOS */
  394. zclosf() { return(0); }
  395.