Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / src / io / prio.c
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
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.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include "primpl.h"
39
40 #include <string.h> /* for memset() */
41
42
43 /************************************************************************/
44
45 PRLock *_pr_flock_lock;
46 PRCondVar *_pr_flock_cv;
47
48 #ifdef WINCE
49 /*
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.
52  */
53 #define STD_INPUT_HANDLE  ((DWORD)-10)
54 #define STD_OUTPUT_HANDLE ((DWORD)-11)
55 #define STD_ERROR_HANDLE  ((DWORD)-12)
56
57 static HANDLE GetStdHandle(DWORD nStdHandle)
58 {
59     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
60     return INVALID_HANDLE_VALUE;
61 }
62 #endif
63
64 void _PR_InitIO(void)
65 {
66     const PRIOMethods *methods = PR_GetFileMethods();
67
68     _PR_InitFdCache();
69
70     _pr_flock_lock = PR_NewLock();
71     _pr_flock_cv = PR_NewCondVar(_pr_flock_lock);
72
73 #ifdef WIN32
74     _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE),
75             methods);
76     _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE),
77             methods);
78     _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE),
79             methods);
80 #ifdef WINNT
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;
84 #endif
85 #else
86     _pr_stdin = PR_AllocFileDesc(0, methods);
87     _pr_stdout = PR_AllocFileDesc(1, methods);
88     _pr_stderr = PR_AllocFileDesc(2, methods);
89 #endif
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);
93
94     _PR_MD_INIT_IO();
95 }
96
97 void _PR_CleanupIO(void)
98 {
99     PR_FreeFileDesc(_pr_stdin);
100     _pr_stdin = NULL;
101     PR_FreeFileDesc(_pr_stdout);
102     _pr_stdout = NULL;
103     PR_FreeFileDesc(_pr_stderr);
104     _pr_stderr = NULL;
105
106     if (_pr_flock_cv) {
107         PR_DestroyCondVar(_pr_flock_cv);
108         _pr_flock_cv = NULL;
109     }
110     if (_pr_flock_lock) {
111         PR_DestroyLock(_pr_flock_lock);
112         _pr_flock_lock = NULL;
113     }
114
115     _PR_CleanupFdCache();
116 }
117
118 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
119 {
120     PRFileDesc *result = NULL;
121     PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
122
123     if (!_pr_initialized) _PR_ImplicitInitialization();
124     
125     switch (osfd)
126     {
127         case PR_StandardInput: result = _pr_stdin; break;
128         case PR_StandardOutput: result = _pr_stdout; break;
129         case PR_StandardError: result = _pr_stderr; break;
130         default:
131             (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
132     }
133     return result;
134 }
135
136 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
137     PROsfd osfd, const PRIOMethods *methods)
138 {
139     PRFileDesc *fd;
140
141 #ifdef XP_UNIX
142         /*
143          * Assert that the file descriptor is small enough to fit in the
144          * fd_set passed to select
145          */
146         PR_ASSERT(osfd < FD_SETSIZE);
147 #endif
148     fd = _PR_Getfd();
149     if (fd) {
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);
155     } else {
156             PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
157     }
158
159     return fd;
160 }
161
162 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
163 {
164     PR_ASSERT(fd);
165     _PR_Putfd(fd);
166 }
167
168 /*
169 ** Wait for some i/o to finish on one or more more poll descriptors.
170 */
171 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
172 {
173         return(_PR_MD_PR_POLL(pds, npds, timeout));
174 }
175
176 /*
177 ** Set the inheritance attribute of a file descriptor.
178 */
179 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
180     PRFileDesc *fd,
181     PRBool inheritable)
182 {
183 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS)
184     /*
185      * Only a non-layered, NSPR file descriptor can be inherited
186      * by a child process.
187      */
188     if (fd->identity != PR_NSPR_IO_LAYER) {
189         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
190         return PR_FAILURE;
191     }
192     if (fd->secret->inheritable != inheritable) {
193         if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
194             return PR_FAILURE;
195         }
196         fd->secret->inheritable = inheritable;
197     }
198     return PR_SUCCESS;
199 #else
200     PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
201     return PR_FAILURE;
202 #endif
203 }
204
205 /*
206 ** This function only has a useful implementation in the debug build of
207 ** the pthreads version.
208 */
209 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
210 {
211     /* do nothing */
212 }  /* PT_FPrintStats */