X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsafe-read.c;h=eb0566bda11c31157865cdac4e6bafaf921beb99;hb=1313deda0c78e9bb8f4d7dd95f4f961928fb012d;hp=7fd4415bb2654b6b993838a1b8a02d3cc92ca806;hpb=5201e892c131789dfc7a2060c78a7d67edfb92ac;p=gnulib.git diff --git a/lib/safe-read.c b/lib/safe-read.c index 7fd4415bb..eb0566bda 100644 --- a/lib/safe-read.c +++ b/lib/safe-read.c @@ -1,5 +1,5 @@ /* safe-read.c -- an interface to read that retries after interrupts - Copyright (C) 1993, 1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1998 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -12,54 +12,48 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#ifdef HAVE_CONFIG_H -#include +#if HAVE_CONFIG_H +# include #endif #include -#ifdef HAVE_UNISTD_H -#include +#if HAVE_UNISTD_H +# include #endif #include -#ifndef STDC_HEADERS +#ifndef errno extern int errno; #endif -/* Read LEN bytes at PTR from descriptor DESC, for file FILENAME, - retrying if necessary. Return a negative value if an error occurs, - otherwise return the actual number of bytes read, - which must be LEN unless end-of-file was reached. */ +#include "safe-read.h" - The canonical source for this function is in the gcc distribution - in file cccp.c. So don't make any changes here. */ +/* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted. + Return the actual number of bytes read, zero for EOF, or negative + for an error. */ -static int -safe_read (desc, ptr, len) - int desc; - char *ptr; - int len; +ssize_t +safe_read (int desc, char *ptr, size_t len) { - int left = len; - while (left > 0) { - int nchars = read (desc, ptr, left); - if (nchars < 0) - { + ssize_t n_chars; + + if (len <= 0) + return len; + #ifdef EINTR - if (errno == EINTR) - continue; + do + { + n_chars = read (desc, ptr, len); + } + while (n_chars < 0 && errno == EINTR); +#else + n_chars = read (desc, ptr, len); #endif - return nchars; - } - if (nchars == 0) - break; - ptr += nchars; - left -= nchars; - } - return len - left; + + return n_chars; }