00001 /* M R E A D . C 00002 * BRL-CAD 00003 * 00004 * Copyright (c) 1992-2006 United States Government as represented by 00005 * the U.S. Army Research Laboratory. 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License as 00009 * published by the Free Software Foundation; either version 2 of the 00010 * License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this file; see the file named COPYING for more 00019 * information. 00020 * 00021 */ 00022 /** @addtogroup bu_log */ 00023 /*@{*/ 00024 /** @file mread.c 00025 * 00026 * @brief multiple-read to fill a buffer 00027 * 00028 * Provide a general means to a read some count of items from a file 00029 * descriptor reading multiple times until the quantity desired is 00030 * obtained. This is useful for pipes and network connections that 00031 * don't necessarily deliver data with the same grouping as it is 00032 * written with. 00033 * 00034 * If a read error occurs, a negative value will be returns and errno 00035 * should be set (by read()). 00036 * 00037 * @authors Robert S. Miles 00038 * @authors Christopher Sean Morrison 00039 * 00040 */ 00041 00042 #include "common.h" 00043 00044 #ifdef HAVE_SYS_TYPES_H 00045 # include <sys/types.h> 00046 #endif 00047 #ifdef HAVE_UNISTD_H 00048 # include <unistd.h> 00049 #endif 00050 00051 #include "machine.h" 00052 #include "bu.h" 00053 00054 00055 /** 00056 * "Multiple try" read. Read multiple times until quantity is 00057 * obtained or an error occurs. This is useful for pipes. 00058 */ 00059 long int 00060 bu_mread(int fd, void *bufp, long int n) 00061 { 00062 register long int count = 0; 00063 register long int nread; 00064 char *cbufp = (char *)bufp; 00065 00066 while (count < n) { 00067 nread = read(fd, cbufp, (size_t)n-count); 00068 if (nread < 0) { 00069 return nread; 00070 } 00071 if (nread == 0) { 00072 return count; 00073 } 00074 count += nread; 00075 cbufp += nread; 00076 } 00077 return count; 00078 } 00079 /*@}*/ 00080 00081 /* Local Variables: 00082 * mode: C 00083 * tab-width: 8 00084 * c-basic-offset: 4 00085 * indent-tabs-mode: t 00086 * End: 00087 * ex: shiftwidth=4 tabstop=8 00088 */