readline.c

来自「this gives details of the network progra」· C语言 代码 · 共 50 行

C
50
字号
#include <stdio.h>	/* standard C i/o facilities */#include <unistd.h>/* readline - from the Stevens book (Unix Network Programming) */intreadline(int fd, void *vptr, int maxlen){	int	n, rc;	char	c, *ptr;	ptr = vptr;	for (n = 1; n < maxlen; n++) {		if ( (rc = read(fd, &c, 1)) == 1) {			*ptr++ = c;			if (c == '\n')				break;		} else if (rc == 0) {			if (n == 1)				return(0);	/* EOF, no data read */			else				break;		/* EOF, some data was read */		} else			return(-1);	/* error */	}	*ptr = 0;	return(n);}/* end readline */int writeline( int fd, char *s ) {  int res;  res = write(fd,s,strlen(s));  if (res!=strlen(s))    return(-1);  if (2!=write(fd,"\r\n",2))     return(-1);      return(res+2);}

⌨️ 快捷键说明

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