📄 ckufio.c
字号:
} zincnt = 0; /* Reset input buffer */ if (n == ZSYSFN) { /* Input from a system function? */#ifdef COMMENT/*** Note, this function should not be called with ZSYSFN ***//*** Always call zxcmd() directly, and give it the real file number ***//*** you want to use. ***/ return(zxcmd(n,name)); /* Try to fork the command */#else debug(F110,"zopeni called with ZSYSFN, failing!",name,0); *nambuf = '\0'; /* No filename. */ return(0); /* fail. */#endif /* COMMENT */ } if (n == ZSTDIO) { /* Standard input? */ if (is_a_tty(0)) { fprintf(stderr,"Terminal input not allowed"); debug(F110,"zopeni: attempts input from unredirected stdin","",0); return(0); } fp[ZIFILE] = stdin; ispipe[ZIFILE] = 0; return(1); }#ifdef CKROOT debug(F111,"zopeni setroot",ckroot,ckrootset); if (ckrootset) if (!zinroot(name)) { debug(F110,"zopeni setroot violation",name,0); return(0); }#endif /* CKROOT */ fp[n] = fopen(name,"r"); /* Real file, open it. */ debug(F111,"zopeni fopen", name, fp[n]);#ifdef ZDEBUG printf("ZOPENI fp[%d]=%ld\n",n,fp[n]);#endif /* ZDEBUG */ ispipe[n] = 0; if (xferlog#ifdef CKSYSLOG || ((ckxsyslog >= SYSLG_FA) && ckxlogging)#endif /* CKSYSLOG */ ) { getfullname(name); debug(F110,"zopeni fullname",fullname,0); } if (fp[n] == NULL) {#ifdef CKSYSLOG if (ckxsyslog >= SYSLG_FA && ckxlogging) { syslog(LOG_INFO, "file[%d] %s: open failed (%m)", n, fullname); perror(fullname); } else#endif /* CKSYSLOG */ perror(name); return(0); } else {#ifdef CKSYSLOG if (ckxsyslog >= SYSLG_FA && ckxlogging) syslog(LOG_INFO, "file[%d] %s: open read ok", n, fullname);#endif /* CKSYSLOG */ clearerr(fp[n]); return(1); }}#ifdef QNX#define DONDELAY#else#ifdef O_NDELAY#define DONDELAY#endif /* O_NDELAY */#endif /* QNX *//* Z O P E N O -- Open a new file for output. *//*ARGSUSED*/ /* zz not used */intzopeno(n,name,zz,fcb)/* zopeno */ int n; char *name; struct zattr *zz; struct filinfo *fcb; { char p[8]; int append = 0;/* As of Version 5A, the attribute structure and the file information *//* structure are included in the arglist. */#ifdef DEBUG debug(F111,"zopeno",name,n); if (fcb) { debug(F101,"zopeno fcb disp","",fcb->dsp); debug(F101,"zopeno fcb type","",fcb->typ); debug(F101,"zopeno fcb char","",fcb->cs); } else { debug(F100,"zopeno fcb is NULL","",0); }#endif /* DEBUG */ if (chkfn(n) != 0) /* Already open? */ return(0); /* Nothing to do. */ if ((n == ZCTERM) || (n == ZSTDIO)) { /* Terminal or standard output */ fp[ZOFILE] = stdout; ispipe[ZOFILE] = 0;#ifdef COMMENT /* This seems right but it breaks client server ops */ fp[n] = stdout; ispipe[n] = 0;#endif /* COMMENT */#ifdef DEBUG if (n != ZDFILE) debug(F101,"zopeno fp[n]=stdout","",fp[n]);#endif /* DEBUG */ zoutcnt = 0; zoutptr = zoutbuffer; return(1); }/* A real file. Open it in desired mode (create or append). */#ifdef CKROOT debug(F111,"zopeno setroot",ckroot,ckrootset); if (ckrootset) if (!zinroot(name)) { debug(F110,"zopeno setroot violation",name,0); return(0); }#endif /* CKROOT */ ckstrncpy(p,"w",8); /* Assume write/create mode */ if (fcb) { /* If called with an FCB... */ if (fcb->dsp == XYFZ_A) { /* Does it say Append? */ ckstrncpy(p,"a",8); /* Yes. */ debug(F100,"zopeno append","",0); append = 1; } } if (xferlog#ifdef CKSYSLOG || ((ckxsyslog >= SYSLG_FC) && ckxlogging)#endif /* CKSYSLOG */ ) { getfullname(name); debug(F110,"zopeno fullname",fullname,0); } debug(F110,"zopeno fopen arg",p,0); fp[n] = fopen(name,p); /* Try to open the file */ ispipe[ZIFILE] = 0;#ifdef ZDEBUG printf("ZOPENO fp[%d]=%ld\n",n,fp[n]);#endif /* ZDEBUG */ if (fp[n] == NULL) { /* Failed */ debug(F101,"zopeno failed errno","",errno);#ifdef CKSYSLOG if (ckxsyslog >= SYSLG_FC && ckxlogging) syslog(LOG_INFO, "file[%d] %s: %s failed (%m)", n, fullname, append ? "append" : "create" );#endif /* CKSYSLOG */#ifdef COMMENT /* Let upper levels print message. */ perror("Can't open output file");#endif /* COMMENT */ } else { /* Succeeded */ extern int zofbuffer, zofblock, zobufsize; debug(F101, "zopeno zobufsize", "", zobufsize); if (n == ZDFILE || n == ZTFILE) { /* If debug or transaction log */ setbuf(fp[n],NULL); /* make it unbuffered. */#ifdef DONDELAY } else if (n == ZOFILE && !zofblock) { /* blocking or nonblocking */ int flags; if ((flags = fcntl(fileno(fp[n]),F_GETFL,0)) > -1) fcntl(fileno(fp[n]),F_SETFL, flags |#ifdef QNX O_NONBLOCK#else O_NDELAY#endif /* QNX */ ); debug(F100,"zopeno ZOFILE nonblocking","",0);#endif /* DONDELAY */ } else if (n == ZOFILE && !zofbuffer) { /* buffered or unbuffered */ setbuf(fp[n],NULL); debug(F100,"zopeno ZOFILE unbuffered","",0); }#ifdef CK_LOGIN /* Enforce anonymous file-creation permission */ if (isguest) if (n == ZWFILE || n == ZMFILE || n == ZOFILE || n == ZDFILE || n == ZTFILE || n == ZPFILE || n == ZSFILE) chmod(name,ckxperms);#endif /* CK_LOGIN */#ifdef CKSYSLOG if (ckxsyslog >= SYSLG_FC && ckxlogging) syslog(LOG_INFO, "file[%d] %s: %s ok", n, fullname, append ? "append" : "create" );#endif /* CKSYSLOG */ debug(F100, "zopeno ok", "", 0); } zoutcnt = 0; /* (PWP) reset output buffer */ zoutptr = zoutbuffer; 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. */intzclose(n) int n; { int x = 0, x2 = 0; extern long ffc; debug(F101,"zclose file number","",n); if (chkfn(n) < 1) return(0); /* Check range of n */ if ((n == ZOFILE) && (zoutcnt > 0)) /* (PWP) output leftovers */ x2 = zoutdump(); if (fp[ZSYSFN] || ispipe[n]) { /* If file is really pipe */#ifndef NOPUSH x = zclosf(n); /* do it specially */#else x = EOF;#endif /* NOPUSH */ debug(F101,"zclose zclosf","",x); debug(F101,"zclose zclosf fp[n]","",fp[n]); } else { if ((fp[n] != stdout) && (fp[n] != stdin)) x = fclose(fp[n]); fp[n] = NULL;#ifdef COMMENT if (n == ZCTERM || n == ZSTDIO) /* See zopeno() */ if (fp[ZOFILE] == stdout) fp[ZOFILE] = NULL;#endif /* COMMENT */ } iflen = -1L; /* Invalidate file length */ if (x == EOF) { /* if we got a close error */ debug(F101,"zclose fclose fails","",x); return(-1); } else if (x2 < 0) { /* or error flushing last buffer */ debug(F101,"zclose error flushing last buffer","",x2); return(-1); /* then return an error */ } else { /* Print log record compatible with wu-ftpd */ if (xferlog && (n == ZIFILE || n == ZOFILE)) { char * s, *p; extern char ttname[]; if (!iklogopen) (VOID) doiklog(); /* Open log if necessary */ debug(F101,"zclose iklogopen","",iklogopen); if (iklogopen) { int len; char * fnam; timenow = time(NULL);#ifdef CK_LOGIN if (logged_in) s = clienthost; else#endif /* CK_LOGIN */ s = (char *)ttname; if (!s) s = ""; if (!*s) s = "*";#ifdef CK_LOGIN if (logged_in) { p = guestpass; if (!*p) p = "*"; } else#endif /* CK_LOGIN */ p = whoami(); len = 24 + 12 + (int)strlen(s) + 16 + (int)strlen(fullname) + 1 + 1 + 1 + 1 + (int)strlen(p) + 6 + 2 + 12; fnam = fullname; if (!*fnam) fnam = "(pipe)"; if (len > IKSDMSGLEN) sprintf(iksdmsg, /* SAFE */ "%.24s [BUFFER WOULD OVERFLOW]\n",ctime(&timenow)); else sprintf(iksdmsg, /* SAFE */ "%.24s %d %s %ld %s %c %s %c %c %s %s %d %s\n", ctime(&timenow), /* date/time */ gtimer(), /* elapsed secs */ s, /* peer name */ ffc, /* byte count */ fnam, /* full pathname of file */ (binary ? 'b' : 'a'), /* binary or ascii */ "_", /* options = none */ n == ZIFILE ? 'o' : 'i', /* in/out */#ifdef CK_LOGIN (isguest ? 'a' : 'r'), /* User type */#else 'r',#endif /* CK_LOGIN */ p, /* Username or guest passwd */#ifdef CK_LOGIN logged_in ? "iks" : "kermit", /* Record ID */#else "kermit",#endif /* CK_LOGIN */ 0, /* User ID on client system unknown */ "*" /* Ditto */ ); debug(F110,"zclose iksdmsg",iksdmsg,0); write(xferlog, iksdmsg, (int)strlen(iksdmsg)); } } debug(F101,"zclose returns","",1); return(1); }}/* Z C H I N -- Get a character from the input file. *//* Returns -1 if EOF, 0 otherwise with character returned in argument */intzchin(n,c) int n; int *c; { int a;#ifdef IKSD if (inserver && !local && (n == ZCTERM || n == ZSTDIO)) { a = coninc(0); if (*c < 0) return(-1); } else#endif /* IKSD */ /* (PWP) Just in case this gets called when it shouldn't. */ if (n == ZIFILE) { a = zminchar(); /* Note: this catches Ctrl-Z */ if (a < 0) /* (See zinfill()...) */ return(-1); } else { a = getc(fp[n]); if (a == EOF) return(-1);#ifdef CK_CTRLZ /* If SET FILE EOF CTRL-Z, first Ctrl-Z marks EOF */ if (!binary && a == 0x1A && eofmethod == XYEOF_Z) return(-1);#endif /* CK_CTRLZ */ } *c = (CHAR) a & 0377; return(0);}/* Z S I N L -- Read a line from a file *//* Writes the line into the address provided by the caller. n is the Kermit "channel number". Writing terminates when newline is encountered, newline is not copied. Writing also terminates upon EOF or if length x is exhausted. Returns 0 on success, -1 on EOF or error.*/intzsinl(n,s,x) int n, x; char *s; { int a, z = 0; /* z is return code. */ int count = 0; int len = 0; char *buf; extern CHAR feol; /* Line terminator */ if (!s || chkfn(n) < 1) /* Make sure file is open, etc */ return(-1); buf = s; s[0] = '\0'; /* Don't return junk */ a = -1; /* Current character, none yet. */ while (x--) { /* Up to given length */ int old = 0; if (feol) /* Previous character */ old = a; if (zchin(n,&a) < 0) { /* Read a character from the file */ debug(F101,"zsinl zchin fail","",count); if (count == 0) z = -1; /* EOF or other error */ break; } else count++; if (feol) { /* Single-character line terminator */ if (a == feol) break; } else { /* CRLF line terminator */ if (a == '\015') /* CR, get next character */ continue; if (old == '\015') { /* Previous character was CR */ if (a == '\012') { /* This one is LF, so we have a line */ break; } else { /* Not LF, deposit CR */ *s++ = '\015'; x--; len++; } } } *s = a; /* Deposit character */ s++; len++; } *s = '\0'; /* Terminate the string */ debug(F011,"zsinl",buf,len); return(z);}/* Z X I N -- Read x bytes from a file *//* Reads x bytes (or less) from channel n and writes them to the address provided by the caller. Returns number of bytes read on success, 0 on EOF or error.*/intzxin(n,s,x) int n, x; char *s; {#ifdef IKSD if (inserver && !local && (n == ZCTERM || n == ZSTDIO)) { int a, i; a = ttchk(); if (a < 1) return(0); for (i = 0; i < a && i < x; i++) s[i] = coninc(0); return(i); }#endif /* IKSD */ return(fread(s, sizeof (char), x, fp[n]));}/* Z I N F I L L -- Buffered file input. (re)fill the file input buffer with data. All file input should go through this routine, usually by calling the zminchar() macro defined in ckcker.h. Returns: Value 0..255 on success, the character that was read. -1 on end of file. -2 on any kind of error other than end of file. -3 timeout when reading from pipe (Kermit packet mode only).*/intzinfill() { extern int kactive, srvping; errno = 0;#ifdef ZDEBUG printf("ZINFILL fp[%d]=%ld\n",ZIFILE,fp[ZIFILE]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -