| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
|---|
| 3 | * Copyright (c) 2011 Petr Koupy
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libposix
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file Standard buffered input/output.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #ifndef POSIX_STDIO_H_
|
|---|
| 37 | #define POSIX_STDIO_H_
|
|---|
| 38 |
|
|---|
| 39 | #ifndef __POSIX_DEF__
|
|---|
| 40 | #define __POSIX_DEF__(x) x
|
|---|
| 41 | /* DEBUG macro does not belong to POSIX stdio.h. Its unconditional
|
|---|
| 42 | * definition in the native stdio.h causes unexpected behaviour of
|
|---|
| 43 | * applications which uses their own DEBUG macro (e.g. debugging
|
|---|
| 44 | * output is printed even if not desirable). */
|
|---|
| 45 | #undef DEBUG
|
|---|
| 46 | #endif
|
|---|
| 47 |
|
|---|
| 48 | #include "stddef.h"
|
|---|
| 49 | #include "unistd.h"
|
|---|
| 50 | #include "libc/io/verify.h"
|
|---|
| 51 | #include "sys/types.h"
|
|---|
| 52 | #include "stdarg.h"
|
|---|
| 53 | #include "limits.h"
|
|---|
| 54 |
|
|---|
| 55 | /*
|
|---|
| 56 | * These are the same as in HelenOS libc.
|
|---|
| 57 | * It would be possible to directly include <stdio.h> but
|
|---|
| 58 | * it is better not to pollute POSIX namespace with other functions
|
|---|
| 59 | * defined in that header.
|
|---|
| 60 | *
|
|---|
| 61 | * Because libposix is always linked with libc, providing only these
|
|---|
| 62 | * forward declarations ought to be enough.
|
|---|
| 63 | */
|
|---|
| 64 | #define EOF (-1)
|
|---|
| 65 |
|
|---|
| 66 | #define BUFSIZ 4096
|
|---|
| 67 |
|
|---|
| 68 | typedef struct _IO_FILE FILE;
|
|---|
| 69 |
|
|---|
| 70 | #ifndef LIBPOSIX_INTERNAL
|
|---|
| 71 | enum _buffer_type {
|
|---|
| 72 | /** No buffering */
|
|---|
| 73 | _IONBF,
|
|---|
| 74 | /** Line buffering */
|
|---|
| 75 | _IOLBF,
|
|---|
| 76 | /** Full buffering */
|
|---|
| 77 | _IOFBF
|
|---|
| 78 | };
|
|---|
| 79 | #endif
|
|---|
| 80 |
|
|---|
| 81 | extern FILE *stdin;
|
|---|
| 82 | extern FILE *stdout;
|
|---|
| 83 | extern FILE *stderr;
|
|---|
| 84 |
|
|---|
| 85 | extern int fgetc(FILE *);
|
|---|
| 86 | extern char *fgets(char *, int, FILE *);
|
|---|
| 87 |
|
|---|
| 88 | extern int getchar(void);
|
|---|
| 89 | extern char *gets(char *, size_t);
|
|---|
| 90 |
|
|---|
| 91 | extern int fputc(wchar_t, FILE *);
|
|---|
| 92 | extern int fputs(const char *, FILE *);
|
|---|
| 93 |
|
|---|
| 94 | extern int putchar(wchar_t);
|
|---|
| 95 | extern int puts(const char *);
|
|---|
| 96 |
|
|---|
| 97 | extern int fprintf(FILE *, const char*, ...) PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 98 | extern int vfprintf(FILE *, const char *, va_list);
|
|---|
| 99 |
|
|---|
| 100 | extern int printf(const char *, ...) PRINTF_ATTRIBUTE(1, 2);
|
|---|
| 101 | extern int vprintf(const char *, va_list);
|
|---|
| 102 |
|
|---|
| 103 | extern int snprintf(char *, size_t , const char *, ...) PRINTF_ATTRIBUTE(3, 4);
|
|---|
| 104 | #ifdef _GNU_SOURCE
|
|---|
| 105 | extern int asprintf(char **, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 106 | #endif
|
|---|
| 107 | extern int vsnprintf(char *, size_t, const char *, va_list);
|
|---|
| 108 |
|
|---|
| 109 | extern FILE *fopen(const char *, const char *);
|
|---|
| 110 | extern FILE *fdopen(int, const char *);
|
|---|
| 111 | extern int fclose(FILE *);
|
|---|
| 112 |
|
|---|
| 113 | extern size_t fread(void *, size_t, size_t, FILE *);
|
|---|
| 114 | extern size_t fwrite(const void *, size_t, size_t, FILE *);
|
|---|
| 115 |
|
|---|
| 116 | extern void rewind(FILE *);
|
|---|
| 117 | extern int feof(FILE *);
|
|---|
| 118 | extern int fileno(FILE *);
|
|---|
| 119 |
|
|---|
| 120 | extern int fflush(FILE *);
|
|---|
| 121 | extern int ferror(FILE *);
|
|---|
| 122 | extern void clearerr(FILE *);
|
|---|
| 123 |
|
|---|
| 124 | extern void setvbuf(FILE *, void *, int, size_t);
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | /* POSIX specific stuff. */
|
|---|
| 128 |
|
|---|
| 129 | /* Identifying the Terminal */
|
|---|
| 130 | #undef L_ctermid
|
|---|
| 131 | #define L_ctermid PATH_MAX
|
|---|
| 132 | extern char *__POSIX_DEF__(ctermid)(char *s);
|
|---|
| 133 |
|
|---|
| 134 | /* Error Recovery */
|
|---|
| 135 | extern void __POSIX_DEF__(clearerr)(FILE *stream);
|
|---|
| 136 |
|
|---|
| 137 | /* Input/Output */
|
|---|
| 138 | #undef putc
|
|---|
| 139 | #define putc fputc
|
|---|
| 140 | extern int __POSIX_DEF__(fputs)(const char *restrict s, FILE *restrict stream);
|
|---|
| 141 | #undef getc
|
|---|
| 142 | #define getc fgetc
|
|---|
| 143 | extern int __POSIX_DEF__(ungetc)(int c, FILE *stream);
|
|---|
| 144 | extern ssize_t __POSIX_DEF__(getdelim)(char **restrict lineptr, size_t *restrict n,
|
|---|
| 145 | int delimiter, FILE *restrict stream);
|
|---|
| 146 | extern ssize_t __POSIX_DEF__(getline)(char **restrict lineptr, size_t *restrict n,
|
|---|
| 147 | FILE *restrict stream);
|
|---|
| 148 |
|
|---|
| 149 | /* Opening Streams */
|
|---|
| 150 | extern FILE *__POSIX_DEF__(freopen)(const char *restrict filename,
|
|---|
| 151 | const char *restrict mode, FILE *restrict stream);
|
|---|
| 152 |
|
|---|
| 153 | /* Error Messages */
|
|---|
| 154 | extern void __POSIX_DEF__(perror)(const char *s);
|
|---|
| 155 |
|
|---|
| 156 | /* File Positioning */
|
|---|
| 157 | typedef struct _posix_fpos __POSIX_DEF__(fpos_t);
|
|---|
| 158 | extern int __POSIX_DEF__(fsetpos)(FILE *stream, const __POSIX_DEF__(fpos_t) *pos);
|
|---|
| 159 | extern int __POSIX_DEF__(fgetpos)(FILE *restrict stream, __POSIX_DEF__(fpos_t) *restrict pos);
|
|---|
| 160 | extern int __POSIX_DEF__(fseek)(FILE *stream, long offset, int whence);
|
|---|
| 161 | extern int __POSIX_DEF__(fseeko)(FILE *stream, __POSIX_DEF__(off_t) offset, int whence);
|
|---|
| 162 | extern long __POSIX_DEF__(ftell)(FILE *stream);
|
|---|
| 163 | extern __POSIX_DEF__(off_t) __POSIX_DEF__(ftello)(FILE *stream);
|
|---|
| 164 |
|
|---|
| 165 | /* Flushing Buffers */
|
|---|
| 166 | extern int __POSIX_DEF__(fflush)(FILE *stream);
|
|---|
| 167 |
|
|---|
| 168 | /* Formatted Output */
|
|---|
| 169 | extern int __POSIX_DEF__(dprintf)(int fildes, const char *restrict format, ...)
|
|---|
| 170 | PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 171 | extern int __POSIX_DEF__(vdprintf)(int fildes, const char *restrict format, va_list ap);
|
|---|
| 172 | extern int __POSIX_DEF__(sprintf)(char *restrict s, const char *restrict format, ...)
|
|---|
| 173 | PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 174 | extern int __POSIX_DEF__(vsprintf)(char *restrict s, const char *restrict format, va_list ap);
|
|---|
| 175 |
|
|---|
| 176 | /* Formatted Input */
|
|---|
| 177 | extern int __POSIX_DEF__(fscanf)(
|
|---|
| 178 | FILE *restrict stream, const char *restrict format, ...);
|
|---|
| 179 | extern int __POSIX_DEF__(vfscanf)(
|
|---|
| 180 | FILE *restrict stream, const char *restrict format, va_list arg);
|
|---|
| 181 | extern int __POSIX_DEF__(scanf)(const char *restrict format, ...);
|
|---|
| 182 | extern int __POSIX_DEF__(vscanf)(const char *restrict format, va_list arg);
|
|---|
| 183 | extern int __POSIX_DEF__(sscanf)(
|
|---|
| 184 | const char *restrict s, const char *restrict format, ...);
|
|---|
| 185 | extern int __POSIX_DEF__(vsscanf)(
|
|---|
| 186 | const char *restrict s, const char *restrict format, va_list arg);
|
|---|
| 187 |
|
|---|
| 188 | /* File Locking */
|
|---|
| 189 | extern void __POSIX_DEF__(flockfile)(FILE *file);
|
|---|
| 190 | extern int __POSIX_DEF__(ftrylockfile)(FILE *file);
|
|---|
| 191 | extern void __POSIX_DEF__(funlockfile)(FILE *file);
|
|---|
| 192 | extern int __POSIX_DEF__(getc_unlocked)(FILE *stream);
|
|---|
| 193 | extern int __POSIX_DEF__(getchar_unlocked)(void);
|
|---|
| 194 | extern int __POSIX_DEF__(putc_unlocked)(int c, FILE *stream);
|
|---|
| 195 | extern int __POSIX_DEF__(putchar_unlocked)(int c);
|
|---|
| 196 |
|
|---|
| 197 | /* Deleting Files */
|
|---|
| 198 | extern int __POSIX_DEF__(remove)(const char *path);
|
|---|
| 199 |
|
|---|
| 200 | /* Renaming Files */
|
|---|
| 201 | extern int __POSIX_DEF__(rename)(const char *oldname, const char *newname);
|
|---|
| 202 |
|
|---|
| 203 | /* Temporary Files */
|
|---|
| 204 | #undef L_tmpnam
|
|---|
| 205 | #define L_tmpnam PATH_MAX
|
|---|
| 206 | extern char *__POSIX_DEF__(tmpnam)(char *s);
|
|---|
| 207 | extern char *__POSIX_DEF__(tempnam)(const char *dir, const char *pfx);
|
|---|
| 208 | extern FILE *__POSIX_DEF__(tmpfile)(void);
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | #endif /* POSIX_STDIO_H_ */
|
|---|
| 212 |
|
|---|
| 213 | /** @}
|
|---|
| 214 | */
|
|---|