source: mainline/uspace/lib/libc/generic/io/io.c@ b78d0bd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b78d0bd was 566f4cfb, checked in by Martin Decky <martin@…>, 17 years ago

use buffering for klog output (this can be used to avoid the ugly usleeps while starting tasks)
unify and cleanup console.c and related files

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[b861b58]1/*
[df4ed85]2 * Copyright (c) 2005 Martin Decky
[b861b58]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
[cc6f688]33 */
[b861b58]34
35#include <libc.h>
[cc6f688]36#include <unistd.h>
37#include <stdio.h>
[4e2cf8b]38#include <io/io.h>
[56fa418]39#include <string.h>
40#include <errno.h>
[566f4cfb]41#include <console.h>
[b861b58]42
[3dbe2d1f]43const static char nl = '\n';
[350514c]44
[3dbe2d1f]45int puts(const char *str)
[b861b58]46{
[cc6f688]47 size_t count;
48
[3dbe2d1f]49 if (str == NULL)
50 return putnchars("(NULL)", 6);
[c9857c6]51
[cc6f688]52 for (count = 0; str[count] != 0; count++);
[566f4cfb]53
54 if (console_write((void *) str, count) == count) {
55 if (console_write(&nl, 1) == 1)
[350514c]56 return 0;
57 }
58
59 return EOF;
[cc6f688]60}
[b861b58]61
[4e2cf8b]62/** Put count chars from buffer to stdout without adding newline
[c9857c6]63 * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
[4e2cf8b]64 * @param count
65 * @return 0 on succes, EOF on fail
66 */
[3dbe2d1f]67int putnchars(const char *buf, size_t count)
[4e2cf8b]68{
[566f4cfb]69 if (console_write((void *) buf, count) == count)
[ff9244a]70 return 0;
[4e2cf8b]71
72 return EOF;
73}
74
75/** Same as puts, but does not print newline at end
76 *
77 */
[3dbe2d1f]78int putstr(const char *str)
[4e2cf8b]79{
80 size_t count;
81
[3dbe2d1f]82 if (str == NULL)
83 return putnchars("(NULL)", 6);
[55cff86]84
[4e2cf8b]85 for (count = 0; str[count] != 0; count++);
[566f4cfb]86 if (console_write((void *) str, count) == count)
[ff9244a]87 return 0;
[4e2cf8b]88
89 return EOF;
90}
91
[c9857c6]92int putchar(int c)
93{
[56fa418]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)
99 return EOF;
100
[566f4cfb]101 if (console_write((void *) buf, offs) == offs)
[ff9244a]102 return c;
[56fa418]103
[c9857c6]104 return EOF;
105}
[b27a97bb]106
107int getchar(void)
108{
109 unsigned char c;
[566f4cfb]110
111 console_flush();
[ff9244a]112 if (read_stdin((void *) &c, 1) == 1)
113 return c;
[b27a97bb]114
115 return EOF;
116}
117
[d2cc7e1]118int fflush(FILE *f)
119{
[566f4cfb]120 /* Dummy implementation */
[d2cc7e1]121 (void) f;
[566f4cfb]122 console_flush();
123 return 0;
[d2cc7e1]124}
125
[fadd381]126/** @}
[b2951e2]127 */
Note: See TracBrowser for help on using the repository browser.