FreeBSD isn't evil - just misguided
[cvsps-yd/commitid.git] / cbtcommon / sio.c
bloba9faf81b3e3be7fda2241ab14bdd46e70c16309e
1 /*
2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
4 */
6 #include <stdio.h>
8 #ifdef WIN32
9 #include <io.h>
10 #else
11 #include <unistd.h>
12 #endif
14 #include <errno.h>
16 #include "sio.h"
17 #include "rcsid.h"
19 RCSID("$Id: sio.c,v 1.5 2001/10/25 18:36:11 adam Exp $");
21 ssize_t readn(int fd, void *buf, size_t len)
24 int nleft,nread;
26 nleft = len;
28 while (nleft > 0)
30 nread = read(fd,buf,nleft);
32 /* there is an issue which EINTR which could leave us a bit haywire
33 * if we get a signal after having read some bytes. special handling
34 * N.B: we *do* return EINTR if no data has been read yet (thanks Karl)
36 if (nread < 0)
38 if (errno == EINTR && nleft != (int)len)
39 continue;
40 else
41 return (nread);
43 else if (nread == 0)
44 break;
46 nleft -= nread;
48 if (nleft)
49 buf = ((char *)buf) + nread;
51 return (len - nleft);
54 ssize_t writen(int fd, const void *buf, size_t len)
57 int nleft, nwritten;
59 nleft = len;
61 while (nleft > 0)
63 nwritten = write(fd,buf,nleft);
65 /* there is an issue with EINTR if we have already written
66 a few bytes! return if we have not written any yet */
67 if (nwritten < 0 && errno == EINTR)
69 if (nleft == (int)len)
70 return nwritten;
72 continue;
76 if (nwritten <= 0)
77 return nwritten;
79 nleft -= nwritten;
81 if (nleft)
82 buf = ((char *)buf) + nwritten;
85 return (len - nleft);