sys_stat: Fix 'implicit declaration of function' warning on OS/2 kLIBC.
[gnulib.git] / lib / stdio-read.c
blob1b1134832ae24e88030f8d335aafaa38b6f8eeef
1 /* POSIX compatible FILE stream read function.
2 Copyright (C) 2008-2019 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 <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <stdio.h>
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
30 a FILE stream. */
32 # if defined _WIN32 && ! defined __CYGWIN__
34 # include <errno.h>
35 # include <io.h>
37 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
38 # include <windows.h>
40 # if GNULIB_MSVC_NOTHROW
41 # include "msvc-nothrow.h"
42 # else
43 # include <io.h>
44 # endif
46 # define CALL_WITH_ERRNO_FIX(RETTYPE, EXPRESSION, FAILED) \
47 if (ferror (stream)) \
48 return (EXPRESSION); \
49 else \
50 { \
51 RETTYPE ret; \
52 SetLastError (0); \
53 ret = (EXPRESSION); \
54 if (FAILED) \
55 { \
56 if (GetLastError () == ERROR_NO_DATA && ferror (stream)) \
57 { \
58 int fd = fileno (stream); \
59 if (fd >= 0) \
60 { \
61 HANDLE h = (HANDLE) _get_osfhandle (fd); \
62 if (GetFileType (h) == FILE_TYPE_PIPE) \
63 { \
64 /* h is a pipe or socket. */ \
65 DWORD state; \
66 if (GetNamedPipeHandleState (h, &state, NULL, NULL, \
67 NULL, NULL, 0) \
68 && (state & PIPE_NOWAIT) != 0) \
69 /* h is a pipe in non-blocking mode. \
70 Change errno from EINVAL to EAGAIN. */ \
71 errno = EAGAIN; \
72 } \
73 } \
74 } \
75 } \
76 return ret; \
79 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
80 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
81 # if GNULIB_SCANF
82 int
83 scanf (const char *format, ...)
85 int retval;
86 va_list args;
88 va_start (args, format);
89 retval = vfscanf (stdin, format, args);
90 va_end (args);
92 return retval;
94 # endif
96 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
97 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
98 # if GNULIB_FSCANF
99 int
100 fscanf (FILE *stream, const char *format, ...)
102 int retval;
103 va_list args;
105 va_start (args, format);
106 retval = vfscanf (stream, format, args);
107 va_end (args);
109 return retval;
111 # endif
113 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
114 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
115 # if GNULIB_VSCANF
117 vscanf (const char *format, va_list args)
119 return vfscanf (stdin, format, args);
121 # endif
123 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
124 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
125 # if GNULIB_VFSCANF
127 vfscanf (FILE *stream, const char *format, va_list args)
128 #undef vfscanf
130 CALL_WITH_ERRNO_FIX (int, vfscanf (stream, format, args), ret == EOF)
132 # endif
135 getchar (void)
137 return fgetc (stdin);
141 fgetc (FILE *stream)
142 #undef fgetc
144 CALL_WITH_ERRNO_FIX (int, fgetc (stream), ret == EOF)
147 char *
148 fgets (char *s, int n, FILE *stream)
149 #undef fgets
151 CALL_WITH_ERRNO_FIX (char *, fgets (s, n, stream), ret == NULL)
154 /* We intentionally don't bother to fix gets. */
156 size_t
157 fread (void *ptr, size_t s, size_t n, FILE *stream)
158 #undef fread
160 CALL_WITH_ERRNO_FIX (size_t, fread (ptr, s, n, stream), ret < n)
163 # endif
164 #endif