⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckufio.c

📁 linux终端仿真程序
💻 C
📖 第 1 页 / 共 5 页
字号:
	  x = fclose(fp[n]);	fp[n] = NULL;#ifdef OS2#ifdef CK_LABELED	if (binary == XYFT_L) {	    debug(F111,"zclose LABELED","file number",n);	    if (n == ZOFILE) {		debug(F111,"zclose LABELED",		      "lf_opts && LBL_EXT",		      lf_opts && LBL_EXT		      );		if (lf_opts && LBL_EXT)		  os2seteas(os2filename);      		os2setattr(os2filename);	    } else if (n == ZIFILE && pFEAList) {		FreeMem(pFEAList);		pFEAList = 0;	    }	}#endif /* CK_LABELED */#endif /* OS2 */    }    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 {	debug(F101,"zclose success","",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, x;    /* (PWP) Just in case this gets called when it shouldn't. */    if (n == ZIFILE) {	x = zminchar();	*c = x;	return(x);    }    /* if (chkfn(n) < 1) return(-1); */    a = getc(fp[n]);    if (a == EOF) return(-1);#ifdef OS2    if (!binary && a == 0x1A)		/* Ctrl-Z marks EOF for text mode*/      return(-1);#endif    *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. */    extern CHAR feol;			/* Line terminator */    if (chkfn(n) < 1) {			/* Make sure file is open */	return(-1);    }    a = -1;				/* Current character, none yet. */    while (x--) {			/* Up to given length */	int old;	if (feol) old = a;		/* Previous character */	if (zchin(n,&a) < 0) {		/* Read a character from the file */	    debug(F101,"zsinl","",a);	    z = -1;			/* EOF or other error */	    break;	}	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';	    }#ifdef OS2	    if (a == '\012') break;	/* Here break on single LF too */#endif /* OS2 */	}	*s = a;				/* Deposit character */	s++;    }    *s = '\0';				/* Terminate the string */    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; {    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.*/intzinfill() {    int x;    errno = 0;#ifdef ZDEBUG printf("ZINFILL fp[%d]=%ld\n",ZIFILE,fp[ZIFILE]);#endif /* ZDEBUG */#ifdef OS2#ifdef CK_LABELED    debug(F101,"zinfill: binary","",binary);    debug(F101,"zinfill: pFEAList","",pFEAList);    if ( binary == XYFT_L && pFEAList ) {	zinptr = zinbuffer ;	do_label_send(os2filename) ;	if (feof(fp[ZIFILE]))	  return(-1);	clearerr(fp[ZIFILE]);	zincnt += fread(zinptr, sizeof (char), INBUFSIZE - zincnt, fp[ZIFILE]);    } else {#endif /* CK_LABELED */#endif /* OS2 */	if (feof(fp[ZIFILE])) {	    debug(F100,"ZINFILL feof","",0);#ifdef ZDEBUG 	    printf("ZINFILL EOF\n");#endif /* ZDEBUG */	    return(-1);	}	clearerr(fp[ZIFILE]);	zincnt = fread(zinbuffer, sizeof (char), INBUFSIZE, fp[ZIFILE]);	debug(F101,"ZINFILL fread","",zincnt);#ifdef ZDEBUG 	printf("FREAD=%d\n",zincnt);#endif /* ZDEBUG */#ifdef OS2#ifdef CK_LABELED    }#endif /* CK_LABELED */#endif /* OS2 */    if (ferror(fp[ZIFILE])) {	debug(F100,"ZINFILL ferror","",0);	debug(F100,"ZINFILL errno","",errno);#ifdef ZDEBUG 	printf("ZINFILL errno=%d\n",errno);#endif /* ZDEBUG */	return(-2);    }    /* In case feof() didn't work just above -- sometimes it doesn't... */    if (zincnt == 0) {       if (feof(fp[ZIFILE]) ) {	   debug(F100,"ZINFILL count 0 EOF return -1","",0);	   return (-1);       } else {	   debug(F100,"ZINFILL count 0 not EOF return -2","",0);	   return(-2);       }    }    zinptr = zinbuffer;	   /* set pointer to beginning, (== &zinbuffer[0]) */    zincnt--;				/* One less char in buffer */    return((int)(*zinptr++) & 0377);	/* because we return the first */}/*  Z S O U T  --  Write a string out to the given file, buffered.  */intzsout(n,s) int n; char *s; {    if (chkfn(n) < 1) return(-1);	/* Keep this, prevents memory faults */    if (!s) return(0);			/* Null pointer, do nothing, succeed */    if (!*s) return(0);			/* empty string, ditto */#ifdef OS2    if (n == ZCTERM)      return(Vscrnprintf(s));#endif /* OS2 */    if (n == ZSFILE)      return(write(fileno(fp[n]),s,(int)strlen(s)));    return(fputs(s,fp[n]) == EOF ? -1 : 0);}/*  Z S O U T L  --  Write string to file, with line terminator, buffered  */intzsoutl(n,s) int n; char *s; {    if (zsout(n,s) < 0)         return(-1);#ifdef OS2    if (n == ZCTERM)      return(Vscrnprintf("\n"));#endif /* OS2 */    if (n == ZSFILE)			/* But session log is unbuffered */      return(write(fileno(fp[n]),"\n",1));     if (fputs("\n",fp[n]) == EOF)         return(-1); /* (===OS2 ? \r\n) */    return(0);}/*  Z S O U T X  --  Write x characters to file, unbuffered.  */intzsoutx(n,s,x) int n, x; char *s; {#ifdef COMMENT    if (chkfn(n) < 1) return(-1);    return(write(fp[n]->_file,s,x));#endif /* COMMENT */#ifdef OS2    if (n == ZCTERM) {        int i;        for (i = 0; i < x; i++)          if (!Vscrnprintf("%c",s[i]))            return(-1);        return(x);    }#endif /* OS2 */    return(write(fileno(fp[n]),s,x) == x ? x : -1);}/*  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)  */int#ifdef CK_ANSICzchout(register int n, char c)#elsezchout(n,c) register int n; char c;#endif /* CK_ANSIC *//* zchout() */ {    /* if (chkfn(n) < 1) return(-1); */#ifdef OS2    if ( n==ZCTERM )         return(Vscrnprintf("%c",c));#endif /* OS2 */    if (n == ZSFILE) 			/* Use unbuffered for session log */    	return(write(fileno(fp[n]),&c,1) == 1 ? 0 : -1);                                /* 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. */}/* (PWP) buffered character output routine to speed up file IO */intzoutdump() {    int x;    zoutptr = zoutbuffer;		/* Reset buffer pointer in all cases */    debug(F101,"zoutdump chars","",zoutcnt);    if (zoutcnt == 0) {			/* Nothing to output */	return(0);    } else if (zoutcnt < 0) {		/* Unexpected negative argument */	zoutcnt = 0;			/* Reset output buffer count */	return(-1);			/* and fail. */    }#ifdef CK_LABELED    if (binary == XYFT_L ) {	x = do_label_recv() ;	if ( x > 0 ) { /* more room in the buffer */	    debug(F101,"zoutdump do_label_recv unfilled buffer","",zoutcnt);	    return 0 ; 	} else if ( x < 0 ) {	    debug(F101,"zoutdump do_label_recv error","",x);	    return -1 ; 	}    }#endif /* CK_LABELED *//* Frank Prindle suggested that replacing this fwrite() by an fflush() *//* followed by a write() would improve the efficiency, especially when *//* writing to stdout.  Subsequent tests showed a 5-fold improvement!   *//* if (x = fwrite(zoutbuffer, 1, zoutcnt, fp[ZOFILE])) {              */    fflush(fp[ZOFILE]);    if ((x = write(fileno(fp[ZOFILE]),zoutbuffer,zoutcnt)) == zoutcnt) {	debug(F101,"zoutdump write ok","",zoutcnt);	zoutcnt = 0;			/* Reset output buffer count */	return(0);			/* write() worked OK */    } else {	debug(F101,"zoutdump write error","",errno);	debug(F101,"zoutdump write returns","",x);	zoutcnt = 0;			/* Reset output buffer count */	return(-1);			/* write() failed */    }}/*  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*/intchkfn(n) int n; {#ifdef COMMENT				/* Save some stack space */    switch (n) {	case ZCTERM:	case ZSTDIO:	case ZIFILE:	case ZOFILE:	case ZDFILE:	case ZTFILE:	case ZPFILE:	case ZSFILE:	case ZSYSFN:        case ZRFILE:        case ZWFILE:#ifdef OS2        case ZPRINT:#endif /* OS2 */	    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 );#else    if (n < 0 || n >= ZNFILS)      return(-1);    else return( (fp[n] == NULL) ? 0 : 1 );#endif /* COMMENT */}/*  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; {#ifdef NT    struct _stat buf;#else    struct stat buf;#endif /* NT */    int x;#ifdef UNIX    x = strlen(name);    if (x == 9 && !strcmp(name,"/dev/null"))      return(0);#endif /* UNIX */    x = stat(name,&buf);    if (x < 0) {	debug(F111,"zchki stat fails",name,errno);	return(-1);    }    if (!S_ISREG (buf.st_mode)		/* Must be regular file */#ifdef S_ISFIFO	&& !S_ISFIFO (buf.st_mode)	/* or FIFO */#endif /* S_ISFIFO */	) {					debug(F111,"zchki skipping:",name,x);	return(-2);    }    debug(F111,"zchki stat ok:",name,x);#ifdef SW_ACC_ID    debug(F100,"zchki swapping ids for access()","",0);    priv_on();#endif /* SW_ACC_ID */#ifdef NT    x = _access(name,R_OK);#else    x = access(name,R_OK);#endif /* NT */#ifdef SW_ACC_ID    priv_off();    debug(F100,"zchki swapped ids restored","",0);#endif /* SW_ACC_ID */    if (x < 0) { 	/* Is the file accessible? */	debug(F111," access failed:",name,x); /* No */    	return(-3);    } else {	iflen = buf.st_size;		      /* Yes, remember size */	strncpy(nambuf,name,MAXNAMLEN);	      /* and name globally. */	debug(F111," access ok:",name,(int) iflen);	return( (iflen > -1L) ? iflen : 0L );    }}/*  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.*/intzchko(name) char *name; {    int i, x;    char *s;    if (!name) return(-1);		/* Watch out for null pointer. */    x = (int)strlen(name);		/* Get length of filename */    debug(F101," length","",x);#ifdef UNIX/*  Writing to null device is OK.*/    if (x == 9 && !strcmp(name,"/dev/null"))      return(0);#endif /* UNIX */    if (isdir(name))			/* Directories are not writeable */      return(-1);    s = malloc(x+3);			/* Must copy because we can't */    if (!s) {				/* write into our argument. */	fprintf(stderr,"Malloc error 46\n");	return(-1);    }    strcpy(s,name);    for (i = x; i > 0; i--)		/* Strip filename from right. */      if (ISDIRSEP(s[i-1])) break;    debug(F101," i","",i);#ifdef COMMENT/* X/OPEN XPG3-compliant systems fail if argument ends with "/"...  */    if (i == 0)				/* If no path, use current directory */      strcpy(s,"./");    else				/* Otherwise, use given one. */      s[i] = '\0';#else/* So now we use "path/." if path given, or "." if no path given. */    s[i++] = '.';			/* Append "." to path. */    s[i] = '\0';#endif /* COMMENT */#ifdef SW_ACC_ID    debug(F100,"zchko swapping ids for access()","",0);    priv_on();#endif /* SW_ACC_ID */#ifdef OS2				/* No unwritable directories in OS/2 */    x = 0;#else#ifdef NT    x = _access(s,W_OK);		/* Check access of path. */#else    x = access(s,W_OK);			/* Check access of path. */#endif /* NT */#endif /* OS2 */#ifdef SW_ACC_ID    priv_off();    debug(F100,"zchko swapped ids restored","",0);#endif /* SW_ACC_ID */    if (x < 0)      debug(F111,"zchko access failed:",s,errno);    else      debug(F111,"zchko access ok:",s,x);    free(s);				/* Free temporary storage */    return((x < 0) ? -1 : 0);		/* and return. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -