📄 ckufio.c
字号:
char *ckzv = "Unix file support, 4E(037) 27 Jan 88";/* C K U F I O -- Kermit file system support for Unix systems *//* 4E, conditionals added for Apollo Aegis. *//* Author: Frank da Cruz (SY.FDC@CU20B), Columbia University Center for Computing Activities, January 1985. Copyright (C) 1985, Trustees of Columbia University in the City of New York. Permission is granted to any individual or institution to use, copy, or redistribute this software so long as it is not sold for profit, provided this copyright notice is retained. *//* Includes */#include <sys/types.h> /* Data types */#include "ckcker.h" /* Kermit definitions */#include "ckcdeb.h" /* Typedefs, debug formats, etc */#include <ctype.h> /* Character types */#include <stdio.h> /* Standard i/o */#ifdef MINIX#include <limits.h>#endif#include <sys/dir.h> /* Directory structure */#include <pwd.h> /* Password file for shell name */#ifdef CIE#include <stat.h> /* File status */#else#include <sys/stat.h>#endif/* Berkeley Unix Version 4.x *//* 4.1bsd support added by Charles E Brooks, EDN-VAX */#ifdef BSD4#ifdef MAXNAMLEN#define BSD42char *ckzsys = " 4.2 BSD";#else#ifdef FT18#define BSD41char *ckzsys = " Fortune For:Pro 1.8";#else#define BSD41char *ckzsys = " 4.1 BSD";#endif#endif#endif/* 2.9bsd support contributed by Bradley Smith, UCLA */#ifdef BSD29char *ckzsys = " 2.9 BSD";#endif/* Version 7 Unix */#ifdef V7char *ckzsys = " Version 7 Unix";#endif/* DEC Professional-300 series with Venturcom Venix v1 */#ifdef PROVX1char *ckzsys = " DEC Pro-3xx/Venix v1";#endif/* NCR Tower support contributed by John Bray, Auburn, AL. *//* Tower OS is like Sys III but with BSD features -- mostly follows BSD. */#ifdef TOWER1char *ckzsys = " NCR Tower 1632, OS 1.02";#endif/* Sys III/V, Xenix, PC/IX,... support by Herm Fischer, Litton Data Systems */#ifdef UXIII#ifdef XENIXchar *ckzsys = " Xenix/286";#else#ifdef PCIXchar *ckzsys = " PC/IX";#else#ifdef ISIIIchar *ckzsys = " Interactive Systems Corp, System III";#else#ifdef ZILOGchar *ckzsys = " Zilog S8000 Zeus 3.21+";#elsechar *ckzsys = " AT&T System III/System V";#endif#endif#endif#endif#endif/* Definitions of some Unix system commands */char *DELCMD = "rm -f "; /* For file deletion */char *PWDCMD = "pwd "; /* For saying where I am */#ifdef FT18char *DIRCMD = "ls -l | more "; /* For directory listing */char *TYPCMD = "more "; /* For typing a file */#elsechar *TYPCMD = "cat "; /* For typing a file */char *DIRCMD = "ls -l "; /* For directory listing */#endif#ifdef FT18#undef BSD4#endif#ifdef BSD4char *SPACMD = "pwd ; quota ; df ."; /* Space/quota of current directory */#else#ifdef FT18char #SPACMD = "pwd ; du ; df .";#elsechar *SPACMD = "df ";#endif#endifchar *SPACM2 = "df "; /* For space in specified directory */#ifdef FT18#define BSD4#endif#ifdef BSD4char *WHOCMD = "finger "; /* For seeing who's logged in */#elsechar *WHOCMD = "who "; /* For seeing who's logged in */#endif/* Functions (n is one of the predefined file numbers from ckermi.h): zopeni(n,name) -- Opens an existing file for input. zopeno(n,name) -- Opens a new file for output. zclose(n) -- Closes a file. zchin(n,&c) -- Gets the next character from an input file. zsout(n,s) -- Write a null-terminated string to output file, buffered. zsoutl(n,s) -- Like zsout, but appends a line terminator. zsoutx(n,s,x) -- Write x characters to output file, unbuffered. zchout(n,c) -- Add a character to an output file, unbuffered. zchki(name) -- Check if named file exists and is readable, return size. zchko(name) -- Check if named file can be created. znewn(name,s) -- Make a new unique file name based on the given name. zdelet(name) -- Delete the named file. zxpand(string) -- Expands the given wildcard string into a list of files. znext(string) -- Returns the next file from the list in "string". zxcmd(cmd) -- Execute the command in a lower fork. zclosf() -- Close input file associated with zxcmd()'s lower fork. zrtol(n1,n2) -- Convert remote filename into local form. zltor(n1,n2) -- Convert local filename into remote form. zchdir(dirnam) -- Change working directory. zhome() -- Return pointer to home directory name string. zkself() -- Kill self, log out own job. */#ifdef FT18#define PROVX1#endif/* Which systems include <sys/file.h>... */#ifndef PROVX1#ifndef aegis#ifndef CIE#ifndef MINIX#ifndef XENIX/* Watch out, some versions of Xenix might need to do this include, *//* but reportedly SCO Xenix 2.2 on an 80x86 system does not. */#include <sys/file.h> /* File access */#endif#endif#endif#endif#endif#ifdef FT18#undef PROVX1#endif/* Some systems define these symbols in include files, others don't... */#ifndef R_OK#define R_OK 4 /* For access */#endif#ifndef W_OK#define W_OK 2#endif#ifdef PROVX1#define MAXNAMLEN DIRSIZ /* Max file name length */#endif#ifdef UXIII#include <fcntl.h>#define MAXNAMLEN DIRSIZ#endif#ifndef O_RDONLY#define O_RDONLY 000#endif#ifndef MAXNAMLEN#define MAXNAMLEN 14 /* If still not defined... */#endif#ifdef PROVX1#define MAXWLD 50 /* Maximum wildcard filenames */#else#ifdef BSD29#define MAXWLD 50 /* Maximum wildcard filenames */#else#define MAXWLD 500#endif#endif/* Declarations */FILE *fp[ZNFILS] = { /* File pointers */ NULL, NULL, NULL, NULL, NULL, NULL, NULL };static int pid; /* pid of child fork */static int fcount; /* Number of files in wild group */static char nambuf[MAXNAMLEN+2]; /* Buffer for a filename */char *malloc(), *getenv(), *strcpy(); /* System functions */extern errno; /* System error code */static char *mtchs[MAXWLD], /* Matches found for filename */ **mtchptr; /* Pointer to current match *//* Z K S E L F -- Kill Self: log out own job, if possible. *//* Note, should get current pid, but if your system doesn't have *//* getppid(), then just kill(0,9)... */zkself() { /* For "bye", but no guarantee! */#ifdef PROVX1 return(kill(0,9));#else#ifdef V7 return(kill(0,9));#else#ifdef TOWER1 return(kill(0,9));#else#ifdef FT18 return(kill(0,9));#else#ifdef aegis return(kill(0,9));#else return(kill(getppid(),1));#endif#endif#endif#endif#endif}/* Z O P E N I -- Open an existing file for input. */zopeni(n,name) int n; char *name; { debug(F111," zopeni",name,n); debug(F101," fp","",(int) fp[n]); if (chkfn(n) != 0) return(0); if (n == ZSYSFN) { /* Input from a system function? */ debug(F110," invoking zxcmd",name,0); return(zxcmd(name)); /* Try to fork the command */ } if (n == ZSTDIO) { /* Standard input? */ if (isatty(0)) { ermsg("Terminal input not allowed"); debug(F110,"zopeni: attempts input from unredirected stdin","",0); return(0); } fp[ZIFILE] = stdin; return(1); } fp[n] = fopen(name,"r"); /* Real file. */ debug(F111," zopeni", name, (int) fp[n]); if (fp[n] == NULL) perror("zopeni"); return((fp[n] != NULL) ? 1 : 0);}/* Z O P E N O -- Open a new file for output. */zopeno(n,name) int n; char *name; { debug(F111," zopeno",name,n); if (chkfn(n) != 0) return(0); if ((n == ZCTERM) || (n == ZSTDIO)) { /* Terminal or standard output */ fp[ZOFILE] = stdout; debug(F101," fp[]=stdout", "", (int) fp[n]); return(1); } fp[n] = fopen(name,"w"); /* A real file, try to open */ if (fp[n] == NULL) { perror("zopeno can't open"); } else { chown(name, getuid(), getgid()); /* In case set[gu]id */ if (n == ZDFILE) setbuf(fp[n],(char *)NULL); /* Debugging file unbuffered */ } debug(F101, " fp[n]", "", (int) fp[n]); return((fp[n] != NULL) ? 1 : 0);}/* Z C L O S E -- Close the given file. *//* Returns 0 if arg out of range, 1 if successful, -1 if close failed. */zclose(n) int n; { int x; if (chkfn(n) < 1) return(0); /* Check range of n */ if ((n == ZIFILE) && fp[ZSYSFN]) { /* If system function */ x = zclosf(); /* do it specially */ } else { if ((fp[n] != stdout) && (fp[n] != stdin)) x = fclose(fp[n]); fp[n] = NULL; } return((x == EOF) ? -1 : 1);}/* Z C H I N -- Get a character from the input file. *//* Returns -1 if EOF, 0 otherwise with character returned in argument */zchin(n,c) int n; char *c; { int a; if (chkfn(n) < 1) return(-1); a = getc(fp[n]); if (a == EOF) return(-1); *c = a & 0377; return(0);}/* Z S O U T -- Write a string to the given file, buffered. */zsout(n,s) int n; char *s; { if (chkfn(n) < 1) return(-1); fputs(s,fp[n]); return(0);}/* Z S O U T L -- Write string to file, with line terminator, buffered */zsoutl(n,s) int n; char *s; { if (chkfn(n) < 1) return(-1); fputs(s,fp[n]); fputs("\n",fp[n]); return(0);}/* Z S O U T X -- Write x characters to file, unbuffered. */zsoutx(n,s,x) int n, x; char *s; { if (chkfn(n) < 1) return(-1);/* return(write(fp[n]->_file,s,x)); */ return(write(fileno(fp[n]),s,x));}/* Z C H O U T -- Add a character to the given file. *//* Should return 0 or greater on success, -1 on failure (e.g. disk full) */zchout(n,c) int n; char c; { if (chkfn(n) < 1) return(-1); if (n == ZSFILE) return(write(fileno(fp[n]),&c,1)); /* Use unbuffered for session log */ else { /* Buffered for everything else */ if (putc(c,fp[n]) == EOF) /* If true, maybe there was an error */ return(ferror(fp[n])?-1:0); /* Check to make sure */ else /* Otherwise... */ return(0); /* There was no error. */ }}/* C H K F N -- Internal function to verify file number is ok *//* Returns: -1: File number n is out of range 0: n is in range, but file is not open 1: n in range and file is open*/chkfn(n) int n; { switch (n) { case ZCTERM: case ZSTDIO: case ZIFILE: case ZOFILE: case ZDFILE: case ZTFILE: case ZPFILE: case ZSFILE: case ZSYSFN: break; default: debug(F101,"chkfn: file number out of range","",n); fprintf(stderr,"?File number out of range - %d\n",n); return(-1); } return( (fp[n] == NULL) ? 0 : 1 );}/* Z C H K I -- Check if input file exists and is readable *//* Returns: >= 0 if the file can be read (returns the size). -1 if file doesn't exist or can't be accessed, -2 if file exists but is not readable (e.g. a directory file). -3 if file exists but protected against read access.*//* For Berkeley Unix, a file must be of type "regular" to be readable. Directory files, special files, and symbolic links are not readable.*/longzchki(name) char *name; { struct stat buf; int x; long y; x = stat(name,&buf); if (x < 0) { debug(F111,"zchki stat fails",name,errno); return(-1); } x = buf.st_mode & S_IFMT; /* Isolate file format field */ if ((x != 0) && (x != S_IFREG)) { debug(F111,"zchki skipping:",name,x); return(-2); } debug(F111,"zchki stat ok:",name,x); if ((x = access(name,R_OK)) < 0) { /* Is the file accessible? */ debug(F111," access failed:",name,x); /* No */ return(-3); } else { y = buf.st_size; debug(F111," access ok:",name,(int) y); /* Yes */ return( (y > -1) ? y : 0 ); }}/* Z C H K O -- Check if output file can be created *//* Returns -1 if write permission for the file would be denied, 0 otherwise.*/zchko(name) char *name; { int i, x; char s[50], *sp; sp = s; /* Make a copy, get length */ x = 0; while ((*sp++ = *name++) != '\0') x++; if (x == 0) return(-1); /* If no filename, fail. */ debug(F101," length","",x); for (i = x; i > 0; i--) /* Strip filename. */ if (s[i-1] == '/') break; debug(F101," i","",i); if (i == 0) /* If no path, use current directory */ strcpy(s,"./"); else /* Otherwise, use given one. */ s[i] = '\0'; x = access(s,W_OK); /* Check access of path. */ if (x < 0) { debug(F111,"zchko access failed:",s,errno); return(-1); } else { debug(F111,"zchko access ok:",s,x); return(0); }}/* Z D E L E T -- Delete the named file. */zdelet(name) char *name; { unlink(name);}/* Z R T O L -- Convert remote filename into local form *//* For UNIX, this means changing uppercase letters to lowercase. */zrtol(name,name2) char *name, *name2; { for ( ; *name != '\0'; name++) { *name2++ = isupper(*name) ? tolower(*name) : *name; } *name2 = '\0'; debug(F110,"zrtol:",name2,0);}/* Z L T O R -- Local TO Remote *//* Convert filename from local format to common (remote) form. */zltor(name,name2) char *name, *name2; { char work[100], *cp, *pp; int dc = 0;#ifdef aegis char *getenv(), *index(), *namechars; int tilde = 0, bslash = 0; if ((namechars = getenv("NAMECHARS")) != NULL) { if (index(namechars, '~' ) != NULL) tilde = '~'; if (index(namechars, '\\') != NULL) bslash = '\\'; } else { tilde = '~'; bslash = '\\'; }#endif debug(F110,"zltor",name,0); pp = work;#ifdef aegis cp = name; if (tilde && *cp == tilde) ++cp; for (; *cp != '\0'; cp++) { /* strip path name */ if (*cp == '/' || *cp == bslash) {#else for (cp = name; *cp != '\0'; cp++) { /* strip path name */ if (*cp == '/') {#endif dc = 0; pp = work; } else if (islower(*cp)) *pp++ = toupper(*cp); /* Uppercase letters */ else if (*cp == '~') *pp++ = 'X'; /* Change tilde to 'X' */ else if (*cp == '#') *pp++ = 'X'; /* Change number sign to 'X' */ else if ((*cp == '.') && (++dc > 1)) *pp++ = 'X'; /* & extra dots */ else *pp++ = *cp; } *pp = '\0'; /* Tie it off. */ cp = name2; /* If nothing before dot, */ if (*work == '.') *cp++ = 'X'; /* insert 'X' */ strcpy(cp,work); debug(F110," name2",name2,0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -