git repos for lemote 502 board
[pmon-gdium.git] / lib / libc / stdio.c
blob2996f8e98eea10117f9367dd1ecd8fa67da142aa
1 /* $Id: stdio.c,v 1.1.1.1 2006/09/14 01:59:06 root Exp $ */
3 /*
4 * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
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 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. 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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Opsycon AB.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <string.h>
34 #include <stdio.h>
35 #include <fcntl.h>
37 /*************************************************************
39 * fprintf --\ putchar ------ putc --\
40 * | |
41 * printf --+-- vfprintf --+-- fputs --+-- write
42 * | |
43 * puts --/ fwrite --/
46 * getchar ----- getc --+-- fgetc ----- read
47 * |
48 * gets ---- fgets --/
50 *************************************************************/
52 FILE _iob[OPEN_MAX] =
54 {0, 1},
55 {1, 1},
56 {2, 1},
57 {3, 1},
58 {4, 1},
61 FILE *
62 fopen(const char *fname, const char *mode)
64 int i, fd, flags;
66 for (i = 0; i < OPEN_MAX && _iob[i].valid; i++);
67 if (i == OPEN_MAX)
68 return (0);
69 if (mode == 0)
70 flags = O_RDONLY;
71 else if (!strcmp(mode, "r"))
72 flags = O_RDONLY;
73 else if (!strcmp(mode, "w"))
74 flags = O_WRONLY;
75 else if (!strcmp(mode, "r+"))
76 flags = O_RDWR;
77 fd = open (fname, flags, 0);
78 if (fd == -1)
79 return (0);
80 _iob[i].fd = fd;
81 _iob[i].valid = 1;
82 return (&_iob[i]);