Back to index

nagios-plugins  1.4.16
full-write.c
Go to the documentation of this file.
00001 /* An interface to read and write that retries (if necessary) until complete.
00002 
00003    Copyright (C) 1993-1994, 1997-2006, 2009-2010 Free Software Foundation, Inc.
00004 
00005    This program is free software: you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 #include <config.h>
00019 
00020 /* Specification.  */
00021 #ifdef FULL_READ
00022 # include "full-read.h"
00023 #else
00024 # include "full-write.h"
00025 #endif
00026 
00027 #include <errno.h>
00028 
00029 #ifdef FULL_READ
00030 # include "safe-read.h"
00031 # define safe_rw safe_read
00032 # define full_rw full_read
00033 # undef const
00034 # define const /* empty */
00035 #else
00036 # include "safe-write.h"
00037 # define safe_rw safe_write
00038 # define full_rw full_write
00039 #endif
00040 
00041 #ifdef FULL_READ
00042 /* Set errno to zero upon EOF.  */
00043 # define ZERO_BYTE_TRANSFER_ERRNO 0
00044 #else
00045 /* Some buggy drivers return 0 when one tries to write beyond
00046    a device's end.  (Example: Linux 1.2.13 on /dev/fd0.)
00047    Set errno to ENOSPC so they get a sensible diagnostic.  */
00048 # define ZERO_BYTE_TRANSFER_ERRNO ENOSPC
00049 #endif
00050 
00051 /* Write(read) COUNT bytes at BUF to(from) descriptor FD, retrying if
00052    interrupted or if a partial write(read) occurs.  Return the number
00053    of bytes transferred.
00054    When writing, set errno if fewer than COUNT bytes are written.
00055    When reading, if fewer than COUNT bytes are read, you must examine
00056    errno to distinguish failure from EOF (errno == 0).  */
00057 size_t
00058 full_rw (int fd, const void *buf, size_t count)
00059 {
00060   size_t total = 0;
00061   const char *ptr = (const char *) buf;
00062 
00063   while (count > 0)
00064     {
00065       size_t n_rw = safe_rw (fd, ptr, count);
00066       if (n_rw == (size_t) -1)
00067         break;
00068       if (n_rw == 0)
00069         {
00070           errno = ZERO_BYTE_TRANSFER_ERRNO;
00071           break;
00072         }
00073       total += n_rw;
00074       ptr += n_rw;
00075       count -= n_rw;
00076     }
00077 
00078   return total;
00079 }