summaryrefslogtreecommitdiff
path: root/user/include/stdio.h
blob: 2e2abf4c2d7ccea4a2d75a3ccf85558d058add6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
 * @file stdio.h
 *
 * @author Freya Murphy <freya@freyacat.org>
 *
 * Standard I/O definitions.
 */

#ifndef _STDIO_H
#define _STDIO_H

#include <stdarg.h>
#include <stddef.h>

#define EOF (-1)

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

// TODO: implement
typedef void FILE;

extern FILE *stdin;
extern FILE *stdout;
#define stdin stdin
#define stdout stdout

/**
 * Get a char from stdin
 *
 * @returns the character or EOF on failure
 */
extern int getchar(void);

/**
 * Get a chracter from a stream
 *
 * @param stream - the stream to read from
 * @returns the character or EOF on failure
 */
extern int getc(FILE *stream);

/**
 * Get a chracter from a stream
 *
 * @param stream - the stream to read from
 * @returns the character or EOF on failure
 */
extern int fgetc(FILE *stream);

/**
 * Reads a string from stdin
 *
 * @param s - the buffer to read into
 * @returns s on success, NULL on error or EOF
 */
extern char *gets(char *s);

/**
 * Reads a string from stdin
 *
 * @param s - the buffer to read into
 * @param size - reads at most 1 less then size
 * @param stream - stream to read from from
 * @returns s on success, NULL on error or EOF
 */
extern char *fgets(char *restrict str, int size, FILE *stream);

/**
 * Prints out a char
 *
 * @param c - the char
 * @returns the character written or EOF on failure
 */
extern int putchar(int c);

/**
 * Prints out a char to a stream
 *
 * @param c - the char
 * @param stream - the stream to print to
 * @returns the character written or EOF on failure
 */
extern int putc(int c, FILE *stream);

/**
 * Prints out a char to a stream
 *
 * @param c - the char
 * @param stream - stream to write to
 * @returns the character written or EOF on failure
 */
extern int fputc(int c, FILE *stream);

/**
 * Prints out a null terminated string with newline
 *
 * @param str - the string
 * @returns nonnegative integer on success, EOF on error
 */
extern int puts(const char *str);

/**
 * Prints out a null terminated string
 *
 * @param str - the string
 * @param stream - stream to write to
 * @returns nonnegative integer on success, EOF on error
 */
extern int fputs(const char *str, FILE *stream);

/**
 * prints out a formatted string
 *
 * @param format - the format string
 * @param ... - variable args for the format
 * @returns number of bytes written
 */
__attribute__((format(printf, 1, 2))) extern int printf(const char *format,
														...);

/**
 * prints out a formatted string to a buffer
 *
 * @param s - the string to write to
 * @param format - the format string
 * @param ... - variable args for the format
 * @returns number of bytes written
 */
__attribute__((format(printf, 2, 3))) extern int
sprintf(char *restrict s, const char *format, ...);

/**
 * prints out a formatted string to a buffer with a given max length
 *
 * @param s - the string to write to
 * @param maxlen - the max len of the buffer
 * @param format - the format string
 * @param ... - variable args for the format
 * @returns number of bytes that would of been written (past maxlen)
 */
__attribute__((format(printf, 3, 4))) extern int
snprintf(char *restrict s, size_t maxlen, const char *format, ...);

/**
 * prints out a formatted string
 *
 * @param format - the format string
 * @param args - variable arg list for the format
 * @returns number of bytes written
 */
extern int vprintf(const char *format, va_list args);

/**
 * prints out a formatted string to a buffer
 *
 * @param s - the string to write to
 * @param format - the format string
 * @param args - variable arg list for the format
 * @returns number of bytes written
 */
extern int vsprintf(char *restrict s, const char *format, va_list args);

/**
 * prints out a formatted string to a buffer with a given max length
 *
 * @param s - the string to write to
 * @param maxlen - the max len of the buffer
 * @param format - the format string
 * @param args - variable arg list for the format
 * @returns number of bytes that would of been written (past maxlen)
 */
extern int vsnprintf(char *restrict s, size_t maxlen, const char *format,
					 va_list args);

/**
 * prints out a formatted string
 *
 * @param stream - the opened stream to print to
 * @param format - the format string
 * @param ... - variable args for the format
 */
__attribute__((format(printf, 2, 3))) extern int
fprintf(FILE *stream, const char *format, ...);

/**
 * prints out a formatted string
 *
 * @param stream - the opened stream to print to
 * @param format - the format string
 * @param args - variable arg list for the format
 */
extern int vfprintf(FILE *stream, const char *format, va_list args);

/**
 * opens a file with a given file name
 *
 * @param filename - the name of the file to open
 * @param modes - the modes to open the file in
 * @returns the file pointer of success, NULL on error
 */
extern FILE *fopen(const char *restrict filename, const char *restrict modes);

/**
 * opens a file with a given file name, replacing en existing stream with it
 *
 * @param filename - the name of the file to open
 * @param modes - the modes to open the file in
 * @param stream - the stream to replace
 * @returns the file pointer of success, NULL on error
 */
extern FILE *freopen(const char *restrict filename, const char *restrict modes,
					 FILE *restrict stream);

/**
 * closes a opened file
 *
 * @param stream - the opened file stream
 */
extern void fclose(FILE *stream);

/**
 * reads data from a file into a pointer
 *
 * @param ptr - the buffer to write into
 * @param size - the size of the block to read
 * @param n - the count of blocks to read
 * @param stream - the file stream to read from
 * @returns the number of blocks read
 */
extern size_t fread(void *restrict ptr, size_t size, size_t n,
					FILE *restrict stream);

/**
 * writes data from a pointer into a filename
 *
 * @param ptr - the buffer to read from
 * @param size - the size of the block to write
 * @param n - the count of blocks to write
 * @param stream - the file stream to write into
 * @returns the number of blocks written
 */
extern size_t fwrite(const void *restrict ptr, size_t size, size_t n,
					 FILE *restrict stream);

/**
 * seek to a certain position on stream
 *
 * @param stream - the stream to seek
 * @param off - the offset from whence
 * @param whence - where to seek from (SEEK_SET, SEEK_CUR, SEEK_END)
 * @returns 0 on success, -1 on error setting errno
 */
extern int fseek(FILE *stream, long int off, int whence);

/**
 * return the current position of stream
 *
 * @param stream - the stream to tell
 * @return the position on success, -1 on error setting errno
 */
extern long int ftell(FILE *stream);

/**
 * rewing to the begining of a stream
 *
 * @param stream - the stream to rewind
 */
extern void rewind(FILE *stream);

#endif /* stdio.h */