2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 FILE *fopen(const char *filename
, const char *mode
)
45 if (strchr(mode
, 'r')) openmode
= O_RDONLY
;
46 else if (strchr(mode
, 'w')) openmode
= O_WRONLY
| O_CREAT
;
47 else if (strchr(mode
, 'a')) openmode
= O_WRONLY
| O_CREAT
;
48 if (strchr(mode
, '+')) openmode
= (openmode
& ~O_ACCMODE
) | O_RDWR
;
49 int fd
= open(filename
, openmode
);
50 if (fd
== -1) return 0;
51 // Return file structure
52 FILE *file
= malloc(sizeof(FILE));
53 memset(file
, 0, sizeof(FILE));
55 if (strchr(mode
, 'a')) file
->append
= 1;
58 int fclose(FILE *file
)
60 if (!file
) return EOF
;
65 FILE *freopen(const char *filename
, const char *mode
, FILE *file
)
67 if (fclose(file
) == EOF
) return 0;
68 return fopen(filename
, mode
);
72 return (file
->error
& ERROR_EOF
) != 0;
74 int ferror(FILE *file
)
76 return (file
->error
& ERROR_ERROR
) != 0;
78 int fseek(FILE *file
, long int offset
, int origin
)
80 if (!file
) return EOF
;
82 if (lseek(file
->fd
, offset
, origin
) == (off_t
)-1) return EOF
;
85 long int ftell(FILE *file
)
87 if (!file
) return EOF
;
88 long int pos
= lseek(file
->fd
, 0, SEEK_CUR
);
91 void rewind(FILE *file
)
93 fseek(file
, 0, SEEK_SET
);
95 void clearerr(FILE *file
)
100 int fgetpos(FILE *file
, fpos_t *pos
)
102 if (!file
|| !pos
) return EOF
;
104 long int position
= lseek(file
->fd
, 0, SEEK_CUR
);
113 int fsetpos(FILE *file
, const fpos_t *pos
)
115 if (!file
|| !pos
) return EOF
;
117 if (lseek(file
->fd
, *pos
, SEEK_SET
) == (off_t
)-1) return -1;
121 int fgetc(FILE *file
)
123 if (!file
) return EOF
;
125 int size
= read(file
->fd
, &c
, 1);
128 file
->error
= ERROR_EOF
;
133 file
->error
= ERROR_ERROR
;
138 char *fgets(char *str
, int num
, FILE *file
)
143 char c
= fgetc(file
);
144 if ((c
== EOF
) && ferror(file
)) return 0;
145 if ((c
== EOF
) && (n
== 0))
149 else if ((c
== EOF
) || (c
== '\n') || (c
== 0))
163 int fscanf(FILE *file
, const char *fmt
, ...)
167 size_t fread(void *ptr
, size_t size
, size_t count
, FILE *file
)
170 int bytecount
= read(file
->fd
, ptr
, size
* count
);
173 file
->error
= ERROR_ERROR
;
176 if (bytecount
!= (int)(size
* count
))
178 file
->error
= ERROR_EOF
;
183 int fputc(int c
, FILE *file
)
185 if (!file
) return EOF
;
186 if (file
->append
) lseek(file
->fd
, 0, SEEK_END
);
187 int size
= write(file
->fd
, &c
, 1);
190 file
->error
= ERROR_EOF
;
195 file
->error
= ERROR_ERROR
;
200 int fputs(const char *s
, FILE *file
)
202 if (!file
) return EOF
;
203 if (file
->append
) lseek(file
->fd
, 0, SEEK_END
);
204 int size
= write(file
->fd
, s
, strlen(s
));
207 file
->error
= ERROR_ERROR
;
212 int vfprintf(FILE *file
, const char *fmt
, va_list args
)
214 if (!file
) return EOF
;
215 if (file
->append
) lseek(file
->fd
, 0, SEEK_END
);
216 // FIXME: Greater length?
218 vsnprintf(buffer
, 1024, fmt
, args
);
220 int written
= write(file
->fd
, buffer
, strlen(buffer
));
223 file
->error
= ERROR_ERROR
;
228 int fprintf(FILE *file
, const char *fmt
, ...)
230 if (!file
) return EOF
;
231 if (file
->append
) lseek(file
->fd
, 0, SEEK_END
);
232 // FIXME: Greater length?
236 vsnprintf(buffer
, 1024, fmt
, args
);
239 int written
= write(file
->fd
, buffer
, strlen(buffer
));
242 file
->error
= ERROR_ERROR
;
247 size_t fwrite(const void *ptr
, size_t size
, size_t count
, FILE *file
)
249 if (!file
) return EOF
;
250 if (file
->append
) lseek(file
->fd
, 0, SEEK_END
);
251 int written
= write(file
->fd
, ptr
, size
* count
);
252 if (written
== -1) return EOF
;
256 int fileno(FILE *file
)
258 if (!file
) return -1;
267 char *tmpnam(char *s
)
273 int ungetc(int character
, FILE *file
)
278 int rename(const char *oldname
, const char *newname
)
282 int remove(const char *filename
)
287 int printf(const char *fmt
, ...)
291 int status
= vfprintf(stdout
, fmt
, args
);
295 int puts(const char *s
)
300 int putc(int c
, FILE *file
)
302 return fputc(c
, file
);
306 return fputc(c
, stdout
);
313 void perror(const char *str
)
318 int fflush(FILE *file
)
323 int setvbuf(FILE *file
, char *buf
, int type
, size_t size
)
328 int scanf(const char *format
, ... )
332 int sscanf(const char *s
, const char *format
, ... )