extractor_common.c (2470B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2012 Vidyut Samanta and Christian Grothoff 4 5 libextractor is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 libextractor is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with libextractor; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 /** 21 * @file main/extractor_common.c 22 * @brief commonly used functions within the library 23 * @author Christian Grothoff 24 */ 25 #include "platform.h" 26 #include "extractor_common.h" 27 #include "extractor_logging.h" 28 #include "extractor.h" 29 #include <sys/types.h> 30 #include <signal.h> 31 32 33 /** 34 * Writes 'size' bytes from 'buf' to 'fd', returns only when 35 * writing is not possible, or when all 'size' bytes were written 36 * (never does partial writes). 37 * 38 * @param fd fd to write into 39 * @param buf buffer to read from 40 * @param size number of bytes to write 41 * @return number of bytes written (that is 'size'), or -1 on error 42 */ 43 ssize_t 44 EXTRACTOR_write_all_ (int fd, 45 const void *buf, 46 size_t size) 47 { 48 const char *data = buf; 49 size_t off = 0; 50 ssize_t ret; 51 52 while (off < size) 53 { 54 ret = write (fd, &data[off], size - off); 55 if (ret <= 0) 56 { 57 if (-1 == ret) 58 LOG_STRERROR ("write"); 59 return -1; 60 } 61 off += ret; 62 } 63 return size; 64 } 65 66 67 /** 68 * Read a buffer from a given descriptor. 69 * 70 * @param fd descriptor to read from 71 * @param buf buffer to fill 72 * @param size number of bytes to read into 'buf' 73 * @return -1 on error, size on success 74 */ 75 ssize_t 76 EXTRACTOR_read_all_ (int fd, 77 void *buf, 78 size_t size) 79 { 80 char *data = buf; 81 size_t off = 0; 82 ssize_t ret; 83 84 while (off < size) 85 { 86 ret = read (fd, &data[off], size - off); 87 if (ret <= 0) 88 { 89 if (-1 == ret) 90 LOG_STRERROR ("write"); 91 return -1; 92 } 93 off += ret; 94 } 95 return size; 96 97 } 98 99 100 /* end of extractor_common.c */