update for HEAD-2003091401
[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 /*
77  *#ifndef _VA_LIST
78  *#define _VA_LIST
79  *typedef   char* va_list;
80  *#endif
81  */
82 #include <msvcrt/stdarg.h>
83
84 /*
85  * FILE should be used as a pointer to an opaque data type. Do not rely on
86  * anything else, especially the size or contents of this structure!
87  */
88 #ifndef _FILE_DEFINED
89 typedef struct {
90     char* _ptr;
91     int   _cnt;
92     char* _base;
93     int   _flag;
94     int   _file;
95     int   _ungotchar;
96     int   _bufsiz;
97     char* _name_to_remove;
98 } FILE;
99 #define _FILE_DEFINED
100 #endif
101
102 //#define _fillsize _bufsiz
103
104 /*
105  * The three standard file pointers provided by the run time library.
106  * NOTE: These will go to the bit-bucket silently in GUI applications!
107  */
108 extern FILE _iob[]; /* an array of FILE */
109 #define stdin   (&_iob[0])
110 #define stdout  (&_iob[1])
111 #define stderr  (&_iob[2])
112 #define stdaux  (&_iob[3])
113 #define stdprn  (&_iob[4])
114
115 /* Returned by various functions on end of file condition or error. */
116 #define EOF (-1)
117
118 /*
119  * The maximum length of a file name. You should use GetVolumeInformation
120  * instead of this constant. But hey, this works.
121  *
122  * NOTE: This is used in the structure _finddata_t (see dir.h) so changing it
123  *       is probably not a good idea.
124  */
125 #define FILENAME_MAX    (260)
126
127 /*
128  * The maximum number of files that may be open at once. I have set this to
129  * a conservative number. The actual value may be higher.
130  */
131 #define FOPEN_MAX   (20)
132
133 /*
134  * File Operations
135  */
136 FILE* fopen(const char* szFileName, const char* szMode);
137 FILE* freopen(const char* szNewFileName, const char* szNewMode, FILE* fileChangeAssociation);
138 int fflush(FILE* fileFlush);
139 int fclose(FILE* fileClose);
140 #define fcloseall _fcloseall
141 int remove(const char* szFileName);
142 int rename(const char* szOldFileName, const char* szNewFileName);
143 FILE* tmpfile(void);
144
145 FILE* _fdopen(int handle, char *mode);
146 FILE* _wfdopen(int handle, wchar_t *mode);
147 FILE* _wfopen(const wchar_t *file, const wchar_t *mode);
148 FILE* _wfreopen(const wchar_t *file, const wchar_t *mode, FILE *f);
149 FILE* _fsopen(const char *file, const char *mode, int shflag);
150 FILE* _wfsopen(const wchar_t *file, const wchar_t *mode, int shflag);
151 int _wremove(const wchar_t* szFileName);
152 int _wrename(const wchar_t *oldName, const wchar_t *newName);
153
154 int _filbuf(FILE* f);
155 int _flsbuf(int c, FILE* f);
156 void _fwalk(void (*func)(FILE*)); // not exported
157 int _fcloseall(void);
158
159 /*
160  * The maximum size of name (including NUL) that will be put in the user
161  * supplied buffer caName.
162  * NOTE: This has not been determined by experiment, but based on the
163  * maximum file name length above it is probably reasonable. I could be
164  * wrong...
165  */
166 #define L_tmpnam    (260)
167
168 char* tmpnam(char caName[]);
169 char* _tempnam(const char *szDir, const char *szPfx);
170
171 wchar_t* _wtmpnam(wchar_t *s);
172 wchar_t *_wtempnam(const wchar_t *dir,const wchar_t *prefix);
173
174 #ifndef _NO_OLDNAMES
175 #define tempnam _tempnam
176 #endif  /* Not _NO_OLDNAMES */
177
178 /*
179  * The three possible buffering mode (nMode) values for setvbuf.
180  * NOTE: _IOFBF works, but _IOLBF seems to work like unbuffered...
181  * maybe I'm testing it wrong?
182  */
183 #define _IOFBF  0   /* fully buffered */
184 #define _IOLBF  1   /* line buffered */
185 #define _IONBF  2   /* unbuffered */
186
187 int setvbuf(FILE* fileSetBuffer, char* caBuffer, int nMode, size_t sizeBuffer);
188
189 /*
190  * The buffer size as used by setbuf such that it is equivalent to
191  * (void) setvbuf(fileSetBuffer, caBuffer, _IOFBF, BUFSIZ).
192  */
193 #define BUFSIZ  512
194
195 void setbuf(FILE* fileSetBuffer, char* caBuffer);
196
197 /*
198  * Pipe Operations
199  */
200   
201 int _pclose(FILE* pipeClose);
202 FILE* _popen(const char* szPipeName, const char* szMode);
203 FILE* _wpopen(const wchar_t *cm, const wchar_t *md);
204
205 #define popen  _popen
206 #define pclose _pclose
207
208 /* Wide character version */
209 FILE*   _wpopen(const wchar_t* szPipeName, const wchar_t* szMode);
210
211 /*
212  * Formatted Output
213  */
214
215 int fprintf(FILE* filePrintTo, const char* szFormat, ...);
216 int printf(const char* szFormat, ...);
217 int sprintf(char* caBuffer, const char* szFormat, ...);
218 int _snprintf(char*, size_t, const char*, ...);
219 int vfprintf(FILE* filePrintTo, const char* szFormat, va_list varg);
220 int vprintf(const char* szFormat, va_list varg);
221 int vsprintf(char* caBuffer, const char* szFormat, va_list varg);
222 int _vsnprintf(char* caBuffer, size_t cnt, const char* szFormat, va_list varg);
223 int _vsnwprintf (wchar_t*, size_t, const wchar_t*, va_list);
224
225 /* Wide character versions */
226 int fwprintf(FILE* filePrintTo, const wchar_t* wsFormat, ...);
227 int wprintf(const wchar_t* wsFormat, ...);
228 int swprintf(wchar_t* wcaBuffer, const wchar_t* wsFormat, ...);
229 int vfwprintf(FILE* filePrintTo, const wchar_t* wsFormat, va_list varg);
230 int vwprintf(const wchar_t* wsFormat, va_list varg);
231 int vswprintf(wchar_t* wcaBuffer, const wchar_t* wsFormat, va_list varg);
232
233 /*
234  * Formatted Input
235  */
236
237 int fscanf(FILE* fileReadFrom, const char* szFormat, ...);
238 int scanf(const char* szFormat, ...);
239 int sscanf(const char* szReadFrom, const char* szFormat, ...);
240
241 /* Wide character versions */
242 int fwscanf(FILE* fileReadFrom, const wchar_t* wsFormat, ...);
243 int wscanf(const wchar_t* wsFormat, ...);
244 int swscanf(const wchar_t* wsReadFrom, const wchar_t* wsFormat, ...);
245
246 /*
247  * Character Input and Output Functions
248  */
249
250 int fgetc(FILE* fileRead);
251 char* fgets(char* caBuffer, int nBufferSize, FILE* fileRead);
252 int fputc(int c, FILE* fileWrite);
253 int fputs(const char* szOutput, FILE* fileWrite);
254 int getc(FILE* fileRead);
255 int getchar(void);
256 char* gets(char* caBuffer);  /* Unsafe: how does gets know how long the buffer is? */
257 int putc(int c, FILE* fileWrite);
258 int putchar(int c);
259 int puts(const char* szOutput);
260 int ungetc(int c, FILE* fileWasRead);
261
262 /* Wide character versions */
263 wint_t fgetwc(FILE* fileRead);
264 wint_t fputwc(wchar_t wc, FILE* fileWrite);
265 wint_t getwc(FILE *fileRead); // not exported from crtdll
266
267 /* TODO: check type wint_t, why doesn't compare to WEOF correctly ??? */
268 /* wint_t putwc(wint_t wc, FILE* fileWrite); */
269 int putwc(wint_t wc, FILE* fileWrite);
270
271 wint_t putwchar(wint_t c);
272 int _putws(const wchar_t* ws);
273
274 wint_t ungetwc(wchar_t wc, FILE* fileWasRead);
275
276 wint_t _filwbuf(FILE *f);
277 wint_t _flswbuf(wchar_t c, FILE *f);
278
279 /*
280  * Not exported by CRTDLL.DLL included for reference purposes.
281  */
282 #if 0
283 wchar_t* fgetws(wchar_t* wcaBuffer, int nBufferSize, FILE* fileRead);
284 int fputws(const wchar_t* wsOutput, FILE* fileWrite);
285 int getwc(FILE* fileRead);
286 int getwchar();
287 wchar_t* getws(wchar_t* wcaBuffer);
288 #endif  /* 0 */
289
290 /* NOTE: putchar has no wide char equivalent even in tchar.h */
291
292 /*
293  * Direct Input and Output Functions
294  */
295
296 size_t fread(void* pBuffer, size_t sizeObject, size_t sizeObjCount, FILE* fileRead);
297 size_t fwrite(const void* pObjArray, size_t sizeObject, size_t sizeObjCount, FILE* fileWrite);
298
299 /*
300  * File Positioning Functions
301  */
302
303 /* Constants for nOrigin indicating the position relative to which fseek
304  * sets the file position. Enclosed in ifdefs because io.h could also
305  * define them. (Though not anymore since io.h includes this file now.) */
306 #ifndef SEEK_SET
307 #define SEEK_SET    (0)
308 #endif
309
310 #ifndef SEEK_CUR
311 #define SEEK_CUR    (1)
312 #endif
313
314 #ifndef SEEK_END
315 #define SEEK_END    (2)
316 #endif
317
318 int fseek(FILE* fileSetPosition, long lnOffset, int nOrigin);
319 long ftell(FILE* fileGetPosition);
320 void rewind(FILE* fileRewind);
321
322 /*
323  * An opaque data type used for storing file positions... The contents of
324  * this type are unknown, but we (the compiler) need to know the size
325  * because the programmer using fgetpos and fsetpos will be setting aside
326  * storage for fpos_t structres. Actually I tested using a byte array and
327  * it is fairly evident that the fpos_t type is a long (in CRTDLL.DLL).
328  * Perhaps an unsigned long? TODO?
329  */
330 typedef long fpos_t;
331
332 int fgetpos(FILE* fileGetPosition, fpos_t* pfpos);
333 int fsetpos(FILE* fileSetPosition, const fpos_t* pfpos);
334
335 /*
336  * Error Functions
337  */
338 #if 0
339 void clearerr(FILE* fileClearErrors);
340 int feof(FILE* fileIsAtEnd);
341 int ferror(FILE* fileIsError);
342 void perror(const char* szErrorMessage);
343 #endif
344
345 void _wperror(const wchar_t *s);
346
347 #define clearerr(f)  (((f)->_flag) &= ~(_IOERR|_IOEOF))
348 #define feof(f)      (((f)->_flag&_IOEOF)!=0)
349 /* #define ferror(f)    (((f)->_flag&_IOERR)!=0) */
350 int ferror(FILE* fileIsError);
351 #define perror(s)    (fprintf(stderr, "%s: %s\n", (s), _strerror(NULL)))
352
353 /*
354  * Non ANSI functions
355  */
356
357 #ifndef __STRICT_ANSI__
358 int _fgetchar(void);
359 int _fputchar(int c);
360 FILE* _fdopen(int nHandle, char* szMode);
361
362 #ifndef _NO_OLDNAMES
363 #define fgetchar    _fgetchar
364 #define fputchar    _fputchar
365 #define fdopen      _fdopen
366 #endif  /* Not _NO_OLDNAMES */
367
368 #endif  /* Not __STRICT_ANSI__ */
369
370 #ifdef __cplusplus
371 }
372 #endif
373
374 #endif /* _STDIO_H_ */