* posix/glob.h (__glob_opendir_hook, __glob_readdir_hook,
[glibc.git] / sysdeps / mach / hurd / defs.c
blobb2cfe7c4ff79244863848ce465bddab2cf57abca
1 /* Definitions of global stdio data structures.
3 Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
21 #include <ansidecl.h>
22 #include <stdio.h>
23 #include <hurd/fd.h>
24 #include <unistd.h>
26 FILE *stdin, *stdout, *stderr;
28 /* Pointer to the first stream in the list. */
29 FILE *__stdio_head = NULL;
31 static void
32 init_stdio (void)
34 inline void init (FILE **streamptr, int fd)
36 /* We want to use the existing FILE object if one has been allocated.
37 (This will only be the case if our image came from something like
38 Emacs's unexec, where we were called in the first run.) */
39 FILE *s = *streamptr ?: __newstream ();
40 struct hurd_fd *d = _hurd_fd_get (fd);
41 if (d == NULL)
43 /* There is no file descriptor allocated. We want the standard
44 streams to always refer to their standard file descriptors, even
45 if those descriptors are not set up until later. So allocate
46 the descriptor structure with no ports and store it in the
47 stream. Operations will fail until ports are installed in the
48 file descriptor. */
49 if (d = _hurd_alloc_fd (NULL, fd))
50 __spin_unlock (&d->port.lock);
52 if (s)
53 s->__cookie = d;
54 *streamptr = s;
56 #define S(NAME, FD, MODE) \
57 init (&NAME, FD); if (NAME) NAME->__mode.__##MODE = 1;
59 S (stdin, STDIN_FILENO, read);
60 S (stdout, STDOUT_FILENO, write);
61 S (stderr, STDERR_FILENO, write);
63 #undef S
65 if (stderr)
66 stderr->__userbuf = 1; /* stderr is always unbuffered. */
68 (void) &init_stdio; /* Avoid "defined but not used" warning. */
70 text_set_element (_hurd_fd_subinit, init_stdio);
72 /* This function MUST be in this file!
73 This is because we want _cleanup to go into the __libc_atexit set
74 when any stdio code is used (and to use any stdio code, one must reference
75 something defined in this file), and since only local symbols can be made
76 set elements, having the set element stab entry here and _cleanup elsewhere
77 loses; and having them both elsewhere loses because there is no reference
78 to cause _cleanup to be linked in. */
80 void
81 DEFUN_VOID(_cleanup)
83 (void) fclose ((FILE *) NULL);
85 text_set_element (__libc_atexit, _cleanup);