libbb: add read_full/write_full functions
These functions read/write all data or return with an error. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
90d036d62c
commit
4c41e245cf
|
@ -32,4 +32,7 @@ int process_escape_sequence(const char *source, char *dest, int destlen);
|
||||||
|
|
||||||
char *simple_itoa(unsigned int i);
|
char *simple_itoa(unsigned int i);
|
||||||
|
|
||||||
|
int write_full(int fd, void *buf, size_t size);
|
||||||
|
int read_full(int fd, void *buf, size_t size);
|
||||||
|
|
||||||
#endif /* __LIBBB_H */
|
#endif /* __LIBBB_H */
|
||||||
|
|
50
lib/libbb.c
50
lib/libbb.c
|
@ -127,3 +127,53 @@ char *simple_itoa(unsigned int i)
|
||||||
return p + 1;
|
return p + 1;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(simple_itoa);
|
EXPORT_SYMBOL(simple_itoa);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* write_full - write to filedescriptor
|
||||||
|
*
|
||||||
|
* Like write, but guarantees to write the full buffer out, else
|
||||||
|
* it returns with an error.
|
||||||
|
*/
|
||||||
|
int write_full(int fd, void *buf, size_t size)
|
||||||
|
{
|
||||||
|
size_t insize = size;
|
||||||
|
int now;
|
||||||
|
|
||||||
|
while (size) {
|
||||||
|
now = write(fd, buf, size);
|
||||||
|
if (now <= 0)
|
||||||
|
return now;
|
||||||
|
size -= now;
|
||||||
|
buf += now;
|
||||||
|
}
|
||||||
|
|
||||||
|
return insize;
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL(write_full);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* read_full - read from filedescriptor
|
||||||
|
*
|
||||||
|
* Like read, but this function only returns less bytes than
|
||||||
|
* requested when the end of file is reached.
|
||||||
|
*/
|
||||||
|
int read_full(int fd, void *buf, size_t size)
|
||||||
|
{
|
||||||
|
size_t insize = size;
|
||||||
|
int now;
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
while (size) {
|
||||||
|
now = read(fd, buf, size);
|
||||||
|
if (now == 0)
|
||||||
|
return total;
|
||||||
|
if (now < 0)
|
||||||
|
return now;
|
||||||
|
total += now;
|
||||||
|
size -= now;
|
||||||
|
buf += now;
|
||||||
|
}
|
||||||
|
|
||||||
|
return insize;
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL(read_full);
|
||||||
|
|
Loading…
Reference in New Issue