Update.
[glibc.git] / sysdeps / mach / hurd / defs.c
blobf60e50b9cb6f0ea0c27e197a7b7fbccca9097552
1 /* Definitions of global stdio data structures.
2 Copyright (C) 1991,92,93,94,95,97,2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdio.h>
21 #include <hurd/fd.h>
22 #include <unistd.h>
24 FILE *stdin, *stdout, *stderr;
26 /* Pointer to the first stream in the list. */
27 FILE *__stdio_head = NULL;
29 /* XXX should be __init_stdio? */
30 static void
31 init_stdio (void)
33 inline void init (FILE **streamptr, int fd)
35 /* We want to use the existing FILE object if one has been allocated.
36 (This will only be the case if our image came from something like
37 Emacs's unexec, where we were called in the first run.) */
38 FILE *s = *streamptr ?: __newstream ();
39 struct hurd_fd *d = _hurd_fd_get (fd);
40 if (d == NULL)
42 /* There is no file descriptor allocated. We want the standard
43 streams to always refer to their standard file descriptors, even
44 if those descriptors are not set up until later. So allocate
45 the descriptor structure with no ports and store it in the
46 stream. Operations will fail until ports are installed in the
47 file descriptor. */
48 if (d = _hurd_alloc_fd (NULL, fd))
49 __spin_unlock (&d->port.lock);
51 if (s)
52 s->__cookie = d;
53 *streamptr = s;
55 #define S(NAME, FD, MODE) \
56 init (&NAME, FD); if (NAME) NAME->__mode.__##MODE = 1;
58 S (stdin, STDIN_FILENO, read);
59 S (stdout, STDOUT_FILENO, write);
60 S (stderr, STDERR_FILENO, write);
62 #undef S
64 if (stderr)
65 stderr->__userbuf = 1; /* stderr is always unbuffered. */
67 (void) &init_stdio; /* Avoid "defined but not used" warning. */
69 /* This initializer will be run along with other vanilla libc initializers
70 in a normal Posixoid environment. The earlier Hurd-speciifc initializer
71 phases cannot use normal facilities like malloc (which stdio uses). */
72 text_set_element (__libc_subinit, init_stdio);
74 /* This function MUST be in this file!
75 This is because we want _cleanup to go into the __libc_atexit set
76 when any stdio code is used (and to use any stdio code, one must reference
77 something defined in this file), and since only local symbols can be made
78 set elements, having the set element stab entry here and _cleanup elsewhere
79 loses; and having them both elsewhere loses because there is no reference
80 to cause _cleanup to be linked in. */
82 void
83 _cleanup (void)
85 __fcloseall ();
87 text_set_element (__libc_atexit, _cleanup);