common safe write(2) wrapper
This commit is contained in:
parent
a304049598
commit
12d2cef2d4
22
lib/libio.c
22
lib/libio.c
@ -10,9 +10,22 @@ _strlen(char *s){
|
||||
return i;
|
||||
}
|
||||
|
||||
static int
|
||||
_write(int fd, char *s, int n){
|
||||
int i;
|
||||
|
||||
for(
|
||||
i = 0;
|
||||
i < n;
|
||||
i += write(fd, s+i, n-i)
|
||||
);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int
|
||||
fdprint(int fd, char *s){
|
||||
write(fd, s, _strlen(s));
|
||||
return _write(fd, s, _strlen(s));
|
||||
}
|
||||
|
||||
/* SLOW. DO NOT USE. */
|
||||
@ -45,7 +58,6 @@ fdputs(int fd, char *s){
|
||||
int r;
|
||||
|
||||
i = _strlen(s);
|
||||
r = 0;
|
||||
|
||||
/* RACE CONDITION */
|
||||
/* This leaves s as an invalid string while the write(2) syscall is
|
||||
@ -53,11 +65,7 @@ fdputs(int fd, char *s){
|
||||
* implementation is unsuitable when s is shared between threads or
|
||||
* whatever the CS term for that is. */
|
||||
s[i] = '\n';
|
||||
for(
|
||||
r = 0;
|
||||
(r += write(fd, s, i + 1)) < i + 1;
|
||||
s += r, i -= r
|
||||
);
|
||||
r = _write(fd, s, i + 1);
|
||||
s[i] = '\0';
|
||||
|
||||
return r;
|
||||
|
Loading…
Reference in New Issue
Block a user