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