openat: work around AIX 7.1 fstatat bug
[gnulib.git] / lib / stdio-read.c
1 /* POSIX compatible FILE stream read function.
2    Copyright (C) 2008-2011 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2011.
4
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.
9
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.
14
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/>.  */
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 __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 #  define CALL_WITH_ERRNO_FIX(RETTYPE, EXPRESSION, FAILED) \
41   if (ferror (stream))                                                        \
42     return (EXPRESSION);                                                      \
43   else                                                                        \
44     {                                                                         \
45       RETTYPE ret;                                                            \
46       SetLastError (0);                                                       \
47       ret = (EXPRESSION);                                                     \
48       if (FAILED)                                                             \
49         {                                                                     \
50           if (GetLastError () == ERROR_NO_DATA && ferror (stream))            \
51             {                                                                 \
52               int fd = fileno (stream);                                       \
53               if (fd >= 0)                                                    \
54                 {                                                             \
55                   HANDLE h = (HANDLE) _get_osfhandle (fd);                    \
56                   if (GetFileType (h) == FILE_TYPE_PIPE)                      \
57                     {                                                         \
58                       /* h is a pipe or socket.  */                           \
59                       DWORD state;                                            \
60                       if (GetNamedPipeHandleState (h, &state, NULL, NULL,     \
61                                                    NULL, NULL, 0)             \
62                           && (state & PIPE_NOWAIT) != 0)                      \
63                         /* h is a pipe in non-blocking mode.                  \
64                            Change errno from EINVAL to EAGAIN.  */            \
65                         errno = EAGAIN;                                       \
66                     }                                                         \
67                 }                                                             \
68             }                                                                 \
69         }                                                                     \
70       return ret;                                                             \
71     }
72
73 int
74 scanf (const char *format, ...)
75 {
76   int retval;
77   va_list args;
78
79   va_start (args, format);
80   retval = vfscanf (stdin, format, args);
81   va_end (args);
82
83   return retval;
84 }
85
86 int
87 fscanf (FILE *stream, const char *format, ...)
88 {
89   int retval;
90   va_list args;
91
92   va_start (args, format);
93   retval = vfscanf (stream, format, args);
94   va_end (args);
95
96   return retval;
97 }
98
99 int
100 vscanf (const char *format, va_list args)
101 {
102   return vfscanf (stdin, format, args);
103 }
104
105 int
106 vfscanf (FILE *stream, const char *format, va_list args)
107 #undef vfscanf
108 {
109   CALL_WITH_ERRNO_FIX (int, vfscanf (stream, format, args), ret == EOF)
110 }
111
112 int
113 getchar (void)
114 {
115   return fgetc (stdin);
116 }
117
118 int
119 fgetc (FILE *stream)
120 #undef fgetc
121 {
122   CALL_WITH_ERRNO_FIX (int, fgetc (stream), ret == EOF)
123 }
124
125 char *
126 fgets (char *s, int n, FILE *stream)
127 #undef fgets
128 {
129   CALL_WITH_ERRNO_FIX (char *, fgets (s, n, stream), ret == NULL)
130 }
131
132 char *
133 gets (char *s)
134 #undef gets
135 {
136   FILE *stream = stdin;
137   CALL_WITH_ERRNO_FIX (char *, gets (s), ret == NULL)
138 }
139
140 size_t
141 fread (void *ptr, size_t s, size_t n, FILE *stream)
142 #undef fread
143 {
144   CALL_WITH_ERRNO_FIX (size_t, fread (ptr, s, n, stream), ret < n)
145 }
146
147 # endif
148 #endif