Changes in / [be2a20ac:a55d76b1] in mainline


Ignore:
Location:
uspace/lib
Files:
1 added
5 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    rbe2a20ac ra55d76b1  
    131131        generic/double_to_str.c \
    132132        generic/malloc.c \
    133         generic/stdio/scanf.c \
    134         generic/stdio/sscanf.c \
    135         generic/stdio/sstream.c \
    136133        generic/sysinfo.c \
    137134        generic/ipc.c \
     
    188185        test/main.c \
    189186        test/io/table.c \
    190         test/stdio/scanf.c \
    191187        test/odict.c \
    192188        test/qsort.c \
  • uspace/lib/c/generic/io/io.c

    rbe2a20ac ra55d76b1  
    5353static void _fflushbuf(FILE *stream);
    5454
    55 static size_t stdio_kio_read(void *, size_t, size_t, FILE *);
    56 static size_t stdio_kio_write(const void *, size_t, size_t, FILE *);
    57 static int stdio_kio_flush(FILE *);
    58 
    59 static size_t stdio_vfs_read(void *, size_t, size_t, FILE *);
    60 static size_t stdio_vfs_write(const void *, size_t, size_t, FILE *);
    61 
    62 static int stdio_vfs_flush(FILE *);
    63 
    64 /** KIO stream ops */
    65 static __stream_ops_t stdio_kio_ops = {
    66         .read = stdio_kio_read,
    67         .write = stdio_kio_write,
    68         .flush = stdio_kio_flush
    69 };
    70 
    71 /** VFS stream ops */
    72 static __stream_ops_t stdio_vfs_ops = {
    73         .read = stdio_vfs_read,
    74         .write = stdio_vfs_write,
    75         .flush = stdio_vfs_flush
    76 };
    77 
    7855static FILE stdin_null = {
    7956        .fd = -1,
     
    8158        .error = true,
    8259        .eof = true,
    83         .ops = &stdio_vfs_ops,
    84         .arg = NULL,
     60        .kio = false,
    8561        .sess = NULL,
    8662        .btype = _IONBF,
     
    9773        .error = false,
    9874        .eof = false,
    99         .ops = &stdio_kio_ops,
    100         .arg = NULL,
     75        .kio = true,
    10176        .sess = NULL,
    10277        .btype = _IOLBF,
     
    11388        .error = false,
    11489        .eof = false,
    115         .ops = &stdio_kio_ops,
    116         .arg = NULL,
     90        .kio = true,
    11791        .sess = NULL,
    11892        .btype = _IONBF,
     
    354328        stream->error = false;
    355329        stream->eof = false;
    356         stream->ops = &stdio_vfs_ops;
    357         stream->arg = NULL;
     330        stream->kio = false;
    358331        stream->sess = NULL;
    359332        stream->need_sync = false;
     
    379352        stream->error = false;
    380353        stream->eof = false;
    381         stream->ops = &stdio_vfs_ops;
    382         stream->arg = NULL;
     354        stream->kio = false;
    383355        stream->sess = NULL;
    384356        stream->need_sync = false;
     
    463435static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
    464436{
    465         return stream->ops->read(buf, size, nmemb, stream);
     437        errno_t rc;
     438        size_t nread;
     439
     440        if (size == 0 || nmemb == 0)
     441                return 0;
     442
     443        rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
     444        if (rc != EOK) {
     445                errno = rc;
     446                stream->error = true;
     447        } else if (nread == 0) {
     448                stream->eof = true;
     449        }
     450
     451        return (nread / size);
    466452}
    467453
     
    478464static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
    479465{
     466        errno_t rc;
    480467        size_t nwritten;
    481468
     
    483470                return 0;
    484471
    485         nwritten = stream->ops->write(buf, size, nmemb, stream);
     472        if (stream->kio) {
     473                rc = kio_write(buf, size * nmemb, &nwritten);
     474                if (rc != EOK) {
     475                        stream->error = true;
     476                        nwritten = 0;
     477                }
     478        } else {
     479                rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb,
     480                    &nwritten);
     481                if (rc != EOK) {
     482                        errno = rc;
     483                        stream->error = true;
     484                }
     485        }
    486486
    487487        if (nwritten > 0)
     
    905905        }
    906906
    907         if (stream->need_sync) {
     907        if (stream->kio) {
     908                kio_update();
     909                return 0;
     910        }
     911
     912        if ((stream->fd >= 0) && (stream->need_sync)) {
     913                errno_t rc;
     914
    908915                /**
    909916                 * Better than syncing always, but probably still not the
    910917                 * right thing to do.
    911918                 */
    912                 if (stream->ops->flush(stream) == EOF)
     919                stream->need_sync = false;
     920                rc = vfs_sync(stream->fd);
     921                if (rc != EOK) {
     922                        errno = rc;
    913923                        return EOF;
    914 
    915                 stream->need_sync = false;
     924                }
     925
     926                return 0;
    916927        }
    917928
     
    937948int fileno(FILE *stream)
    938949{
    939         if (stream->ops != &stdio_vfs_ops) {
     950        if (stream->kio) {
    940951                errno = EBADF;
    941952                return EOF;
     
    967978}
    968979
    969 /** Read from KIO stream. */
    970 static size_t stdio_kio_read(void *buf, size_t size, size_t nmemb, FILE *stream)
    971 {
    972         stream->eof = true;
    973         return 0;
    974 }
    975 
    976 /** Write to KIO stream. */
    977 static size_t stdio_kio_write(const void *buf, size_t size, size_t nmemb,
    978     FILE *stream)
    979 {
    980         errno_t rc;
    981         size_t nwritten;
    982 
    983         rc = kio_write(buf, size * nmemb, &nwritten);
    984         if (rc != EOK) {
    985                 stream->error = true;
    986                 nwritten = 0;
    987         }
    988 
    989         return nwritten / size;
    990 }
    991 
    992 /** Flush KIO stream. */
    993 static int stdio_kio_flush(FILE *stream)
    994 {
    995         kio_update();
    996         return 0;
    997 }
    998 
    999 /** Read from VFS stream. */
    1000 static size_t stdio_vfs_read(void *buf, size_t size, size_t nmemb, FILE *stream)
    1001 {
    1002         errno_t rc;
    1003         size_t nread;
    1004 
    1005         if (size == 0 || nmemb == 0)
    1006                 return 0;
    1007 
    1008         rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
    1009         if (rc != EOK) {
    1010                 errno = rc;
    1011                 stream->error = true;
    1012         } else if (nread == 0) {
    1013                 stream->eof = true;
    1014         }
    1015 
    1016         return (nread / size);
    1017 }
    1018 
    1019 /** Write to VFS stream. */
    1020 static size_t stdio_vfs_write(const void *buf, size_t size, size_t nmemb,
    1021     FILE *stream)
    1022 {
    1023         errno_t rc;
    1024         size_t nwritten;
    1025 
    1026         rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, &nwritten);
    1027         if (rc != EOK) {
    1028                 errno = rc;
    1029                 stream->error = true;
    1030         }
    1031 
    1032         return nwritten / size;
    1033 }
    1034 
    1035 /** Flush VFS stream. */
    1036 static int stdio_vfs_flush(FILE *stream)
    1037 {
    1038         errno_t rc;
    1039 
    1040         rc = vfs_sync(stream->fd);
    1041         if (rc != EOK) {
    1042                 errno = rc;
    1043                 return EOF;
    1044         }
    1045 
    1046         return 0;
    1047 }
    1048 
    1049980/** @}
    1050981 */
  • uspace/lib/c/generic/private/stdio.h

    rbe2a20ac ra55d76b1  
    3939#include <stdio.h>
    4040#include <async.h>
    41 #include <stddef.h>
    4241
    4342/** Maximum characters that can be pushed back by ungetc() */
    4443#define UNGETC_MAX 1
    45 
    46 /** Stream operations */
    47 typedef struct {
    48         /** Read from stream */
    49         size_t (*read)(void *buf, size_t size, size_t nmemb, FILE *stream);
    50         /** Write to stream */
    51         size_t (*write)(const void *buf, size_t size, size_t nmemb,
    52             FILE *stream);
    53         /** Flush stream */
    54         int (*flush)(FILE *stream);
    55 } __stream_ops_t;
    5644
    5745struct _IO_FILE {
     
    5947        link_t link;
    6048
    61         /** Stream operations */
    62         __stream_ops_t *ops;
    63 
    6449        /** Underlying file descriptor. */
    6550        int fd;
    66 
    67         /** Instance argument */
    68         void *arg;
    6951
    7052        /** File position. */
     
    7658        /** End-of-file indicator. */
    7759        int eof;
     60
     61        /** KIO indicator */
     62        int kio;
    7863
    7964        /** Session to the file provider */
  • uspace/lib/c/include/stdio.h

    rbe2a20ac ra55d76b1  
    101101extern int vsnprintf(char *, size_t, const char *, va_list);
    102102
    103 /* Formatted input */
    104 extern int scanf(const char *, ...);
    105 extern int vscanf(const char *, va_list);
    106 extern int fscanf(FILE *, const char *, ...);
    107 extern int vfscanf(FILE *, const char *, va_list);
    108 extern int sscanf(const char *, const char *, ...);
    109 extern int vsscanf(const char *, const char *, va_list);
    110 
    111103/* File stream functions */
    112104extern FILE *fopen(const char *, const char *);
  • uspace/lib/c/test/main.c

    rbe2a20ac ra55d76b1  
    3636PCUT_IMPORT(odict);
    3737PCUT_IMPORT(qsort);
    38 PCUT_IMPORT(scanf);
    3938PCUT_IMPORT(sprintf);
    4039PCUT_IMPORT(str);
  • uspace/lib/posix/Makefile

    rbe2a20ac ra55d76b1  
    7070        src/signal.c \
    7171        src/stdio.c \
     72        src/stdio/scanf.c \
    7273        src/stdlib.c \
    7374        src/stdlib/strtold.c \
  • uspace/lib/posix/include/posix/stdio.h

    rbe2a20ac ra55d76b1  
    164164extern int vsprintf(char *__restrict__ s, const char *__restrict__ format, va_list ap);
    165165
     166/* Formatted Input */
     167extern int fscanf(
     168    FILE *__restrict__ stream, const char *__restrict__ format, ...);
     169extern int vfscanf(
     170    FILE *__restrict__ stream, const char *__restrict__ format, va_list arg);
     171extern int scanf(const char *__restrict__ format, ...);
     172extern int vscanf(const char *__restrict__ format, va_list arg);
     173extern int sscanf(
     174    const char *__restrict__ s, const char *__restrict__ format, ...);
     175extern int vsscanf(
     176    const char *__restrict__ s, const char *__restrict__ format, va_list arg);
     177
    166178/* File Locking */
    167179extern void flockfile(FILE *file);
  • uspace/lib/posix/src/stdio.c

    rbe2a20ac ra55d76b1  
    351351
    352352/**
     353 * Convert formatted input from the stream.
     354 *
     355 * @param stream Input stream.
     356 * @param format Format description.
     357 * @return The number of converted output items or EOF on failure.
     358 */
     359int fscanf(FILE *restrict stream, const char *restrict format, ...)
     360{
     361        va_list list;
     362        va_start(list, format);
     363        int result = vfscanf(stream, format, list);
     364        va_end(list);
     365        return result;
     366}
     367
     368/**
     369 * Convert formatted input from the standard input.
     370 *
     371 * @param format Format description.
     372 * @return The number of converted output items or EOF on failure.
     373 */
     374int scanf(const char *restrict format, ...)
     375{
     376        va_list list;
     377        va_start(list, format);
     378        int result = vscanf(format, list);
     379        va_end(list);
     380        return result;
     381}
     382
     383/**
     384 * Convert formatted input from the standard input.
     385 *
     386 * @param format Format description.
     387 * @param arg Output items.
     388 * @return The number of converted output items or EOF on failure.
     389 */
     390int vscanf(const char *restrict format, va_list arg)
     391{
     392        return vfscanf(stdin, format, arg);
     393}
     394
     395/**
     396 * Convert formatted input from the string.
     397 *
     398 * @param s Input string.
     399 * @param format Format description.
     400 * @return The number of converted output items or EOF on failure.
     401 */
     402int sscanf(const char *restrict s, const char *restrict format, ...)
     403{
     404        va_list list;
     405        va_start(list, format);
     406        int result = vsscanf(s, format, list);
     407        va_end(list);
     408        return result;
     409}
     410
     411/**
    353412 * Acquire file stream for the thread.
    354413 *
Note: See TracChangeset for help on using the changeset viewer.