00001 /* 00002 writeall and readall 00003 AUP2, Sec. 2.09, 2.10 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 00023 /* 00024 Supposed to be a substitute for write(), so doesn't use ec. 00025 Unlike write(), can return -1 and also complete one or more 00026 partial writes. 00027 */ 00028 /*[writeall]*/ 00029 ssize_t writeall(int fd, const void *buf, size_t nbyte) 00030 { 00031 ssize_t nwritten = 0, n; 00032 00033 do { 00034 if ((n = write(fd, &((const char *)buf)[nwritten], 00035 nbyte - nwritten)) == -1) { 00036 if (errno == EINTR) 00037 continue; 00038 else 00039 return -1; 00040 } 00041 nwritten += n; 00042 } while (nwritten < nbyte); 00043 return nwritten; 00044 } 00045 /*[readall]*/ 00046 ssize_t readall(int fd, void *buf, size_t nbyte) 00047 { 00048 ssize_t nread = 0, n; 00049 00050 do { 00051 if ((n = read(fd, &((char *)buf)[nread], nbyte - nread)) == -1) { 00052 if (errno == EINTR) 00053 continue; 00054 else 00055 return -1; 00056 } 00057 if (n == 0) 00058 return nread; 00059 nread += n; 00060 } while (nread < nbyte); 00061 return nread; 00062 } 00063 /*[]*/