[b861b58] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Martin Decky
|
---|
[b861b58] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[2595dab] | 33 | */
|
---|
[b861b58] | 34 |
|
---|
[cc6f688] | 35 | #include <stdio.h>
|
---|
[2595dab] | 36 | #include <unistd.h>
|
---|
| 37 | #include <fcntl.h>
|
---|
[ef8bcc6] | 38 | #include <assert.h>
|
---|
[19f857a] | 39 | #include <str.h>
|
---|
[56fa418] | 40 | #include <errno.h>
|
---|
[3e6a98c5] | 41 | #include <stdbool.h>
|
---|
[2595dab] | 42 | #include <malloc.h>
|
---|
[64d2b10] | 43 | #include <async.h>
|
---|
[6fa9a99d] | 44 | #include <io/kio.h>
|
---|
[2595dab] | 45 | #include <vfs/vfs.h>
|
---|
[79ae36dd] | 46 | #include <vfs/vfs_sess.h>
|
---|
[bb9ec2d] | 47 | #include <vfs/inbox.h>
|
---|
[15f3c3f] | 48 | #include <ipc/loc.h>
|
---|
[d9c8c81] | 49 | #include <adt/list.h>
|
---|
[47b7006] | 50 | #include "../private/io.h"
|
---|
[79ae36dd] | 51 | #include "../private/stdio.h"
|
---|
[b861b58] | 52 |
|
---|
[facebd56] | 53 | static void _ffillbuf(FILE *stream);
|
---|
[ef8bcc6] | 54 | static void _fflushbuf(FILE *stream);
|
---|
| 55 |
|
---|
[a68f737] | 56 | static FILE stdin_null = {
|
---|
[2595dab] | 57 | .fd = -1,
|
---|
[58898d1d] | 58 | .pos = 0,
|
---|
[2595dab] | 59 | .error = true,
|
---|
| 60 | .eof = true,
|
---|
[6fa9a99d] | 61 | .kio = false,
|
---|
[79ae36dd] | 62 | .sess = NULL,
|
---|
[ef8bcc6] | 63 | .btype = _IONBF,
|
---|
| 64 | .buf = NULL,
|
---|
| 65 | .buf_size = 0,
|
---|
[facebd56] | 66 | .buf_head = NULL,
|
---|
| 67 | .buf_tail = NULL,
|
---|
[c70703a] | 68 | .buf_state = _bs_empty
|
---|
[2595dab] | 69 | };
|
---|
[350514c] | 70 |
|
---|
[6fa9a99d] | 71 | static FILE stdout_kio = {
|
---|
[2595dab] | 72 | .fd = -1,
|
---|
[58898d1d] | 73 | .pos = 0,
|
---|
[2595dab] | 74 | .error = false,
|
---|
| 75 | .eof = false,
|
---|
[6fa9a99d] | 76 | .kio = true,
|
---|
[79ae36dd] | 77 | .sess = NULL,
|
---|
[ef8bcc6] | 78 | .btype = _IOLBF,
|
---|
| 79 | .buf = NULL,
|
---|
| 80 | .buf_size = BUFSIZ,
|
---|
[facebd56] | 81 | .buf_head = NULL,
|
---|
| 82 | .buf_tail = NULL,
|
---|
| 83 | .buf_state = _bs_empty
|
---|
[2595dab] | 84 | };
|
---|
| 85 |
|
---|
[6fa9a99d] | 86 | static FILE stderr_kio = {
|
---|
[a68f737] | 87 | .fd = -1,
|
---|
[58898d1d] | 88 | .pos = 0,
|
---|
[a68f737] | 89 | .error = false,
|
---|
| 90 | .eof = false,
|
---|
[6fa9a99d] | 91 | .kio = true,
|
---|
[79ae36dd] | 92 | .sess = NULL,
|
---|
[ef8bcc6] | 93 | .btype = _IONBF,
|
---|
| 94 | .buf = NULL,
|
---|
| 95 | .buf_size = 0,
|
---|
[facebd56] | 96 | .buf_head = NULL,
|
---|
| 97 | .buf_tail = NULL,
|
---|
| 98 | .buf_state = _bs_empty
|
---|
[a68f737] | 99 | };
|
---|
| 100 |
|
---|
| 101 | FILE *stdin = NULL;
|
---|
| 102 | FILE *stdout = NULL;
|
---|
| 103 | FILE *stderr = NULL;
|
---|
| 104 |
|
---|
| 105 | static LIST_INITIALIZE(files);
|
---|
| 106 |
|
---|
[bb9ec2d] | 107 | void __stdio_init(void)
|
---|
[a68f737] | 108 | {
|
---|
[bb9ec2d] | 109 | /* The first three standard file descriptors are assigned for compatibility.
|
---|
| 110 | * This will probably be removed later.
|
---|
| 111 | */
|
---|
| 112 |
|
---|
| 113 | int infd = inbox_get("stdin");
|
---|
| 114 | if (infd >= 0) {
|
---|
[fcab7ef] | 115 | int stdinfd = vfs_clone(infd, -1, false);
|
---|
[bb9ec2d] | 116 | assert(stdinfd == 0);
|
---|
| 117 | _vfs_open(stdinfd, MODE_READ);
|
---|
| 118 | stdin = fdopen(stdinfd, "r");
|
---|
[a68f737] | 119 | } else {
|
---|
| 120 | stdin = &stdin_null;
|
---|
| 121 | list_append(&stdin->link, &files);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[bb9ec2d] | 124 | int outfd = inbox_get("stdout");
|
---|
| 125 | if (outfd >= 0) {
|
---|
[fcab7ef] | 126 | int stdoutfd = vfs_clone(outfd, -1, false);
|
---|
[bb9ec2d] | 127 | assert(stdoutfd <= 1);
|
---|
[fcab7ef] | 128 | while (stdoutfd < 1)
|
---|
| 129 | stdoutfd = vfs_clone(outfd, -1, false);
|
---|
[bb9ec2d] | 130 | _vfs_open(stdoutfd, MODE_APPEND);
|
---|
| 131 | stdout = fdopen(stdoutfd, "a");
|
---|
[a68f737] | 132 | } else {
|
---|
[6fa9a99d] | 133 | stdout = &stdout_kio;
|
---|
[a68f737] | 134 | list_append(&stdout->link, &files);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[bb9ec2d] | 137 | int errfd = inbox_get("stderr");
|
---|
| 138 | if (errfd >= 0) {
|
---|
[fcab7ef] | 139 | int stderrfd = vfs_clone(errfd, -1, false);
|
---|
[bb9ec2d] | 140 | assert(stderrfd <= 2);
|
---|
[fcab7ef] | 141 | while (stderrfd < 2)
|
---|
| 142 | stderrfd = vfs_clone(errfd, -1, false);
|
---|
[bb9ec2d] | 143 | _vfs_open(stderrfd, MODE_APPEND);
|
---|
| 144 | stderr = fdopen(stderrfd, "a");
|
---|
[a68f737] | 145 | } else {
|
---|
[6fa9a99d] | 146 | stderr = &stderr_kio;
|
---|
[a68f737] | 147 | list_append(&stderr->link, &files);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[db24058] | 151 | void __stdio_done(void)
|
---|
[a68f737] | 152 | {
|
---|
[b72efe8] | 153 | while (!list_empty(&files)) {
|
---|
| 154 | FILE *file = list_get_instance(list_first(&files), FILE, link);
|
---|
[a68f737] | 155 | fclose(file);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[2595dab] | 158 |
|
---|
| 159 | static bool parse_mode(const char *mode, int *flags)
|
---|
[b861b58] | 160 | {
|
---|
[2595dab] | 161 | /* Parse mode except first character. */
|
---|
| 162 | const char *mp = mode;
|
---|
| 163 | if (*mp++ == 0) {
|
---|
| 164 | errno = EINVAL;
|
---|
| 165 | return false;
|
---|
| 166 | }
|
---|
[cc6f688] | 167 |
|
---|
[2595dab] | 168 | if ((*mp == 'b') || (*mp == 't'))
|
---|
| 169 | mp++;
|
---|
[c9857c6] | 170 |
|
---|
[2595dab] | 171 | bool plus;
|
---|
| 172 | if (*mp == '+') {
|
---|
| 173 | mp++;
|
---|
| 174 | plus = true;
|
---|
| 175 | } else
|
---|
| 176 | plus = false;
|
---|
[566f4cfb] | 177 |
|
---|
[2595dab] | 178 | if (*mp != 0) {
|
---|
| 179 | errno = EINVAL;
|
---|
| 180 | return false;
|
---|
[350514c] | 181 | }
|
---|
| 182 |
|
---|
[2595dab] | 183 | /* Parse first character of mode and determine flags for open(). */
|
---|
| 184 | switch (mode[0]) {
|
---|
| 185 | case 'r':
|
---|
| 186 | *flags = plus ? O_RDWR : O_RDONLY;
|
---|
| 187 | break;
|
---|
| 188 | case 'w':
|
---|
| 189 | *flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
|
---|
| 190 | break;
|
---|
| 191 | case 'a':
|
---|
| 192 | /* TODO: a+ must read from beginning, append to the end. */
|
---|
| 193 | if (plus) {
|
---|
| 194 | errno = ENOTSUP;
|
---|
| 195 | return false;
|
---|
| 196 | }
|
---|
| 197 | *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
|
---|
[82582e4] | 198 | break;
|
---|
[2595dab] | 199 | default:
|
---|
| 200 | errno = EINVAL;
|
---|
| 201 | return false;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | return true;
|
---|
[cc6f688] | 205 | }
|
---|
[b861b58] | 206 |
|
---|
[ef8bcc6] | 207 | /** Set stream buffer. */
|
---|
| 208 | void setvbuf(FILE *stream, void *buf, int mode, size_t size)
|
---|
| 209 | {
|
---|
| 210 | stream->btype = mode;
|
---|
| 211 | stream->buf = buf;
|
---|
| 212 | stream->buf_size = size;
|
---|
| 213 | stream->buf_head = stream->buf;
|
---|
[facebd56] | 214 | stream->buf_tail = stream->buf;
|
---|
| 215 | stream->buf_state = _bs_empty;
|
---|
[ef8bcc6] | 216 | }
|
---|
| 217 |
|
---|
[7699c21] | 218 | /** Set stream buffer.
|
---|
| 219 | *
|
---|
| 220 | * When @p buf is NULL, the stream is set as unbuffered, otherwise
|
---|
| 221 | * full buffering is enabled.
|
---|
| 222 | */
|
---|
| 223 | void setbuf(FILE *stream, void *buf)
|
---|
| 224 | {
|
---|
| 225 | if (buf == NULL) {
|
---|
| 226 | setvbuf(stream, NULL, _IONBF, BUFSIZ);
|
---|
| 227 | } else {
|
---|
| 228 | setvbuf(stream, buf, _IOFBF, BUFSIZ);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[bfd247f] | 232 | static void _setvbuf(FILE *stream)
|
---|
| 233 | {
|
---|
| 234 | /* FIXME: Use more complex rules for setting buffering options. */
|
---|
| 235 |
|
---|
| 236 | switch (stream->fd) {
|
---|
| 237 | case 1:
|
---|
| 238 | setvbuf(stream, NULL, _IOLBF, BUFSIZ);
|
---|
| 239 | break;
|
---|
| 240 | case 0:
|
---|
| 241 | case 2:
|
---|
| 242 | setvbuf(stream, NULL, _IONBF, 0);
|
---|
| 243 | break;
|
---|
| 244 | default:
|
---|
| 245 | setvbuf(stream, NULL, _IOFBF, BUFSIZ);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[ef8bcc6] | 249 | /** Allocate stream buffer. */
|
---|
| 250 | static int _fallocbuf(FILE *stream)
|
---|
| 251 | {
|
---|
| 252 | assert(stream->buf == NULL);
|
---|
[bfd247f] | 253 |
|
---|
[ef8bcc6] | 254 | stream->buf = malloc(stream->buf_size);
|
---|
| 255 | if (stream->buf == NULL) {
|
---|
| 256 | errno = ENOMEM;
|
---|
[6afc9d7] | 257 | return EOF;
|
---|
[ef8bcc6] | 258 | }
|
---|
[bfd247f] | 259 |
|
---|
[ef8bcc6] | 260 | stream->buf_head = stream->buf;
|
---|
[facebd56] | 261 | stream->buf_tail = stream->buf;
|
---|
[ef8bcc6] | 262 | return 0;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[2595dab] | 265 | /** Open a stream.
|
---|
| 266 | *
|
---|
| 267 | * @param path Path of the file to open.
|
---|
| 268 | * @param mode Mode string, (r|w|a)[b|t][+].
|
---|
| 269 | *
|
---|
[4e2cf8b] | 270 | */
|
---|
[2595dab] | 271 | FILE *fopen(const char *path, const char *mode)
|
---|
[4e2cf8b] | 272 | {
|
---|
[2595dab] | 273 | int flags;
|
---|
| 274 | if (!parse_mode(mode, &flags))
|
---|
| 275 | return NULL;
|
---|
[4e2cf8b] | 276 |
|
---|
[2595dab] | 277 | /* Open file. */
|
---|
| 278 | FILE *stream = malloc(sizeof(FILE));
|
---|
| 279 | if (stream == NULL) {
|
---|
| 280 | errno = ENOMEM;
|
---|
| 281 | return NULL;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | stream->fd = open(path, flags, 0666);
|
---|
| 285 | if (stream->fd < 0) {
|
---|
| 286 | /* errno was set by open() */
|
---|
| 287 | free(stream);
|
---|
| 288 | return NULL;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[58898d1d] | 291 | stream->pos = 0;
|
---|
[2595dab] | 292 | stream->error = false;
|
---|
| 293 | stream->eof = false;
|
---|
[6fa9a99d] | 294 | stream->kio = false;
|
---|
[79ae36dd] | 295 | stream->sess = NULL;
|
---|
[facebd56] | 296 | stream->need_sync = false;
|
---|
[bfd247f] | 297 | _setvbuf(stream);
|
---|
[e86a617a] | 298 | stream->ungetc_chars = 0;
|
---|
[2595dab] | 299 |
|
---|
[a68f737] | 300 | list_append(&stream->link, &files);
|
---|
| 301 |
|
---|
[2595dab] | 302 | return stream;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[080ad7f] | 305 | FILE *fdopen(int fd, const char *mode)
|
---|
| 306 | {
|
---|
| 307 | /* Open file. */
|
---|
| 308 | FILE *stream = malloc(sizeof(FILE));
|
---|
| 309 | if (stream == NULL) {
|
---|
| 310 | errno = ENOMEM;
|
---|
| 311 | return NULL;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | stream->fd = fd;
|
---|
[58898d1d] | 315 | stream->pos = 0;
|
---|
[080ad7f] | 316 | stream->error = false;
|
---|
| 317 | stream->eof = false;
|
---|
[6fa9a99d] | 318 | stream->kio = false;
|
---|
[79ae36dd] | 319 | stream->sess = NULL;
|
---|
[facebd56] | 320 | stream->need_sync = false;
|
---|
[bfd247f] | 321 | _setvbuf(stream);
|
---|
[e86a617a] | 322 | stream->ungetc_chars = 0;
|
---|
[080ad7f] | 323 |
|
---|
| 324 | list_append(&stream->link, &files);
|
---|
| 325 |
|
---|
| 326 | return stream;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[80bee81] | 329 |
|
---|
| 330 | static int _fclose_nofree(FILE *stream)
|
---|
[2595dab] | 331 | {
|
---|
| 332 | int rc = 0;
|
---|
| 333 |
|
---|
| 334 | fflush(stream);
|
---|
| 335 |
|
---|
[79ae36dd] | 336 | if (stream->sess != NULL)
|
---|
| 337 | async_hangup(stream->sess);
|
---|
[2595dab] | 338 |
|
---|
| 339 | if (stream->fd >= 0)
|
---|
| 340 | rc = close(stream->fd);
|
---|
| 341 |
|
---|
[a68f737] | 342 | list_remove(&stream->link);
|
---|
| 343 |
|
---|
[80bee81] | 344 | if (rc != 0) {
|
---|
| 345 | /* errno was set by close() */
|
---|
| 346 | return EOF;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | return 0;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | int fclose(FILE *stream)
|
---|
| 353 | {
|
---|
| 354 | int rc = _fclose_nofree(stream);
|
---|
| 355 |
|
---|
[a68f737] | 356 | if ((stream != &stdin_null)
|
---|
[6fa9a99d] | 357 | && (stream != &stdout_kio)
|
---|
| 358 | && (stream != &stderr_kio))
|
---|
[2595dab] | 359 | free(stream);
|
---|
| 360 |
|
---|
[80bee81] | 361 | return rc;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | FILE *freopen(const char *path, const char *mode, FILE *stream)
|
---|
| 365 | {
|
---|
| 366 | FILE *nstr;
|
---|
[2595dab] | 367 |
|
---|
[80bee81] | 368 | if (path == NULL) {
|
---|
| 369 | /* Changing mode is not supported */
|
---|
| 370 | return NULL;
|
---|
[2595dab] | 371 | }
|
---|
| 372 |
|
---|
[80bee81] | 373 | (void) _fclose_nofree(stream);
|
---|
| 374 | nstr = fopen(path, mode);
|
---|
| 375 | if (nstr == NULL) {
|
---|
| 376 | free(stream);
|
---|
| 377 | return NULL;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | list_remove(&nstr->link);
|
---|
| 381 | *stream = *nstr;
|
---|
| 382 | list_append(&stream->link, &files);
|
---|
| 383 |
|
---|
| 384 | free(nstr);
|
---|
| 385 |
|
---|
| 386 | return stream;
|
---|
[4e2cf8b] | 387 | }
|
---|
| 388 |
|
---|
[facebd56] | 389 | /** Read from a stream (unbuffered).
|
---|
[2595dab] | 390 | *
|
---|
| 391 | * @param buf Destination buffer.
|
---|
| 392 | * @param size Size of each record.
|
---|
| 393 | * @param nmemb Number of records to read.
|
---|
| 394 | * @param stream Pointer to the stream.
|
---|
[6afc9d7] | 395 | *
|
---|
| 396 | * @return Number of elements successfully read. On error this is less than
|
---|
| 397 | * nmemb, stream error indicator is set and errno is set.
|
---|
[4e2cf8b] | 398 | */
|
---|
[facebd56] | 399 | static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
[4e2cf8b] | 400 | {
|
---|
[002252a] | 401 | if (size == 0 || nmemb == 0)
|
---|
| 402 | return 0;
|
---|
| 403 |
|
---|
[58898d1d] | 404 | ssize_t rd = read(stream->fd, &stream->pos, buf, size * nmemb);
|
---|
| 405 | if (rd < 0) {
|
---|
| 406 | /* errno was set by read() */
|
---|
| 407 | stream->error = true;
|
---|
| 408 | rd = 0;
|
---|
| 409 | } else if (rd == 0) {
|
---|
| 410 | stream->eof = true;
|
---|
[2595dab] | 411 | }
|
---|
[4e2cf8b] | 412 |
|
---|
[58898d1d] | 413 | return (rd / size);
|
---|
[2595dab] | 414 | }
|
---|
[55cff86] | 415 |
|
---|
[facebd56] | 416 | /** Write to a stream (unbuffered).
|
---|
| 417 | *
|
---|
| 418 | * @param buf Source buffer.
|
---|
| 419 | * @param size Size of each record.
|
---|
| 420 | * @param nmemb Number of records to write.
|
---|
| 421 | * @param stream Pointer to the stream.
|
---|
[6afc9d7] | 422 | *
|
---|
| 423 | * @return Number of elements successfully written. On error this is less than
|
---|
| 424 | * nmemb, stream error indicator is set and errno is set.
|
---|
[facebd56] | 425 | */
|
---|
[ef8bcc6] | 426 | static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
[2595dab] | 427 | {
|
---|
[002252a] | 428 | if (size == 0 || nmemb == 0)
|
---|
| 429 | return 0;
|
---|
| 430 |
|
---|
[58898d1d] | 431 | ssize_t wr;
|
---|
| 432 | if (stream->kio) {
|
---|
| 433 | size_t nwritten;
|
---|
| 434 | wr = kio_write(buf, size * nmemb, &nwritten);
|
---|
| 435 | if (wr != EOK) {
|
---|
| 436 | stream->error = true;
|
---|
| 437 | wr = 0;
|
---|
[6afc9d7] | 438 | } else {
|
---|
[58898d1d] | 439 | wr = nwritten;
|
---|
[6afc9d7] | 440 | }
|
---|
[58898d1d] | 441 | } else {
|
---|
| 442 | wr = write(stream->fd, &stream->pos, buf, size * nmemb);
|
---|
| 443 | if (wr < 0) {
|
---|
| 444 | /* errno was set by write() */
|
---|
[2595dab] | 445 | stream->error = true;
|
---|
[58898d1d] | 446 | wr = 0;
|
---|
[2595dab] | 447 | }
|
---|
| 448 | }
|
---|
[facebd56] | 449 |
|
---|
[58898d1d] | 450 | if (wr > 0)
|
---|
[facebd56] | 451 | stream->need_sync = true;
|
---|
[2595dab] | 452 |
|
---|
[58898d1d] | 453 | return (wr / size);
|
---|
[4e2cf8b] | 454 | }
|
---|
| 455 |
|
---|
[6afc9d7] | 456 | /** Read some data in stream buffer.
|
---|
| 457 | *
|
---|
| 458 | * On error, stream error indicator is set and errno is set.
|
---|
| 459 | */
|
---|
[facebd56] | 460 | static void _ffillbuf(FILE *stream)
|
---|
| 461 | {
|
---|
| 462 | ssize_t rc;
|
---|
| 463 |
|
---|
| 464 | stream->buf_head = stream->buf_tail = stream->buf;
|
---|
| 465 |
|
---|
[58898d1d] | 466 | rc = read(stream->fd, &stream->pos, stream->buf, stream->buf_size);
|
---|
[facebd56] | 467 | if (rc < 0) {
|
---|
[6afc9d7] | 468 | /* errno was set by read() */
|
---|
[facebd56] | 469 | stream->error = true;
|
---|
| 470 | return;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | if (rc == 0) {
|
---|
| 474 | stream->eof = true;
|
---|
| 475 | return;
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | stream->buf_head += rc;
|
---|
| 479 | stream->buf_state = _bs_read;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | /** Write out stream buffer, do not sync stream. */
|
---|
[ef8bcc6] | 483 | static void _fflushbuf(FILE *stream)
|
---|
| 484 | {
|
---|
| 485 | size_t bytes_used;
|
---|
[facebd56] | 486 |
|
---|
[bfd247f] | 487 | if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
|
---|
[ef8bcc6] | 488 | return;
|
---|
[facebd56] | 489 |
|
---|
| 490 | bytes_used = stream->buf_head - stream->buf_tail;
|
---|
| 491 |
|
---|
| 492 | /* If buffer has prefetched read data, we need to seek back. */
|
---|
[58898d1d] | 493 | if (bytes_used > 0 && stream->buf_state == _bs_read)
|
---|
| 494 | stream->pos -= bytes_used;
|
---|
[facebd56] | 495 |
|
---|
| 496 | /* If buffer has unwritten data, we need to write them out. */
|
---|
[6afc9d7] | 497 | if (bytes_used > 0 && stream->buf_state == _bs_write) {
|
---|
[facebd56] | 498 | (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
|
---|
[6afc9d7] | 499 | /* On error stream error indicator and errno are set by _fwrite */
|
---|
| 500 | if (stream->error)
|
---|
| 501 | return;
|
---|
| 502 | }
|
---|
[facebd56] | 503 |
|
---|
[ef8bcc6] | 504 | stream->buf_head = stream->buf;
|
---|
[facebd56] | 505 | stream->buf_tail = stream->buf;
|
---|
| 506 | stream->buf_state = _bs_empty;
|
---|
[ef8bcc6] | 507 | }
|
---|
| 508 |
|
---|
[facebd56] | 509 | /** Read from a stream.
|
---|
| 510 | *
|
---|
| 511 | * @param dest Destination buffer.
|
---|
| 512 | * @param size Size of each record.
|
---|
| 513 | * @param nmemb Number of records to read.
|
---|
| 514 | * @param stream Pointer to the stream.
|
---|
| 515 | *
|
---|
| 516 | */
|
---|
| 517 | size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
|
---|
| 518 | {
|
---|
| 519 | uint8_t *dp;
|
---|
| 520 | size_t bytes_left;
|
---|
| 521 | size_t now;
|
---|
| 522 | size_t data_avail;
|
---|
| 523 | size_t total_read;
|
---|
| 524 | size_t i;
|
---|
| 525 |
|
---|
| 526 | if (size == 0 || nmemb == 0)
|
---|
| 527 | return 0;
|
---|
| 528 |
|
---|
[bd5414e] | 529 | bytes_left = size * nmemb;
|
---|
| 530 | total_read = 0;
|
---|
| 531 | dp = (uint8_t *) dest;
|
---|
| 532 |
|
---|
| 533 | /* Bytes from ungetc() buffer */
|
---|
| 534 | while (stream->ungetc_chars > 0 && bytes_left > 0) {
|
---|
| 535 | *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
|
---|
| 536 | ++total_read;
|
---|
[a955fcc] | 537 | --bytes_left;
|
---|
[bd5414e] | 538 | }
|
---|
| 539 |
|
---|
[facebd56] | 540 | /* If not buffered stream, read in directly. */
|
---|
| 541 | if (stream->btype == _IONBF) {
|
---|
[bd5414e] | 542 | total_read += _fread(dest, 1, bytes_left, stream);
|
---|
| 543 | return total_read / size;
|
---|
[facebd56] | 544 | }
|
---|
| 545 |
|
---|
| 546 | /* Make sure no data is pending write. */
|
---|
| 547 | if (stream->buf_state == _bs_write)
|
---|
| 548 | _fflushbuf(stream);
|
---|
| 549 |
|
---|
| 550 | /* Perform lazy allocation of stream buffer. */
|
---|
| 551 | if (stream->buf == NULL) {
|
---|
| 552 | if (_fallocbuf(stream) != 0)
|
---|
| 553 | return 0; /* Errno set by _fallocbuf(). */
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
|
---|
| 557 | if (stream->buf_head == stream->buf_tail)
|
---|
| 558 | _ffillbuf(stream);
|
---|
| 559 |
|
---|
[6afc9d7] | 560 | if (stream->error || stream->eof) {
|
---|
| 561 | /* On error errno was set by _ffillbuf() */
|
---|
[facebd56] | 562 | break;
|
---|
[6afc9d7] | 563 | }
|
---|
[facebd56] | 564 |
|
---|
| 565 | data_avail = stream->buf_head - stream->buf_tail;
|
---|
| 566 |
|
---|
| 567 | if (bytes_left > data_avail)
|
---|
| 568 | now = data_avail;
|
---|
| 569 | else
|
---|
| 570 | now = bytes_left;
|
---|
| 571 |
|
---|
| 572 | for (i = 0; i < now; i++) {
|
---|
| 573 | dp[i] = stream->buf_tail[i];
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | dp += now;
|
---|
| 577 | stream->buf_tail += now;
|
---|
| 578 | bytes_left -= now;
|
---|
| 579 | total_read += now;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | return (total_read / size);
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 |
|
---|
[ef8bcc6] | 586 | /** Write to a stream.
|
---|
| 587 | *
|
---|
| 588 | * @param buf Source buffer.
|
---|
| 589 | * @param size Size of each record.
|
---|
| 590 | * @param nmemb Number of records to write.
|
---|
| 591 | * @param stream Pointer to the stream.
|
---|
| 592 | *
|
---|
| 593 | */
|
---|
| 594 | size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
| 595 | {
|
---|
| 596 | uint8_t *data;
|
---|
| 597 | size_t bytes_left;
|
---|
| 598 | size_t now;
|
---|
| 599 | size_t buf_free;
|
---|
| 600 | size_t total_written;
|
---|
| 601 | size_t i;
|
---|
| 602 | uint8_t b;
|
---|
| 603 | bool need_flush;
|
---|
[002252a] | 604 |
|
---|
| 605 | if (size == 0 || nmemb == 0)
|
---|
| 606 | return 0;
|
---|
| 607 |
|
---|
[ef8bcc6] | 608 | /* If not buffered stream, write out directly. */
|
---|
[bfd247f] | 609 | if (stream->btype == _IONBF) {
|
---|
| 610 | now = _fwrite(buf, size, nmemb, stream);
|
---|
| 611 | fflush(stream);
|
---|
| 612 | return now;
|
---|
| 613 | }
|
---|
[facebd56] | 614 |
|
---|
| 615 | /* Make sure buffer contains no prefetched data. */
|
---|
| 616 | if (stream->buf_state == _bs_read)
|
---|
| 617 | _fflushbuf(stream);
|
---|
| 618 |
|
---|
[ef8bcc6] | 619 | /* Perform lazy allocation of stream buffer. */
|
---|
| 620 | if (stream->buf == NULL) {
|
---|
| 621 | if (_fallocbuf(stream) != 0)
|
---|
| 622 | return 0; /* Errno set by _fallocbuf(). */
|
---|
| 623 | }
|
---|
[bfd247f] | 624 |
|
---|
[ef8bcc6] | 625 | data = (uint8_t *) buf;
|
---|
| 626 | bytes_left = size * nmemb;
|
---|
| 627 | total_written = 0;
|
---|
| 628 | need_flush = false;
|
---|
[bfd247f] | 629 |
|
---|
| 630 | while ((!stream->error) && (bytes_left > 0)) {
|
---|
[ef8bcc6] | 631 | buf_free = stream->buf_size - (stream->buf_head - stream->buf);
|
---|
| 632 | if (bytes_left > buf_free)
|
---|
| 633 | now = buf_free;
|
---|
| 634 | else
|
---|
| 635 | now = bytes_left;
|
---|
[bfd247f] | 636 |
|
---|
[ef8bcc6] | 637 | for (i = 0; i < now; i++) {
|
---|
| 638 | b = data[i];
|
---|
| 639 | stream->buf_head[i] = b;
|
---|
[bfd247f] | 640 |
|
---|
| 641 | if ((b == '\n') && (stream->btype == _IOLBF))
|
---|
[ef8bcc6] | 642 | need_flush = true;
|
---|
| 643 | }
|
---|
[bfd247f] | 644 |
|
---|
[0803134] | 645 | data += now;
|
---|
[ef8bcc6] | 646 | stream->buf_head += now;
|
---|
| 647 | buf_free -= now;
|
---|
| 648 | bytes_left -= now;
|
---|
| 649 | total_written += now;
|
---|
[0803134] | 650 | stream->buf_state = _bs_write;
|
---|
[bfd247f] | 651 |
|
---|
[ef8bcc6] | 652 | if (buf_free == 0) {
|
---|
| 653 | /* Only need to drain buffer. */
|
---|
| 654 | _fflushbuf(stream);
|
---|
[6afc9d7] | 655 | if (!stream->error)
|
---|
| 656 | need_flush = false;
|
---|
[ef8bcc6] | 657 | }
|
---|
| 658 | }
|
---|
[facebd56] | 659 |
|
---|
[ef8bcc6] | 660 | if (need_flush)
|
---|
| 661 | fflush(stream);
|
---|
[bfd247f] | 662 |
|
---|
[ef8bcc6] | 663 | return (total_written / size);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
[2595dab] | 666 | int fputc(wchar_t c, FILE *stream)
|
---|
[c9857c6] | 667 | {
|
---|
[56fa418] | 668 | char buf[STR_BOUNDS(1)];
|
---|
[2595dab] | 669 | size_t sz = 0;
|
---|
| 670 |
|
---|
| 671 | if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
|
---|
[2a5af223] | 672 | size_t wr = fwrite(buf, 1, sz, stream);
|
---|
[2595dab] | 673 |
|
---|
| 674 | if (wr < sz)
|
---|
| 675 | return EOF;
|
---|
| 676 |
|
---|
| 677 | return (int) c;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | return EOF;
|
---|
| 681 | }
|
---|
[56fa418] | 682 |
|
---|
[2595dab] | 683 | int putchar(wchar_t c)
|
---|
| 684 | {
|
---|
| 685 | return fputc(c, stdout);
|
---|
| 686 | }
|
---|
[56fa418] | 687 |
|
---|
[2595dab] | 688 | int fputs(const char *str, FILE *stream)
|
---|
| 689 | {
|
---|
[7db5cfd] | 690 | (void) fwrite(str, str_size(str), 1, stream);
|
---|
| 691 | if (ferror(stream))
|
---|
| 692 | return EOF;
|
---|
| 693 | return 0;
|
---|
[2595dab] | 694 | }
|
---|
[56fa418] | 695 |
|
---|
[2595dab] | 696 | int puts(const char *str)
|
---|
| 697 | {
|
---|
| 698 | return fputs(str, stdout);
|
---|
[c9857c6] | 699 | }
|
---|
[b27a97bb] | 700 |
|
---|
[2595dab] | 701 | int fgetc(FILE *stream)
|
---|
[b27a97bb] | 702 | {
|
---|
[2595dab] | 703 | char c;
|
---|
[bfd247f] | 704 |
|
---|
[bac82eeb] | 705 | /* This could be made faster by only flushing when needed. */
|
---|
[b8e57e8c] | 706 | if (stdout)
|
---|
| 707 | fflush(stdout);
|
---|
| 708 | if (stderr)
|
---|
| 709 | fflush(stderr);
|
---|
[bfd247f] | 710 |
|
---|
[2595dab] | 711 | if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
|
---|
| 712 | return EOF;
|
---|
[b27a97bb] | 713 |
|
---|
[2595dab] | 714 | return (int) c;
|
---|
| 715 | }
|
---|
| 716 |
|
---|
[c62d2e1] | 717 | char *fgets(char *str, int size, FILE *stream)
|
---|
| 718 | {
|
---|
[a634485] | 719 | int c;
|
---|
[c62d2e1] | 720 | int idx;
|
---|
| 721 |
|
---|
| 722 | idx = 0;
|
---|
| 723 | while (idx < size - 1) {
|
---|
| 724 | c = fgetc(stream);
|
---|
| 725 | if (c == EOF)
|
---|
| 726 | break;
|
---|
| 727 |
|
---|
| 728 | str[idx++] = c;
|
---|
| 729 |
|
---|
| 730 | if (c == '\n')
|
---|
| 731 | break;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | if (ferror(stream))
|
---|
| 735 | return NULL;
|
---|
| 736 |
|
---|
| 737 | if (idx == 0)
|
---|
| 738 | return NULL;
|
---|
| 739 |
|
---|
| 740 | str[idx] = '\0';
|
---|
| 741 | return str;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
[2595dab] | 744 | int getchar(void)
|
---|
| 745 | {
|
---|
| 746 | return fgetc(stdin);
|
---|
[b27a97bb] | 747 | }
|
---|
| 748 |
|
---|
[bd5414e] | 749 | int ungetc(int c, FILE *stream)
|
---|
| 750 | {
|
---|
| 751 | if (c == EOF)
|
---|
| 752 | return EOF;
|
---|
| 753 |
|
---|
| 754 | if (stream->ungetc_chars >= UNGETC_MAX)
|
---|
| 755 | return EOF;
|
---|
| 756 |
|
---|
| 757 | stream->ungetc_buf[stream->ungetc_chars++] =
|
---|
| 758 | (uint8_t)c;
|
---|
| 759 |
|
---|
| 760 | stream->eof = false;
|
---|
| 761 | return (uint8_t)c;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
[ed903174] | 764 | int fseek(FILE *stream, off64_t offset, int whence)
|
---|
[d2cc7e1] | 765 | {
|
---|
[23a0368] | 766 | int rc;
|
---|
| 767 |
|
---|
[6afc9d7] | 768 | if (stream->error)
|
---|
[58898d1d] | 769 | return -1;
|
---|
[6afc9d7] | 770 |
|
---|
[facebd56] | 771 | _fflushbuf(stream);
|
---|
[6afc9d7] | 772 | if (stream->error) {
|
---|
| 773 | /* errno was set by _fflushbuf() */
|
---|
[58898d1d] | 774 | return -1;
|
---|
[6afc9d7] | 775 | }
|
---|
| 776 |
|
---|
[bd5414e] | 777 | stream->ungetc_chars = 0;
|
---|
[facebd56] | 778 |
|
---|
[58898d1d] | 779 | struct stat st;
|
---|
| 780 | switch (whence) {
|
---|
| 781 | case SEEK_SET:
|
---|
| 782 | stream->pos = offset;
|
---|
| 783 | break;
|
---|
| 784 | case SEEK_CUR:
|
---|
| 785 | stream->pos += offset;
|
---|
| 786 | break;
|
---|
| 787 | case SEEK_END:
|
---|
[23a0368] | 788 | rc = vfs_stat(stream->fd, &st);
|
---|
| 789 | if (rc != EOK) {
|
---|
| 790 | errno = rc;
|
---|
[58898d1d] | 791 | stream->error = true;
|
---|
| 792 | return -1;
|
---|
| 793 | }
|
---|
| 794 | stream->pos = st.size + offset;
|
---|
| 795 | break;
|
---|
[2595dab] | 796 | }
|
---|
[facebd56] | 797 |
|
---|
[2595dab] | 798 | stream->eof = false;
|
---|
[566f4cfb] | 799 | return 0;
|
---|
[d2cc7e1] | 800 | }
|
---|
| 801 |
|
---|
[ed903174] | 802 | off64_t ftell(FILE *stream)
|
---|
[08232ee] | 803 | {
|
---|
[6afc9d7] | 804 | if (stream->error)
|
---|
| 805 | return EOF;
|
---|
| 806 |
|
---|
[8ad496d2] | 807 | _fflushbuf(stream);
|
---|
[6afc9d7] | 808 | if (stream->error) {
|
---|
| 809 | /* errno was set by _fflushbuf() */
|
---|
| 810 | return EOF;
|
---|
| 811 | }
|
---|
| 812 |
|
---|
[58898d1d] | 813 | return stream->pos - stream->ungetc_chars;
|
---|
[08232ee] | 814 | }
|
---|
| 815 |
|
---|
[080ad7f] | 816 | void rewind(FILE *stream)
|
---|
| 817 | {
|
---|
| 818 | (void) fseek(stream, 0, SEEK_SET);
|
---|
| 819 | }
|
---|
| 820 |
|
---|
[2595dab] | 821 | int fflush(FILE *stream)
|
---|
| 822 | {
|
---|
[6afc9d7] | 823 | if (stream->error)
|
---|
| 824 | return EOF;
|
---|
| 825 |
|
---|
[ef8bcc6] | 826 | _fflushbuf(stream);
|
---|
[6afc9d7] | 827 | if (stream->error) {
|
---|
| 828 | /* errno was set by _fflushbuf() */
|
---|
| 829 | return EOF;
|
---|
| 830 | }
|
---|
[bfd247f] | 831 |
|
---|
[6fa9a99d] | 832 | if (stream->kio) {
|
---|
| 833 | kio_update();
|
---|
[6afc9d7] | 834 | return 0;
|
---|
[2595dab] | 835 | }
|
---|
| 836 |
|
---|
[79ae36dd] | 837 | if ((stream->fd >= 0) && (stream->need_sync)) {
|
---|
[facebd56] | 838 | /**
|
---|
| 839 | * Better than syncing always, but probably still not the
|
---|
| 840 | * right thing to do.
|
---|
| 841 | */
|
---|
| 842 | stream->need_sync = false;
|
---|
[6afc9d7] | 843 | if (fsync(stream->fd) != 0) {
|
---|
| 844 | /* errno was set by fsync() */
|
---|
| 845 | return EOF;
|
---|
| 846 | }
|
---|
| 847 |
|
---|
| 848 | return 0;
|
---|
[facebd56] | 849 | }
|
---|
[2595dab] | 850 |
|
---|
[6afc9d7] | 851 | return 0;
|
---|
[2595dab] | 852 | }
|
---|
| 853 |
|
---|
| 854 | int feof(FILE *stream)
|
---|
| 855 | {
|
---|
| 856 | return stream->eof;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | int ferror(FILE *stream)
|
---|
| 860 | {
|
---|
| 861 | return stream->error;
|
---|
| 862 | }
|
---|
| 863 |
|
---|
[c77a64f] | 864 | void clearerr(FILE *stream)
|
---|
| 865 | {
|
---|
| 866 | stream->eof = false;
|
---|
| 867 | stream->error = false;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
[3629481] | 870 | int fileno(FILE *stream)
|
---|
| 871 | {
|
---|
[6fa9a99d] | 872 | if (stream->kio) {
|
---|
[3629481] | 873 | errno = EBADF;
|
---|
[6afc9d7] | 874 | return EOF;
|
---|
[3629481] | 875 | }
|
---|
| 876 |
|
---|
| 877 | return stream->fd;
|
---|
| 878 | }
|
---|
| 879 |
|
---|
[6afc9d7] | 880 | async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
|
---|
[2595dab] | 881 | {
|
---|
| 882 | if (stream->fd >= 0) {
|
---|
[79ae36dd] | 883 | if (stream->sess == NULL)
|
---|
[6afc9d7] | 884 | stream->sess = vfs_fd_session(stream->fd, iface);
|
---|
[2595dab] | 885 |
|
---|
[79ae36dd] | 886 | return stream->sess;
|
---|
[2595dab] | 887 | }
|
---|
| 888 |
|
---|
[79ae36dd] | 889 | return NULL;
|
---|
[2595dab] | 890 | }
|
---|
| 891 |
|
---|
[6afc9d7] | 892 | int vfs_fhandle(FILE *stream, int *handle)
|
---|
[2595dab] | 893 | {
|
---|
[7171760] | 894 | if (stream->fd >= 0) {
|
---|
| 895 | *handle = stream->fd;
|
---|
| 896 | return EOK;
|
---|
| 897 | }
|
---|
[a68f737] | 898 |
|
---|
| 899 | return ENOENT;
|
---|
[2595dab] | 900 | }
|
---|
| 901 |
|
---|
[fadd381] | 902 | /** @}
|
---|
[b2951e2] | 903 | */
|
---|