blob: 7bbacb8acc14aa7bf8303e21ea13f10b756d5a1d [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 */
Rob Landley53437472006-07-16 08:14:35 +000017ssize_t 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
27 if (cc < 0)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000028 return cc; /* write() returns -1 on failure. */
Eric Andersenaad1a882001-03-16 22:47:14 +000029
Eric Andersenaad1a882001-03-16 22:47:14 +000030 total += cc;
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 buf = ((const char *)buf) + cc;
Eric Andersenaad1a882001-03-16 22:47:14 +000032 len -= cc;
33 }
34
35 return total;
36}