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;
|
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
|
int
|
||||||
fdprint(int fd, char *s){
|
fdprint(int fd, char *s){
|
||||||
write(fd, s, _strlen(s));
|
return _write(fd, s, _strlen(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SLOW. DO NOT USE. */
|
/* SLOW. DO NOT USE. */
|
||||||
@ -45,7 +58,6 @@ fdputs(int fd, char *s){
|
|||||||
int r;
|
int r;
|
||||||
|
|
||||||
i = _strlen(s);
|
i = _strlen(s);
|
||||||
r = 0;
|
|
||||||
|
|
||||||
/* RACE CONDITION */
|
/* RACE CONDITION */
|
||||||
/* This leaves s as an invalid string while the write(2) syscall is
|
/* 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
|
* implementation is unsuitable when s is shared between threads or
|
||||||
* whatever the CS term for that is. */
|
* whatever the CS term for that is. */
|
||||||
s[i] = '\n';
|
s[i] = '\n';
|
||||||
for(
|
r = _write(fd, s, i + 1);
|
||||||
r = 0;
|
|
||||||
(r += write(fd, s, i + 1)) < i + 1;
|
|
||||||
s += r, i -= r
|
|
||||||
);
|
|
||||||
s[i] = '\0';
|
s[i] = '\0';
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
|
Loading…
Reference in New Issue
Block a user