1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 #include <string.h> /* for memset() */
43 /************************************************************************/
45 PRLock *_pr_flock_lock;
46 PRCondVar *_pr_flock_cv;
50 * There are no stdin, stdout, stderr in Windows CE. INVALID_HANDLE_VALUE
51 * should cause all I/O functions on the handle to fail.
53 #define STD_INPUT_HANDLE ((DWORD)-10)
54 #define STD_OUTPUT_HANDLE ((DWORD)-11)
55 #define STD_ERROR_HANDLE ((DWORD)-12)
57 static HANDLE GetStdHandle(DWORD nStdHandle)
59 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
60 return INVALID_HANDLE_VALUE;
66 const PRIOMethods *methods = PR_GetFileMethods();
70 _pr_flock_lock = PR_NewLock();
71 _pr_flock_cv = PR_NewCondVar(_pr_flock_lock);
74 _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE),
76 _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE),
78 _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE),
81 _pr_stdin->secret->md.sync_file_io = PR_TRUE;
82 _pr_stdout->secret->md.sync_file_io = PR_TRUE;
83 _pr_stderr->secret->md.sync_file_io = PR_TRUE;
86 _pr_stdin = PR_AllocFileDesc(0, methods);
87 _pr_stdout = PR_AllocFileDesc(1, methods);
88 _pr_stderr = PR_AllocFileDesc(2, methods);
90 _PR_MD_INIT_FD_INHERITABLE(_pr_stdin, PR_TRUE);
91 _PR_MD_INIT_FD_INHERITABLE(_pr_stdout, PR_TRUE);
92 _PR_MD_INIT_FD_INHERITABLE(_pr_stderr, PR_TRUE);
97 void _PR_CleanupIO(void)
99 PR_FreeFileDesc(_pr_stdin);
101 PR_FreeFileDesc(_pr_stdout);
103 PR_FreeFileDesc(_pr_stderr);
107 PR_DestroyCondVar(_pr_flock_cv);
110 if (_pr_flock_lock) {
111 PR_DestroyLock(_pr_flock_lock);
112 _pr_flock_lock = NULL;
115 _PR_CleanupFdCache();
118 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
120 PRFileDesc *result = NULL;
121 PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
123 if (!_pr_initialized) _PR_ImplicitInitialization();
127 case PR_StandardInput: result = _pr_stdin; break;
128 case PR_StandardOutput: result = _pr_stdout; break;
129 case PR_StandardError: result = _pr_stderr; break;
131 (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
136 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
137 PROsfd osfd, const PRIOMethods *methods)
143 * Assert that the file descriptor is small enough to fit in the
144 * fd_set passed to select
146 PR_ASSERT(osfd < FD_SETSIZE);
150 /* Initialize the members of PRFileDesc and PRFilePrivate */
151 fd->methods = methods;
152 fd->secret->state = _PR_FILEDESC_OPEN;
153 fd->secret->md.osfd = osfd;
154 _PR_MD_INIT_FILEDESC(fd);
156 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
162 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
169 ** Wait for some i/o to finish on one or more more poll descriptors.
171 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
173 return(_PR_MD_PR_POLL(pds, npds, timeout));
177 ** Set the inheritance attribute of a file descriptor.
179 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
183 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS)
185 * Only a non-layered, NSPR file descriptor can be inherited
186 * by a child process.
188 if (fd->identity != PR_NSPR_IO_LAYER) {
189 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
192 if (fd->secret->inheritable != inheritable) {
193 if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
196 fd->secret->inheritable = inheritable;
200 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
206 ** This function only has a useful implementation in the debug build of
207 ** the pthreads version.
209 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
212 } /* PT_FPrintStats */