#include /* standard C i/o facilities */ #include /* getline - from the Stevens book (Unix Network Programming) reads a line of text from a descriptor. returns only when it finds a newline (\n) or EOF is detected. This function strips the \n and null terminates the string. -also- strip any car. returns (\r) */ int getline(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) { if (c == '\n') break; if (c != '\r') *ptr++ = c; } 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); } /* write a line of text to a descriptor checks for complete write, if not everything was written, keeps trying */ int writeline( int fd, char *s ) { int res; res = write(fd,s,strlen(s)); if (res!=strlen(s)) return(-1); if (1!=write(fd,"\n",1)) return(-1); return(res+2); }