blob: 2b7983f4c1e3da76de0beeba807eb50b1fbaaa18 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Eric Andersenaad1a882001-03-16 22:47:14 +00009#include "libbb.h"
10
11/*
12 * Write all of the supplied buffer out to a file.
13 * This does multiple writes as necessary.
14 * Returns the amount written, or -1 on an error.
15 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000016ssize_t FAST_FUNC full_write(int fd, const void *buf, size_t len)
Eric Andersenaad1a882001-03-16 22:47:14 +000017{
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 ssize_t cc;
19 ssize_t total;
Eric Andersenaad1a882001-03-16 22:47:14 +000020
21 total = 0;
22
Denis Vlasenko88ca0672006-10-12 22:44:13 +000023 while (len) {
Eric Andersendae099b2003-10-09 08:35:42 +000024 cc = safe_write(fd, buf, len);
Eric Andersenaad1a882001-03-16 22:47:14 +000025
Denis Vlasenko394eebe2008-02-25 20:30:24 +000026 if (cc < 0) {
27 if (total) {
28 /* we already wrote some! */
29 /* user can do another write to know the error code */
30 return total;
31 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020032 return cc; /* write() returns -1 on failure. */
Denis Vlasenko394eebe2008-02-25 20:30:24 +000033 }
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Eric Andersenaad1a882001-03-16 22:47:14 +000035 total += cc;
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 buf = ((const char *)buf) + cc;
Eric Andersenaad1a882001-03-16 22:47:14 +000037 len -= cc;
38 }
39
40 return total;
41}