The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
stdio.h
Go to the documentation of this file.
1#pragma once
2
3#include <sys/types.h>
4
5/**
6 * @file stdio.h
7 * @brief buffered streams and formatted input/output.
8 *
9 * a FILE buffers the bytes of one file descriptor. a stream on a terminal
10 * is written out at the end of every stdio call (so printf and write
11 * calls stay in order); other streams are written out when the buffer is
12 * full, by fflush, by fclose, and by exit (not by _exit).
13 */
14
15#ifndef NULL
16#define NULL ((void *)0)
17#endif
18
19#include <stdarg.h>
20
21typedef struct stdio_file FILE;
22
23extern FILE *stdin;
24extern FILE *stdout;
25extern FILE *stderr;
26
27#define EOF (-1)
28#define BUFSIZ 1024
29#define FILENAME_MAX 256 // the longest path (PATH_MAX)
30
31// setvbuf modes
32#define _IOFBF 0 // full buffering
33#define _IOLBF 1 // line buffering
34#define _IONBF 2 // no buffering
35
36// fseek whence
37#ifndef SEEK_SET
38#define SEEK_SET 0
39#define SEEK_CUR 1
40#define SEEK_END 2
41#endif
42
43// opening and closing
44FILE *fopen(const char *path, const char *mode);
45FILE *fdopen(int fd, const char *mode);
46int fclose(FILE *stream);
47int fflush(FILE *stream); // NULL flushes every stream
48
49/**
50 * give a stream another file: the old file is closed, the stream keeps
51 * its fd number (so freopen on stdin or stdout changes fd 0 or 1).
52 * @param path the new file; NULL keeps the file and changes only the mode.
53 * @return stream, or NULL (errno set; the stream is closed).
54 */
55FILE *freopen(const char *path, const char *mode, FILE *stream);
56
57/**
58 * a new file for reading and writing that goes when it is closed (it is
59 * made in /tmp and removed at once).
60 */
62
63/**
64 * run "sh -c command" with a pipe to its standard output ("r") or from
65 * its standard input ("w").
66 * @return the stream of the pipe, or NULL (errno set).
67 */
68FILE *popen(const char *command, const char *mode);
69
70/**
71 * close a stream of popen and wait for the command.
72 * @return its status as waitpid gives it, or -1.
73 */
74int pclose(FILE *stream);
75int setvbuf(FILE *stream, char *buf, int mode, size_t size);
76void setbuf(FILE *stream, char *buf);
77int fileno(FILE *stream);
78
79// bytes and lines
80size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
81size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
82int fgetc(FILE *stream);
83int getc(FILE *stream);
84int getchar(void);
85int ungetc(int c, FILE *stream);
86char *fgets(char *buf, int size, FILE *stream);
87int fputc(int c, FILE *stream);
88int putc(int c, FILE *stream);
89int putchar(int c);
90int fputs(const char *s, FILE *stream);
91int puts(const char *s);
92
93// a position in a stream (fgetpos, fsetpos)
94typedef off_t fpos_t;
95
96// position and state. fseek and ftell use long, which is 32 bits on i686:
97// past 2 GiB they fail with EOVERFLOW; fseeko and ftello do not.
98int fseek(FILE *stream, long offset, int whence);
99long ftell(FILE *stream);
100int fseeko(FILE *stream, off_t offset, int whence);
102int fgetpos(FILE *stream, fpos_t *pos);
103int fsetpos(FILE *stream, const fpos_t *pos);
104void rewind(FILE *stream);
105int feof(FILE *stream);
106int ferror(FILE *stream);
107void clearerr(FILE *stream);
108
109/**
110 * formatted output. conversions: d i u o x X c s p n f F e E g G %;
111 * flags - + space # 0; a width and a precision (also as *); lengths hh h
112 * l ll z j t L.
113 */
114int printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
115int fprintf(FILE *stream, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
116int dprintf(int fd, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
117int sprintf(char *buf, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
118int snprintf(char *buf, size_t size, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
119int vprintf(const char *fmt, va_list ap);
120int vfprintf(FILE *stream, const char *fmt, va_list ap);
121int vdprintf(int fd, const char *fmt, va_list ap);
122int vsprintf(char *buf, const char *fmt, va_list ap);
123int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap);
124
125/**
126 * formatted input from a string: %d %i %u %x %o %c %s %[...] %n and %%,
127 * with widths and the lengths hh h l ll z.
128 * @return the number of fields stored, or EOF at the end of the input.
129 */
130int sscanf(const char *str, const char *fmt, ...);
131
132/**
133 * rename a file or directory (on the same filesystem).
134 * @return 0, or -1 (errno set; EXDEV across filesystems).
135 */
136int rename(const char *old_path, const char *new_path);
137int renameat(int old_dirfd, const char *old_path, int new_dirfd, const char *new_path);
138
139/**
140 * remove a file or an empty directory.
141 */
142int remove(const char *path);
143
144/**
145 * write "s: " (when s is not NULL or empty) and the text of errno to
146 * stderr (in errno.c; errno.h declares it too).
147 */
148void perror(const char *s);
149
150// the lock of a stream. once the process has threads, each stdio call
151// on a stream takes it; a thread may take it again (it counts). the
152// _unlocked calls are the plain ones.
153void flockfile(FILE *stream);
154int ftrylockfile(FILE *stream); // 0: locked
155void funlockfile(FILE *stream);
156int getc_unlocked(FILE *stream);
158int putc_unlocked(int c, FILE *stream);
160
161/**
162 * read a line, with its newline, into *line (made larger with realloc
163 * as it needs; *line may be NULL and *size 0 at first).
164 * @return the bytes read, or -1 at the end of the file or for an error.
165 */
166ssize_t getline(char **line, size_t *size, FILE *stream);
167/** as getline, up to and with delimiter instead of a newline */
168ssize_t getdelim(char **line, size_t *size, int delimiter, FILE *stream);
169
170/**
171 * a stream on size bytes of memory (or on new memory, freed by fclose,
172 * when memory is NULL). reads stop at the content's end; writes stop at
173 * size and keep a null byte after the content while there is room.
174 */
175FILE *fmemopen(void *memory, size_t size, const char *mode);
void perror(const char *s)
print a message followed by the string for the current errno.
variable argument lists (the compiler's builtins).
__builtin_va_list va_list
variadic arguments, from the compiler's builtins.
Definition stdarg.h:8
struct stdio_file FILE
Definition stdio.h:21
int putchar_unlocked(int c)
int int fprintf(FILE *stream, const char *fmt,...) __attribute__((format(printf
int vfprintf(FILE *stream, const char *fmt, va_list ap)
int getc_unlocked(FILE *stream)
int int int int int int vprintf(const char *fmt, va_list ap)
FILE * fmemopen(void *memory, size_t size, const char *mode)
a stream on size bytes of memory (or on new memory, freed by fclose, when memory is NULL).
int ftrylockfile(FILE *stream)
FILE * tmpfile(void)
a new file for reading and writing that goes when it is closed (it is made in /tmp and removed at onc...
FILE * fopen(const char *path, const char *mode)
int vsprintf(char *buf, const char *fmt, va_list ap)
void flockfile(FILE *stream)
int remove(const char *path)
remove a file or an empty directory.
int putc_unlocked(int c, FILE *stream)
void setbuf(FILE *stream, char *buf)
int putc(int c, FILE *stream)
int getchar(void)
int ferror(FILE *stream)
int ungetc(int c, FILE *stream)
int int int int int snprintf(char *buf, size_t size, const char *fmt,...) __attribute__((format(printf
int int int dprintf(int fd, const char *fmt,...) __attribute__((format(printf
ssize_t getline(char **line, size_t *size, FILE *stream)
read a line, with its newline, into *line (made larger with realloc as it needs; *line may be NULL an...
long ftell(FILE *stream)
int fseek(FILE *stream, long offset, int whence)
int sscanf(const char *str, const char *fmt,...)
formatted input from a string: d i u x o c s %[...] n and %%, with widths and the lengths hh h l ll z...
int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
int fputs(const char *s, FILE *stream)
FILE * popen(const char *command, const char *mode)
run "sh -c command" with a pipe to its standard output ("r") or from its standard input ("w").
FILE * freopen(const char *path, const char *mode, FILE *stream)
give a stream another file: the old file is closed, the stream keeps its fd number (so freopen on std...
int fclose(FILE *stream)
void funlockfile(FILE *stream)
int fsetpos(FILE *stream, const fpos_t *pos)
off_t ftello(FILE *stream)
ssize_t getdelim(char **line, size_t *size, int delimiter, FILE *stream)
as getline, up to and with delimiter instead of a newline
int fseeko(FILE *stream, off_t offset, int whence)
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
int feof(FILE *stream)
void rewind(FILE *stream)
off_t fpos_t
Definition stdio.h:94
int rename(const char *old_path, const char *new_path)
rename a file or directory (on the same filesystem).
int fgetc(FILE *stream)
int renameat(int old_dirfd, const char *old_path, int new_dirfd, const char *new_path)
void clearerr(FILE *stream)
int printf(const char *fmt,...) __attribute__((format(printf
formatted output.
int fputc(int c, FILE *stream)
FILE * stdout
FILE * stderr
FILE * stdin
int puts(const char *s)
Definition stdio.c:286
int getc(FILE *stream)
int fflush(FILE *stream)
char * fgets(char *buf, int size, FILE *stream)
int vdprintf(int fd, const char *fmt, va_list ap)
int int int int sprintf(char *buf, const char *fmt,...) __attribute__((format(printf
int pclose(FILE *stream)
close a stream of popen and wait for the command.
int fgetpos(FILE *stream, fpos_t *pos)
int putchar(int c)
int setvbuf(FILE *stream, char *buf, int mode, size_t size)
int fileno(FILE *stream)
FILE * fdopen(int fd, const char *mode)
size_t fread(void *ptr, size_t size, size_t count, FILE *stream)
int getchar_unlocked(void)
ELF32 file header.
Definition elf.h:51
basic system types.
__INT64_TYPE__ off_t
Definition types.h:20
__PTRDIFF_TYPE__ ssize_t
Definition types.h:15