Debian Codegraph @

lib/stdio-read.c

raw
1 /* POSIX compatible FILE stream read function.
2 Copyright (C) 2008-2026 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2011.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include <stdio.h>
22
23 /* Replace these functions only if module 'nonblocking' is requested. */
24 #if GNULIB_NONBLOCKING
25
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. */
31
32 # if defined _WIN32 && ! defined __CYGWIN__
33
34 # include <errno.h>
35 # include <io.h>
36
37 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
38 # include <windows.h>
39
40 # if GNULIB_MSVC_NOTHROW
41 # include "msvc-nothrow.h"
42 # else
43 # include <io.h>
44 # endif
45
46 /* Don't assume that UNICODE is not defined. */
47 # undef GetNamedPipeHandleState
48 # define GetNamedPipeHandleState GetNamedPipeHandleStateA
49
50 # define CALL_WITH_ERRNO_FIX(RETTYPE, EXPRESSION, FAILED) \
51 if (ferror (stream)) \
52 return (EXPRESSION); \
53 else \
54 { \
55 SetLastError (0); \
56 RETTYPE ret = (EXPRESSION); \
57 if (FAILED) \
58 { \
59 if (GetLastError () == ERROR_NO_DATA && ferror (stream)) \
60 { \
61 int fd = fileno (stream); \
62 if (fd >= 0) \
63 { \
64 HANDLE h = (HANDLE) _get_osfhandle (fd); \
65 if (GetFileType (h) == FILE_TYPE_PIPE) \
66 { \
67 /* h is a pipe or socket. */ \
68 DWORD state; \
69 if (GetNamedPipeHandleState (h, &state, NULL, NULL, \
70 NULL, NULL, 0) \
71 && (state & PIPE_NOWAIT) != 0) \
72 /* h is a pipe in non-blocking mode. \
73 Change errno from EINVAL to EAGAIN. */ \
74 errno = EAGAIN; \
75 } \
76 } \
77 } \
78 } \
79 return ret; \
80 }
81
82 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
83 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
84 # if GNULIB_SCANF
85 int
86 scanf (const char *format, ...)
87 {
88 va_list args;
89 va_start (args, format);
90 int retval = vfscanf (stdin, format, args);
91 va_end (args);
92
93 return retval;
94 }
95 # endif
96
97 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
98 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
99 # if GNULIB_FSCANF
100 int
101 fscanf (FILE *stream, const char *format, ...)
102 {
103 va_list args;
104 va_start (args, format);
105 int retval = vfscanf (stream, format, args);
106 va_end (args);
107
108 return retval;
109 }
110 # endif
111
112 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
113 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
114 # if GNULIB_VSCANF
115 int
116 vscanf (const char *format, va_list args)
117 {
118 return vfscanf (stdin, format, args);
119 }
120 # endif
121
122 /* Enable this function definition only if gnulib's <stdio.h> has prepared it.
123 Otherwise we get a function definition conflict with mingw64's <stdio.h>. */
124 # if GNULIB_VFSCANF
125 int
126 vfscanf (FILE *stream, const char *format, va_list args)
127 #undef vfscanf
128 {
129 CALL_WITH_ERRNO_FIX (int, vfscanf (stream, format, args), ret == EOF)
130 }
131 # endif
132
133 int
134 getchar (void)
135 {
136 return fgetc (stdin);
137 }
138
139 int
140 fgetc (FILE *stream)
141 #undef fgetc
142 {
143 CALL_WITH_ERRNO_FIX (int, fgetc (stream), ret == EOF)
144 }
145
146 char *
147 fgets (char *s, int n, FILE *stream)
148 #undef fgets
149 {
150 CALL_WITH_ERRNO_FIX (char *, fgets (s, n, stream), ret == NULL)
151 }
152
153 /* We intentionally don't bother to fix gets. */
154
155 size_t
156 fread (void *ptr, size_t s, size_t n, FILE *stream)
157 #undef fread
158 {
159 CALL_WITH_ERRNO_FIX (size_t, fread (ptr, s, n, stream), ret < n)
160 }
161
162 # endif
163 #endif