New file, incorporated from 4.4BSD-Lite for compatibility.
[glibc/history.git] / stdio / glue.c
blob6ef52a7adaef7b70876428039ff6aad136984e0a
1 /* Copyright (C) 1991, 1992 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, 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 <ansidecl.h>
31 #include <stdio.h>
32 #include <errno.h>
34 typedef union
36 struct
38 int magic;
39 FILE **streamp; /* Overlaps GNU stdio `bufp' member. */
40 /* These two overlap the GNU stdio `get_limit' and `put_limit'
41 members. They must be <= `streamp'/`bufp' for GNU getc and putc
42 to do the right thing. */
43 FILE **streamp2, **streamp3;
44 } glue;
45 struct _iobuf
47 int _cnt;
48 unsigned char *_ptr;
49 unsigned char *_base;
50 int _bufsiz;
51 short int _flag;
52 char _file;
53 } unix_iobuf;
54 FILE gnu_stream;
55 } unix_FILE;
57 /* These are the Unix stdio's stdin, stdout, and stderr.
58 In Unix stdin is (&_iob[0]), stdout is (&_iob[1]), and stderr is
59 (&_iob[2]). The magic number marks these as glued streams. The
60 __validfp macro in stdio.h is used by every stdio function. It checks
61 for glued streams, and replaces them with the GNU stdio stream. */
62 unix_FILE _iob[] =
64 #define S(name) { { _GLUEMAGIC, &name, &name, &name } }
65 S (stdin),
66 S (stdout),
67 S (stderr),
68 #undef S
71 /* Called by the Unix stdio `getc' macro.
72 The macro is assumed to look something like:
73 (--file->_cnt < 0 ? _filbuf (file) ...)
74 In a Unix stdio FILE `_cnt' is the first element.
75 In a GNU stdio or glued FILE, the first element is the magic number. */
76 int
77 DEFUN(_filbuf, (file), unix_FILE *file)
79 switch (++file->glue.magic) /* Compensate for Unix getc's decrement. */
81 case _GLUEMAGIC:
82 /* This is a glued stream. */
83 return getc (*file->glue.streamp);
85 case _IOMAGIC:
86 /* This is a normal GNU stdio stream. */
87 return getc ((FILE *) file);
89 default:
90 /* Bogus stream. */
91 errno = EINVAL;
92 return EOF;
96 /* Called by the Unix stdio `putc' macro. Much like getc, above. */
97 int
98 DEFUN(_flsbuf, (c, file),
99 int c AND unix_FILE *file)
101 /* Compensate for putc's decrement. */
102 switch (++file->glue.magic)
104 case _GLUEMAGIC:
105 return putc (c, *file->glue.streamp);
107 case _IOMAGIC:
108 return putc (c, (FILE *) file);
110 default:
111 errno = EINVAL;
112 return EOF;