#include "rcs_defs.hh"
#include "readn.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include "rcs_prnt.hh"
#include "sokintrf.h"
Include dependency graph for readline.c:
Go to the source code of this file.
Functions | |
int | readn (int fd, void *vptr, int n, double _timeout) |
|
Definition at line 44 of file readline.c. 00045 { 00046 int nleft, nread; 00047 char *ptr; 00048 double int_part; 00049 struct timeval timeout_tv; 00050 fd_set read_fd_set; 00051 timeout_tv.tv_sec = (long) _timeout; 00052 timeout_timeval.tv_usec = (long) (_timeout * 1000000.0); 00053 if (timeout_timeval.tv_usec >= 1000000) 00054 { 00055 timeout_timeval.tv_usec = timeout_timeval.tv_usec % 1000000; 00056 } 00057 FD_ZERO (&read_fd_set); 00058 RCS_FD_SET (fd, &read_fd_set); 00059 00060 00061 ptr = (char *) vptr; 00062 nleft = n; 00063 while (nleft > 0) 00064 { 00065 if (_timeout > 0.0) 00066 { 00067 switch (dl_select 00068 (fd + 1, &read_fd_set, (fd_set *) NULL, (fd_set *) NULL, 00069 &timeout_tv)) 00070 { 00071 case -1: 00072 rcs_print_error ("Error in select: %d -> %s\n", errno, 00073 strerror (errno)); 00074 return -1; 00075 00076 case 0: 00077 rcs_print_error ("Read timed out.\n"); 00078 return -1; 00079 00080 default: 00081 break; 00082 } 00083 } 00084 if ((nread = read (fd, ptr, nleft)) == -1) 00085 { 00086 rcs_print_error ("Read error: %d = %s\n", errno, strerror (errno)); 00087 return (-1); /* error, return < 0 */ 00088 } 00089 else if (nread == 0) 00090 { 00091 rcs_print_error ("readn: Premature EOF recieved.\n"); 00092 return (-1); 00093 } 00094 00095 nleft -= nread; 00096 ptr += nread; 00097 } 00098 return (n - nleft); /* return >= 0 */ 00099 } |