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