+ exec* functions
[meinos.git] / apps / include / stdio.h
blob8164d4f6d15996fe45c49bfcf4274fb6b53488d7
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef _STDIO_H_
20 #define _STDIO_H_
22 #include <sys/types.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
28 #define BUFSIZ 2*1024
30 #define _IOLBF
32 #define SEEK_SET 0
33 #define SEEK_CUR 1
34 #define SEEK_END 2
36 #define EOF -1
38 #ifndef NULL
39 #define NULL ((void*)0)
40 #endif
42 #define L_tmpnam PATH_MAX
43 #define P_tmpdir "/tmp"
45 typedef struct {
46 int fh;
47 int error;
48 int oflag;
49 int eof;
50 void *buf;
51 void *bufcur;
52 int dobuf;
53 } FILE;
55 typedef off_t fpos_t;
57 FILE *stdin;
58 FILE *stdout;
59 FILE *stderr;
61 void clearerr(FILE *file);
62 int feof(FILE *stream);
63 int ferror(FILE *stream);
64 int fclose(FILE *stream);
65 FILE *fdopen(int fh,const char *oflag);
66 int fileno(FILE *stream);
67 FILE *fopen(const char *name,const char *oflag);
68 int fflush(FILE *stream);
69 int fgetpos(FILE *stream,fpos_t *pos);
70 int fsetpos(FILE *stream,fpos_t *pos);
71 FILE *freopen(const char *filename,const char *oflag,FILE *stream);
73 int rename(const char *old, const char *new);
75 int puts(const char *s);
76 size_t _fwrite(const void *ptr,size_t size,FILE *stream);
78 char *fgets(char *s,int n,FILE *stream);
79 size_t _fread(void *ptr,size_t size,FILE *stream);
80 int ungetc(int c,FILE *stream);
82 int printf(const char * format, ...);
83 int sprintf(char * buffer, const char * format, ...);
84 int snprintf(char * buffer, size_t size, const char * format, ...);
85 int fprintf(FILE * fp, const char * format, ...);
86 int asprintf(char ** buffer, const char * format, ...);
88 int vprintf(const char * format, va_list);
89 int vsprintf(char * buffer, const char * format, va_list);
90 int vsnprintf(char * buffer, size_t size, const char * format, va_list);
91 int vfprintf(FILE * fp, const char * format, va_list);
92 int vasprintf(char ** buffer, const char * format, va_list);
94 int sscanf(const char *str,const char *format,...);
96 int vsscanf(const char *buffer,const char *format,va_list ap);
99 static __inline__ int remove(const char *path) {
100 return unlink(path);
104 * Puts a char on stream
105 * @param c Character
106 * @param stream Stream
107 * @return Char written
109 static __inline__ int fputc(int c,FILE *stream) {
110 char chr = c;
111 return _fwrite(&chr,1,stream)==1?c:EOF;
115 * Puts a string on a stream
116 * @param s String to put on stream
117 * @param stream Stream
118 * @return How many bytes written
120 static __inline__ int fputs(const char *s,FILE *stream) {
121 return _fwrite(s,strlen(s),stream);
125 * Writes binary
126 * @param ptr Buffer to get data from
127 * @param size Size of each element
128 * @param nitems How many items to write
129 * @param stream Stream to write to
130 * @return How many elements written
132 static __inline__ size_t fwrite(const void *ptr,size_t size,size_t nelem,FILE *stream) {
133 return _fwrite(ptr,size*nelem,stream);
137 * Seeks in stream
138 * @param stream Stream
139 * @param offset Offset
140 * @param whence Whence
141 * @return Success?
143 static __inline__ int fseeko(FILE *stream,off_t offset,int whence) {
144 fflush(stream);
145 return lseek(stream->fh,offset,whence)==offset?0:-1;
149 * Seeks in stream
150 * @param stream Stream
151 * @param offset Offset
152 * @param whence Whence
153 * @return Success?
155 static __inline__ int fseek(FILE *stream,long offset,int whence) {
156 return fseeko(stream,(off_t)offset,whence);
160 * Returns file offset in stream
161 * @param stream Stream
162 * @return Offset
164 static __inline__ off_t ftello(FILE *stream) {
165 return lseek(stream->fh,0,SEEK_CUR);
169 * Returns file offset in stream
170 * @param stream Stream
171 * @return Offset
173 static __inline__ long ftell(FILE *stream) {
174 fflush(stream);
175 return (long)ftello(stream);
179 * Resets the file position idicator
180 * @param stream Stream
182 static __inline__ void rewind(FILE *stream) {
183 fseek(stream,0,SEEK_SET);
187 * Puts a character on stream
188 * @param c Character
189 * @param stream Stream
190 * @return Character
192 static __inline__ int putc(int c,FILE *stream) {
193 return fputc(c,stream);
197 * Puts a character on stdout
198 * @param c Character
199 * @param stream Stream
200 * @return Character
202 static __inline__ int putchar(int c) {
203 return fputc(c,stdout);
207 * Gets char from stream
208 * @param stream Stream
209 * @return Character
211 static __inline__ int fgetc(FILE *stream) {
212 char c = EOF;
213 _fread(&c,1,stream);
214 if (c==EOF) stream->eof = 1;
215 return c;
219 * Gets string from stdin
220 * @param s Buffer for string
221 * @return
223 static __inline__ char* gets(char *s) {
224 return fgets(s,BUFSIZ,stdin);
228 * Gets a character from stream
229 * @param stream Stream
230 * @return Character
232 static __inline__ int getc(FILE *stream) {
233 return fgetc(stream);
237 * Gets a character from stdin
238 * @return Character
240 static __inline__ int getchar(void) {
241 return fgetc(stdin);
245 * Reads binary
246 * @param ptr Buffer to store data
247 * @param size Size of each element
248 * @param nitems How many items to read
249 * @param stream Stream to read from
250 * @return How many elements read
252 static __inline__ size_t fread(void *ptr,size_t size,size_t nitems,FILE *stream) {
253 return _fread(ptr,size*nitems,stream);
257 * Writes error message to stderr
258 * @param str Additional error text (can be NULL)
260 static __inline__ void perror(const char *str) {
261 fprintf(stderr,"%s: %s\n",str!=NULL?str:"",strerror(errno));
265 #endif