Changeset 1978a5f in mainline


Ignore:
Timestamp:
2011-07-01T23:39:27Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
557a747
Parents:
fb872c1
Message:

Implemented ungetc() and commented some functions from stdio.c.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/stdio.c

    rfb872c1 r1978a5f  
    3838#include <assert.h>
    3939#include <errno.h>
     40#include <bool.h>
    4041
    4142#include "internal/common.h"
     
    4445#include "string.h"
    4546#include "libc/str.h"
    46 
    47 /* not the best of solutions, but freopen will eventually
     47#include "sys/types.h"
     48
     49/* not the best of solutions, but freopen and ungetc will eventually
    4850 * need to be implemented in libc anyway
    4951 */
     
    5254/** Clears the stream's error and end-of-file indicators.
    5355 *
    54  * @param stream
     56 * @param stream Stream whose indicators shall be cleared.
    5557 */
    5658void posix_clearerr(FILE *stream)
     
    6163
    6264/**
    63  *
    64  * @param s
    65  * @return
     65 * Generate a pathname for the controlling terminal.
     66 *
     67 * @param s Allocated buffer to which the pathname shall be put.
     68 * @return Either s or static location filled with the requested pathname.
    6669 */
    6770char *posix_ctermid(char *s)
     
    8184
    8285/**
    83  *
    84  * @param c
     86 * Push byte back into input stream.
     87 *
     88 * @param c Byte to be pushed back.
     89 * @param stream Stream to where the byte shall be pushed.
     90 * @return Provided byte on succes or EOF if not possible.
     91 */
     92int posix_ungetc(int c, FILE *stream)
     93{
     94        uint8_t b = (uint8_t) c;
     95
     96        bool can_unget =
     97                /* Provided character is legal. */
     98            c != EOF &&
     99                /* Stream is consistent. */
     100            !stream->error &&
     101                /* Stream is buffered. */
     102            stream->btype != _IONBF &&
     103                /* Last operation on the stream was a read operation. */
     104            stream->buf_state == _bs_read &&
     105                /* Stream buffer is already allocated (i.e. there was already carried
     106                 * out either write or read operation on the stream). This is probably
     107                 * redundant check but let's be safe. */
     108            stream->buf != NULL &&
     109                /* There is still space in the stream to retreat. POSIX demands the
     110                 * possibility to unget at least 1 character. It should be always
     111                 * possible, assuming the last operation on the stream read at least 1
     112                 * character, because the buffer is refilled in the lazily manner. */
     113            stream->buf_tail > stream->buf;
     114
     115        if (can_unget) {
     116                --stream->buf_tail;
     117                stream->buf_tail[0] = b;
     118                stream->eof = false;
     119                return (int) b;
     120        } else {
     121                return EOF;
     122        }
     123}
     124
     125/**
     126 *
     127 * @param lineptr
     128 * @param n
     129 * @param delimiter
    85130 * @param stream
    86  * @return
    87  */
    88 int posix_ungetc(int c, FILE *stream)
     131 * @return
     132 */
     133ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
     134    int delimiter, FILE *restrict stream)
    89135{
    90136        // TODO
     
    92138}
    93139
    94 ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
    95     int delimiter, FILE *restrict stream)
     140/**
     141 *
     142 * @param lineptr
     143 * @param n
     144 * @param stream
     145 * @return
     146 */
     147ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
     148    FILE *restrict stream)
    96149{
    97150        // TODO
     
    99152}
    100153
    101 ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
    102     FILE *restrict stream)
    103 {
    104         // TODO
    105         not_implemented();
    106 }
    107 
    108 /**
    109  *
    110  * @param filename
    111  * @param mode
    112  * @param stream
    113  * @return
     154/**
     155 * Reopen a file stream.
     156 *
     157 * @param filename Pathname of a file to be reopened or NULL for changing
     158 *     the mode of the stream.
     159 * @param mode Mode to be used for reopening the file or changing current
     160 *     mode of the stream.
     161 * @param stream Current stream associated with the opened file.
     162 * @return On success, either a stream of the reopened file or the provided
     163 *     stream with a changed mode. NULL otherwise.
    114164 */
    115165FILE *posix_freopen(
     
    159209}
    160210
     211/**
     212 *
     213 * @param buf
     214 * @param size
     215 * @param mode
     216 * @return
     217 */
    161218FILE *posix_fmemopen(void *restrict buf, size_t size,
    162219    const char *restrict mode)
     
    166223}
    167224
     225/**
     226 *
     227 * @param bufp
     228 * @param sizep
     229 * @return
     230 */
    168231FILE *posix_open_memstream(char **bufp, size_t *sizep)
    169232{
     
    173236
    174237/**
    175  *
    176  * @param s
     238 * Write error messages to standard error.
     239 *
     240 * @param s Error message.
    177241 */
    178242void posix_perror(const char *s)
     
    217281
    218282/**
    219  *
    220  * @param stream
    221  * @param offset
    222  * @param whence
    223  * @return
     283 * Reposition a file-position indicator in a stream.
     284 *
     285 * @param stream Stream to seek in.
     286 * @param offset Direction and amount of bytes to seek.
     287 * @param whence From where to seek.
     288 * @return Zero on success, -1 otherwise.
    224289 */
    225290int posix_fseek(FILE *stream, long offset, int whence)
     
    229294
    230295/**
    231  *
    232  * @param stream
    233  * @param offset
    234  * @param whence
    235  * @return
     296 * Reposition a file-position indicator in a stream.
     297 *
     298 * @param stream Stream to seek in.
     299 * @param offset Direction and amount of bytes to seek.
     300 * @param whence From where to seek.
     301 * @return Zero on success, -1 otherwise.
    236302 */
    237303int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
     
    241307
    242308/**
    243  *
    244  * @param stream
    245  * @return
     309 * Discover current file offset in a stream.
     310 *
     311 * @param stream Stream for which the offset shall be retrieved.
     312 * @return Current offset or -1 if not possible.
    246313 */
    247314long posix_ftell(FILE *stream)
     
    251318
    252319/**
    253  *
    254  * @param stream
    255  * @return
     320 * Discover current file offset in a stream.
     321 *
     322 * @param stream Stream for which the offset shall be retrieved.
     323 * @return Current offset or -1 if not possible.
    256324 */
    257325posix_off_t posix_ftello(FILE *stream)
     
    260328}
    261329
     330/**
     331 * Print formatted output to the opened file.
     332 *
     333 * @param fildes File descriptor of the opened file.
     334 * @param format Format description.
     335 * @return Either the number of printed characters or negative value on error.
     336 */
    262337int posix_dprintf(int fildes, const char *restrict format, ...)
    263338{
     
    269344}
    270345
     346/**
     347 * Write ordinary string to the opened file.
     348 *
     349 * @param str String to be written.
     350 * @param size Size of the string (in bytes)..
     351 * @param fd File descriptor of the opened file.
     352 * @return The number of written characters.
     353 */
    271354static int _dprintf_str_write(const char *str, size_t size, void *fd)
    272355{
     
    275358}
    276359
     360/**
     361 * Write wide string to the opened file.
     362 *
     363 * @param str String to be written.
     364 * @param size Size of the string (in bytes).
     365 * @param fd File descriptor of the opened file.
     366 * @return The number of written characters.
     367 */
    277368static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
    278369{
     
    299390}
    300391
     392/**
     393 * Print formatted output to the opened file.
     394 *
     395 * @param fildes File descriptor of the opened file.
     396 * @param format Format description.
     397 * @param ap Print arguments.
     398 * @return Either the number of printed characters or negative value on error.
     399 */
    301400int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
    302401{
     
    311410
    312411/**
    313  *
    314  * @param s
    315  * @param format
    316  * @param ...
    317  * @return
     412 * Print formatted output to the string.
     413 *
     414 * @param s Output string.
     415 * @param format Format description.
     416 * @return Either the number of printed characters (excluding null byte) or
     417 *     negative value on error.
    318418 */
    319419int posix_sprintf(char *s, const char *format, ...)
     
    327427
    328428/**
    329  *
    330  * @param s
    331  * @param format
    332  * @param ap
    333  * @return
     429 * Print formatted output to the string.
     430 *
     431 * @param s Output string.
     432 * @param format Format description.
     433 * @param ap Print arguments.
     434 * @return Either the number of printed characters (excluding null byte) or
     435 *     negative value on error.
    334436 */
    335437int posix_vsprintf(char *s, const char *format, va_list ap)
     
    339441
    340442/**
    341  *
    342  * @param stream
    343  * @param format
    344  * @param ...
    345  * @return
     443 * Convert formatted input from the stream.
     444 *
     445 * @param stream Input stream.
     446 * @param format Format description.
     447 * @return The number of converted output items or EOF on failure.
    346448 */
    347449int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
     
    354456}
    355457
     458/**
     459 * Convert formatted input from the stream.
     460 *
     461 * @param stream Input stream.
     462 * @param format Format description.
     463 * @param arg Output items.
     464 * @return The number of converted output items or EOF on failure.
     465 */
    356466int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg)
    357467{
     
    361471
    362472/**
    363  *
    364  * @param format
    365  * @param ...
    366  * @return
     473 * Convert formatted input from the standard input.
     474 *
     475 * @param format Format description.
     476 * @return The number of converted output items or EOF on failure.
    367477 */
    368478int posix_scanf(const char *restrict format, ...)
     
    376486
    377487/**
    378  *
    379  * @param format
    380  * @param arg
    381  * @return
     488 * Convert formatted input from the standard input.
     489 *
     490 * @param format Format description.
     491 * @param arg Output items.
     492 * @return The number of converted output items or EOF on failure.
    382493 */
    383494int posix_vscanf(const char *restrict format, va_list arg)
     
    387498
    388499/**
    389  *
    390  * @param s
    391  * @param format
    392  * @param ...
    393  * @return
     500 * Convert formatted input from the string.
     501 *
     502 * @param s Input string.
     503 * @param format Format description.
     504 * @return The number of converted output items or EOF on failure.
    394505 */
    395506int posix_sscanf(const char *s, const char *format, ...)
     
    403514
    404515/**
    405  *
    406  * @param s
    407  * @param format
    408  * @param arg
    409  * @return
     516 * Convert formatted input from the string.
     517 *
     518 * @param s Input string.
     519 * @param format Format description.
     520 * @param arg Output items.
     521 * @return The number of converted output items or EOF on failure.
    410522 */
    411523int posix_vsscanf(
     
    416528}
    417529
     530/**
     531 * Acquire file stream for the thread.
     532 *
     533 * @param file File stream to lock.
     534 */
    418535void posix_flockfile(FILE *file)
    419536{
     
    421538}
    422539
     540/**
     541 * Acquire file stream for the thread (non-blocking).
     542 *
     543 * @param file File stream to lock.
     544 * @return Zero for success and non-zero if the lock cannot be acquired.
     545 */
    423546int posix_ftrylockfile(FILE *file)
    424547{
     
    427550}
    428551
     552/**
     553 * Relinquish the ownership of the locked file stream.
     554 *
     555 * @param file File stream to unlock.
     556 */
    429557void posix_funlockfile(FILE *file)
    430558{
     
    432560}
    433561
     562/**
     563 * Get a byte from a stream (thread-unsafe).
     564 *
     565 * @param stream Input file stream.
     566 * @return Either read byte or EOF.
     567 */
    434568int posix_getc_unlocked(FILE *stream)
    435569{
     
    437571}
    438572
     573/**
     574 * Get a byte from the standard input stream (thread-unsafe).
     575 *
     576 * @return Either read byte or EOF.
     577 */
    439578int posix_getchar_unlocked(void)
    440579{
     
    442581}
    443582
     583/**
     584 * Put a byte on a stream (thread-unsafe).
     585 *
     586 * @param c Byte to output.
     587 * @param stream Output file stream.
     588 * @return Either written byte or EOF.
     589 */
    444590int posix_putc_unlocked(int c, FILE *stream)
    445591{
     
    447593}
    448594
     595/**
     596 * Put a byte on the standard output stream (thread-unsafe).
     597 *
     598 * @param c Byte to output.
     599 * @return Either written byte or EOF.
     600 */
    449601int posix_putchar_unlocked(int c)
    450602{
     
    453605
    454606/**
    455  *
    456  * @param path
    457  * @return
     607 * Remove a file.
     608 *
     609 * @param path Pathname of the file that shall be removed.
     610 * @return Zero on success, -1 otherwise.
    458611 */
    459612int posix_remove(const char *path)
Note: See TracChangeset for help on using the changeset viewer.