1 /*
2 * Copyright (c) 2008-2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef __STDIO_H
24 #define __STDIO_H
25
26 #include <lk/compiler.h>
27 #include <printf.h>
28 #include <sys/types.h>
29 #include <lib/io.h>
30
31 __BEGIN_CDECLS
32
33 typedef struct FILE {
34 io_handle_t *io;
35 } FILE;
36
37 extern FILE __stdio_FILEs[];
38
39 #define stdin (&__stdio_FILEs[0])
40 #define stdout (&__stdio_FILEs[1])
41 #define stderr (&__stdio_FILEs[2])
42
43 FILE *fopen(const char *filename, const char *mode);
44 int fclose(FILE *stream);
45 size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
46 size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
47 int fflush(FILE *stream);
48 int feof(FILE *stream);
49
50 #define SEEK_SET 0
51 #define SEEK_CUR 1
52 #define SEEK_END 2
53
54 int fseek(FILE *stream, long offset, int whence);
55 long ftell(FILE *stream);
56
57 int fputc(int c, FILE *fp);
58 #define putc(c, fp) fputc(c, fp)
59 int putchar(int c);
60
61 int fputs(const char *s, FILE *fp);
62 int puts(const char *str);
63
64 int getc(FILE *fp);
65 int getchar(void);
66
67 int printf(const char *fmt, ...) __PRINTFLIKE(1, 2);
68 int vprintf(const char *fmt, va_list ap);
69
70 int fprintf(FILE *fp, const char *fmt, ...) __PRINTFLIKE(2, 3);
71 int vfprintf_worker(FILE *fp, const char *fmt, va_list ap, int filtered_on_release);
vfprintf(FILE * fp,const char * fmt,va_list ap)72 static inline int vfprintf(FILE *fp, const char *fmt, va_list ap) {
73 return vfprintf_worker(fp, fmt, ap, 1);
74 }
75
76 int sprintf(char *str, const char *fmt, ...) __PRINTFLIKE(2, 3);
77 int snprintf(char *str, size_t len, const char *fmt, ...) __PRINTFLIKE(3, 4);
78 int snprintf_filtered(char *str, size_t len, const char *fmt, ...) __PRINTFLIKE(3, 4);
79 int vsprintf(char *str, const char *fmt, va_list ap);
80 int vsnprintf(char *str, size_t len, const char *fmt, va_list ap);
81 int vsnprintf_filtered(char *str, size_t len, const char *fmt, va_list ap);
82
83 // sccanf is not implemented.
84 int sscanf(const char* str, const char* format, ...);
85
86 io_handle_t* fd_io_handle(int fd);
87 io_handle_t* file_io_handle(FILE* file);
88
89 __END_CDECLS
90
91 #endif
92
93