source: mainline/uspace/lib/posix/stdio.c@ 2346e78

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2346e78 was 221afc9e, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

Redefinitions of some libc functions (mainly return value corrections).

  • Property mode set to 100644
File size: 16.8 KB
RevLine 
[09b0b1fb]1/*
2 * Copyright (c) 2011 Jiri Zarevucky
[4f4b4e7]3 * Copyright (c) 2011 Petr Koupy
[09b0b1fb]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file
34 */
35
[491e1ee]36#define LIBPOSIX_INTERNAL
[09b0b1fb]37
[a6d908c1]38/* Has to be first. */
39#include "stdbool.h"
[4f4b4e7]40
[9b1503e]41#include "internal/common.h"
[09b0b1fb]42#include "stdio.h"
[a6d908c1]43
44#include "assert.h"
45#include "errno.h"
[ab547063]46#include "string.h"
[1978a5f]47#include "sys/types.h"
[4f4b4e7]48
[a6d908c1]49#include "libc/io/printf_core.h"
50#include "libc/str.h"
[08053f7]51#include "libc/malloc.h"
[a6d908c1]52
53
[1978a5f]54/* not the best of solutions, but freopen and ungetc will eventually
[4f4b4e7]55 * need to be implemented in libc anyway
56 */
[ab547063]57#include "../c/generic/private/stdio.h"
[09b0b1fb]58
[8b5fb5e]59/** Clears the stream's error and end-of-file indicators.
60 *
[1978a5f]61 * @param stream Stream whose indicators shall be cleared.
[8b5fb5e]62 */
63void posix_clearerr(FILE *stream)
64{
65 stream->error = 0;
66 stream->eof = 0;
67}
68
69/**
[1978a5f]70 * Generate a pathname for the controlling terminal.
[8b5fb5e]71 *
[1978a5f]72 * @param s Allocated buffer to which the pathname shall be put.
73 * @return Either s or static location filled with the requested pathname.
[8b5fb5e]74 */
75char *posix_ctermid(char *s)
76{
77 /* Currently always returns an error value (empty string). */
78 // TODO: return a real terminal path
79
80 static char dummy_path[L_ctermid] = {'\0'};
81
82 if (s == NULL) {
83 return dummy_path;
84 }
85
86 s[0] = '\0';
87 return s;
88}
89
[221afc9e]90/**
91 * Put a string on the stream.
92 *
93 * @param s String to be written.
94 * @param stream Output stream.
95 * @return Non-negative on success, EOF on failure.
96 */
97int posix_fputs(const char *restrict s, FILE *restrict stream)
98{
99 int rc = fputs(s, stream);
100 if (rc == 0) {
101 return EOF;
102 } else {
103 return 0;
104 }
105}
106
[59f799b]107/**
[1978a5f]108 * Push byte back into input stream.
[59f799b]109 *
[1978a5f]110 * @param c Byte to be pushed back.
111 * @param stream Stream to where the byte shall be pushed.
[08053f7]112 * @return Provided byte on success or EOF if not possible.
[59f799b]113 */
114int posix_ungetc(int c, FILE *stream)
115{
[1978a5f]116 uint8_t b = (uint8_t) c;
117
118 bool can_unget =
[46ac986]119 /* Provided character is legal. */
[1978a5f]120 c != EOF &&
[46ac986]121 /* Stream is consistent. */
[1978a5f]122 !stream->error &&
[46ac986]123 /* Stream is buffered. */
[1978a5f]124 stream->btype != _IONBF &&
[46ac986]125 /* Last operation on the stream was a read operation. */
[1978a5f]126 stream->buf_state == _bs_read &&
[46ac986]127 /* Stream buffer is already allocated (i.e. there was already carried
128 * out either write or read operation on the stream). This is probably
129 * redundant check but let's be safe. */
[1978a5f]130 stream->buf != NULL &&
[46ac986]131 /* There is still space in the stream to retreat. POSIX demands the
132 * possibility to unget at least 1 character. It should be always
133 * possible, assuming the last operation on the stream read at least 1
134 * character, because the buffer is refilled in the lazily manner. */
[1978a5f]135 stream->buf_tail > stream->buf;
136
137 if (can_unget) {
138 --stream->buf_tail;
139 stream->buf_tail[0] = b;
140 stream->eof = false;
141 return (int) b;
142 } else {
143 return EOF;
144 }
[59f799b]145}
146
[1978a5f]147/**
[08053f7]148 * Read a stream until the delimiter (or EOF) is encountered.
[1978a5f]149 *
[08053f7]150 * @param lineptr Pointer to the output buffer in which there will be stored
151 * nul-terminated string together with the delimiter (if encountered).
152 * Will be resized if necessary.
153 * @param n Pointer to the size of the output buffer. Will be increased if
154 * necessary.
155 * @param delimiter Delimiter on which to finish reading the stream.
156 * @param stream Input stream.
157 * @return Number of fetched characters (including delimiter if encountered)
158 * or -1 on error (set in errno).
[1978a5f]159 */
[8b5fb5e]160ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
161 int delimiter, FILE *restrict stream)
162{
[08053f7]163 /* Check arguments for sanity. */
164 if (!lineptr || !n) {
165 errno = EINVAL;
166 return -1;
167 }
168
169 size_t alloc_step = 80; /* Buffer size gain during reallocation. */
170 char *pos = *lineptr; /* Next free byte of the output buffer. */
171 size_t cnt = 0; /* Number of fetched characters. */
172 int c = fgetc(stream); /* Current input character. Might be EOF. */
173
174 do {
175 /* Mask EOF as NUL to terminate string. */
176 if (c == EOF) {
177 c = '\0';
178 }
179
180 /* Ensure there is still space left in the buffer. */
181 if (pos == *lineptr + *n) {
182 *lineptr = realloc(*lineptr, *n + alloc_step);
183 if (*lineptr) {
184 pos = *lineptr + *n;
185 *n += alloc_step;
186 } else {
187 errno = ENOMEM;
188 return -1;
189 }
190 }
191
192 /* Store the fetched character. */
193 *pos = c;
194
195 /* Fetch the next character according to the current character. */
196 if (c != '\0') {
197 ++pos;
198 ++cnt;
199 if (c == delimiter) {
200 /* Delimiter was just stored. Provide EOF as the next
201 * character - it will be masked as NUL and output string
202 * will be properly terminated. */
203 c = EOF;
204 } else {
205 /* Neither delimiter nor EOF were encountered. Just fetch
206 * the next character from the stream. */
207 c = fgetc(stream);
208 }
209 }
210 } while (c != '\0');
211
212 if (errno == EOK && cnt > 0) {
213 return cnt;
214 } else {
215 /* Either some error occured or the stream was already at EOF. */
216 return -1;
217 }
[8b5fb5e]218}
219
[1978a5f]220/**
[08053f7]221 * Read a stream until the newline (or EOF) is encountered.
[1978a5f]222 *
[08053f7]223 * @param lineptr Pointer to the output buffer in which there will be stored
224 * nul-terminated string together with the delimiter (if encountered).
225 * Will be resized if necessary.
226 * @param n Pointer to the size of the output buffer. Will be increased if
227 * necessary.
228 * @param stream Input stream.
229 * @return Number of fetched characters (including newline if encountered)
230 * or -1 on error (set in errno).
[1978a5f]231 */
[8b5fb5e]232ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
233 FILE *restrict stream)
234{
[08053f7]235 return posix_getdelim(lineptr, n, '\n', stream);
[8b5fb5e]236}
237
[4f4b4e7]238/**
[1978a5f]239 * Reopen a file stream.
[4f4b4e7]240 *
[1978a5f]241 * @param filename Pathname of a file to be reopened or NULL for changing
242 * the mode of the stream.
243 * @param mode Mode to be used for reopening the file or changing current
244 * mode of the stream.
245 * @param stream Current stream associated with the opened file.
246 * @return On success, either a stream of the reopened file or the provided
247 * stream with a changed mode. NULL otherwise.
[4f4b4e7]248 */
[46ac986]249FILE *posix_freopen(const char *restrict filename,
250 const char *restrict mode, FILE *restrict stream)
[09b0b1fb]251{
[ab547063]252 assert(mode != NULL);
253 assert(stream != NULL);
254
255 if (filename == NULL) {
256 // TODO
257
258 /* print error to stderr as well, to avoid hard to find problems
[4f4b4e7]259 * with buggy apps that expect this to work
260 */
261 fprintf(stderr,
262 "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
263 " libposix does not support that yet, the application may function improperly.\n");
[ab547063]264 errno = ENOTSUP;
265 return NULL;
266 }
267
268 FILE* copy = malloc(sizeof(FILE));
269 if (copy == NULL) {
270 errno = ENOMEM;
271 return NULL;
272 }
273 memcpy(copy, stream, sizeof(FILE));
[4f4b4e7]274 fclose(copy); /* copy is now freed */
[ab547063]275
[4f4b4e7]276 copy = fopen(filename, mode); /* open new stream */
[ab547063]277 if (copy == NULL) {
278 /* fopen() sets errno */
279 return NULL;
280 }
281
282 /* move the new stream to the original location */
283 memcpy(stream, copy, sizeof (FILE));
284 free(copy);
285
286 /* update references in the file list */
287 stream->link.next->prev = &stream->link;
288 stream->link.prev->next = &stream->link;
289
290 return stream;
[09b0b1fb]291}
292
[1978a5f]293/**
294 *
295 * @param buf
296 * @param size
297 * @param mode
298 * @return
299 */
[8b5fb5e]300FILE *posix_fmemopen(void *restrict buf, size_t size,
301 const char *restrict mode)
302{
303 // TODO
304 not_implemented();
305}
306
[1978a5f]307/**
308 *
309 * @param bufp
310 * @param sizep
311 * @return
312 */
[8b5fb5e]313FILE *posix_open_memstream(char **bufp, size_t *sizep)
314{
315 // TODO
316 not_implemented();
317}
318
[4f4b4e7]319/**
[1978a5f]320 * Write error messages to standard error.
[4f4b4e7]321 *
[1978a5f]322 * @param s Error message.
[4f4b4e7]323 */
[09b0b1fb]324void posix_perror(const char *s)
325{
[8b5fb5e]326 if (s == NULL || s[0] == '\0') {
327 fprintf(stderr, "%s\n", posix_strerror(errno));
328 } else {
329 fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
330 }
331}
332
333struct _posix_fpos {
334 off64_t offset;
335};
336
337/** Restores stream a to position previously saved with fgetpos().
338 *
339 * @param stream Stream to restore
340 * @param pos Position to restore
341 * @return Zero on success, non-zero (with errno set) on failure
342 */
343int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
344{
345 return fseek(stream, pos->offset, SEEK_SET);
346}
347
348/** Saves the stream's position for later use by fsetpos().
349 *
350 * @param stream Stream to save
351 * @param pos Place to store the position
352 * @return Zero on success, non-zero (with errno set) on failure
353 */
354int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
355{
356 off64_t ret = ftell(stream);
357 if (ret == -1) {
358 return errno;
359 }
360 pos->offset = ret;
361 return 0;
362}
363
364/**
[1978a5f]365 * Reposition a file-position indicator in a stream.
[8b5fb5e]366 *
[1978a5f]367 * @param stream Stream to seek in.
368 * @param offset Direction and amount of bytes to seek.
369 * @param whence From where to seek.
370 * @return Zero on success, -1 otherwise.
[8b5fb5e]371 */
372int posix_fseek(FILE *stream, long offset, int whence)
373{
374 return fseek(stream, (off64_t) offset, whence);
[09b0b1fb]375}
376
[b08ef1fd]377/**
[1978a5f]378 * Reposition a file-position indicator in a stream.
[b08ef1fd]379 *
[1978a5f]380 * @param stream Stream to seek in.
381 * @param offset Direction and amount of bytes to seek.
382 * @param whence From where to seek.
383 * @return Zero on success, -1 otherwise.
[b08ef1fd]384 */
385int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
386{
[8b5fb5e]387 return fseek(stream, (off64_t) offset, whence);
388}
389
390/**
[1978a5f]391 * Discover current file offset in a stream.
[8b5fb5e]392 *
[1978a5f]393 * @param stream Stream for which the offset shall be retrieved.
394 * @return Current offset or -1 if not possible.
[8b5fb5e]395 */
396long posix_ftell(FILE *stream)
397{
398 return (long) ftell(stream);
[b08ef1fd]399}
400
401/**
[1978a5f]402 * Discover current file offset in a stream.
[b08ef1fd]403 *
[1978a5f]404 * @param stream Stream for which the offset shall be retrieved.
405 * @return Current offset or -1 if not possible.
[b08ef1fd]406 */
407posix_off_t posix_ftello(FILE *stream)
408{
[8b5fb5e]409 return (posix_off_t) ftell(stream);
410}
411
[221afc9e]412/**
413 * Discard prefetched data or write unwritten data.
414 *
415 * @param stream Stream that shall be flushed.
416 * @return Zero on success, EOF on failure.
417 */
418int posix_fflush(FILE *stream)
419{
420 int rc = fflush(stream);
421 if (rc < 0) {
422 errno = -rc;
423 return EOF;
424 } else {
425 return 0;
426 }
427}
428
[1978a5f]429/**
430 * Print formatted output to the opened file.
431 *
432 * @param fildes File descriptor of the opened file.
433 * @param format Format description.
434 * @return Either the number of printed characters or negative value on error.
435 */
[8b5fb5e]436int posix_dprintf(int fildes, const char *restrict format, ...)
437{
438 va_list list;
439 va_start(list, format);
440 int result = posix_vdprintf(fildes, format, list);
441 va_end(list);
442 return result;
443}
444
[1978a5f]445/**
446 * Write ordinary string to the opened file.
447 *
448 * @param str String to be written.
449 * @param size Size of the string (in bytes)..
450 * @param fd File descriptor of the opened file.
451 * @return The number of written characters.
452 */
[8b5fb5e]453static int _dprintf_str_write(const char *str, size_t size, void *fd)
454{
455 ssize_t wr = write(*(int *) fd, str, size);
456 return str_nlength(str, wr);
457}
458
[1978a5f]459/**
460 * Write wide string to the opened file.
461 *
462 * @param str String to be written.
463 * @param size Size of the string (in bytes).
464 * @param fd File descriptor of the opened file.
465 * @return The number of written characters.
466 */
[8b5fb5e]467static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
468{
469 size_t offset = 0;
470 size_t chars = 0;
471 size_t sz;
472 char buf[4];
473
474 while (offset < size) {
475 sz = 0;
476 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
477 break;
478 }
479
480 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
481 break;
482 }
483
484 chars++;
485 offset += sizeof(wchar_t);
486 }
487
488 return chars;
489}
490
[1978a5f]491/**
492 * Print formatted output to the opened file.
493 *
494 * @param fildes File descriptor of the opened file.
495 * @param format Format description.
496 * @param ap Print arguments.
497 * @return Either the number of printed characters or negative value on error.
498 */
[8b5fb5e]499int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
500{
501 printf_spec_t spec = {
502 .str_write = _dprintf_str_write,
503 .wstr_write = _dprintf_wstr_write,
504 .data = &fildes
505 };
506
507 return printf_core(format, &spec, ap);
[b08ef1fd]508}
509
[59f799b]510/**
[1978a5f]511 * Print formatted output to the string.
[59f799b]512 *
[1978a5f]513 * @param s Output string.
514 * @param format Format description.
515 * @return Either the number of printed characters (excluding null byte) or
516 * negative value on error.
[59f799b]517 */
[08053f7]518int posix_sprintf(char *s, const char *restrict format, ...)
[59f799b]519{
[8b5fb5e]520 va_list list;
521 va_start(list, format);
522 int result = posix_vsprintf(s, format, list);
523 va_end(list);
524 return result;
[59f799b]525}
526
[823a929]527/**
[1978a5f]528 * Print formatted output to the string.
[823a929]529 *
[1978a5f]530 * @param s Output string.
531 * @param format Format description.
532 * @param ap Print arguments.
533 * @return Either the number of printed characters (excluding null byte) or
534 * negative value on error.
[823a929]535 */
[08053f7]536int posix_vsprintf(char *s, const char *restrict format, va_list ap)
[823a929]537{
[8b5fb5e]538 return vsnprintf(s, STR_NO_LIMIT, format, ap);
539}
540
541/**
[1978a5f]542 * Convert formatted input from the stream.
[8b5fb5e]543 *
[1978a5f]544 * @param stream Input stream.
545 * @param format Format description.
546 * @return The number of converted output items or EOF on failure.
[8b5fb5e]547 */
548int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
549{
550 va_list list;
551 va_start(list, format);
552 int result = posix_vfscanf(stream, format, list);
553 va_end(list);
554 return result;
555}
556
557/**
[1978a5f]558 * Convert formatted input from the standard input.
[8b5fb5e]559 *
[1978a5f]560 * @param format Format description.
561 * @return The number of converted output items or EOF on failure.
[8b5fb5e]562 */
563int posix_scanf(const char *restrict format, ...)
564{
565 va_list list;
566 va_start(list, format);
567 int result = posix_vscanf(format, list);
568 va_end(list);
569 return result;
570}
571
572/**
[1978a5f]573 * Convert formatted input from the standard input.
[8b5fb5e]574 *
[1978a5f]575 * @param format Format description.
576 * @param arg Output items.
577 * @return The number of converted output items or EOF on failure.
[8b5fb5e]578 */
579int posix_vscanf(const char *restrict format, va_list arg)
580{
581 return posix_vfscanf(stdin, format, arg);
582}
583
[59f799b]584/**
[1978a5f]585 * Convert formatted input from the string.
[59f799b]586 *
[1978a5f]587 * @param s Input string.
588 * @param format Format description.
589 * @return The number of converted output items or EOF on failure.
[59f799b]590 */
[08053f7]591int posix_sscanf(const char *restrict s, const char *restrict format, ...)
[8b5fb5e]592{
593 va_list list;
594 va_start(list, format);
595 int result = posix_vsscanf(s, format, list);
596 va_end(list);
597 return result;
598}
599
[1978a5f]600/**
601 * Acquire file stream for the thread.
602 *
603 * @param file File stream to lock.
604 */
[8b5fb5e]605void posix_flockfile(FILE *file)
606{
607 /* dummy */
608}
609
[1978a5f]610/**
611 * Acquire file stream for the thread (non-blocking).
612 *
613 * @param file File stream to lock.
614 * @return Zero for success and non-zero if the lock cannot be acquired.
615 */
[8b5fb5e]616int posix_ftrylockfile(FILE *file)
617{
618 /* dummy */
619 return 0;
620}
621
[1978a5f]622/**
623 * Relinquish the ownership of the locked file stream.
624 *
625 * @param file File stream to unlock.
626 */
[8b5fb5e]627void posix_funlockfile(FILE *file)
628{
629 /* dummy */
630}
631
[1978a5f]632/**
633 * Get a byte from a stream (thread-unsafe).
634 *
635 * @param stream Input file stream.
636 * @return Either read byte or EOF.
637 */
[8b5fb5e]638int posix_getc_unlocked(FILE *stream)
639{
640 return getc(stream);
641}
642
[1978a5f]643/**
644 * Get a byte from the standard input stream (thread-unsafe).
645 *
646 * @return Either read byte or EOF.
647 */
[8b5fb5e]648int posix_getchar_unlocked(void)
649{
650 return getchar();
651}
652
[1978a5f]653/**
654 * Put a byte on a stream (thread-unsafe).
655 *
656 * @param c Byte to output.
657 * @param stream Output file stream.
658 * @return Either written byte or EOF.
659 */
[8b5fb5e]660int posix_putc_unlocked(int c, FILE *stream)
661{
662 return putc(c, stream);
663}
664
[1978a5f]665/**
666 * Put a byte on the standard output stream (thread-unsafe).
667 *
668 * @param c Byte to output.
669 * @return Either written byte or EOF.
670 */
[8b5fb5e]671int posix_putchar_unlocked(int c)
672{
673 return putchar(c);
674}
675
[823a929]676/**
[1978a5f]677 * Remove a file.
[823a929]678 *
[1978a5f]679 * @param path Pathname of the file that shall be removed.
680 * @return Zero on success, -1 otherwise.
[823a929]681 */
682int posix_remove(const char *path)
683{
[8b5fb5e]684 // FIXME: unlink() and rmdir() seem to be equivalent at the moment,
685 // but that does not have to be true forever
686 return unlink(path);
[823a929]687}
688
689/**
690 *
691 * @param s
692 * @return
693 */
694char *posix_tmpnam(char *s)
695{
696 // TODO: low priority, just a compile-time dependency of binutils
697 not_implemented();
698}
699
[09b0b1fb]700/** @}
701 */
Note: See TracBrowser for help on using the repository browser.