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