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