1 /*
2 * Copyright (c) 2008-2015 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #pragma once
9
10 #include <lk/compiler.h>
11 #include <printf.h>
12 #include <sys/types.h>
13 #include <lib/io.h>
14 #if defined(WITH_LIB_FS)
15 #include <lib/fs.h>
16 #endif // WITH_LIB_FS
17
18 __BEGIN_CDECLS
19
20 #if defined(WITH_LIB_FS)
21 struct fs_handle {
22 filehandle *handle;
23 off_t offset;
24 bool readonly;
25 };
26 #endif // WITH_LIB_FS
27 typedef struct FILE {
28 #if defined(WITH_LIB_FS)
29 union {
30 io_handle_t *io;
31 struct fs_handle fs_handle;
32 };
33 bool use_fs;
34 #else
35 io_handle_t *io;
36 #endif // WITH_LIB_FS
37 } FILE;
38
39 extern FILE __stdio_FILEs[];
40
41 #define stdin (&__stdio_FILEs[0])
42 #define stdout (&__stdio_FILEs[1])
43 #define stderr (&__stdio_FILEs[2])
44
45 #define EOF (-1)
46
47 FILE *fopen(const char *filename, const char *mode);
48 int fclose(FILE *stream);
49 size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
50 size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
51 int fflush(FILE *stream);
52 int feof(FILE *stream);
53
54 #define SEEK_SET 0
55 #define SEEK_CUR 1
56 #define SEEK_END 2
57
58 int fseek(FILE *stream, long offset, int whence);
59 long ftell(FILE *stream);
60
61 int fputc(int c, FILE *fp);
62 #define putc(c, fp) fputc(c, fp)
63 int putchar(int c);
64
65 int fputs(const char *s, FILE *fp);
66 int puts(const char *str);
67
68 int fgetc(FILE *fp);
69 #define getc(fp) fgetc(fp)
70 int getchar(void);
71
72 char *fgets(char *s, int size, FILE *stream);
73
74 #if !DISABLE_DEBUG_OUTPUT
75 int printf(const char *fmt, ...) __PRINTFLIKE(1, 2);
76 int vprintf(const char *fmt, va_list ap);
77 #else
printf(const char * fmt,...)78 static inline int __PRINTFLIKE(1, 2) printf(const char *fmt, ...) { return 0; }
vprintf(const char * fmt,va_list ap)79 static inline int vprintf(const char *fmt, va_list ap) { return 0; }
80 #endif
81
82 int fprintf(FILE *fp, const char *fmt, ...) __PRINTFLIKE(2, 3);
83 int vfprintf(FILE *fp, const char *fmt, va_list ap);
84 int _fprintf_output_func(const char *str, size_t len, void *state);
85
86 int sprintf(char *str, const char *fmt, ...) __PRINTFLIKE(2, 3);
87 int snprintf(char *str, size_t len, const char *fmt, ...) __PRINTFLIKE(3, 4);
88 int vsprintf(char *str, const char *fmt, va_list ap);
89 int vsnprintf(char *str, size_t len, const char *fmt, va_list ap);
90
91
92 __END_CDECLS
93
94