blob: d812d04b42a1f4dc99e8bd0f8d244b4f6b19327e [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
10#include <stdio.h>
11#include <unistd.h>
12#include "libbb.h"
13
14/*
15 * Write all of the supplied buffer out to a file.
16 * This does multiple writes as necessary.
17 * Returns the amount written, or -1 on an error.
18 */
Rob Landley53437472006-07-16 08:14:35 +000019ssize_t full_write(int fd, const void *buf, size_t len)
Eric Andersenaad1a882001-03-16 22:47:14 +000020{
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 ssize_t cc;
22 ssize_t total;
Eric Andersenaad1a882001-03-16 22:47:14 +000023
24 total = 0;
25
26 while (len > 0) {
Eric Andersendae099b2003-10-09 08:35:42 +000027 cc = safe_write(fd, buf, len);
Eric Andersenaad1a882001-03-16 22:47:14 +000028
29 if (cc < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 return cc; /* write() returns -1 on failure. */
Eric Andersenaad1a882001-03-16 22:47:14 +000031
Eric Andersenaad1a882001-03-16 22:47:14 +000032 total += cc;
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 buf = ((const char *)buf) + cc;
Eric Andersenaad1a882001-03-16 22:47:14 +000034 len -= cc;
35 }
36
37 return total;
38}