source: mainline/uspace/lib/c/generic/io/kio.c@ fcab7ef

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fcab7ef was 6afc9d7, checked in by Jiri Svoboda <jiri@…>, 10 years ago

UNIX-like I/O functions should use errno to return error code for many reasons.

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[b861b58]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[2595dab]3 * Copyright (c) 2006 Jakub Vana
[b861b58]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.
[4e2cf8b]28 */
[b861b58]29
[fadd381]30/** @addtogroup libc
[b2951e2]31 * @{
32 */
33/** @file
34 */
35
[2595dab]36#include <libc.h>
[19f857a]37#include <str.h>
[5d4e90f0]38#include <sys/types.h>
[2595dab]39#include <unistd.h>
[d8de5d3]40#include <errno.h>
[6fa9a99d]41#include <abi/kio.h>
42#include <io/kio.h>
[d8de5d3]43#include <io/printf_core.h>
[350514c]44
[6afc9d7]45int kio_write(const void *buf, size_t size, size_t *nwritten)
[2595dab]46{
[6afc9d7]47 int rc = (int) __SYSCALL3(SYS_KIO, KIO_WRITE, (sysarg_t) buf, size);
[a217358]48
[6afc9d7]49 if (rc == EOK)
50 *nwritten = size;
51 return rc;
[2595dab]52}
[b861b58]53
[6fa9a99d]54void kio_update(void)
[2595dab]55{
[6fa9a99d]56 (void) __SYSCALL3(SYS_KIO, KIO_UPDATE, (uintptr_t) NULL, 0);
[2595dab]57}
[b2951e2]58
[6fa9a99d]59void kio_command(const void *buf, size_t size)
[ec85df0]60{
[6fa9a99d]61 (void) __SYSCALL3(SYS_KIO, KIO_COMMAND, (sysarg_t) buf, (sysarg_t) size);
[ec85df0]62}
63
[6fa9a99d]64/** Print formatted text to kio.
[d8de5d3]65 *
66 * @param fmt Format string
67 *
68 * \see For more details about format string see printf_core.
69 *
70 */
[6fa9a99d]71int kio_printf(const char *fmt, ...)
[d8de5d3]72{
73 va_list args;
74 va_start(args, fmt);
75
[6fa9a99d]76 int ret = kio_vprintf(fmt, args);
[d8de5d3]77
78 va_end(args);
79
80 return ret;
81}
82
[6fa9a99d]83static int kio_vprintf_str_write(const char *str, size_t size, void *data)
[d8de5d3]84{
[6afc9d7]85 size_t wr;
86
87 wr = 0;
88 (void) kio_write(str, size, &wr);
[d8de5d3]89 return str_nlength(str, wr);
90}
91
[6fa9a99d]92static int kio_vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
[d8de5d3]93{
94 size_t offset = 0;
95 size_t chars = 0;
[6afc9d7]96 size_t wr;
[d8de5d3]97
98 while (offset < size) {
99 char buf[STR_BOUNDS(1)];
100 size_t sz = 0;
101
102 if (chr_encode(str[chars], buf, &sz, STR_BOUNDS(1)) == EOK)
[6afc9d7]103 kio_write(buf, sz, &wr);
[d8de5d3]104
105 chars++;
106 offset += sizeof(wchar_t);
107 }
108
109 return chars;
110}
111
[6fa9a99d]112/** Print formatted text to kio.
[d8de5d3]113 *
114 * @param fmt Format string
115 * @param ap Format parameters
116 *
117 * \see For more details about format string see printf_core.
118 *
119 */
[6fa9a99d]120int kio_vprintf(const char *fmt, va_list ap)
[d8de5d3]121{
122 printf_spec_t ps = {
[6fa9a99d]123 kio_vprintf_str_write,
124 kio_vprintf_wstr_write,
[d8de5d3]125 NULL
126 };
127
128 return printf_core(fmt, &ps, ap);
129}
130
[fadd381]131/** @}
[b2951e2]132 */
Note: See TracBrowser for help on using the repository browser.