1 /* POSIX compatible FILE stream read function.
2 Copyright (C) 2008-2012 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Replace these functions only if module 'nonblocking' is requested. */
24 #if GNULIB_NONBLOCKING
26 /* On native Windows platforms, when read() is called on a non-blocking pipe
27 with an empty buffer, ReadFile() fails with error GetLastError() =
28 ERROR_NO_DATA, and read() in consequence fails with error EINVAL. This
29 read() function is at the basis of the function which fills the buffer of
32 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
37 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
40 # include "msvc-nothrow.h"
42 # define CALL_WITH_ERRNO_FIX(RETTYPE, EXPRESSION, FAILED) \
43 if (ferror (stream)) \
44 return (EXPRESSION); \
52 if (GetLastError () == ERROR_NO_DATA && ferror (stream)) \
54 int fd = fileno (stream); \
57 HANDLE h = (HANDLE) _get_osfhandle (fd); \
58 if (GetFileType (h) == FILE_TYPE_PIPE) \
60 /* h is a pipe or socket. */ \
62 if (GetNamedPipeHandleState (h, &state, NULL, NULL, \
64 && (state & PIPE_NOWAIT) != 0) \
65 /* h is a pipe in non-blocking mode. \
66 Change errno from EINVAL to EAGAIN. */ \
75 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
76 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
79 scanf (const char *format, ...)
84 va_start (args, format);
85 retval = vfscanf (stdin, format, args);
92 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
93 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
96 fscanf (FILE *stream, const char *format, ...)
101 va_start (args, format);
102 retval = vfscanf (stream, format, args);
109 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
110 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
113 vscanf (const char *format, va_list args)
115 return vfscanf (stdin, format, args);
119 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
120 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
123 vfscanf (FILE *stream, const char *format, va_list args)
126 CALL_WITH_ERRNO_FIX (int, vfscanf (stream, format, args), ret == EOF)
133 return fgetc (stdin);
140 CALL_WITH_ERRNO_FIX (int, fgetc (stream), ret == EOF)
144 fgets (char *s, int n, FILE *stream)
147 CALL_WITH_ERRNO_FIX (char *, fgets (s, n, stream), ret == NULL)
150 /* We intentionally don't bother to fix gets. */
153 fread (void *ptr, size_t s, size_t n, FILE *stream)
156 CALL_WITH_ERRNO_FIX (size_t, fread (ptr, s, n, stream), ret < n)