Changeset 2595dab in mainline


Ignore:
Timestamp:
2009-06-03T19:26:28Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d00ae4c
Parents:
ca3ba3a
Message:

I/O subsystem overhaul:

  • add more POSIX-like file and stream functions (with real functionality of stdin, stdout, stderr)
  • cleanup console access methods (now generic to any console-like device)
  • remove unsafe stream functions
  • add special open_node(), fd_node(), fd_phone() (file) and fopen_node(), fnode(), fphone() (stream) functions for HelenOS-specific I/O operations
Location:
uspace/lib/libc
Files:
7 deleted
16 edited
7 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/Makefile

    rca3ba3a r2595dab  
    4747        generic/as.c \
    4848        generic/cap.c \
    49         generic/console.c \
    5049        generic/devmap.c \
    5150        generic/event.c \
     
    6261        generic/io/io.c \
    6362        generic/io/printf.c \
    64         generic/io/fprintf.c \
    65         generic/io/stdio.c \
    66         generic/io/stream.c \
    67         generic/io/sprintf.c \
     63        generic/io/klog.c \
    6864        generic/io/snprintf.c \
    6965        generic/io/vprintf.c \
    70         generic/io/vsprintf.c \
    7166        generic/io/vsnprintf.c \
    7267        generic/io/printf_core.c \
     68        generic/io/console.c \
    7369        malloc/malloc.c \
    7470        generic/sysinfo.c \
     
    8278        generic/err.c \
    8379        generic/stdlib.c \
    84         generic/kbd.c \
    8580        generic/mman.c \
    8681        generic/udebug.c \
  • uspace/lib/libc/generic/io/asprintf.c

    rca3ba3a r2595dab  
    3737#include <stdio.h>
    3838#include <stdlib.h>
     39#include <string.h>
    3940#include <io/printf_core.h>
    4041
    41 static int asprintf_prewrite(const char *str, size_t count, void *unused)
     42static int asprintf_str_write(const char *str, size_t count, void *unused)
    4243{
    43         return count;
     44        return str_nlength(str, count);
     45}
     46
     47static int asprintf_wstr_write(const wchar_t *str, size_t count, void *unused)
     48{
     49        return wstr_nlength(str, count);
    4450}
    4551
    4652/** Allocate and print to string.
    4753 *
    48  * @param strp          Address of the pointer where to store the address of
    49  *                      the newly allocated string.
    50  * @fmt                 Format strin.
     54 * @param strp Address of the pointer where to store the address of
     55 *             the newly allocated string.
     56 * @fmt        Format string.
    5157 *
    52  * @return              Number of characters printed or a negative error code.
     58 * @return Number of characters printed or a negative error code.
     59 *
    5360 */
    5461int asprintf(char **strp, const char *fmt, ...)
    5562{
    5663        struct printf_spec ps = {
    57                  asprintf_prewrite,
    58                  NULL
     64                asprintf_str_write,
     65                asprintf_wstr_write,
     66                NULL
    5967        };
    60         int ret;
     68       
    6169        va_list args;
    62 
    6370        va_start(args, fmt);
    64         ret = printf_core(fmt, &ps, args);
     71       
     72        int ret = printf_core(fmt, &ps, args);
    6573        va_end(args);
     74       
    6675        if (ret > 0) {
    67                 *strp = malloc(ret + 20);
    68                 if (!*strp)
     76                *strp = malloc(STR_BOUNDS(ret) + 1);
     77                if (*strp == NULL)
    6978                        return -1;
     79               
    7080                va_start(args, fmt);
    71                 vsprintf(*strp, fmt, args);
    72                 va_end(args);           
     81                vsnprintf(*strp, STR_BOUNDS(ret) + 1, fmt, args);
     82                va_end(args);
    7383        }
    74 
     84       
    7585        return ret;
    7686}
  • uspace/lib/libc/generic/io/console.c

    rca3ba3a r2595dab  
    22 * Copyright (c) 2006 Josef Cejka
    33 * Copyright (c) 2006 Jakub Vana
     4 * Copyright (c) 2008 Jiri Svoboda
    45 * All rights reserved.
    56 *
     
    3435 */
    3536
    36 #include <io/io.h>
    37 #include <io/stream.h>
    38 #include <string.h>
    39 #include <malloc.h>
    4037#include <libc.h>
    41 #include <ipc/ipc.h>
    42 #include <ipc/ns.h>
    43 #include <ipc/fb.h>
    44 #include <ipc/services.h>
     38#include <async.h>
     39#include <io/console.h>
    4540#include <ipc/console.h>
    46 #include <console.h>
    47 #include <kbd/kbd.h>
    48 #include <unistd.h>
    49 #include <async.h>
    50 #include <sys/types.h>
    5141
    52 ssize_t read_stdin(void *buf, size_t count)
     42void console_clear(int phone)
    5343{
    54         int cons_phone = console_open(false);
    55        
    56         if (cons_phone >= 0) {
    57                 kbd_event_t ev;
    58                 int rc;
    59                 size_t i = 0;
    60                
    61                 while (i < count) {
    62                         do {
    63                                 rc = kbd_get_event(&ev);
    64                                 if (rc < 0) return -1;
    65                         } while (ev.c == 0 || ev.type == KE_RELEASE);
    66                        
    67                         ((char *) buf)[i++] = ev.c;
    68                 }
    69                 return i;
    70         } else
    71                 return -1;
     44        async_msg_0(phone, CONSOLE_CLEAR);
    7245}
    7346
    74 /** Write a string to klog. */
    75 int klog_puts(const char *str)
     47int console_get_size(int phone, ipcarg_t *rows, ipcarg_t *cols)
    7648{
    77         return __SYSCALL3(SYS_KLOG, 1, (sysarg_t) str, str_size(str));
     49        return async_req_0_2(phone, CONSOLE_GET_SIZE, rows, cols);
    7850}
    7951
    80 void klog_update(void)
     52void console_set_style(int phone, int style)
    8153{
    82         (void) __SYSCALL3(SYS_KLOG, 1, NULL, 0);
     54        async_msg_1(phone, CONSOLE_SET_STYLE, style);
     55}
     56
     57void console_set_color(int phone, int fg_color, int bg_color, int flags)
     58{
     59        async_msg_3(phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags);
     60}
     61
     62void console_set_rgb_color(int phone, int fg_color, int bg_color)
     63{
     64        async_msg_2(phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color);
     65}
     66
     67void console_cursor_visibility(int phone, bool show)
     68{
     69        async_msg_1(phone, CONSOLE_CURSOR_VISIBILITY, show != false);
     70}
     71
     72void console_kcon_enable(int phone)
     73{
     74        async_msg_0(phone, CONSOLE_KCON_ENABLE);
     75}
     76
     77void console_goto(int phone, ipcarg_t row, ipcarg_t col)
     78{
     79        async_msg_2(phone, CONSOLE_GOTO, row, col);
     80}
     81
     82bool console_get_event(int phone, console_event_t *event)
     83{
     84        ipcarg_t type;
     85        ipcarg_t key;
     86        ipcarg_t mods;
     87        ipcarg_t c;
     88       
     89        int rc = async_req_0_4(phone, CONSOLE_GET_EVENT, &type, &key, &mods, &c);
     90        if (rc < 0)
     91                return false;
     92       
     93        event->type = type;
     94        event->key = key;
     95        event->mods = mods;
     96        event->c = c;
     97       
     98        return true;
    8399}
    84100
  • uspace/lib/libc/generic/io/io.c

    rca3ba3a r2595dab  
    3131 */
    3232/** @file
    33  */ 
    34 
    35 #include <libc.h>
     33 */
     34
     35#include <stdio.h>
    3636#include <unistd.h>
    37 #include <stdio.h>
    38 #include <io/io.h>
     37#include <fcntl.h>
    3938#include <string.h>
    4039#include <errno.h>
    41 #include <console.h>
    42 
    43 const static char nl = '\n';
     40#include <bool.h>
     41#include <malloc.h>
     42#include <io/klog.h>
     43#include <vfs/vfs.h>
     44#include <ipc/devmap.h>
     45
     46FILE stdin_null = {
     47        .fd = -1,
     48        .error = true,
     49        .eof = true,
     50        .klog = false,
     51        .phone = -1
     52};
     53
     54FILE stdout_klog = {
     55        .fd = -1,
     56        .error = false,
     57        .eof = false,
     58        .klog = true,
     59        .phone = -1
     60};
     61
     62FILE *stdin = &stdin_null;
     63FILE *stdout = &stdout_klog;
     64FILE *stderr = &stdout_klog;
     65
     66static bool parse_mode(const char *mode, int *flags)
     67{
     68        /* Parse mode except first character. */
     69        const char *mp = mode;
     70        if (*mp++ == 0) {
     71                errno = EINVAL;
     72                return false;
     73        }
     74       
     75        if ((*mp == 'b') || (*mp == 't'))
     76                mp++;
     77       
     78        bool plus;
     79        if (*mp == '+') {
     80                mp++;
     81                plus = true;
     82        } else
     83                plus = false;
     84       
     85        if (*mp != 0) {
     86                errno = EINVAL;
     87                return false;
     88        }
     89       
     90        /* Parse first character of mode and determine flags for open(). */
     91        switch (mode[0]) {
     92        case 'r':
     93                *flags = plus ? O_RDWR : O_RDONLY;
     94                break;
     95        case 'w':
     96                *flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
     97                break;
     98        case 'a':
     99                /* TODO: a+ must read from beginning, append to the end. */
     100                if (plus) {
     101                        errno = ENOTSUP;
     102                        return false;
     103                }
     104                *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
     105        default:
     106                errno = EINVAL;
     107                return false;
     108        }
     109       
     110        return true;
     111}
     112
     113/** Open a stream.
     114 *
     115 * @param path Path of the file to open.
     116 * @param mode Mode string, (r|w|a)[b|t][+].
     117 *
     118 */
     119FILE *fopen(const char *path, const char *mode)
     120{
     121        int flags;
     122        if (!parse_mode(mode, &flags))
     123                return NULL;
     124       
     125        /* Open file. */
     126        FILE *stream = malloc(sizeof(FILE));
     127        if (stream == NULL) {
     128                errno = ENOMEM;
     129                return NULL;
     130        }
     131       
     132        stream->fd = open(path, flags, 0666);
     133        if (stream->fd < 0) {
     134                /* errno was set by open() */
     135                free(stream);
     136                return NULL;
     137        }
     138       
     139        stream->error = false;
     140        stream->eof = false;
     141        stream->klog = false;
     142        stream->phone = -1;
     143       
     144        return stream;
     145}
     146
     147FILE *fopen_node(fs_node_t *node, const char *mode)
     148{
     149        int flags;
     150        if (!parse_mode(mode, &flags))
     151                return NULL;
     152       
     153        /* Open file. */
     154        FILE *stream = malloc(sizeof(FILE));
     155        if (stream == NULL) {
     156                errno = ENOMEM;
     157                return NULL;
     158        }
     159       
     160        stream->fd = open_node(node, flags);
     161        if (stream->fd < 0) {
     162                /* errno was set by open_node() */
     163                free(stream);
     164                return NULL;
     165        }
     166       
     167        stream->error = false;
     168        stream->eof = false;
     169        stream->klog = false;
     170        stream->phone = -1;
     171       
     172        return stream;
     173}
     174
     175int fclose(FILE *stream)
     176{
     177        int rc = 0;
     178       
     179        fflush(stream);
     180       
     181        if (stream->phone >= 0)
     182                ipc_hangup(stream->phone);
     183       
     184        if (stream->fd >= 0)
     185                rc = close(stream->fd);
     186       
     187        if ((stream != &stdin_null) && (stream != &stdout_klog))
     188                free(stream);
     189       
     190        stream = NULL;
     191       
     192        if (rc != 0) {
     193                /* errno was set by close() */
     194                return EOF;
     195        }
     196       
     197        return 0;
     198}
     199
     200/** Read from a stream.
     201 *
     202 * @param buf    Destination buffer.
     203 * @param size   Size of each record.
     204 * @param nmemb  Number of records to read.
     205 * @param stream Pointer to the stream.
     206 *
     207 */
     208size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
     209{
     210        size_t left = size * nmemb;
     211        size_t done = 0;
     212       
     213        while ((left > 0) && (!stream->error) && (!stream->eof)) {
     214                ssize_t rd = read(stream->fd, buf + done, left);
     215               
     216                if (rd < 0)
     217                        stream->error = true;
     218                else if (rd == 0)
     219                        stream->eof = true;
     220                else {
     221                        left -= rd;
     222                        done += rd;
     223                }
     224        }
     225       
     226        return (done / size);
     227}
     228
     229/** Write to a stream.
     230 *
     231 * @param buf    Source buffer.
     232 * @param size   Size of each record.
     233 * @param nmemb  Number of records to write.
     234 * @param stream Pointer to the stream.
     235 *
     236 */
     237size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
     238{
     239        size_t left = size * nmemb;
     240        size_t done = 0;
     241       
     242        while ((left > 0) && (!stream->error)) {
     243                ssize_t wr;
     244               
     245                if (stream->klog)
     246                        wr = klog_write(buf + done, left);
     247                else
     248                        wr = write(stream->fd, buf + done, left);
     249               
     250                if (wr <= 0)
     251                        stream->error = true;
     252                else {
     253                        left -= wr;
     254                        done += wr;
     255                }
     256        }
     257       
     258        return (done / size);
     259}
     260
     261int fputc(wchar_t c, FILE *stream)
     262{
     263        char buf[STR_BOUNDS(1)];
     264        size_t sz = 0;
     265       
     266        if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
     267                size_t wr = fwrite(buf, sz, 1, stream);
     268               
     269                if (wr < sz)
     270                        return EOF;
     271               
     272                return (int) c;
     273        }
     274       
     275        return EOF;
     276}
     277
     278int putchar(wchar_t c)
     279{
     280        return fputc(c, stdout);
     281}
     282
     283int fputs(const char *str, FILE *stream)
     284{
     285        return fwrite(str, str_size(str), 1, stream);
     286}
    44287
    45288int puts(const char *str)
    46289{
    47         size_t count;
    48        
    49         if (str == NULL)
    50                 return putnchars("(NULL)", 6);
    51        
    52         for (count = 0; str[count] != 0; count++);
    53        
    54         if (console_write((void *) str, count) == count) {
    55                 if (console_write(&nl, 1) == 1)
    56                         return 0;
    57         }
    58        
    59         return EOF;
    60 }
    61 
    62 /** Put count chars from buffer to stdout without adding newline
    63  * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
    64  * @param count
    65  * @return 0 on succes, EOF on fail
    66  */
    67 int putnchars(const char *buf, size_t count)
    68 {
    69         if (console_write((void *) buf, count) == count)
    70                 return 0;
    71        
    72         return EOF;
    73 }
    74 
    75 /** Same as puts, but does not print newline at end
    76  *
    77  */
    78 int putstr(const char *str)
    79 {
    80         size_t count;
    81        
    82         if (str == NULL)
    83                 return putnchars("(NULL)", 6);
    84 
    85         for (count = 0; str[count] != 0; count++);
    86         if (console_write((void *) str, count) == count)
    87                 return 0;
    88        
    89         return EOF;
    90 }
    91 
    92 int putchar(int c)
    93 {
    94         char buf[STR_BOUNDS(1)];
    95         size_t offs;
    96 
    97         offs = 0;
    98         if (chr_encode(c, buf, &offs, STR_BOUNDS(1)) != EOK)
     290        return fputs(str, stdout);
     291}
     292
     293int fgetc(FILE *stream)
     294{
     295        char c;
     296       
     297        if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
    99298                return EOF;
    100 
    101         if (console_write((void *) buf, offs) == offs)
    102                 return c;
    103 
    104         return EOF;
     299       
     300        return (int) c;
    105301}
    106302
    107303int getchar(void)
    108304{
    109         unsigned char c;
    110        
    111         console_flush();
    112         if (read_stdin((void *) &c, 1) == 1)
    113                 return c;
    114        
    115         return EOF;
    116 }
    117 
    118 int fflush(FILE *f)
    119 {
    120         /* Dummy implementation */
    121         (void) f;
    122         console_flush();
     305        return fgetc(stdin);
     306}
     307
     308int fseek(FILE *stream, long offset, int origin)
     309{
     310        off_t rc = lseek(stream->fd, offset, origin);
     311        if (rc == (off_t) (-1)) {
     312                /* errno has been set by lseek. */
     313                return -1;
     314        }
     315       
     316        stream->eof = false;
     317       
    123318        return 0;
    124319}
    125320
     321int fflush(FILE *stream)
     322{
     323        if (stream->klog) {
     324                klog_update();
     325                return EOK;
     326        }
     327       
     328        if (stream->fd >= 0)
     329                return fsync(stream->fd);
     330       
     331        return ENOENT;
     332}
     333
     334int feof(FILE *stream)
     335{
     336        return stream->eof;
     337}
     338
     339int ferror(FILE *stream)
     340{
     341        return stream->error;
     342}
     343
     344int fphone(FILE *stream)
     345{
     346        if (stream->fd >= 0) {
     347                if (stream->phone < 0)
     348                        stream->phone = fd_phone(stream->fd);
     349               
     350                return stream->phone;
     351        }
     352       
     353        return -1;
     354}
     355
     356void fnode(FILE *stream, fs_node_t *node)
     357{
     358        if (stream->fd >= 0) {
     359                fd_node(stream->fd, node);
     360        } else {
     361                node->fs_handle = 0;
     362                node->dev_handle = 0;
     363                node->index = 0;
     364        }
     365}
     366
    126367/** @}
    127368 */
  • uspace/lib/libc/generic/io/klog.c

    rca3ba3a r2595dab  
    11/*
    22 * Copyright (c) 2006 Josef Cejka
     3 * Copyright (c) 2006 Jakub Vana
    34 * All rights reserved.
    45 *
     
    3334 */
    3435
    35 #ifndef LIBC_IO_H_
    36 #define LIBC_IO_H_
     36#include <libc.h>
     37#include <string.h>
     38#include <sys/types.h>
     39#include <unistd.h>
     40#include <io/klog.h>
    3741
    38 #include <sys/types.h>
     42size_t klog_write(const void *buf, size_t size)
     43{
     44        return (size_t) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) buf, size);
     45}
    3946
    40 int putnchars(const char * buf, size_t count);
    41 int putstr(const char * str);
    42 int putchar(int c);
    43 int getchar(void);
    44 
    45 #endif
     47void klog_update(void)
     48{
     49        (void) __SYSCALL3(SYS_KLOG, 1, NULL, 0);
     50}
    4651
    4752/** @}
    4853 */
    49  
    50  
  • uspace/lib/libc/generic/io/printf.c

    rca3ba3a r2595dab  
    3535#include <io/printf_core.h>
    3636#include <stdio.h>
    37 #include <stdio.h>
    3837
    3938/** Print formatted text.
    40  * @param fmt   format string
     39 *
     40 * @param stream Output stream
     41 * @param fmt    Format string
     42 *
    4143 * \see For more details about format string see printf_core.
     44 *
     45 */
     46int fprintf(FILE *stream, const char *fmt, ...)
     47{
     48        va_list args;
     49        va_start(args, fmt);
     50       
     51        int ret = vfprintf(stream, fmt, args);
     52       
     53        va_end(args);
     54       
     55        return ret;
     56}
     57
     58/** Print formatted text to stdout.
     59 *
     60 * @param fmt Format string
     61 *
     62 * \see For more details about format string see printf_core.
     63 *
    4264 */
    4365int printf(const char *fmt, ...)
    4466{
    45         int ret;
    4667        va_list args;
    47 
    4868        va_start(args, fmt);
    49 
    50         ret = vprintf(fmt, args);
     69       
     70        int ret = vprintf(fmt, args);
    5171       
    5272        va_end(args);
    53 
     73       
    5474        return ret;
    5575}
  • uspace/lib/libc/generic/io/printf_core.c

    rca3ba3a r2595dab  
    174174static int print_char(const char ch, int width, uint32_t flags, printf_spec_t *ps)
    175175{
    176         count_t counter = 0;
     176        size_t counter = 0;
    177177        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    178178                while (--width > 0) {
     
    212212static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps)
    213213{
    214         count_t counter = 0;
     214        size_t counter = 0;
    215215        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    216216                while (--width > 0) {
     
    255255
    256256        /* Print leading spaces. */
    257         count_t strw = str_length(str);
     257        size_t strw = str_length(str);
    258258        if (precision == 0)
    259259                precision = strw;
    260260
    261261        /* Left padding */
    262         count_t counter = 0;
     262        size_t counter = 0;
    263263        width -= precision;
    264264        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
     
    311311       
    312312        /* Left padding */
    313         count_t counter = 0;
     313        size_t counter = 0;
    314314        width -= precision;
    315315        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
     
    433433       
    434434        width -= precision + size - number_size;
    435         count_t counter = 0;
     435        size_t counter = 0;
    436436       
    437437        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
     
    596596        size_t j = 0;    /* Index to the first not printed nonformating character */
    597597       
    598         count_t counter = 0;  /* Number of characters printed */
     598        size_t counter = 0;   /* Number of characters printed */
    599599        int retval;           /* Return values from nested functions */
    600600       
  • uspace/lib/libc/generic/io/snprintf.c

    rca3ba3a r2595dab  
    3838
    3939/** Print formatted to the given buffer with limited size.
    40  * @param str   buffer
    41  * @param size  buffer size
    42  * @param fmt   format string
     40 *
     41 * @param str  Buffer
     42 * @param size Buffer size
     43 * @param fmt  Format string
     44 *
    4345 * \see For more details about format string see printf_core.
     46 *
    4447 */
    4548int snprintf(char *str, size_t size, const char *fmt, ...)
    4649{
    47         int ret;
    4850        va_list args;
     51        va_start(args, fmt);
    4952       
    50         va_start(args, fmt);
    51         ret = vsnprintf(str, size, fmt, args);
    52 
     53        int ret = vsnprintf(str, size, fmt, args);
     54       
    5355        va_end(args);
    54 
     56       
    5557        return ret;
    5658}
  • uspace/lib/libc/generic/io/vprintf.c

    rca3ba3a r2595dab  
    3939#include <futex.h>
    4040#include <async.h>
    41 #include <console.h>
     41#include <string.h>
    4242
    4343static atomic_t printf_futex = FUTEX_INITIALIZER;
    4444
    45 static int vprintf_str_write(const char *str, size_t size, void *data)
     45static int vprintf_str_write(const char *str, size_t size, void *stream)
     46{
     47        size_t wr = fwrite(str, 1, size, (FILE *) stream);
     48        return str_nlength(str, wr);
     49}
     50
     51static int vprintf_wstr_write(const wchar_t *str, size_t size, void *stream)
    4652{
    4753        size_t offset = 0;
    48         size_t prev;
    49         count_t chars = 0;
     54        size_t chars = 0;
    5055       
    5156        while (offset < size) {
    52                 prev = offset;
    53                 str_decode(str, &offset, size);
    54                 console_write(str + prev, offset - prev);
    55                 chars++;
    56         }
    57        
    58         return chars;
    59 }
    60 
    61 static int vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
    62 {
    63         size_t offset = 0;
    64         size_t boff;
    65         count_t chars = 0;
    66         char buf[4];
    67        
    68         while (offset < size) {
    69                 boff = 0;
    70                 chr_encode(str[chars], buf, &boff, 4);
    71                 console_write(buf, boff);
     57                if (fputc(str[chars], (FILE *) stream) <= 0)
     58                        break;
     59               
    7260                chars++;
    7361                offset += sizeof(wchar_t);
     
    7765}
    7866
    79 
    8067/** Print formatted text.
    81  * @param fmt   format string
    82  * @param ap    format parameters
     68 *
     69 * @param stream Output stream
     70 * @param fmt    Format string
     71 * @param ap     Format parameters
     72 *
    8373 * \see For more details about format string see printf_core.
     74 *
    8475 */
    85 int vprintf(const char *fmt, va_list ap)
     76int vfprintf(FILE *stream, const char *fmt, va_list ap)
    8677{
    8778        struct printf_spec ps = {
    8879                vprintf_str_write,
    8980                vprintf_wstr_write,
    90                  NULL
     81                stream
    9182        };
     83       
    9284        /*
    9385         * Prevent other threads to execute printf_core()
    9486         */
    9587        futex_down(&printf_futex);
     88       
    9689        /*
    9790         * Prevent other pseudo threads of the same thread
     
    9992         */
    10093        async_serialize_start();
     94       
    10195        int ret = printf_core(fmt, &ps, ap);
     96       
    10297        async_serialize_end();
    10398        futex_up(&printf_futex);
     99       
    104100        return ret;
     101}
     102
     103/** Print formatted text to stdout.
     104 *
     105 * @param file Output stream
     106 * @param fmt  Format string
     107 * @param ap   Format parameters
     108 *
     109 * \see For more details about format string see printf_core.
     110 *
     111 */
     112int vprintf(const char *fmt, va_list ap)
     113{
     114        return vfprintf(stdout, fmt, ap);
    105115}
    106116
  • uspace/lib/libc/generic/io/vsnprintf.c

    rca3ba3a r2595dab  
    8383                 * of string
    8484                 */
    85                 index_t index = 0;
     85                size_t index = 0;
    8686               
    8787                while (index < size) {
     
    131131static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data)
    132132{
    133         index_t index = 0;
     133        size_t index = 0;
    134134       
    135135        while (index < (size / sizeof(wchar_t))) {
  • uspace/lib/libc/generic/vfs/vfs.c

    rca3ba3a r2595dab  
    5050#include <string.h>
    5151#include <devmap.h>
    52 #include "../../../srv/vfs/vfs.h"
    53 
    54 int vfs_phone = -1;
    55 futex_t vfs_phone_futex = FUTEX_INITIALIZER;
    56 
    57 futex_t cwd_futex = FUTEX_INITIALIZER;
     52#include <ipc/vfs.h>
     53#include <ipc/devmap.h>
     54
     55static int vfs_phone = -1;
     56static futex_t vfs_phone_futex = FUTEX_INITIALIZER;
     57static futex_t cwd_futex = FUTEX_INITIALIZER;
     58
    5859DIR *cwd_dir = NULL;
    5960char *cwd_path = NULL;
     
    211212        futex_up(&vfs_phone_futex);
    212213        free(pa);
    213 
     214       
    214215        if (rc != EOK)
    215216            return (int) rc;
     217       
    216218        return (int) IPC_GET_ARG1(answer);
    217219}
     
    220222{
    221223        return _open(path, L_FILE, oflag);
     224}
     225
     226int open_node(fs_node_t *node, int oflag)
     227{
     228        futex_down(&vfs_phone_futex);
     229        async_serialize_start();
     230        vfs_connect();
     231       
     232        ipc_call_t answer;
     233        aid_t req = async_send_4(vfs_phone, VFS_OPEN_NODE, node->fs_handle,
     234            node->dev_handle, node->index, oflag, &answer);
     235       
     236        ipcarg_t rc;
     237        async_wait_for(req, &rc);
     238        async_serialize_end();
     239        futex_up(&vfs_phone_futex);
     240       
     241        if (rc != EOK)
     242            return (int) rc;
     243       
     244        return (int) IPC_GET_ARG1(answer);
    222245}
    223246
     
    290313        else
    291314                return -1;
     315}
     316
     317int fd_phone(int fildes)
     318{
     319        futex_down(&vfs_phone_futex);
     320        async_serialize_start();
     321        vfs_connect();
     322       
     323        ipcarg_t device;
     324        ipcarg_t rc = async_req_1_1(vfs_phone, VFS_DEVICE, fildes, &device);
     325       
     326        async_serialize_end();
     327        futex_up(&vfs_phone_futex);
     328       
     329        if (rc != EOK)
     330                return -1;
     331       
     332        return devmap_device_connect((dev_handle_t) device, 0);
     333}
     334
     335void fd_node(int fildes, fs_node_t *node)
     336{
     337        futex_down(&vfs_phone_futex);
     338        async_serialize_start();
     339        vfs_connect();
     340       
     341        ipcarg_t fs_handle;
     342        ipcarg_t dev_handle;
     343        ipcarg_t index;
     344        ipcarg_t rc = async_req_1_3(vfs_phone, VFS_NODE, fildes, &fs_handle,
     345            &dev_handle, &index);
     346       
     347        async_serialize_end();
     348        futex_up(&vfs_phone_futex);
     349       
     350        if (rc == EOK) {
     351                node->fs_handle = (fs_handle_t) fs_handle;
     352                node->dev_handle = (dev_handle_t) dev_handle;
     353                node->index = (fs_index_t) index;
     354        } else {
     355                node->fs_handle = 0;
     356                node->dev_handle = 0;
     357                node->index = 0;
     358        }
     359}
     360
     361int fsync(int fildes)
     362{
     363        futex_down(&vfs_phone_futex);
     364        async_serialize_start();
     365        vfs_connect();
     366       
     367        ipcarg_t rc = async_req_1_0(vfs_phone, VFS_SYNC, fildes);
     368       
     369        async_serialize_end();
     370        futex_up(&vfs_phone_futex);
     371       
     372        return (int) rc;
    292373}
    293374
     
    387468        futex_up(&vfs_phone_futex);
    388469        free(pa);
    389         return rc; 
     470        return rc;
    390471}
    391472
     
    417498        futex_up(&vfs_phone_futex);
    418499        free(pa);
    419         return rc; 
     500        return rc;
    420501}
    421502
  • uspace/lib/libc/include/dirent.h

    rca3ba3a r2595dab  
    3636#define LIBC_DIRENT_H_
    3737
    38 #define NAME_MAX        256     
     38#define NAME_MAX  256
    3939
    4040struct dirent {
     
    4747} DIR;
    4848
    49 
    5049extern DIR *opendir(const char *);
    5150extern struct dirent *readdir(DIR *);
  • uspace/lib/libc/include/fcntl.h

    rca3ba3a r2595dab  
    3636#define LIBC_FCNTL_H_
    3737
    38 #define O_CREAT         1
    39 #define O_EXCL          2
    40 #define O_TRUNC         4
    41 #define O_APPEND        8
    42 #define O_RDONLY        16
    43 #define O_RDWR          32
    44 #define O_WRONLY        64
     38#define O_CREAT   1
     39#define O_EXCL    2
     40#define O_TRUNC   4
     41#define O_APPEND  8
     42#define O_RDONLY  16
     43#define O_RDWR    32
     44#define O_WRONLY  64
    4545
    4646extern int open(const char *, int, ...);
  • uspace/lib/libc/include/fibril.h

    rca3ba3a r2595dab  
    4141
    4242#ifndef context_set
    43 #define context_set(c, _pc, stack, size, ptls)                  \
    44         (c)->pc = (sysarg_t) (_pc);                             \
    45         (c)->sp = ((sysarg_t) (stack)) + (size) - SP_DELTA;     \
    46         (c)->tls = (sysarg_t) (ptls);
     43#define context_set(c, _pc, stack, size, ptls) \
     44        (c)->pc = (sysarg_t) (_pc); \
     45        (c)->sp = ((sysarg_t) (stack)) + (size) - SP_DELTA; \
     46        (c)->tls = (sysarg_t) (ptls);
    4747#endif /* context_set */
    4848
    49 #define FIBRIL_SERIALIZED   1
     49#define FIBRIL_SERIALIZED  1
    5050
    5151typedef enum {
  • uspace/lib/libc/include/io/color.h

    rca3ba3a r2595dab  
    2828
    2929/** @addtogroup libc
    30  * @{ 
     30 * @{
    3131 */
    3232/** @file
    3333 */
    3434
    35 #ifndef LIBC_CONSOLE_COLOR_H_
    36 #define LIBC_CONSOLE_COLOR_H_
     35#ifndef LIBC_IO_COLOR_H_
     36#define LIBC_IO_COLOR_H_
    3737
    3838enum console_color {
    39         COLOR_BLACK     = 0,
    40         COLOR_BLUE      = 1,
    41         COLOR_GREEN     = 2,
    42         COLOR_CYAN      = 3,
    43         COLOR_RED       = 4,
    44         COLOR_MAGENTA   = 5,
    45         COLOR_YELLOW    = 6,
    46         COLOR_WHITE     = 7,
    47 
    48         CATTR_BRIGHT    = 8,
    49         CATTR_BLINK     = 8
     39        COLOR_BLACK   = 0,
     40        COLOR_BLUE    = 1,
     41        COLOR_GREEN   = 2,
     42        COLOR_CYAN    = 3,
     43        COLOR_RED     = 4,
     44        COLOR_MAGENTA = 5,
     45        COLOR_YELLOW  = 6,
     46        COLOR_WHITE   = 7,
     47       
     48        CATTR_BRIGHT  = 8,
     49        CATTR_BLINK   = 8
    5050};
    5151
    5252#endif
    53  
     53
    5454/** @}
    5555 */
  • uspace/lib/libc/include/io/console.h

    rca3ba3a r2595dab  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2008 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    2828
    2929/** @addtogroup libc
    30  * @{ 
     30 * @{
    3131 */
    3232/** @file
    3333 */
    3434
    35 #ifndef LIBC_KBD_H_
    36 #define LIBC_KBD_H_
     35#ifndef LIBC_IO_CONSOLE_H_
     36#define LIBC_IO_CONSOLE_H_
    3737
    38 #include <sys/types.h>
     38#include <ipc/ipc.h>
     39#include <bool.h>
    3940
    40 typedef enum kbd_ev_type {
    41         KE_PRESS,
    42         KE_RELEASE
    43 } kbd_ev_type_t;
     41typedef enum {
     42        KEY_PRESS,
     43        KEY_RELEASE
     44} console_ev_type_t;
    4445
    45 /** Keyboard event structure. */
     46/** Console event structure. */
    4647typedef struct {
    4748        /** Press or release event. */
    48         kbd_ev_type_t type;
    49 
     49        console_ev_type_t type;
     50       
    5051        /** Keycode of the key that was pressed or released. */
    5152        unsigned int key;
    52 
     53       
    5354        /** Bitmask of modifiers held. */
    5455        unsigned int mods;
    55 
     56       
    5657        /** The character that was generated or '\0' for none. */
    5758        wchar_t c;
    58 } kbd_event_t;
     59} console_event_t;
    5960
    60 extern int kbd_get_event(kbd_event_t *);
     61extern void console_clear(int phone);
     62
     63extern int console_get_size(int phone, ipcarg_t *rows, ipcarg_t *cols);
     64extern void console_goto(int phone, ipcarg_t row, ipcarg_t col);
     65
     66extern void console_set_style(int phone, int style);
     67extern void console_set_color(int phone, int fg_color, int bg_color, int flags);
     68extern void console_set_rgb_color(int phone, int fg_color, int bg_color);
     69
     70extern void console_cursor_visibility(int phone, bool show);
     71extern void console_kcon_enable(int phone);
     72
     73extern bool console_get_event(int phone, console_event_t *event);
    6174
    6275#endif
    63  
     76
    6477/** @}
    6578 */
  • uspace/lib/libc/include/io/keycode.h

    rca3ba3a r2595dab  
    2828
    2929/** @addtogroup libc
    30  * @{ 
     30 * @{
    3131 */
    3232/** @file
    3333 */
    3434
    35 #ifndef LIBC_KBD_KEYCODE_H_
    36 #define LIBC_KBD_KEYCODE_H_
     35#ifndef LIBC_IO_KEYCODE_H_
     36#define LIBC_IO_KEYCODE_H_
    3737
    3838/** Keycode definitions.
     
    200200
    201201enum keymod {
    202         KM_LSHIFT       = 0x001,
    203         KM_RSHIFT       = 0x002,
    204         KM_LCTRL        = 0x004,
    205         KM_RCTRL        = 0x008,
    206         KM_LALT         = 0x010,
    207         KM_RALT         = 0x020,
    208         KM_CAPS_LOCK    = 0x040,
    209         KM_NUM_LOCK     = 0x080,
    210         KM_SCROLL_LOCK  = 0x100,
    211 
    212         KM_SHIFT        = KM_LSHIFT | KM_RSHIFT,
    213         KM_CTRL         = KM_LCTRL | KM_RCTRL,
    214         KM_ALT          = KM_LALT | KM_RALT
     202        KM_LSHIFT      = 0x001,
     203        KM_RSHIFT      = 0x002,
     204        KM_LCTRL       = 0x004,
     205        KM_RCTRL       = 0x008,
     206        KM_LALT        = 0x010,
     207        KM_RALT        = 0x020,
     208        KM_CAPS_LOCK   = 0x040,
     209        KM_NUM_LOCK    = 0x080,
     210        KM_SCROLL_LOCK = 0x100,
     211       
     212        KM_SHIFT       = KM_LSHIFT | KM_RSHIFT,
     213        KM_CTRL        = KM_LCTRL | KM_RCTRL,
     214        KM_ALT         = KM_LALT | KM_RALT
    215215} keymod_t;
    216216
    217217#endif
    218  
     218
    219219/** @}
    220220 */
  • uspace/lib/libc/include/io/klog.h

    rca3ba3a r2595dab  
    3636#define LIBC_STREAM_H_
    3737
    38 #include <libarch/types.h>
     38#include <sys/types.h>
    3939
    40 #define EMFILE  -17
    41 
    42 extern ssize_t read_stdin(void *, size_t);
    43 extern int klog_puts(const char *);
     40extern size_t klog_write(const void *buf, size_t size);
    4441extern void klog_update(void);
    4542
  • uspace/lib/libc/include/io/printf_core.h

    rca3ba3a r2595dab  
    5757/** @}
    5858 */
    59  
    60  
  • uspace/lib/libc/include/io/style.h

    rca3ba3a r2595dab  
    2828
    2929/** @addtogroup libc
    30  * @{ 
     30 * @{
    3131 */
    3232/** @file
    3333 */
    3434
    35 #ifndef LIBC_CONSOLE_STYLE_H_
    36 #define LIBC_CONSOLE_STYLE_H_
     35#ifndef LIBC_IO_STYLE_H_
     36#define LIBC_IO_STYLE_H_
    3737
    3838enum console_style {
    39         STYLE_NORMAL    = 0,
    40         STYLE_EMPHASIS  = 1
     39        STYLE_NORMAL   = 0,
     40        STYLE_EMPHASIS = 1
    4141};
    4242
    4343#endif
    44  
     44
    4545/** @}
    4646 */
  • uspace/lib/libc/include/stdio.h

    rca3ba3a r2595dab  
    3939#include <stdarg.h>
    4040
    41 #define EOF (-1)
    42 
    43 #include <string.h>
    44 #include <io/stream.h>
     41#define EOF  (-1)
    4542
    4643#define DEBUG(fmt, ...) \
    4744{ \
    4845        char buf[256]; \
    49         int n; \
    50         n = snprintf(buf, sizeof(buf), fmt, ##__VA_ARGS__); \
     46        int n = snprintf(buf, sizeof(buf), fmt, ##__VA_ARGS__); \
    5147        if (n > 0) \
    5248                (void) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) buf, str_size(buf)); \
    5349}
    5450
     51#ifndef SEEK_SET
     52        #define SEEK_SET  0
     53        #define SEEK_CUR  1
     54        #define SEEK_END  2
     55#endif
     56
    5557typedef struct {
    5658        /** Underlying file descriptor. */
    5759        int fd;
    58 
     60       
    5961        /** Error indicator. */
    6062        int error;
    61 
     63       
    6264        /** End-of-file indicator. */
    6365        int eof;
     66       
     67        /** Klog indicator */
     68        int klog;
     69       
     70        /** Phone to the file provider */
     71        int phone;
    6472} FILE;
    6573
    66 extern FILE *stdin, *stdout, *stderr;
     74extern FILE stdin_null;
     75extern FILE stdout_klog;
     76
     77extern FILE *stdin;
     78extern FILE *stdout;
     79extern FILE *stderr;
     80
     81/* Character and string input functions */
     82extern int fgetc(FILE *);
     83extern char *fgets(char *, size_t, FILE *);
    6784
    6885extern int getchar(void);
     86extern char *gets(char *, size_t);
    6987
     88/* Character and string output functions */
     89extern int fputc(wchar_t, FILE *);
     90extern int fputs(const char *, FILE *);
     91
     92extern int putchar(wchar_t);
    7093extern int puts(const char *);
    71 extern int putchar(int);
    72 extern int fflush(FILE *);
     94
     95/* Formatted string output functions */
     96extern int fprintf(FILE *, const char*, ...);
     97extern int vfprintf(FILE *, const char *, va_list);
    7398
    7499extern int printf(const char *, ...);
     100extern int vprintf(const char *, va_list);
     101
     102extern int snprintf(char *, size_t , const char *, ...);
    75103extern int asprintf(char **, const char *, ...);
    76 extern int sprintf(char *, const char *, ...);
    77 extern int snprintf(char *, size_t , const char *, ...);
    78 
    79 extern int vprintf(const char *, va_list);
    80 extern int vsprintf(char *, const char *, va_list);
    81104extern int vsnprintf(char *, size_t, const char *, va_list);
    82105
    83 extern int rename(const char *, const char *);
    84 
     106/* File stream functions */
    85107extern FILE *fopen(const char *, const char *);
    86108extern int fclose(FILE *);
     109
    87110extern size_t fread(void *, size_t, size_t, FILE *);
    88111extern size_t fwrite(const void *, size_t, size_t, FILE *);
     112
     113extern int fseek(FILE *, long, int);
     114extern int ftell(FILE *);
    89115extern int feof(FILE *);
     116
     117extern int fflush(FILE *);
    90118extern int ferror(FILE *);
    91119extern void clearerr(FILE *);
    92120
    93 extern int fgetc(FILE *);
    94 extern int fputc(int, FILE *);
    95 extern int fputs(const char *, FILE *);
    96 
    97 extern int fprintf(FILE *, const char *, ...);
    98 extern int vfprintf(FILE *, const char *, va_list);
    99 
    100 #define getc fgetc
    101 #define putc fputc
    102 
    103 extern int fseek(FILE *, long, int);
    104 
    105 #ifndef SEEK_SET
    106         #define SEEK_SET        0
    107         #define SEEK_CUR        1
    108         #define SEEK_END        2
    109 #endif
     121/* Misc file functions */
     122extern int rename(const char *, const char *);
    110123
    111124#endif
  • uspace/lib/libc/include/unistd.h

    rca3ba3a r2595dab  
    4040
    4141#ifndef NULL
    42 #define NULL 0
     42        #define NULL 0
    4343#endif
    4444
    45 #define getpagesize()     (PAGE_SIZE)
     45#define getpagesize()  (PAGE_SIZE)
    4646
    4747#ifndef SEEK_SET
    48         #define SEEK_SET        0
    49         #define SEEK_CUR        1
    50         #define SEEK_END        2
     48        #define SEEK_SET  0
     49        #define SEEK_CUR  1
     50        #define SEEK_END  2
    5151#endif
    5252
    5353extern ssize_t write(int, const void *, size_t);
    5454extern ssize_t read(int, void *, size_t);
     55
    5556extern off_t lseek(int, off_t, int);
    5657extern int ftruncate(int, off_t);
     58
    5759extern int close(int);
     60extern int fsync(int);
    5861extern int unlink(const char *);
     62
     63extern char *getcwd(char *buf, size_t);
    5964extern int rmdir(const char *);
    6065extern int chdir(const char *);
    61 extern char *getcwd(char *buf, size_t);
    6266
    6367extern void _exit(int status) __attribute__ ((noreturn));
  • uspace/lib/libc/include/vfs/vfs.h

    rca3ba3a r2595dab  
    3737
    3838#include <sys/types.h>
     39#include <ipc/vfs.h>
     40#include <ipc/devmap.h>
     41#include <stdio.h>
     42
     43typedef struct {
     44        fs_handle_t fs_handle;
     45        dev_handle_t dev_handle;
     46        fs_index_t index;
     47} fs_node_t;
    3948
    4049extern char *absolutize(const char *, size_t *);
     
    4352    unsigned int flags);
    4453
     54extern int open_node(fs_node_t *node, int oflag);
     55extern int fd_phone(int);
     56extern void fd_node(int, fs_node_t *);
     57
     58extern FILE *fopen_node(fs_node_t *node, const char *);
     59extern int fphone(FILE *);
     60extern void fnode(FILE *stream, fs_node_t *node);
     61
    4562#endif
    4663
Note: See TracChangeset for help on using the changeset viewer.