Back to index

nagios-plugins  1.4.16
write.c
Go to the documentation of this file.
00001 /* POSIX compatible write() function.
00002    Copyright (C) 2008-2010 Free Software Foundation, Inc.
00003    Written by Bruno Haible <bruno@clisp.org>, 2008.
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 #include <unistd.h>
00022 
00023 /* Replace this function only if module 'sigpipe' is requested.  */
00024 #if GNULIB_SIGPIPE
00025 
00026 /* On native Windows platforms, SIGPIPE does not exist.  When write() is
00027    called on a pipe with no readers, WriteFile() fails with error
00028    GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
00029    error EINVAL.  */
00030 
00031 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00032 
00033 #  include <errno.h>
00034 #  include <signal.h>
00035 #  include <io.h>
00036 
00037 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
00038 #  include <windows.h>
00039 
00040 ssize_t
00041 rpl_write (int fd, const void *buf, size_t count)
00042 #undef write
00043 {
00044   ssize_t ret = write (fd, buf, count);
00045 
00046   if (ret < 0)
00047     {
00048       if (GetLastError () == ERROR_NO_DATA
00049           && GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_PIPE)
00050         {
00051           /* Try to raise signal SIGPIPE.  */
00052           raise (SIGPIPE);
00053           /* If it is currently blocked or ignored, change errno from EINVAL
00054              to EPIPE.  */
00055           errno = EPIPE;
00056         }
00057     }
00058   return ret;
00059 }
00060 
00061 # endif
00062 #endif