Changeset 8b5fb5e in mainline


Ignore:
Timestamp:
2011-06-28T20:51:10Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4ec6820
Parents:
244d6fd
Message:

stdio.h: Add and implement more functions

Location:
uspace/lib/posix
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/stdint.h

    r244d6fd r8b5fb5e  
    3535#ifndef POSIX_STDINT_H_
    3636#define POSIX_STDINT_H_
     37
     38#include "libc/stdint.h"
    3739
    3840#undef INT8_MAX
  • uspace/lib/posix/stdio.c

    r244d6fd r8b5fb5e  
    4141#include "internal/common.h"
    4242#include "stdio.h"
     43#include "libc/io/printf_core.h"
    4344#include "string.h"
     45#include "libc/str.h"
    4446
    4547/* not the best of solutions, but freopen will eventually
     
    4850#include "../c/generic/private/stdio.h"
    4951
     52/** Clears the stream's error and end-of-file indicators.
     53 *
     54 * @param stream
     55 */
     56void posix_clearerr(FILE *stream)
     57{
     58        stream->error = 0;
     59        stream->eof = 0;
     60}
     61
     62/**
     63 *
     64 * @param s
     65 * @return
     66 */
     67char *posix_ctermid(char *s)
     68{
     69        /* Currently always returns an error value (empty string). */
     70        // TODO: return a real terminal path
     71
     72        static char dummy_path[L_ctermid] = {'\0'};
     73
     74        if (s == NULL) {
     75                return dummy_path;
     76        }
     77
     78        s[0] = '\0';
     79        return s;
     80}
     81
    5082/**
    5183 *
     
    5587 */
    5688int posix_ungetc(int c, FILE *stream)
     89{
     90        // TODO
     91        not_implemented();
     92}
     93
     94ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
     95    int delimiter, FILE *restrict stream)
     96{
     97        // TODO
     98        not_implemented();
     99}
     100
     101ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
     102    FILE *restrict stream)
    57103{
    58104        // TODO
     
    113159}
    114160
     161FILE *posix_fmemopen(void *restrict buf, size_t size,
     162    const char *restrict mode)
     163{
     164        // TODO
     165        not_implemented();
     166}
     167
     168FILE *posix_open_memstream(char **bufp, size_t *sizep)
     169{
     170        // TODO
     171        not_implemented();
     172}
     173
    115174/**
    116175 *
     
    119178void posix_perror(const char *s)
    120179{
    121         // TODO
    122         not_implemented();
     180        if (s == NULL || s[0] == '\0') {
     181                fprintf(stderr, "%s\n", posix_strerror(errno));
     182        } else {
     183                fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
     184        }
     185}
     186
     187struct _posix_fpos {
     188        off64_t offset;
     189};
     190
     191/** Restores stream a to position previously saved with fgetpos().
     192 *
     193 * @param stream Stream to restore
     194 * @param pos Position to restore
     195 * @return Zero on success, non-zero (with errno set) on failure
     196 */
     197int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
     198{
     199        return fseek(stream, pos->offset, SEEK_SET);
     200}
     201
     202/** Saves the stream's position for later use by fsetpos().
     203 *
     204 * @param stream Stream to save
     205 * @param pos Place to store the position
     206 * @return Zero on success, non-zero (with errno set) on failure
     207 */
     208int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
     209{
     210        off64_t ret = ftell(stream);
     211        if (ret == -1) {
     212                return errno;
     213        }
     214        pos->offset = ret;
     215        return 0;
    123216}
    124217
     
    130223 * @return
    131224 */
     225int posix_fseek(FILE *stream, long offset, int whence)
     226{
     227        return fseek(stream, (off64_t) offset, whence);
     228}
     229
     230/**
     231 *
     232 * @param stream
     233 * @param offset
     234 * @param whence
     235 * @return
     236 */
    132237int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
    133238{
    134         // TODO
    135         not_implemented();
     239        return fseek(stream, (off64_t) offset, whence);
     240}
     241
     242/**
     243 *
     244 * @param stream
     245 * @return
     246 */
     247long posix_ftell(FILE *stream)
     248{
     249        return (long) ftell(stream);
    136250}
    137251
     
    143257posix_off_t posix_ftello(FILE *stream)
    144258{
    145         // TODO
    146         not_implemented();
     259        return (posix_off_t) ftell(stream);
     260}
     261
     262int posix_dprintf(int fildes, const char *restrict format, ...)
     263{
     264        va_list list;
     265        va_start(list, format);
     266        int result = posix_vdprintf(fildes, format, list);
     267        va_end(list);
     268        return result;
     269}
     270
     271static int _dprintf_str_write(const char *str, size_t size, void *fd)
     272{
     273        ssize_t wr = write(*(int *) fd, str, size);
     274        return str_nlength(str, wr);
     275}
     276
     277static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
     278{
     279        size_t offset = 0;
     280        size_t chars = 0;
     281        size_t sz;
     282        char buf[4];
     283       
     284        while (offset < size) {
     285                sz = 0;
     286                if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
     287                        break;
     288                }
     289               
     290                if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
     291                        break;
     292                }
     293               
     294                chars++;
     295                offset += sizeof(wchar_t);
     296        }
     297       
     298        return chars;
     299}
     300
     301int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
     302{
     303        printf_spec_t spec = {
     304                .str_write = _dprintf_str_write,
     305                .wstr_write = _dprintf_wstr_write,
     306                .data = &fildes
     307        };
     308       
     309        return printf_core(format, &spec, ap);
    147310}
    148311
     
    156319int posix_sprintf(char *s, const char *format, ...)
    157320{
    158         // TODO
    159         not_implemented();
    160 }
    161 
    162 /**
    163  *
    164  * @param s
     321        va_list list;
     322        va_start(list, format);
     323        int result = posix_vsprintf(s, format, list);
     324        va_end(list);
     325        return result;
     326}
     327
     328/**
     329 *
     330 * @param s
     331 * @param format
     332 * @param ap
     333 * @return
     334 */
     335int posix_vsprintf(char *s, const char *format, va_list ap)
     336{
     337        return vsnprintf(s, STR_NO_LIMIT, format, ap);
     338}
     339
     340/**
     341 *
     342 * @param stream
    165343 * @param format
    166344 * @param ...
    167345 * @return
    168346 */
    169 int posix_vsprintf(char *s, const char *format, va_list ap)
     347int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
     348{
     349        va_list list;
     350        va_start(list, format);
     351        int result = posix_vfscanf(stream, format, list);
     352        va_end(list);
     353        return result;
     354}
     355
     356int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg)
     357{
     358        // TODO
     359        not_implemented();
     360}
     361
     362/**
     363 *
     364 * @param format
     365 * @param ...
     366 * @return
     367 */
     368int posix_scanf(const char *restrict format, ...)
     369{
     370        va_list list;
     371        va_start(list, format);
     372        int result = posix_vscanf(format, list);
     373        va_end(list);
     374        return result;
     375}
     376
     377/**
     378 *
     379 * @param format
     380 * @param arg
     381 * @return
     382 */
     383int posix_vscanf(const char *restrict format, va_list arg)
     384{
     385        return posix_vfscanf(stdin, format, arg);
     386}
     387
     388/**
     389 *
     390 * @param s
     391 * @param format
     392 * @param ...
     393 * @return
     394 */
     395int posix_sscanf(const char *s, const char *format, ...)
     396{
     397        va_list list;
     398        va_start(list, format);
     399        int result = posix_vsscanf(s, format, list);
     400        va_end(list);
     401        return result;
     402}
     403
     404/**
     405 *
     406 * @param s
     407 * @param format
     408 * @param arg
     409 * @return
     410 */
     411int posix_vsscanf(
     412    const char *restrict s, const char *restrict format, va_list arg)
     413{
     414        // TODO
     415        not_implemented();
     416}
     417
     418void posix_flockfile(FILE *file)
     419{
     420        /* dummy */
     421}
     422
     423int posix_ftrylockfile(FILE *file)
     424{
     425        /* dummy */
     426        return 0;
     427}
     428
     429void posix_funlockfile(FILE *file)
     430{
     431        /* dummy */
     432}
     433
     434int posix_getc_unlocked(FILE *stream)
     435{
     436        return getc(stream);
     437}
     438
     439int posix_getchar_unlocked(void)
     440{
     441        return getchar();
     442}
     443
     444int posix_putc_unlocked(int c, FILE *stream)
     445{
     446        return putc(c, stream);
     447}
     448
     449int posix_putchar_unlocked(int c)
     450{
     451        return putchar(c);
     452}
     453
     454/**
     455 *
     456 * @param path
     457 * @return
     458 */
     459int posix_remove(const char *path)
     460{
     461        // FIXME: unlink() and rmdir() seem to be equivalent at the moment,
     462        //        but that does not have to be true forever
     463        return unlink(path);
     464}
     465
     466/**
     467 *
     468 * @param s
     469 * @return
     470 */
     471char *posix_tmpnam(char *s)
    170472{
    171473        // TODO: low priority, just a compile-time dependency of binutils
     
    173475}
    174476
    175 /**
    176  *
    177  * @param s
    178  * @param format
    179  * @param ...
    180  * @return
    181  */
    182 int posix_sscanf(const char *s, const char *format, ...)
    183 {
    184         // TODO
    185         not_implemented();
    186 }
    187 
    188 /**
    189  *
    190  * @param path
    191  * @return
    192  */
    193 int posix_remove(const char *path)
    194 {
    195         // TODO: low priority, just a compile-time dependency of binutils
    196         not_implemented();
    197 }
    198 
    199 /**
    200  *
    201  * @param s
    202  * @return
    203  */
    204 char *posix_tmpnam(char *s)
    205 {
    206         // TODO: low priority, just a compile-time dependency of binutils
    207         not_implemented();
    208 }
    209 
    210477/** @}
    211478 */
  • uspace/lib/posix/stdio.h

    r244d6fd r8b5fb5e  
    4040#include "sys/types.h"
    4141#include "libc/stdarg.h"
     42#include "limits.h"
    4243
    43 /* Character Input/Output */
     44#undef L_ctermid
     45#define L_ctermid PATH_MAX
     46
     47extern void posix_clearerr(FILE *stream);
     48extern char *posix_ctermid(char *s);
     49
     50/* Input/Output */
    4451#undef putc
    4552#define putc fputc
     
    4754#define getc fgetc
    4855extern int posix_ungetc(int c, FILE *stream);
     56
     57extern ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
     58    int delimiter, FILE *restrict stream);
     59extern ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
     60    FILE *restrict stream);
    4961
    5062/* Opening Streams */
     
    5466   FILE *restrict stream);
    5567
     68/* Memory Streams */
     69
     70extern FILE *posix_fmemopen(void *restrict buf, size_t size,
     71    const char *restrict mode);
     72extern FILE *posix_open_memstream(char **bufp, size_t *sizep);
     73
    5674/* Error Messages */
    5775extern void posix_perror(const char *s);
    5876
    5977/* File Positioning */
     78
     79typedef struct _posix_fpos posix_fpos_t;
     80extern int posix_fsetpos(FILE *stream, const posix_fpos_t *pos);
     81extern int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos);
     82extern int posix_fseek(FILE *stream, long offset, int whence);
    6083extern int posix_fseeko(FILE *stream, posix_off_t offset, int whence);
     84extern long posix_ftell(FILE *stream);
    6185extern posix_off_t posix_ftello(FILE *stream);
    6286
    6387/* Formatted Input/Output */
    64 extern int posix_sprintf(char *restrict s, const char *restrict format, ...);
     88extern int posix_dprintf(int fildes, const char *restrict format, ...)
     89    PRINTF_ATTRIBUTE(2, 3);
     90extern int posix_vdprintf(int fildes, const char *restrict format, va_list ap);
     91extern int posix_sprintf(char *restrict s, const char *restrict format, ...)
     92    PRINTF_ATTRIBUTE(2, 3);
    6593extern int posix_vsprintf(char *restrict s, const char *restrict format, va_list ap);
    66 extern int posix_sscanf(const char *restrict s, const char *restrict format, ...);
     94
     95extern int posix_fscanf(
     96    FILE *restrict stream, const char *restrict format, ...);
     97extern int posix_vfscanf(
     98    FILE *restrict stream, const char *restrict format, va_list arg);
     99extern int posix_scanf(const char *restrict format, ...);
     100extern int posix_vscanf(const char *restrict format, va_list arg);
     101extern int posix_sscanf(
     102    const char *restrict s, const char *restrict format, ...);
     103extern int posix_vsscanf(
     104    const char *restrict s, const char *restrict format, va_list arg);
     105
     106/* File Locking */
     107
     108extern void posix_flockfile(FILE *file);
     109extern int posix_ftrylockfile(FILE *file);
     110extern void posix_funlockfile(FILE *file);
     111
     112extern int posix_getc_unlocked(FILE *stream);
     113extern int posix_getchar_unlocked(void);
     114extern int posix_putc_unlocked(int c, FILE *stream);
     115extern int posix_putchar_unlocked(int c);
    67116
    68117/* Deleting Files */
     
    76125
    77126#ifndef LIBPOSIX_INTERNAL
     127        #define clearerr posix_clearerr
     128        #define ctermid posix_ctermid
     129
    78130        #define ungetc posix_ungetc
     131
     132        #define getdelim posix_getdelim
     133        #define getline posix_getline
    79134
    80135        #define freopen posix_freopen
    81136
     137        #define fmemopen posix_fmemopen
     138        #define open_memstream posix_open_memstream
     139
    82140        #define perror posix_perror
    83141
     142        #define fpos_t posix_fpos_t
     143        #define fsetpos posix_fsetpos
     144        #define fgetpos posix_fgetpos
     145        #define fseek posix_fseek
    84146        #define fseeko posix_fseeko
     147        #define ftell posix_ftell
    85148        #define ftello posix_ftello
    86149
     150        #define dprintf posix_dprintf
     151        #define vdprintf posix_vdprintf
    87152        #define sprintf posix_sprintf
    88153        #define vsprintf posix_vsprintf
     154
     155        #define fscanf posix_fscanf
     156        #define vfscanf posix_vfscanf
     157        #define vscanf posix_vscanf
     158        #define scanf posix_scanf
    89159        #define sscanf posix_sscanf
     160        #define vsscanf posix_vsscanf
     161
     162        #define flockfile posix_flockfile
     163        #define ftrylockfile posix_ftrylockfile
     164        #define funlockfile posix_funlockfile
     165
     166        #define getc_unlocked posix_getc_unlocked
     167        #define getchar_unlocked posix_getchar_unlocked
     168        #define putc_unlocked posix_putc_unlocked
     169        #define putchar_unlocked posix_putchar_unlocked
    90170
    91171        #define remove posix_remove
Note: See TracChangeset for help on using the changeset viewer.