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