2001-03-16 22:47:14 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
2006-05-19 19:29:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write all of the supplied buffer out to a file.
|
|
|
|
* This does multiple writes as necessary.
|
|
|
|
* Returns the amount written, or -1 on an error.
|
|
|
|
*/
|
2008-06-27 02:52:20 +00:00
|
|
|
ssize_t FAST_FUNC full_write(int fd, const void *buf, size_t len)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
2003-03-19 09:13:01 +00:00
|
|
|
ssize_t cc;
|
|
|
|
ssize_t total;
|
2001-03-16 22:47:14 +00:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
|
2006-10-12 22:44:13 +00:00
|
|
|
while (len) {
|
2003-10-09 08:35:42 +00:00
|
|
|
cc = safe_write(fd, buf, len);
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
if (cc < 0) {
|
|
|
|
if (total) {
|
|
|
|
/* we already wrote some! */
|
|
|
|
/* user can do another write to know the error code */
|
|
|
|
return total;
|
|
|
|
}
|
2006-10-31 15:55:56 +00:00
|
|
|
return cc; /* write() returns -1 on failure. */
|
2008-02-25 20:30:24 +00:00
|
|
|
}
|
2001-03-16 22:47:14 +00:00
|
|
|
|
|
|
|
total += cc;
|
2003-03-19 09:13:01 +00:00
|
|
|
buf = ((const char *)buf) + cc;
|
2001-03-16 22:47:14 +00:00
|
|
|
len -= cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|