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