e2fdff2355dcd593820c51daaedb65ffeea2780d
[reactos.git] / include / msvcrt / stdio.h
1 /*
2  * stdio.h
3  *
4  * Definitions of types and prototypes of functions for standard input and
5  * output.
6  *
7  * NOTE: The file manipulation functions provided by Microsoft seem to
8  * work with either slash (/) or backslash (\) as the path separator.
9  *
10  * This file is part of the Mingw32 package.
11  *
12  * Contributors:
13  *  Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
14  *
15  *  THIS SOFTWARE IS NOT COPYRIGHTED
16  *
17  *  This source code is offered for use in the public domain. You may
18  *  use, modify or distribute it freely.
19  *
20  *  This code is distributed in the hope that it will be useful but
21  *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
22  *  DISCLAIMED. This includes but is not limited to warranties of
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24  *
25  * $Revision$
26  * $Author$
27  * $Date$
28  *
29  */
30 /* Appropriated for Reactos Crtdll by Ariadne */
31 /* implemented clearerr feof ferror perror as macros */
32 /* added _IOCOMMIT */
33 /* added filbuf and flsbuf and fwalk   */
34
35 #ifndef _STDIO_H_
36 #define _STDIO_H_
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #define __need_size_t
43 #define __need_NULL
44 #define __need_wchar_t
45 #define __need_wint_t
46 #include <msvcrt/stddef.h>
47
48
49 /* Some flags for the iobuf structure provided by djgpp stdio.h */
50 #define _IOREAD   0x000010
51 #define _IOWRT    0x000020
52 #define _IOMYBUF  0x000040
53 #define _IOEOF    0x000100
54 #define _IOERR    0x000200
55 #define _IOSTRG   0x000400
56
57 #define _IOBINARY 0x000800
58 #define _IOTEXT   0x000000
59
60 #define _IOAPPEND 0x002000
61 #define _IORMONCL 0x004000  /* remove on close, for temp files */
62 /* if _flag & _IORMONCL, ._name_to_remove needs freeing */
63 #define _IOUNGETC 0x010000  /* there is an ungetc'ed character in the buffer */
64 #define _IOCOMMIT 0x008000
65
66 #define _IODIRTY  0x000080
67 #define _IOAHEAD  0x000008
68 #define _IORW (_IOREAD | _IOWRITE )
69
70
71 /*
72  * I used to include stdarg.h at this point, in order to allow for the
73  * functions later on in the file which use va_list. That conflicts with
74  * using stdio.h and varargs.h in the same file, so I do the typedef myself.
75  */
76 //#ifndef _VA_LIST
77 //#define _VA_LIST
78 //typedef   char* va_list;
79 //#endif
80 #include <msvcrt/stdarg.h>
81
82 /*
83  * FILE should be used as a pointer to an opaque data type. Do not rely on
84  * anything else, especially the size or contents of this structure!
85  */
86 #ifndef _FILE_DEFINED
87 typedef struct {
88     char* _ptr;
89     int   _cnt;
90     char* _base;
91     int   _flag;
92     int   _file;
93     int   _ungotchar;
94     int   _bufsiz;
95     char* _name_to_remove;
96 } FILE;
97 #define _FILE_DEFINED
98 #endif
99
100 //#define _fillsize _bufsiz
101
102 /*
103  * The three standard file pointers provided by the run time library.
104  * NOTE: These will go to the bit-bucket silently in GUI applications!
105  */
106 extern FILE _iob[]; /* an array of FILE */
107 #define stdin   (&_iob[0])
108 #define stdout  (&_iob[1])
109 #define stderr  (&_iob[2])
110 #define stdaux  (&_iob[3])
111 #define stdprn  (&_iob[4])
112
113 /* Returned by various functions on end of file condition or error. */
114 #define EOF (-1)
115
116 /*
117  * The maximum length of a file name. You should use GetVolumeInformation
118  * instead of this constant. But hey, this works.
119  *
120  * NOTE: This is used in the structure _finddata_t (see dir.h) so changing it
121  *       is probably not a good idea.
122  */
123 #define FILENAME_MAX    (260)
124
125 /*
126  * The maximum number of files that may be open at once. I have set this to
127  * a conservative number. The actual value may be higher.
128  */
129 #define FOPEN_MAX   (20)
130
131 /*
132  * File Operations
133  */
134 FILE* fopen(const char* szFileName, const char* szMode);
135 FILE* freopen(const char* szNewFileName, const char* szNewMode, FILE* fileChangeAssociation);
136 int fflush(FILE* fileFlush);
137 int fclose(FILE* fileClose);
138 #define fcloseall _fcloseall
139 int remove(const char* szFileName);
140 int rename(const char* szOldFileName, const char* szNewFileName);
141 FILE* tmpfile(void);
142
143 FILE* _fdopen(int handle, char *mode);
144 FILE* _wfdopen(int handle, wchar_t *mode);
145 FILE* _wfopen(const wchar_t *file, const wchar_t *mode);
146 FILE* _wfreopen(const wchar_t *file, const wchar_t *mode, FILE *f);
147 FILE* _fsopen(const char *file, const char *mode, int shflag);
148 FILE* _wfsopen(const wchar_t *file, const wchar_t *mode, int shflag);
149 int _wremove(const wchar_t* szFileName);
150 int _wrename(const wchar_t *oldName, const wchar_t *newName);
151
152 int _filbuf(FILE* f);
153 int _flsbuf(int c, FILE* f);
154 void _fwalk(void (*func)(FILE*)); // not exported
155 int _fcloseall(void);
156
157 /*
158  * The maximum size of name (including NUL) that will be put in the user
159  * supplied buffer caName.
160  * NOTE: This has not been determined by experiment, but based on the
161  * maximum file name length above it is probably reasonable. I could be
162  * wrong...
163  */
164 #define L_tmpnam    (260)
165
166 char* tmpnam(char caName[]);
167 char* _tempnam(const char *szDir, const char *szPfx);
168
169 wchar_t* _wtmpnam(wchar_t *s);
170 wchar_t *_wtempnam(const wchar_t *dir,const wchar_t *prefix);
171
172 #ifndef _NO_OLDNAMES
173 #define tempnam _tempnam
174 #endif  /* Not _NO_OLDNAMES */
175
176 /*
177  * The three possible buffering mode (nMode) values for setvbuf.
178  * NOTE: _IOFBF works, but _IOLBF seems to work like unbuffered...
179  * maybe I'm testing it wrong?
180  */
181 #define _IOFBF  0   /* fully buffered */
182 #define _IOLBF  1   /* line buffered */
183 #define _IONBF  2   /* unbuffered */
184
185 int setvbuf(FILE* fileSetBuffer, char* caBuffer, int nMode, size_t sizeBuffer);
186
187 /*
188  * The buffer size as used by setbuf such that it is equivalent to
189  * (void) setvbuf(fileSetBuffer, caBuffer, _IOFBF, BUFSIZ).
190  */
191 #define BUFSIZ  512
192
193 void setbuf(FILE* fileSetBuffer, char* caBuffer);
194
195 /*
196  * Pipe Operations
197  */
198   
199 int _pclose(FILE* pipeClose);
200 FILE* _popen(const char* szPipeName, const char* szMode);
201 FILE* _wpopen(const wchar_t *cm, const wchar_t *md);
202
203 #define popen  _popen
204 #define pclose _pclose
205
206 /* Wide character version */
207 FILE*   _wpopen(const wchar_t* szPipeName, const wchar_t* szMode);
208
209 /*
210  * Formatted Output
211  */
212
213 int fprintf(FILE* filePrintTo, const char* szFormat, ...);
214 int printf(const char* szFormat, ...);
215 int sprintf(char* caBuffer, const char* szFormat, ...);
216 int _snprintf(char*, size_t, const char*, ...);
217 int vfprintf(FILE* filePrintTo, const char* szFormat, va_list varg);
218 int vprintf(const char* szFormat, va_list varg);
219 int vsprintf(char* caBuffer, const char* szFormat, va_list varg);
220 int _vsnprintf(char* caBuffer, size_t cnt, const char* szFormat, va_list varg);
221 int _vsnwprintf (wchar_t*, size_t, const wchar_t*, va_list);
222
223 /* Wide character versions */
224 int fwprintf(FILE* filePrintTo, const wchar_t* wsFormat, ...);
225 int wprintf(const wchar_t* wsFormat, ...);
226 int swprintf(wchar_t* wcaBuffer, const wchar_t* wsFormat, ...);
227 int vfwprintf(FILE* filePrintTo, const wchar_t* wsFormat, va_list varg);
228 int vwprintf(const wchar_t* wsFormat, va_list varg);
229 int vswprintf(wchar_t* wcaBuffer, const wchar_t* wsFormat, va_list varg);
230
231 /*
232  * Formatted Input
233  */
234
235 int fscanf(FILE* fileReadFrom, const char* szFormat, ...);
236 int scanf(const char* szFormat, ...);
237 int sscanf(const char* szReadFrom, const char* szFormat, ...);
238
239 /* Wide character versions */
240 int fwscanf(FILE* fileReadFrom, const wchar_t* wsFormat, ...);
241 int wscanf(const wchar_t* wsFormat, ...);
242 int swscanf(const wchar_t* wsReadFrom, const wchar_t* wsFormat, ...);
243
244 /*
245  * Character Input and Output Functions
246  */
247
248 int fgetc(FILE* fileRead);
249 char* fgets(char* caBuffer, int nBufferSize, FILE* fileRead);
250 int fputc(int c, FILE* fileWrite);
251 int fputs(const char* szOutput, FILE* fileWrite);
252 int getc(FILE* fileRead);
253 int getchar(void);
254 char* gets(char* caBuffer);  /* Unsafe: how does gets know how long the buffer is? */
255 int putc(int c, FILE* fileWrite);
256 int putchar(int c);
257 int puts(const char* szOutput);
258 int ungetc(int c, FILE* fileWasRead);
259
260 /* Wide character versions */
261 wint_t fgetwc(FILE* fileRead);
262 wint_t fputwc(wchar_t wc, FILE* fileWrite);
263 wint_t getwc(FILE *fileRead); // not exported from crtdll
264
265 // TODO: check type wint_t, why doesn't compare to WEOF correctly ???
266 //wint_t putwc(wint_t wc, FILE* fileWrite);
267 int putwc(wint_t wc, FILE* fileWrite);
268
269 wint_t putwchar(wint_t c);
270 int _putws(const wchar_t* ws);
271
272 wint_t ungetwc(wchar_t wc, FILE* fileWasRead);
273
274 wint_t _filwbuf(FILE *f);
275 wint_t _flswbuf(wchar_t c, FILE *f);
276
277 /*
278  * Not exported by CRTDLL.DLL included for reference purposes.
279  */
280 #if 0
281 wchar_t* fgetws(wchar_t* wcaBuffer, int nBufferSize, FILE* fileRead);
282 int fputws(const wchar_t* wsOutput, FILE* fileWrite);
283 int getwc(FILE* fileRead);
284 int getwchar();
285 wchar_t* getws(wchar_t* wcaBuffer);
286 #endif  /* 0 */
287
288 /* NOTE: putchar has no wide char equivalent even in tchar.h */
289
290 /*
291  * Direct Input and Output Functions
292  */
293
294 size_t fread(void* pBuffer, size_t sizeObject, size_t sizeObjCount, FILE* fileRead);
295 size_t fwrite(const void* pObjArray, size_t sizeObject, size_t sizeObjCount, FILE* fileWrite);
296
297 /*
298  * File Positioning Functions
299  */
300
301 /* Constants for nOrigin indicating the position relative to which fseek
302  * sets the file position. Enclosed in ifdefs because io.h could also
303  * define them. (Though not anymore since io.h includes this file now.) */
304 #ifndef SEEK_SET
305 #define SEEK_SET    (0)
306 #endif
307
308 #ifndef SEEK_CUR
309 #define SEEK_CUR    (1)
310 #endif
311
312 #ifndef SEEK_END
313 #define SEEK_END    (2)
314 #endif
315
316 int fseek(FILE* fileSetPosition, long lnOffset, int nOrigin);
317 long ftell(FILE* fileGetPosition);
318 void rewind(FILE* fileRewind);
319
320 /*
321  * An opaque data type used for storing file positions... The contents of
322  * this type are unknown, but we (the compiler) need to know the size
323  * because the programmer using fgetpos and fsetpos will be setting aside
324  * storage for fpos_t structres. Actually I tested using a byte array and
325  * it is fairly evident that the fpos_t type is a long (in CRTDLL.DLL).
326  * Perhaps an unsigned long? TODO?
327  */
328 typedef long fpos_t;
329
330 int fgetpos(FILE* fileGetPosition, fpos_t* pfpos);
331 int fsetpos(FILE* fileSetPosition, const fpos_t* pfpos);
332
333 /*
334  * Error Functions
335  */
336 #if 0
337 void clearerr(FILE* fileClearErrors);
338 int feof(FILE* fileIsAtEnd);
339 int ferror(FILE* fileIsError);
340 void perror(const char* szErrorMessage);
341 #endif
342
343 void _wperror(const wchar_t *s);
344
345 #define clearerr(f)  (((f)->_flag) &= ~(_IOERR|_IOEOF))
346 #define feof(f)      (((f)->_flag&_IOEOF)!=0)
347 //#define ferror(f)    (((f)->_flag&_IOERR)!=0)
348 int ferror(FILE* fileIsError);
349 #define perror(s)    (fprintf(stderr, "%s: %s\n", (s), _strerror(NULL)))
350
351 /*
352  * Non ANSI functions
353  */
354
355 #ifndef __STRICT_ANSI__
356 int _fgetchar(void);
357 int _fputchar(int c);
358 FILE* _fdopen(int nHandle, char* szMode);
359
360 #ifndef _NO_OLDNAMES
361 #define fgetchar    _fgetchar
362 #define fputchar    _fputchar
363 #define fdopen      _fdopen
364 #endif  /* Not _NO_OLDNAMES */
365
366 #endif  /* Not __STRICT_ANSI__ */
367
368 #ifdef __cplusplus
369 }
370 #endif
371
372 #endif /* _STDIO_H_ */