source: mainline/uspace/lib/libc/generic/io/io.c@ 153fbf9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 153fbf9 was c62d2e1, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Implement fgets().

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[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>
[56fa418]39#include <string.h>
40#include <errno.h>
[2595dab]41#include <bool.h>
42#include <malloc.h>
43#include <io/klog.h>
44#include <vfs/vfs.h>
45#include <ipc/devmap.h>
[d9c8c81]46#include <adt/list.h>
[b861b58]47
[ef8bcc6]48static void _fflushbuf(FILE *stream);
49
[a68f737]50static FILE stdin_null = {
[2595dab]51 .fd = -1,
52 .error = true,
53 .eof = true,
54 .klog = false,
[ef8bcc6]55 .phone = -1,
56 .btype = _IONBF,
57 .buf = NULL,
58 .buf_size = 0,
59 .buf_head = NULL
[2595dab]60};
[350514c]61
[a68f737]62static FILE stdout_klog = {
[2595dab]63 .fd = -1,
64 .error = false,
65 .eof = false,
66 .klog = true,
[ef8bcc6]67 .phone = -1,
68 .btype = _IOLBF,
69 .buf = NULL,
70 .buf_size = BUFSIZ,
71 .buf_head = NULL
[2595dab]72};
73
[a68f737]74static FILE stderr_klog = {
75 .fd = -1,
76 .error = false,
77 .eof = false,
78 .klog = true,
[ef8bcc6]79 .phone = -1,
80 .btype = _IONBF,
81 .buf = NULL,
82 .buf_size = 0,
83 .buf_head = NULL
[a68f737]84};
85
86FILE *stdin = NULL;
87FILE *stdout = NULL;
88FILE *stderr = NULL;
89
90static LIST_INITIALIZE(files);
91
[db24058]92void __stdio_init(int filc, fdi_node_t *filv[])
[a68f737]93{
94 if (filc > 0) {
95 stdin = fopen_node(filv[0], "r");
96 } else {
97 stdin = &stdin_null;
98 list_append(&stdin->link, &files);
99 }
100
101 if (filc > 1) {
102 stdout = fopen_node(filv[1], "w");
103 } else {
104 stdout = &stdout_klog;
105 list_append(&stdout->link, &files);
106 }
107
108 if (filc > 2) {
109 stderr = fopen_node(filv[2], "w");
110 } else {
111 stderr = &stderr_klog;
112 list_append(&stderr->link, &files);
113 }
114}
115
[db24058]116void __stdio_done(void)
[a68f737]117{
118 link_t *link = files.next;
119
120 while (link != &files) {
121 FILE *file = list_get_instance(link, FILE, link);
122 fclose(file);
123 link = files.next;
124 }
125}
[2595dab]126
127static bool parse_mode(const char *mode, int *flags)
[b861b58]128{
[2595dab]129 /* Parse mode except first character. */
130 const char *mp = mode;
131 if (*mp++ == 0) {
132 errno = EINVAL;
133 return false;
134 }
[cc6f688]135
[2595dab]136 if ((*mp == 'b') || (*mp == 't'))
137 mp++;
[c9857c6]138
[2595dab]139 bool plus;
140 if (*mp == '+') {
141 mp++;
142 plus = true;
143 } else
144 plus = false;
[566f4cfb]145
[2595dab]146 if (*mp != 0) {
147 errno = EINVAL;
148 return false;
[350514c]149 }
150
[2595dab]151 /* Parse first character of mode and determine flags for open(). */
152 switch (mode[0]) {
153 case 'r':
154 *flags = plus ? O_RDWR : O_RDONLY;
155 break;
156 case 'w':
157 *flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
158 break;
159 case 'a':
160 /* TODO: a+ must read from beginning, append to the end. */
161 if (plus) {
162 errno = ENOTSUP;
163 return false;
164 }
165 *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
166 default:
167 errno = EINVAL;
168 return false;
169 }
170
171 return true;
[cc6f688]172}
[b861b58]173
[ef8bcc6]174/** Set stream buffer. */
175void setvbuf(FILE *stream, void *buf, int mode, size_t size)
176{
177 stream->btype = mode;
178 stream->buf = buf;
179 stream->buf_size = size;
180 stream->buf_head = stream->buf;
181}
182
[bfd247f]183static void _setvbuf(FILE *stream)
184{
185 /* FIXME: Use more complex rules for setting buffering options. */
186
187 switch (stream->fd) {
188 case 1:
189 setvbuf(stream, NULL, _IOLBF, BUFSIZ);
190 break;
191 case 0:
192 case 2:
193 setvbuf(stream, NULL, _IONBF, 0);
194 break;
195 default:
196 setvbuf(stream, NULL, _IOFBF, BUFSIZ);
197 }
198}
199
[ef8bcc6]200/** Allocate stream buffer. */
201static int _fallocbuf(FILE *stream)
202{
203 assert(stream->buf == NULL);
[bfd247f]204
[ef8bcc6]205 stream->buf = malloc(stream->buf_size);
206 if (stream->buf == NULL) {
207 errno = ENOMEM;
208 return -1;
209 }
[bfd247f]210
[ef8bcc6]211 stream->buf_head = stream->buf;
212 return 0;
213}
214
[2595dab]215/** Open a stream.
216 *
217 * @param path Path of the file to open.
218 * @param mode Mode string, (r|w|a)[b|t][+].
219 *
[4e2cf8b]220 */
[2595dab]221FILE *fopen(const char *path, const char *mode)
[4e2cf8b]222{
[2595dab]223 int flags;
224 if (!parse_mode(mode, &flags))
225 return NULL;
[4e2cf8b]226
[2595dab]227 /* Open file. */
228 FILE *stream = malloc(sizeof(FILE));
229 if (stream == NULL) {
230 errno = ENOMEM;
231 return NULL;
232 }
233
234 stream->fd = open(path, flags, 0666);
235 if (stream->fd < 0) {
236 /* errno was set by open() */
237 free(stream);
238 return NULL;
239 }
240
241 stream->error = false;
242 stream->eof = false;
243 stream->klog = false;
244 stream->phone = -1;
[bfd247f]245 _setvbuf(stream);
[2595dab]246
[a68f737]247 list_append(&stream->link, &files);
248
[2595dab]249 return stream;
250}
251
[080ad7f]252FILE *fdopen(int fd, const char *mode)
253{
254 /* Open file. */
255 FILE *stream = malloc(sizeof(FILE));
256 if (stream == NULL) {
257 errno = ENOMEM;
258 return NULL;
259 }
260
261 stream->fd = fd;
262 stream->error = false;
263 stream->eof = false;
264 stream->klog = false;
265 stream->phone = -1;
[bfd247f]266 _setvbuf(stream);
[080ad7f]267
268 list_append(&stream->link, &files);
269
270 return stream;
271}
272
[99272a3]273FILE *fopen_node(fdi_node_t *node, const char *mode)
[2595dab]274{
275 int flags;
276 if (!parse_mode(mode, &flags))
277 return NULL;
278
279 /* Open file. */
280 FILE *stream = malloc(sizeof(FILE));
281 if (stream == NULL) {
282 errno = ENOMEM;
283 return NULL;
284 }
285
286 stream->fd = open_node(node, flags);
287 if (stream->fd < 0) {
288 /* errno was set by open_node() */
289 free(stream);
290 return NULL;
291 }
292
293 stream->error = false;
294 stream->eof = false;
295 stream->klog = false;
296 stream->phone = -1;
[bfd247f]297 _setvbuf(stream);
[2595dab]298
[a68f737]299 list_append(&stream->link, &files);
300
[2595dab]301 return stream;
302}
303
304int fclose(FILE *stream)
305{
306 int rc = 0;
307
308 fflush(stream);
309
310 if (stream->phone >= 0)
311 ipc_hangup(stream->phone);
312
313 if (stream->fd >= 0)
314 rc = close(stream->fd);
315
[a68f737]316 list_remove(&stream->link);
317
318 if ((stream != &stdin_null)
319 && (stream != &stdout_klog)
320 && (stream != &stderr_klog))
[2595dab]321 free(stream);
322
323 stream = NULL;
324
325 if (rc != 0) {
326 /* errno was set by close() */
327 return EOF;
328 }
329
330 return 0;
[4e2cf8b]331}
332
[2595dab]333/** Read from a stream.
334 *
335 * @param buf Destination buffer.
336 * @param size Size of each record.
337 * @param nmemb Number of records to read.
338 * @param stream Pointer to the stream.
[4e2cf8b]339 *
340 */
[2595dab]341size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]342{
[002252a]343 size_t left, done;
344
345 if (size == 0 || nmemb == 0)
346 return 0;
347
[ef8bcc6]348 /* Make sure no data is pending write. */
349 _fflushbuf(stream);
[002252a]350
351 left = size * nmemb;
352 done = 0;
[bfd247f]353
[2595dab]354 while ((left > 0) && (!stream->error) && (!stream->eof)) {
355 ssize_t rd = read(stream->fd, buf + done, left);
356
357 if (rd < 0)
358 stream->error = true;
359 else if (rd == 0)
360 stream->eof = true;
361 else {
362 left -= rd;
363 done += rd;
364 }
365 }
[4e2cf8b]366
[2595dab]367 return (done / size);
368}
[55cff86]369
[ef8bcc6]370static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]371{
[002252a]372 size_t left;
373 size_t done;
374
375 if (size == 0 || nmemb == 0)
376 return 0;
377
378 left = size * nmemb;
379 done = 0;
380
[2595dab]381 while ((left > 0) && (!stream->error)) {
382 ssize_t wr;
383
384 if (stream->klog)
385 wr = klog_write(buf + done, left);
386 else
387 wr = write(stream->fd, buf + done, left);
388
389 if (wr <= 0)
390 stream->error = true;
391 else {
392 left -= wr;
393 done += wr;
394 }
395 }
396
397 return (done / size);
[4e2cf8b]398}
399
[ef8bcc6]400/** Drain stream buffer, do not sync stream. */
401static void _fflushbuf(FILE *stream)
402{
403 size_t bytes_used;
[bfd247f]404
405 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
[ef8bcc6]406 return;
[bfd247f]407
[ef8bcc6]408 bytes_used = stream->buf_head - stream->buf;
409 if (bytes_used == 0)
410 return;
[bfd247f]411
[ef8bcc6]412 (void) _fwrite(stream->buf, 1, bytes_used, stream);
413 stream->buf_head = stream->buf;
414}
415
416/** Write to a stream.
417 *
418 * @param buf Source buffer.
419 * @param size Size of each record.
420 * @param nmemb Number of records to write.
421 * @param stream Pointer to the stream.
422 *
423 */
424size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
425{
426 uint8_t *data;
427 size_t bytes_left;
428 size_t now;
429 size_t buf_free;
430 size_t total_written;
431 size_t i;
432 uint8_t b;
433 bool need_flush;
[002252a]434
435 if (size == 0 || nmemb == 0)
436 return 0;
437
[ef8bcc6]438 /* If not buffered stream, write out directly. */
[bfd247f]439 if (stream->btype == _IONBF) {
440 now = _fwrite(buf, size, nmemb, stream);
441 fflush(stream);
442 return now;
443 }
444
[ef8bcc6]445 /* Perform lazy allocation of stream buffer. */
446 if (stream->buf == NULL) {
447 if (_fallocbuf(stream) != 0)
448 return 0; /* Errno set by _fallocbuf(). */
449 }
[bfd247f]450
[ef8bcc6]451 data = (uint8_t *) buf;
452 bytes_left = size * nmemb;
453 total_written = 0;
454 need_flush = false;
[bfd247f]455
456 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]457 buf_free = stream->buf_size - (stream->buf_head - stream->buf);
458 if (bytes_left > buf_free)
459 now = buf_free;
460 else
461 now = bytes_left;
[bfd247f]462
[ef8bcc6]463 for (i = 0; i < now; i++) {
464 b = data[i];
465 stream->buf_head[i] = b;
[bfd247f]466
467 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]468 need_flush = true;
469 }
[bfd247f]470
[ef8bcc6]471 buf += now;
472 stream->buf_head += now;
473 buf_free -= now;
474 bytes_left -= now;
475 total_written += now;
[bfd247f]476
[ef8bcc6]477 if (buf_free == 0) {
478 /* Only need to drain buffer. */
479 _fflushbuf(stream);
480 need_flush = false;
481 }
482 }
[bfd247f]483
[ef8bcc6]484 if (need_flush)
485 fflush(stream);
[bfd247f]486
[ef8bcc6]487 return (total_written / size);
488}
489
[2595dab]490int fputc(wchar_t c, FILE *stream)
[c9857c6]491{
[56fa418]492 char buf[STR_BOUNDS(1)];
[2595dab]493 size_t sz = 0;
494
495 if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
[2a5af223]496 size_t wr = fwrite(buf, 1, sz, stream);
[2595dab]497
498 if (wr < sz)
499 return EOF;
500
501 return (int) c;
502 }
503
504 return EOF;
505}
[56fa418]506
[2595dab]507int putchar(wchar_t c)
508{
509 return fputc(c, stdout);
510}
[56fa418]511
[2595dab]512int fputs(const char *str, FILE *stream)
513{
514 return fwrite(str, str_size(str), 1, stream);
515}
[56fa418]516
[2595dab]517int puts(const char *str)
518{
519 return fputs(str, stdout);
[c9857c6]520}
[b27a97bb]521
[2595dab]522int fgetc(FILE *stream)
[b27a97bb]523{
[2595dab]524 char c;
[bfd247f]525
[bac82eeb]526 /* This could be made faster by only flushing when needed. */
[b8e57e8c]527 if (stdout)
528 fflush(stdout);
529 if (stderr)
530 fflush(stderr);
[bfd247f]531
[2595dab]532 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
533 return EOF;
[b27a97bb]534
[2595dab]535 return (int) c;
536}
537
[c62d2e1]538char *fgets(char *str, int size, FILE *stream)
539{
540 char c;
541 int idx;
542
543 idx = 0;
544 while (idx < size - 1) {
545 c = fgetc(stream);
546 if (c == EOF)
547 break;
548
549 str[idx++] = c;
550
551 if (c == '\n')
552 break;
553 }
554
555 if (ferror(stream))
556 return NULL;
557
558 if (idx == 0)
559 return NULL;
560
561 str[idx] = '\0';
562 return str;
563}
564
[2595dab]565int getchar(void)
566{
567 return fgetc(stdin);
[b27a97bb]568}
569
[ed903174]570int fseek(FILE *stream, off64_t offset, int whence)
[d2cc7e1]571{
[ed903174]572 off64_t rc = lseek(stream->fd, offset, whence);
573 if (rc == (off64_t) (-1)) {
574 /* errno has been set by lseek64. */
[2595dab]575 return -1;
576 }
577
578 stream->eof = false;
579
[566f4cfb]580 return 0;
[d2cc7e1]581}
582
[ed903174]583off64_t ftell(FILE *stream)
[08232ee]584{
[ed903174]585 return lseek(stream->fd, 0, SEEK_CUR);
[08232ee]586}
587
[080ad7f]588void rewind(FILE *stream)
589{
590 (void) fseek(stream, 0, SEEK_SET);
591}
592
[2595dab]593int fflush(FILE *stream)
594{
[ef8bcc6]595 _fflushbuf(stream);
[bfd247f]596
[2595dab]597 if (stream->klog) {
598 klog_update();
599 return EOK;
600 }
601
602 if (stream->fd >= 0)
603 return fsync(stream->fd);
604
605 return ENOENT;
606}
607
608int feof(FILE *stream)
609{
610 return stream->eof;
611}
612
613int ferror(FILE *stream)
614{
615 return stream->error;
616}
617
[c77a64f]618void clearerr(FILE *stream)
619{
620 stream->eof = false;
621 stream->error = false;
622}
623
[2595dab]624int fphone(FILE *stream)
625{
626 if (stream->fd >= 0) {
627 if (stream->phone < 0)
628 stream->phone = fd_phone(stream->fd);
629
630 return stream->phone;
631 }
632
633 return -1;
634}
635
[a68f737]636int fnode(FILE *stream, fdi_node_t *node)
[2595dab]637{
[a68f737]638 if (stream->fd >= 0)
639 return fd_node(stream->fd, node);
640
641 return ENOENT;
[2595dab]642}
643
[fadd381]644/** @}
[b2951e2]645 */
Note: See TracBrowser for help on using the repository browser.