Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

readn.c

Go to the documentation of this file.
00001 /************************************************************************
00002 * File: readn.c
00003 * Purpose: Provides a C file for the readn function from
00004 * the book Advanced Programming in the UNIX Environment  by Richard Stevens.
00005 * The readn function calls the read function repeatedly until n bytes
00006 * have been read from the file descriptor.
00007 *************************************************************************/
00008 
00009 /* This is neccessary to avoid muliple definitions of fd_set, etc when both
00010 * PCNFS and Windows Sockets are to be available. */
00011 #ifdef USE_PCNFS
00012 #undef USE_PCNFS
00013 #endif
00014 
00015 #include "rcs_defs.hh"          /* _Windows */
00016 
00017 #include "readn.h"              /* readn(int, void *, int, double) */
00018 #if !defined(_Windows) && !defined(MSDOS) || defined(gnuwin32)
00019 #include <unistd.h>             /* read(), select() */
00020 #include <sys/types.h>          /* typedef fd_set, FD_ZERO, FD_SET */
00021 #ifdef VXWORKS
00022 #include <sys/times.h>          /* struct timeval */
00023 #else
00024 #include <sys/time.h>           /* struct timeval */
00025 #endif
00026 #else
00027 #ifdef _Windows
00028 #ifndef UNDER_CE
00029 #include <io.h>                 /* read() */
00030 #endif
00031 #ifdef USE_OLD_WINSOCK
00032 #include <winsock.h>            /* select(), typedef fd_set, FD_ZERO, FD_SET, struct */
00033 #else
00034 #include <winsock2.h>
00035 #endif
00036 
00037 #else
00038 #include <tklib.h>
00039 #endif /* struct timeval */
00040 #endif
00041 #include <stddef.h>             /* size_t */
00042 #include <errno.h>              /* errno */
00043 #include <string.h>             /* strerror() */
00044 #include <math.h>               /* modf() */
00045 #include "rcs_prnt.hh"          /* rcs_print_error() */
00046 #include "sokintrf.h"           /* dl_select() */
00047 
00048 /* Read "n" bytes from a descriptor. */
00049 int
00050 readn (int fd, void *vptr, int n, double _timeout)
00051 {
00052   int nleft, nread;
00053   char *ptr;
00054   double int_part;
00055   struct timeval timeout_tv;
00056   fd_set read_fd_set;
00057   timeout_tv.tv_sec = (long) _timeout;
00058   timeout_tv.tv_usec = (long) (_timeout * 1000000.0);
00059   if (timeout_tv.tv_usec >= 1000000)
00060     {
00061       timeout_tv.tv_usec = timeout_tv.tv_usec % 1000000;
00062     }
00063   FD_ZERO (&read_fd_set);
00064   RCS_FD_SET (fd, &read_fd_set);
00065 
00066 
00067   ptr = (char *) vptr;
00068   nleft = n;
00069   while (nleft > 0)
00070     {
00071       if (_timeout > 0.0)
00072         {
00073           switch (dl_select
00074                   (fd + 1, &read_fd_set, (fd_set *) NULL, (fd_set *) NULL,
00075                    &timeout_tv))
00076             {
00077             case -1:
00078               rcs_print_error ("Error in select: %d -> %s\n", errno,
00079                                strerror (errno));
00080               return -1;
00081 
00082             case 0:
00083               rcs_print_error ("Read timed out.\n");
00084               return -1;
00085 
00086             default:
00087               break;
00088             }
00089         }
00090       if ((nread = read (fd, ptr, nleft)) == -1)
00091         {
00092           rcs_print_error ("Read error: %d = %s\n", errno, strerror (errno));
00093           return (-1);          /* error, return < 0 */
00094         }
00095       else if (nread == 0)
00096         {
00097           rcs_print_error ("readn: Premature EOF recieved.\n");
00098           return (-1);
00099         }
00100 
00101       nleft -= nread;
00102       ptr += nread;
00103     }
00104   return (n - nleft);           /* return >= 0 */
00105 }

Generated on Sun Dec 2 15:56:51 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001