blob: f353b7d55a5d16bc3710527aad9a7ab26e80d50c [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
12/*
13 * Write all of the supplied buffer out to a file.
14 * This does multiple writes as necessary.
15 * Returns the amount written, or -1 on an error.
16 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000017ssize_t FAST_FUNC full_write(int fd, const void *buf, size_t len)
Eric Andersenaad1a882001-03-16 22:47:14 +000018{
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 ssize_t cc;
20 ssize_t total;
Eric Andersenaad1a882001-03-16 22:47:14 +000021
22 total = 0;
23
Denis Vlasenko88ca0672006-10-12 22:44:13 +000024 while (len) {
Eric Andersendae099b2003-10-09 08:35:42 +000025 cc = safe_write(fd, buf, len);
Eric Andersenaad1a882001-03-16 22:47:14 +000026
Denis Vlasenko394eebe2008-02-25 20:30:24 +000027 if (cc < 0) {
28 if (total) {
29 /* we already wrote some! */
30 /* user can do another write to know the error code */
31 return total;
32 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000033 return cc; /* write() returns -1 on failure. */
Denis Vlasenko394eebe2008-02-25 20:30:24 +000034 }
Eric Andersenaad1a882001-03-16 22:47:14 +000035
Eric Andersenaad1a882001-03-16 22:47:14 +000036 total += cc;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 buf = ((const char *)buf) + cc;
Eric Andersenaad1a882001-03-16 22:47:14 +000038 len -= cc;
39 }
40
41 return total;
42}