Update.
[glibc.git] / stdio / glue.c
blobb184524c822b5e48d29dc1bbb782d8f95a029e99
1 /* Copyright (C) 1991, 1992, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* This file provides glue between Unix stdio and GNU stdio.
20 It supports use of Unix stdio `getc' and `putc' (and, by extension,
21 `getchar' and `putchar') macros on GNU stdio streams (they are slow, but
22 they work). It also supports all stdio operations (including Unix
23 `getc' and `putc') on Unix's stdin, stdout, and stderr (the elements of
24 `_iob').
26 The reasoning behind this is to allow programs (and especially
27 libraries) compiled with Unix header files to work with the GNU C
28 library. */
30 #include <stdio.h>
31 #include <errno.h>
33 typedef union
35 struct
37 int magic;
38 FILE **streamp; /* Overlaps GNU stdio `bufp' member. */
39 /* These two overlap the GNU stdio `get_limit' and `put_limit'
40 members. They must be <= `streamp'/`bufp' for GNU getc and putc
41 to do the right thing. */
42 FILE **streamp2, **streamp3;
43 } glue;
44 struct _iobuf
46 int _cnt;
47 unsigned char *_ptr;
48 unsigned char *_base;
49 int _bufsiz;
50 short int _flag;
51 char _file;
52 } unix_iobuf;
53 FILE gnu_stream;
54 } unix_FILE;
56 /* These are the Unix stdio's stdin, stdout, and stderr.
57 In Unix stdin is (&_iob[0]), stdout is (&_iob[1]), and stderr is
58 (&_iob[2]). The magic number marks these as glued streams. The
59 __validfp macro in stdio.h is used by every stdio function. It checks
60 for glued streams, and replaces them with the GNU stdio stream. */
61 unix_FILE _iob[] =
63 #define S(name) { { _GLUEMAGIC, &name, &name, &name } }
64 S (stdin),
65 S (stdout),
66 S (stderr),
67 #undef S
70 /* Called by the Unix stdio `getc' macro.
71 The macro is assumed to look something like:
72 (--file->_cnt < 0 ? _filbuf (file) ...)
73 In a Unix stdio FILE `_cnt' is the first element.
74 In a GNU stdio or glued FILE, the first element is the magic number. */
75 int
76 _filbuf (unix_FILE *file)
78 switch (++file->glue.magic) /* Compensate for Unix getc's decrement. */
80 case _GLUEMAGIC:
81 /* This is a glued stream. */
82 return getc (*file->glue.streamp);
84 case _IOMAGIC:
85 /* This is a normal GNU stdio stream. */
86 return getc ((FILE *) file);
88 default:
89 /* Bogus stream. */
90 __set_errno (EINVAL);
91 return EOF;
95 /* Called by the Unix stdio `putc' macro. Much like getc, above. */
96 int
97 _flsbuf (int c, unix_FILE *file)
99 /* Compensate for putc's decrement. */
100 switch (++file->glue.magic)
102 case _GLUEMAGIC:
103 return putc (c, *file->glue.streamp);
105 case _IOMAGIC:
106 return putc (c, (FILE *) file);
108 default:
109 __set_errno (EINVAL);
110 return EOF;