blob: b5837d5bd9b96e648ccc749a1110d54afa0279a7 [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
Eric Andersenaad1a882001-03-16 22:47:14 +000014/*
15 * Read all of the supplied buffer from a file.
16 * This does multiple reads as necessary.
17 * Returns the amount read, or -1 on an error.
18 * A short read is returned on an end of file.
19 */
Rob Landley53437472006-07-16 08:14:35 +000020ssize_t full_read(int fd, void *buf, size_t len)
Eric Andersenaad1a882001-03-16 22:47:14 +000021{
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 ssize_t cc;
23 ssize_t total;
Eric Andersenaad1a882001-03-16 22:47:14 +000024
25 total = 0;
26
27 while (len > 0) {
Eric Andersena5c48842003-07-03 09:48:07 +000028 cc = safe_read(fd, buf, len);
Eric Andersenaad1a882001-03-16 22:47:14 +000029
30 if (cc < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 return cc; /* read() returns -1 on failure. */
Eric Andersenaad1a882001-03-16 22:47:14 +000032
33 if (cc == 0)
34 break;
35
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 buf = ((char *)buf) + cc;
Eric Andersenaad1a882001-03-16 22:47:14 +000037 total += cc;
38 len -= cc;
39 }
40
41 return total;
42}