1 /* assuan-io.c - Wraps the read and write functions.
2 * Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_SOCKET_H
29 # include <sys/socket.h>
33 #ifdef HAVE_W32_SYSTEM
36 # include <sys/wait.h>
39 #include "assuan-defs.h"
42 #ifndef HAVE_W32_SYSTEM
44 _assuan_waitpid (pid_t pid, int *status, int options)
46 return waitpid (pid, status, options);
52 _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size)
54 #ifdef HAVE_W32_SYSTEM
55 /* Due to the peculiarities of the W32 API we can't use read for a
56 network socket and thus we try to use recv first and fallback to
57 read if recv detects that it is not a network socket. */
60 n = recv (ctx->inbound.fd, buffer, size, 0);
61 if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
65 n = ReadFile ((HANDLE)ctx->inbound.fd, buffer, size, &nread, NULL);
68 switch (GetLastError())
70 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
79 #else /*!HAVE_W32_SYSTEM*/
80 return read (ctx->inbound.fd, buffer, size);
81 #endif /*!HAVE_W32_SYSTEM*/
85 _assuan_simple_write (assuan_context_t ctx, const void *buffer, size_t size)
87 #ifdef HAVE_W32_SYSTEM
88 /* Due to the peculiarities of the W32 API we can't use write for a
89 network socket and thus we try to use send first and fallback to
90 write if send detects that it is not a network socket. */
93 n = send (ctx->outbound.fd, buffer, size, 0);
94 if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
98 n = WriteFile ((HANDLE)ctx->outbound.fd, buffer, size, &nwrite, NULL);
101 switch (GetLastError ())
103 case ERROR_BROKEN_PIPE:
104 case ERROR_NO_DATA: errno = EPIPE; break;
105 default: errno = EIO; break;
113 #else /*!HAVE_W32_SYSTEM*/
114 return write (ctx->outbound.fd, buffer, size);
115 #endif /*!HAVE_W32_SYSTEM*/
119 #ifdef HAVE_W32_SYSTEM
121 _assuan_simple_sendmsg (assuan_context_t ctx, void *msg)
124 _assuan_simple_sendmsg (assuan_context_t ctx, struct msghdr *msg)
127 #ifdef HAVE_W32_SYSTEM
128 return _assuan_error (ASSUAN_Not_Implemented);
131 while ( (ret = sendmsg (ctx->outbound.fd, msg, 0)) == -1 && errno == EINTR)
138 #ifdef HAVE_W32_SYSTEM
140 _assuan_simple_recvmsg (assuan_context_t ctx, void *msg)
143 _assuan_simple_recvmsg (assuan_context_t ctx, struct msghdr *msg)
146 #ifdef HAVE_W32_SYSTEM
147 return _assuan_error (ASSUAN_Not_Implemented);
150 while ( (ret = recvmsg (ctx->inbound.fd, msg, 0)) == -1 && errno == EINTR)