diff options
-rw-r--r-- | GNUmakefile | 8 | ||||
-rw-r--r-- | gl/getaddrinfo.c | 16 | ||||
-rw-r--r-- | gl/m4/getaddrinfo.m4 | 5 | ||||
-rw-r--r-- | lgl/Makefile.am | 7 | ||||
-rw-r--r-- | lgl/m4/gnulib-comp.m4 | 2 | ||||
-rw-r--r-- | lgl/m4/stdio_h.m4 | 4 | ||||
-rw-r--r-- | lgl/m4/sys_ioctl_h.m4 | 60 | ||||
-rw-r--r-- | lgl/m4/sys_socket_h.m4 | 8 | ||||
-rw-r--r-- | lgl/m4/unistd_h.m4 | 5 | ||||
-rw-r--r-- | lgl/stdio-write.c | 148 | ||||
-rw-r--r-- | lgl/stdio.in.h | 15 | ||||
-rw-r--r-- | lgl/sys_socket.in.h | 34 | ||||
-rw-r--r-- | lgl/unistd.in.h | 27 | ||||
-rw-r--r-- | lgl/winsock.c | 348 |
14 files changed, 669 insertions, 18 deletions
diff --git a/GNUmakefile b/GNUmakefile index 7816416eec..0fdd3fa735 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -72,18 +72,18 @@ ifeq ($(_have-git-version-gen)0,yes$(MAKELEVEL)) # should run 'autoreconf' (or something like 'make distcheck') to # fix the version, 'make all' to propagate it, then 'make install'. $(info WARNING: version string $(VERSION) is out of date;) - $(info run '$(MAKE) version' to fix it) + $(info run '$(MAKE) _version' to fix it) else $(info INFO: running autoreconf for new version string: $(_curr-ver)) - _dummy := $(shell $(MAKE) $(AM_MAKEFLAGS) version) + _dummy := $(shell $(MAKE) $(AM_MAKEFLAGS) _version) endif endif endif endif endif -.PHONY: version -version: +.PHONY: _version +_version: cd $(srcdir) && rm -rf autom4te.cache .version && $(_autoreconf) else diff --git a/gl/getaddrinfo.c b/gl/getaddrinfo.c index 8c6a9d1026..3016c93f20 100644 --- a/gl/getaddrinfo.c +++ b/gl/getaddrinfo.c @@ -301,6 +301,22 @@ getaddrinfo (const char *restrict nodename, tmp->ai_addr->sa_family = he->h_addrtype; tmp->ai_family = he->h_addrtype; +#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN + switch (he->h_addrtype) + { +#if HAVE_IPV4 + case AF_INET: + tmp->ai_addr->sa_len = sizeof (struct sockaddr_in); + break; +#endif +#if HAVE_IPV6 + case AF_INET6: + tmp->ai_addr->sa_len = sizeof (struct sockaddr_in6); + break; +#endif + } +#endif + /* FIXME: If more than one address, create linked list of addrinfo's. */ *res = tmp; diff --git a/gl/m4/getaddrinfo.m4 b/gl/m4/getaddrinfo.m4 index 5d36c19505..52135ba947 100644 --- a/gl/m4/getaddrinfo.m4 +++ b/gl/m4/getaddrinfo.m4 @@ -99,6 +99,11 @@ AC_DEFUN([gl_PREREQ_GETADDRINFO], [ AC_REQUIRE([gl_HEADER_SYS_SOCKET]) AC_REQUIRE([AC_C_INLINE]) AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + dnl Including sys/socket.h is wrong for Windows, but Windows does not + dnl have sa_len so the result is correct anyway. + AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [#include <sys/socket.h>]) + AC_CHECK_HEADERS_ONCE(netinet/in.h netdb.h) AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, gai_strerror, getnameinfo],,,[ /* sys/types.h is not needed according to POSIX, but the diff --git a/lgl/Makefile.am b/lgl/Makefile.am index 4908e84784..4e826f1695 100644 --- a/lgl/Makefile.am +++ b/lgl/Makefile.am @@ -349,6 +349,7 @@ stdio.h: stdio.in.h -e 's|@''GNULIB_FTELL''@|$(GNULIB_FTELL)|g' \ -e 's|@''GNULIB_FTELLO''@|$(GNULIB_FTELLO)|g' \ -e 's|@''GNULIB_FFLUSH''@|$(GNULIB_FFLUSH)|g' \ + -e 's|@''GNULIB_FCLOSE''@|$(GNULIB_FCLOSE)|g' \ -e 's|@''GNULIB_FPUTC''@|$(GNULIB_FPUTC)|g' \ -e 's|@''GNULIB_PUTC''@|$(GNULIB_PUTC)|g' \ -e 's|@''GNULIB_PUTCHAR''@|$(GNULIB_PUTCHAR)|g' \ @@ -381,6 +382,7 @@ stdio.h: stdio.in.h -e 's|@''REPLACE_FTELLO''@|$(REPLACE_FTELLO)|g' \ -e 's|@''REPLACE_FTELL''@|$(REPLACE_FTELL)|g' \ -e 's|@''REPLACE_FFLUSH''@|$(REPLACE_FFLUSH)|g' \ + -e 's|@''REPLACE_FCLOSE''@|$(REPLACE_FCLOSE)|g' \ -e 's|@''HAVE_DECL_GETDELIM''@|$(HAVE_DECL_GETDELIM)|g' \ -e 's|@''HAVE_DECL_GETLINE''@|$(HAVE_DECL_GETLINE)|g' \ -e 's|@''REPLACE_GETLINE''@|$(REPLACE_GETLINE)|g' \ @@ -576,6 +578,7 @@ sys/socket.h: sys_socket.in.h -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ -e 's|@''NEXT_SYS_SOCKET_H''@|$(NEXT_SYS_SOCKET_H)|g' \ -e 's|@''HAVE_SYS_SOCKET_H''@|$(HAVE_SYS_SOCKET_H)|g' \ + -e 's|@''GNULIB_CLOSE''@|$(GNULIB_CLOSE)|g' \ -e 's|@''GNULIB_SOCKET''@|$(GNULIB_SOCKET)|g' \ -e 's|@''GNULIB_CONNECT''@|$(GNULIB_CONNECT)|g' \ -e 's|@''GNULIB_ACCEPT''@|$(GNULIB_ACCEPT)|g' \ @@ -589,6 +592,7 @@ sys/socket.h: sys_socket.in.h -e 's|@''GNULIB_RECVFROM''@|$(GNULIB_RECVFROM)|g' \ -e 's|@''GNULIB_SENDTO''@|$(GNULIB_SENDTO)|g' \ -e 's|@''GNULIB_SETSOCKOPT''@|$(GNULIB_SETSOCKOPT)|g' \ + -e 's|@''GNULIB_SHUTDOWN''@|$(GNULIB_SHUTDOWN)|g' \ -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \ -e 's|@''HAVE_WS2TCPIP_H''@|$(HAVE_WS2TCPIP_H)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ @@ -679,6 +683,7 @@ unistd.h: unistd.in.h -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ -e 's|@''NEXT_UNISTD_H''@|$(NEXT_UNISTD_H)|g' \ -e 's|@''GNULIB_CHOWN''@|$(GNULIB_CHOWN)|g' \ + -e 's|@''GNULIB_CLOSE''@|$(GNULIB_CLOSE)|g' \ -e 's|@''GNULIB_DUP2''@|$(GNULIB_DUP2)|g' \ -e 's|@''GNULIB_ENVIRON''@|$(GNULIB_ENVIRON)|g' \ -e 's|@''GNULIB_FCHDIR''@|$(GNULIB_FCHDIR)|g' \ @@ -706,12 +711,14 @@ unistd.h: unistd.in.h -e 's|@''HAVE_OS_H''@|$(HAVE_OS_H)|g' \ -e 's|@''HAVE_SYS_PARAM_H''@|$(HAVE_SYS_PARAM_H)|g' \ -e 's|@''REPLACE_CHOWN''@|$(REPLACE_CHOWN)|g' \ + -e 's|@''REPLACE_CLOSE''@|$(REPLACE_CLOSE)|g' \ -e 's|@''REPLACE_FCHDIR''@|$(REPLACE_FCHDIR)|g' \ -e 's|@''REPLACE_GETCWD''@|$(REPLACE_GETCWD)|g' \ -e 's|@''REPLACE_GETPAGESIZE''@|$(REPLACE_GETPAGESIZE)|g' \ -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \ -e 's|@''REPLACE_LSEEK''@|$(REPLACE_LSEEK)|g' \ -e 's|@''REPLACE_WRITE''@|$(REPLACE_WRITE)|g' \ + -e 's|@''UNISTD_H_HAVE_WINSOCK2_H''@|$(UNISTD_H_HAVE_WINSOCK2_H)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ < $(srcdir)/unistd.in.h; \ } > $@-t diff --git a/lgl/m4/gnulib-comp.m4 b/lgl/m4/gnulib-comp.m4 index d383e12b09..b6074b3a0c 100644 --- a/lgl/m4/gnulib-comp.m4 +++ b/lgl/m4/gnulib-comp.m4 @@ -79,6 +79,7 @@ AC_DEFUN([lgl_INIT], gl_FUNC_STRVERSCMP gl_STRING_MODULE_INDICATOR([strverscmp]) gl_HEADER_SYS_SOCKET + gl_MODULE_INDICATOR([sys_socket]) AC_PROG_MKDIR_P gl_HEADER_SYS_STAT_H AC_PROG_MKDIR_P @@ -332,6 +333,7 @@ AC_DEFUN([lgl_FILE_LIST], [ m4/string_h.m4 m4/strings_h.m4 m4/strverscmp.m4 + m4/sys_ioctl_h.m4 m4/sys_socket_h.m4 m4/sys_stat_h.m4 m4/threadlib.m4 diff --git a/lgl/m4/stdio_h.m4 b/lgl/m4/stdio_h.m4 index dea81e3aa5..60b4bd791a 100644 --- a/lgl/m4/stdio_h.m4 +++ b/lgl/m4/stdio_h.m4 @@ -1,4 +1,4 @@ -# stdio_h.m4 serial 13 +# stdio_h.m4 serial 14 dnl Copyright (C) 2007-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -63,6 +63,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], GNULIB_FTELL=0; AC_SUBST([GNULIB_FTELL]) GNULIB_FTELLO=0; AC_SUBST([GNULIB_FTELLO]) GNULIB_FFLUSH=0; AC_SUBST([GNULIB_FFLUSH]) + GNULIB_FCLOSE=0; AC_SUBST([GNULIB_FCLOSE]) GNULIB_FPUTC=0; AC_SUBST([GNULIB_FPUTC]) GNULIB_PUTC=0; AC_SUBST([GNULIB_PUTC]) GNULIB_PUTCHAR=0; AC_SUBST([GNULIB_PUTCHAR]) @@ -98,6 +99,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], REPLACE_FTELLO=0; AC_SUBST([REPLACE_FTELLO]) REPLACE_FTELL=0; AC_SUBST([REPLACE_FTELL]) REPLACE_FFLUSH=0; AC_SUBST([REPLACE_FFLUSH]) + REPLACE_FCLOSE=0; AC_SUBST([REPLACE_FCLOSE]) HAVE_DECL_GETDELIM=1; AC_SUBST([HAVE_DECL_GETDELIM]) HAVE_DECL_GETLINE=1; AC_SUBST([HAVE_DECL_GETLINE]) REPLACE_GETLINE=0; AC_SUBST([REPLACE_GETLINE]) diff --git a/lgl/m4/sys_ioctl_h.m4 b/lgl/m4/sys_ioctl_h.m4 new file mode 100644 index 0000000000..5488039b6c --- /dev/null +++ b/lgl/m4/sys_ioctl_h.m4 @@ -0,0 +1,60 @@ +# sys_ioctl_h.m4 serial 1 +dnl Copyright (C) 2008 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +dnl Written by Bruno Haible. + +AC_DEFUN([gl_SYS_IOCTL_H], +[ + dnl Use AC_REQUIRE here, so that the default behavior below is expanded + dnl once only, before all statements that occur in other macros. + AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS]) + + AC_CHECK_HEADERS_ONCE([sys/ioctl.h]) + if test $ac_cv_header_sys_ioctl_h = yes; then + HAVE_SYS_IOCTL_H=1 + dnl Test whether <sys/ioctl.h> declares ioctl(), or whether some other + dnl header file, such as <unistd.h> or <stropts.h>, is needed for that. + AC_CACHE_CHECK([whether <sys/ioctl.h> declares ioctl], + [gl_cv_decl_ioctl_in_sys_ioctl_h], + [AC_CHECK_DECL([ioctl], + [gl_cv_decl_ioctl_in_sys_ioctl_h=yes], + [gl_cv_decl_ioctl_in_sys_ioctl_h=no], + [#include <sys/ioctl.h>]) + ]) + if test $gl_cv_decl_ioctl_in_sys_ioctl_h != yes; then + SYS_IOCTL_H='sys/ioctl.h' + fi + else + HAVE_SYS_IOCTL_H=0 + SYS_IOCTL_H='sys/ioctl.h' + fi + AC_SUBST([HAVE_SYS_IOCTL_H]) + dnl Execute this unconditionally, because SYS_IOCTL_H may be set by other + dnl modules, after this code is executed. + gl_CHECK_NEXT_HEADERS([sys/ioctl.h]) +]) + +dnl Unconditionally enables the replacement of <sys/ioctl.h>. +AC_DEFUN([gl_REPLACE_SYS_IOCTL_H], +[ + AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS]) + SYS_IOCTL_H='sys/ioctl.h' +]) + +AC_DEFUN([gl_SYS_IOCTL_MODULE_INDICATOR], +[ + dnl Use AC_REQUIRE here, so that the default settings are expanded once only. + AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS]) + GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1 +]) + +AC_DEFUN([gl_SYS_IOCTL_H_DEFAULTS], +[ + GNULIB_IOCTL=0; AC_SUBST([GNULIB_IOCTL]) + dnl Assume proper GNU behavior unless another module says otherwise. + SYS_IOCTL_H_HAVE_WINSOCK2_H=0; AC_SUBST([SYS_IOCTL_H_HAVE_WINSOCK2_H]) + SYS_IOCTL_H=''; AC_SUBST([SYS_IOCTL_H]) +]) diff --git a/lgl/m4/sys_socket_h.m4 b/lgl/m4/sys_socket_h.m4 index 1e38ee3a9e..fc43d5c5c0 100644 --- a/lgl/m4/sys_socket_h.m4 +++ b/lgl/m4/sys_socket_h.m4 @@ -1,4 +1,4 @@ -# sys_socket_h.m4 serial 8 +# sys_socket_h.m4 serial 11 dnl Copyright (C) 2005-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -71,6 +71,8 @@ AC_DEFUN([gl_HEADER_SYS_SOCKET], # Sets and substitutes HAVE_WINSOCK2_H. AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2], [ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS]) AC_CHECK_HEADERS_ONCE([sys/socket.h]) if test $ac_cv_header_sys_socket_h != yes; then dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make @@ -81,6 +83,8 @@ AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2], fi if test "$ac_cv_header_winsock2_h" = yes; then HAVE_WINSOCK2_H=1 + UNISTD_H_HAVE_WINSOCK2_H=1 + SYS_IOCTL_H_HAVE_WINSOCK2_H=1 else HAVE_WINSOCK2_H=0 fi @@ -97,6 +101,7 @@ AC_DEFUN([gl_SYS_SOCKET_MODULE_INDICATOR], AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS], [ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) dnl for GNULIB_CLOSE GNULIB_SOCKET=0; AC_SUBST([GNULIB_SOCKET]) GNULIB_CONNECT=0; AC_SUBST([GNULIB_CONNECT]) GNULIB_ACCEPT=0; AC_SUBST([GNULIB_ACCEPT]) @@ -110,4 +115,5 @@ AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS], GNULIB_RECVFROM=0; AC_SUBST([GNULIB_RECVFROM]) GNULIB_SENDTO=0; AC_SUBST([GNULIB_SENDTO]) GNULIB_SETSOCKOPT=0; AC_SUBST([GNULIB_SETSOCKOPT]) + GNULIB_SHUTDOWN=0; AC_SUBST([GNULIB_SHUTDOWN]) ]) diff --git a/lgl/m4/unistd_h.m4 b/lgl/m4/unistd_h.m4 index e6eb045cad..e9f7a7d58b 100644 --- a/lgl/m4/unistd_h.m4 +++ b/lgl/m4/unistd_h.m4 @@ -1,4 +1,4 @@ -# unistd_h.m4 serial 13 +# unistd_h.m4 serial 14 dnl Copyright (C) 2006-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -33,6 +33,7 @@ AC_DEFUN([gl_UNISTD_MODULE_INDICATOR], AC_DEFUN([gl_UNISTD_H_DEFAULTS], [ GNULIB_CHOWN=0; AC_SUBST([GNULIB_CHOWN]) + GNULIB_CLOSE=0; AC_SUBST([GNULIB_CLOSE]) GNULIB_DUP2=0; AC_SUBST([GNULIB_DUP2]) GNULIB_ENVIRON=0; AC_SUBST([GNULIB_ENVIRON]) GNULIB_FCHDIR=0; AC_SUBST([GNULIB_FCHDIR]) @@ -61,10 +62,12 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], HAVE_OS_H=0; AC_SUBST([HAVE_OS_H]) HAVE_SYS_PARAM_H=0; AC_SUBST([HAVE_SYS_PARAM_H]) REPLACE_CHOWN=0; AC_SUBST([REPLACE_CHOWN]) + REPLACE_CLOSE=0; AC_SUBST([REPLACE_CLOSE]) REPLACE_FCHDIR=0; AC_SUBST([REPLACE_FCHDIR]) REPLACE_GETCWD=0; AC_SUBST([REPLACE_GETCWD]) REPLACE_GETPAGESIZE=0; AC_SUBST([REPLACE_GETPAGESIZE]) REPLACE_LCHOWN=0; AC_SUBST([REPLACE_LCHOWN]) REPLACE_LSEEK=0; AC_SUBST([REPLACE_LSEEK]) REPLACE_WRITE=0; AC_SUBST([REPLACE_WRITE]) + UNISTD_H_HAVE_WINSOCK2_H=0; AC_SUBST([UNISTD_H_HAVE_WINSOCK2_H]) ]) diff --git a/lgl/stdio-write.c b/lgl/stdio-write.c new file mode 100644 index 0000000000..0abcfc4088 --- /dev/null +++ b/lgl/stdio-write.c @@ -0,0 +1,148 @@ +/* POSIX compatible FILE stream write function. + Copyright (C) 2008 Free Software Foundation, Inc. + Written by Bruno Haible <bruno@clisp.org>, 2008. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <stdio.h> + +/* Replace these functions only if module 'sigpipe' is requested. */ +#if GNULIB_SIGPIPE + +/* On native Windows platforms, SIGPIPE does not exist. When write() is + called on a pipe with no readers, WriteFile() fails with error + GetLastError() = ERROR_NO_DATA, and write() in consequence fails with + error EINVAL. This write() function is at the basis of the function + which flushes the buffer of a FILE stream. */ + +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + +# include <errno.h> +# include <signal.h> +# include <io.h> + +# define WIN32_LEAN_AND_MEAN /* avoid including junk */ +# include <windows.h> + +# define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \ + if (ferror (stream)) \ + return (EXPRESSION); \ + else \ + { \ + RETTYPE ret; \ + SetLastError (0); \ + ret = (EXPRESSION); \ + if (FAILED && GetLastError () == ERROR_NO_DATA && ferror (stream)) \ + { \ + int fd = fileno (stream); \ + if (fd >= 0 \ + && GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_PIPE)\ + { \ + /* Try to raise signal SIGPIPE. */ \ + raise (SIGPIPE); \ + /* If it is currently blocked or ignored, change errno from \ + EINVAL to EPIPE. */ \ + errno = EPIPE; \ + } \ + } \ + return ret; \ + } + +# if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */ +int +printf (const char *format, ...) +{ + int retval; + va_list args; + + va_start (args, format); + retval = vfprintf (stdout, format, args); + va_end (args); + + return retval; +} +# endif + +# if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */ +int +fprintf (FILE *stream, const char *format, ...) +{ + int retval; + va_list args; + + va_start (args, format); + retval = vfprintf (stream, format, args); + va_end (args); + + return retval; +} +# endif + +# if !REPLACE_VFPRINTF_POSIX /* avoid collision with vprintf.c */ +int +vprintf (const char *format, va_list args) +{ + return vfprintf (stdout, format, args); +} +# endif + +# if !REPLACE_VPRINTF_POSIX /* avoid collision with vfprintf.c */ +int +vfprintf (FILE *stream, const char *format, va_list args) +#undef vfprintf +{ + CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF) +} +# endif + +int +putchar (int c) +{ + return fputc (c, stdout); +} + +int +fputc (int c, FILE *stream) +#undef fputc +{ + CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF) +} + +int +fputs (const char *string, FILE *stream) +#undef fputs +{ + CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF) +} + +int +puts (const char *string) +#undef puts +{ + FILE *stream = stdout; + CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF) +} + +size_t +fwrite (const void *ptr, size_t s, size_t n, FILE *stream) +#undef fwrite +{ + CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n) +} + +# endif +#endif diff --git a/lgl/stdio.in.h b/lgl/stdio.in.h index 202c3cc6f8..297780111d 100644 --- a/lgl/stdio.in.h +++ b/lgl/stdio.in.h @@ -373,6 +373,21 @@ extern long rpl_ftell (FILE *fp); fflush (f)) #endif +#if @GNULIB_FCLOSE@ +# if @REPLACE_FCLOSE@ +# define fclose rpl_fclose + /* Close STREAM and its underlying file descriptor. */ +extern int fclose (FILE *stream); +# endif +#elif defined GNULIB_POSIXCHECK +# undef fclose +# define fclose(f) \ + (GL_LINK_WARNING ("fclose is not always POSIX compliant - " \ + "use gnulib module fclose for portable " \ + "POSIX compliance"), \ + fclose (f)) +#endif + #if @GNULIB_FPUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@ # undef fputc # define fputc rpl_fputc diff --git a/lgl/sys_socket.in.h b/lgl/sys_socket.in.h index 4d24d5fc69..980dd59710 100644 --- a/lgl/sys_socket.in.h +++ b/lgl/sys_socket.in.h @@ -138,10 +138,9 @@ rpl_fd_isset (int fd, fd_set * set) /* Wrap everything else to use libc file descriptors for sockets. */ -# if @HAVE_WINSOCK2_H@ +# if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H # undef close -# define close rpl_close -extern int rpl_close(int); +# define close close_used_without_including_unistd_h # endif # if @GNULIB_SOCKET@ @@ -280,12 +279,6 @@ extern int rpl_listen (int, int); listen (s, b)) # endif -# if @HAVE_WINSOCK2_H@ -# undef ioctl -# define ioctl rpl_ioctl -extern int rpl_ioctl (int, unsigned long, char *); -# endif - # if @GNULIB_RECV@ # if @HAVE_WINSOCK2_H@ # undef recv @@ -371,11 +364,34 @@ extern int rpl_setsockopt (int, int, int, const void *, int); setsockopt (s, lvl, o, v, l)) # endif +# if @GNULIB_SHUTDOWN@ +# if @HAVE_WINSOCK2_H@ +# undef shutdown +# define shutdown rpl_shutdown +extern int rpl_shutdown (int, int); +# endif +# elif @HAVE_WINSOCK2_H@ +# undef shutdown +# define shutdown shutdown_used_without_requesting_gnulib_module_shutdown +# elif defined GNULIB_POSIXCHECK +# undef shutdown +# define shutdown(s,h) \ + (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \ + "use gnulib module shutdown for portability"), \ + shutdown (s, h)) +# endif + # if @HAVE_WINSOCK2_H@ # undef select # define select select_used_without_including_sys_select_h # endif +# if @GNULIB_CLOSE@ && @HAVE_WINSOCK2_H@ +/* gnulib internal function. */ +# define HAVE__GL_CLOSE_FD_MAYBE_SOCKET 1 +extern int _gl_close_fd_maybe_socket (int fd); +# endif + # ifdef __cplusplus } # endif diff --git a/lgl/unistd.in.h b/lgl/unistd.in.h index 017c4e2146..ddf5c11c17 100644 --- a/lgl/unistd.in.h +++ b/lgl/unistd.in.h @@ -75,6 +75,25 @@ extern int chown (const char *file, uid_t uid, gid_t gid); #endif +#if @GNULIB_CLOSE@ +# if @REPLACE_CLOSE@ +/* Automatically included by modules that need a replacement for close. */ +# undef close +# define close rpl_close +extern int close (int); +# endif +#elif @UNISTD_H_HAVE_WINSOCK2_H@ +# undef close +# define close close_used_without_requesting_gnulib_module_close +#elif defined GNULIB_POSIXCHECK +# undef close +# define close(f) \ + (GL_LINK_WARNING ("close does not portably work on sockets - " \ + "use gnulib module close for portability"), \ + close (f)) +#endif + + #if @GNULIB_DUP2@ # if !@HAVE_DUP2@ /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if @@ -123,8 +142,6 @@ extern char **environ; <http://www.opengroup.org/susv3xsh/fchdir.html>. */ extern int fchdir (int /*fd*/); -# define close rpl_close -extern int close (int); # define dup rpl_dup extern int dup (int); # define dup2 rpl_dup2 @@ -379,6 +396,12 @@ extern ssize_t write (int fd, const void *buf, size_t count); #endif +#ifdef FCHDIR_REPLACEMENT +/* gnulib internal function. */ +extern void _gl_unregister_fd (int fd); +#endif + + #ifdef __cplusplus } #endif diff --git a/lgl/winsock.c b/lgl/winsock.c new file mode 100644 index 0000000000..44654439f6 --- /dev/null +++ b/lgl/winsock.c @@ -0,0 +1,348 @@ +/* winsock.c --- wrappers for Windows socket functions + + Copyright (C) 2008 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Written by Paolo Bonzini */ + +#include <config.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <io.h> +#include <sys/socket.h> +#if GNULIB_IOCTL +#include <sys/ioctl.h> +#endif + +#undef socket +#undef connect +#undef accept +#undef bind +#undef getpeername +#undef getsockname +#undef getsockopt +#undef listen +#undef recv +#undef send +#undef recvfrom +#undef sendto +#undef setsockopt +#undef shutdown + +#define FD_TO_SOCKET(fd) ((SOCKET) _get_osfhandle ((fd))) +#define SOCKET_TO_FD(fh) (_open_osfhandle ((long) (fh), O_RDWR | O_BINARY)) + + +static inline void +set_winsock_errno (void) +{ + int err = WSAGetLastError (); + WSASetLastError (0); + + /* Map some WSAE* errors to the runtime library's error codes. */ + switch (err) + { + case WSA_INVALID_HANDLE: + errno = EBADF; + break; + case WSA_NOT_ENOUGH_MEMORY: + errno = ENOMEM; + break; + case WSA_INVALID_PARAMETER: + errno = EINVAL; + break; + case WSAEWOULDBLOCK: + errno = EWOULDBLOCK; + break; + case WSAENAMETOOLONG: + errno = ENAMETOOLONG; + break; + case WSAENOTEMPTY: + errno = ENOTEMPTY; + break; + default: + errno = (err > 10000 && err < 10025) ? err - 10000 : err; + break; + } +} + + +/* Hook for gnulib module close. */ + +#if HAVE__GL_CLOSE_FD_MAYBE_SOCKET +int +_gl_close_fd_maybe_socket (int fd) +{ + SOCKET sock = FD_TO_SOCKET (fd); + WSANETWORKEVENTS ev; + + ev.lNetworkEvents = 0xDEADBEEF; + WSAEnumNetworkEvents (sock, NULL, &ev); + if (ev.lNetworkEvents != 0xDEADBEEF) + { + /* FIXME: other applications, like squid, use an undocumented + _free_osfhnd free function. But this is not enough: The 'osfile' + flags for fd also needs to be cleared, but it is hard to access it. + Instead, here we just close twice the file descriptor. */ + if (closesocket (sock)) + { + set_winsock_errno (); + return -1; + } + else + { + /* This call frees the file descriptor and does a + CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */ + _close (fd); + return 0; + } + } + else + return _close (fd); +} +#endif + + +/* Wrappers for WinSock functions. */ + +#if GNULIB_SOCKET +int +rpl_socket (int domain, int type, int protocol) +{ + /* We have to use WSASocket() to create non-overlapped IO sockets. + Overlapped IO sockets cannot be used with read/write. */ + SOCKET fh = WSASocket (domain, type, protocol, NULL, 0, 0); + + if (fh == INVALID_SOCKET) + { + set_winsock_errno (); + return -1; + } + else + return SOCKET_TO_FD (fh); +} +#endif + +#if GNULIB_CONNECT +int +rpl_connect (int fd, struct sockaddr *sockaddr, int len) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = connect (sock, sockaddr, len); + if (r < 0) + { + /* EINPROGRESS is not returned by WinSock 2.0; for backwards + compatibility, connect(2) uses EWOULDBLOCK. */ + if (WSAGetLastError () == WSAEWOULDBLOCK) + WSASetLastError (WSAEINPROGRESS); + + set_winsock_errno (); + } + + return r; +} +#endif + +#if GNULIB_ACCEPT +int +rpl_accept (int fd, struct sockaddr *addr, int *addrlen) +{ + SOCKET fh = accept (FD_TO_SOCKET (fd), addr, addrlen); + if (fh == INVALID_SOCKET) + { + set_winsock_errno (); + return -1; + } + else + return SOCKET_TO_FD (fh); +} +#endif + +#if GNULIB_BIND +int +rpl_bind (int fd, struct sockaddr *sockaddr, int len) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = bind (sock, sockaddr, len); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_GETPEERNAME +int +rpl_getpeername (int fd, struct sockaddr *addr, int *addrlen) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = getpeername (sock, addr, addrlen); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_GETSOCKNAME +int +rpl_getsockname (int fd, struct sockaddr *addr, int *addrlen) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = getsockname (sock, addr, addrlen); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_GETSOCKOPT +int +rpl_getsockopt (int fd, int level, int optname, void *optval, int *optlen) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = getsockopt (sock, level, optname, optval, optlen); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_LISTEN +int +rpl_listen (int fd, int backlog) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = listen (sock, backlog); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_IOCTL +int +rpl_ioctl (int fd, int req, ...) +{ + void *buf; + va_list args; + SOCKET sock; + int r; + + va_start (args, req); + buf = va_arg (args, void *); + va_end (args); + + sock = FD_TO_SOCKET (fd); + r = ioctlsocket (sock, req, buf); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_RECV +int +rpl_recv (int fd, void *buf, int len, int flags) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = recv (sock, buf, len, flags); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_SEND +int +rpl_send (int fd, const void *buf, int len, int flags) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = send (sock, buf, len, flags); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_RECVFROM +int +rpl_recvfrom (int fd, void *buf, int len, int flags, struct sockaddr *from, + int *fromlen) +{ + int frombufsize = *fromlen; + SOCKET sock = FD_TO_SOCKET (fd); + int r = recvfrom (sock, buf, len, flags, from, fromlen); + + if (r < 0) + set_winsock_errno (); + + /* Winsock recvfrom() only returns a valid 'from' when the socket is + connectionless. POSIX gives a valid 'from' for all types of sockets. */ + else if (*fromlen == frombufsize) + rpl_getpeername (fd, from, fromlen); + + return r; +} +#endif + +#if GNULIB_SENDTO +int +rpl_sendto (int fd, const void *buf, int len, int flags, + struct sockaddr *to, int tolen) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = sendto (sock, buf, len, flags, to, tolen); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_SETSOCKOPT +int +rpl_setsockopt (int fd, int level, int optname, const void *optval, int optlen) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = setsockopt (sock, level, optname, optval, optlen); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif + +#if GNULIB_SHUTDOWN +int +rpl_shutdown (int fd, int how) +{ + SOCKET sock = FD_TO_SOCKET (fd); + int r = shutdown (sock, how); + if (r < 0) + set_winsock_errno (); + + return r; +} +#endif |