00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifdef USE_PCNFS
00012 #undef USE_PCNFS
00013 #endif
00014
00015 #include "rcs_defs.hh"
00016
00017 #include "readn.h"
00018 #ifndef _Windows
00019 #include <unistd.h>
00020 #include <sys/types.h>
00021 #ifdef VXWORKS
00022 #include <sys/times.h>
00023 #else
00024 #include <sys/time.h>
00025 #endif
00026 #else
00027 #include <io.h>
00028 #ifdef USE_OLD_WINSOCK
00029 #include <winsock.h>
00030 #else
00031 #include <winsock2.h>
00032 #endif
00033
00034 #endif
00035 #include <stddef.h>
00036 #include <errno.h>
00037 #include <string.h>
00038 #include <math.h>
00039 #include "rcs_prnt.hh"
00040 #include "sokintrf.h"
00041
00042
00043 int
00044 readn (int fd, void *vptr, int n, double _timeout)
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);
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);
00099 }