| 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 | /** Size of buffers used in stdio header. */
|
|---|
| 67 | #define BUFSIZ 4096
|
|---|
| 68 |
|
|---|
| 69 | /** Maximum size in bytes of the longest filename. */
|
|---|
| 70 | #define FILENAME_MAX 4096
|
|---|
| 71 |
|
|---|
| 72 | typedef struct _IO_FILE FILE;
|
|---|
| 73 |
|
|---|
| 74 | #ifndef LIBPOSIX_INTERNAL
|
|---|
| 75 | enum _buffer_type {
|
|---|
| 76 | /** No buffering */
|
|---|
| 77 | _IONBF,
|
|---|
| 78 | /** Line buffering */
|
|---|
| 79 | _IOLBF,
|
|---|
| 80 | /** Full buffering */
|
|---|
| 81 | _IOFBF
|
|---|
| 82 | };
|
|---|
| 83 | #endif
|
|---|
| 84 |
|
|---|
| 85 | extern FILE *stdin;
|
|---|
| 86 | extern FILE *stdout;
|
|---|
| 87 | extern FILE *stderr;
|
|---|
| 88 |
|
|---|
| 89 | extern int fgetc(FILE *);
|
|---|
| 90 | extern char *fgets(char *, int, FILE *);
|
|---|
| 91 |
|
|---|
| 92 | extern int getchar(void);
|
|---|
| 93 | extern char *gets(char *, size_t);
|
|---|
| 94 |
|
|---|
| 95 | extern int fputc(wchar_t, FILE *);
|
|---|
| 96 | extern int fputs(const char *, FILE *);
|
|---|
| 97 |
|
|---|
| 98 | extern int putchar(wchar_t);
|
|---|
| 99 | extern int puts(const char *);
|
|---|
| 100 |
|
|---|
| 101 | extern int fprintf(FILE *, const char*, ...) _HELENOS_PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 102 | extern int vfprintf(FILE *, const char *, va_list);
|
|---|
| 103 |
|
|---|
| 104 | extern int printf(const char *, ...) _HELENOS_PRINTF_ATTRIBUTE(1, 2);
|
|---|
| 105 | extern int vprintf(const char *, va_list);
|
|---|
| 106 |
|
|---|
| 107 | extern int snprintf(char *, size_t , const char *, ...) _HELENOS_PRINTF_ATTRIBUTE(3, 4);
|
|---|
| 108 | #ifdef _GNU_SOURCE
|
|---|
| 109 | extern int vasprintf(char **, const char *, va_list);
|
|---|
| 110 | extern int asprintf(char **, const char *, ...) _HELENOS_PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 111 | #endif
|
|---|
| 112 | extern int vsnprintf(char *, size_t, const char *, va_list);
|
|---|
| 113 |
|
|---|
| 114 | extern FILE *fopen(const char *, const char *);
|
|---|
| 115 | extern FILE *fdopen(int, const char *);
|
|---|
| 116 | extern int fclose(FILE *);
|
|---|
| 117 |
|
|---|
| 118 | extern size_t fread(void *, size_t, size_t, FILE *);
|
|---|
| 119 | extern size_t fwrite(const void *, size_t, size_t, FILE *);
|
|---|
| 120 |
|
|---|
| 121 | extern void rewind(FILE *);
|
|---|
| 122 | extern int feof(FILE *);
|
|---|
| 123 | extern int fileno(FILE *);
|
|---|
| 124 |
|
|---|
| 125 | extern int fflush(FILE *);
|
|---|
| 126 | extern int ferror(FILE *);
|
|---|
| 127 | extern void clearerr(FILE *);
|
|---|
| 128 |
|
|---|
| 129 | extern void setvbuf(FILE *, void *, int, size_t);
|
|---|
| 130 | extern void setbuf(FILE *, void *);
|
|---|
| 131 |
|
|---|
| 132 | /* POSIX specific stuff. */
|
|---|
| 133 |
|
|---|
| 134 | /* Identifying the Terminal */
|
|---|
| 135 | #undef L_ctermid
|
|---|
| 136 | #define L_ctermid PATH_MAX
|
|---|
| 137 | extern char *__POSIX_DEF__(ctermid)(char *s);
|
|---|
| 138 |
|
|---|
| 139 | /* Error Recovery */
|
|---|
| 140 | extern void __POSIX_DEF__(clearerr)(FILE *stream);
|
|---|
| 141 |
|
|---|
| 142 | /* Input/Output */
|
|---|
| 143 | #undef putc
|
|---|
| 144 | #define putc fputc
|
|---|
| 145 | extern int __POSIX_DEF__(fputs)(const char *__restrict__ s, FILE *__restrict__ stream);
|
|---|
| 146 | #undef getc
|
|---|
| 147 | #define getc fgetc
|
|---|
| 148 | extern int __POSIX_DEF__(ungetc)(int c, FILE *stream);
|
|---|
| 149 | extern ssize_t __POSIX_DEF__(getdelim)(char **__restrict__ lineptr, size_t *__restrict__ n,
|
|---|
| 150 | int delimiter, FILE *__restrict__ stream);
|
|---|
| 151 | extern ssize_t __POSIX_DEF__(getline)(char **__restrict__ lineptr, size_t *__restrict__ n,
|
|---|
| 152 | FILE *__restrict__ stream);
|
|---|
| 153 |
|
|---|
| 154 | /* Opening Streams */
|
|---|
| 155 | extern FILE *__POSIX_DEF__(freopen)(const char *__restrict__ filename,
|
|---|
| 156 | const char *__restrict__ mode, FILE *__restrict__ stream);
|
|---|
| 157 |
|
|---|
| 158 | /* Error Messages */
|
|---|
| 159 | extern void __POSIX_DEF__(perror)(const char *s);
|
|---|
| 160 |
|
|---|
| 161 | /* File Positioning */
|
|---|
| 162 | typedef struct {
|
|---|
| 163 | off64_t offset;
|
|---|
| 164 | } __POSIX_DEF__(fpos_t);
|
|---|
| 165 |
|
|---|
| 166 | extern int __POSIX_DEF__(fsetpos)(FILE *stream, const __POSIX_DEF__(fpos_t) *pos);
|
|---|
| 167 | extern int __POSIX_DEF__(fgetpos)(FILE *__restrict__ stream, __POSIX_DEF__(fpos_t) *__restrict__ pos);
|
|---|
| 168 | extern int __POSIX_DEF__(fseek)(FILE *stream, long offset, int whence);
|
|---|
| 169 | extern int __POSIX_DEF__(fseeko)(FILE *stream, __POSIX_DEF__(off_t) offset, int whence);
|
|---|
| 170 | extern long __POSIX_DEF__(ftell)(FILE *stream);
|
|---|
| 171 | extern __POSIX_DEF__(off_t) __POSIX_DEF__(ftello)(FILE *stream);
|
|---|
| 172 |
|
|---|
| 173 | /* Flushing Buffers */
|
|---|
| 174 | extern int __POSIX_DEF__(fflush)(FILE *stream);
|
|---|
| 175 |
|
|---|
| 176 | /* Formatted Output */
|
|---|
| 177 | extern int __POSIX_DEF__(dprintf)(int fildes, const char *__restrict__ format, ...)
|
|---|
| 178 | _HELENOS_PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 179 | extern int __POSIX_DEF__(vdprintf)(int fildes, const char *__restrict__ format, va_list ap);
|
|---|
| 180 | extern int __POSIX_DEF__(sprintf)(char *__restrict__ s, const char *__restrict__ format, ...)
|
|---|
| 181 | _HELENOS_PRINTF_ATTRIBUTE(2, 3);
|
|---|
| 182 | extern int __POSIX_DEF__(vsprintf)(char *__restrict__ s, const char *__restrict__ format, va_list ap);
|
|---|
| 183 |
|
|---|
| 184 | /* Formatted Input */
|
|---|
| 185 | extern int __POSIX_DEF__(fscanf)(
|
|---|
| 186 | FILE *__restrict__ stream, const char *__restrict__ format, ...);
|
|---|
| 187 | extern int __POSIX_DEF__(vfscanf)(
|
|---|
| 188 | FILE *__restrict__ stream, const char *__restrict__ format, va_list arg);
|
|---|
| 189 | extern int __POSIX_DEF__(scanf)(const char *__restrict__ format, ...);
|
|---|
| 190 | extern int __POSIX_DEF__(vscanf)(const char *__restrict__ format, va_list arg);
|
|---|
| 191 | extern int __POSIX_DEF__(sscanf)(
|
|---|
| 192 | const char *__restrict__ s, const char *__restrict__ format, ...);
|
|---|
| 193 | extern int __POSIX_DEF__(vsscanf)(
|
|---|
| 194 | const char *__restrict__ s, const char *__restrict__ format, va_list arg);
|
|---|
| 195 |
|
|---|
| 196 | /* File Locking */
|
|---|
| 197 | extern void __POSIX_DEF__(flockfile)(FILE *file);
|
|---|
| 198 | extern int __POSIX_DEF__(ftrylockfile)(FILE *file);
|
|---|
| 199 | extern void __POSIX_DEF__(funlockfile)(FILE *file);
|
|---|
| 200 | extern int __POSIX_DEF__(getc_unlocked)(FILE *stream);
|
|---|
| 201 | extern int __POSIX_DEF__(getchar_unlocked)(void);
|
|---|
| 202 | extern int __POSIX_DEF__(putc_unlocked)(int c, FILE *stream);
|
|---|
| 203 | extern int __POSIX_DEF__(putchar_unlocked)(int c);
|
|---|
| 204 |
|
|---|
| 205 | /* Deleting Files */
|
|---|
| 206 | extern int __POSIX_DEF__(remove)(const char *path);
|
|---|
| 207 |
|
|---|
| 208 | /* Renaming Files */
|
|---|
| 209 | extern int __POSIX_DEF__(rename)(const char *oldname, const char *newname);
|
|---|
| 210 |
|
|---|
| 211 | /* Temporary Files */
|
|---|
| 212 | #undef L_tmpnam
|
|---|
| 213 | #define L_tmpnam PATH_MAX
|
|---|
| 214 | extern char *__POSIX_DEF__(tmpnam)(char *s);
|
|---|
| 215 | extern char *__POSIX_DEF__(tempnam)(const char *dir, const char *pfx);
|
|---|
| 216 | extern FILE *__POSIX_DEF__(tmpfile)(void);
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | #endif /* POSIX_STDIO_H_ */
|
|---|
| 220 |
|
|---|
| 221 | /** @}
|
|---|
| 222 | */
|
|---|