]> git.saurik.com Git - apple/libc.git/blob - gen/sysconf.c
Libc-320.tar.gz
[apple/libc.git] / gen / sysconf.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Sean Eric Fagan of Cygnus Support.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61
62 #include <sys/param.h>
63 #include <sys/sysctl.h>
64 #include <sys/time.h>
65 #include <sys/resource.h>
66
67 #include <errno.h>
68 #include <unistd.h>
69
70 /*
71 * sysconf --
72 * get configurable system variables.
73 *
74 * XXX
75 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
76 * not change during the lifetime of the calling process. This would seem
77 * to require that any change to system limits kill all running processes.
78 * A workaround might be to cache the values when they are first retrieved
79 * and then simply return the cached value on subsequent calls. This is
80 * less useful than returning up-to-date values, however.
81 */
82 long
83 sysconf(name)
84 int name;
85 {
86 struct rlimit rl;
87 size_t len;
88 int mib[2], value;
89
90 len = sizeof(value);
91
92 switch (name) {
93 /* 1003.1 */
94 case _SC_ARG_MAX:
95 mib[0] = CTL_KERN;
96 mib[1] = KERN_ARGMAX;
97 break;
98 case _SC_CHILD_MAX:
99 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
100 case _SC_CLK_TCK:
101 return (CLK_TCK);
102 case _SC_JOB_CONTROL:
103 mib[0] = CTL_KERN;
104 mib[1] = KERN_JOB_CONTROL;
105 goto yesno;
106 case _SC_NGROUPS_MAX:
107 mib[0] = CTL_KERN;
108 mib[1] = KERN_NGROUPS;
109 break;
110 case _SC_OPEN_MAX:
111 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
112 case _SC_STREAM_MAX:
113 mib[0] = CTL_USER;
114 mib[1] = USER_STREAM_MAX;
115 break;
116 case _SC_TZNAME_MAX:
117 mib[0] = CTL_USER;
118 mib[1] = USER_TZNAME_MAX;
119 break;
120 case _SC_SAVED_IDS:
121 mib[0] = CTL_KERN;
122 mib[1] = KERN_SAVED_IDS;
123 goto yesno;
124 case _SC_VERSION:
125 mib[0] = CTL_KERN;
126 mib[1] = KERN_POSIX1;
127 break;
128
129 /* 1003.2 */
130 case _SC_BC_BASE_MAX:
131 mib[0] = CTL_USER;
132 mib[1] = USER_BC_BASE_MAX;
133 break;
134 case _SC_BC_DIM_MAX:
135 mib[0] = CTL_USER;
136 mib[1] = USER_BC_DIM_MAX;
137 break;
138 case _SC_BC_SCALE_MAX:
139 mib[0] = CTL_USER;
140 mib[1] = USER_BC_SCALE_MAX;
141 break;
142 case _SC_BC_STRING_MAX:
143 mib[0] = CTL_USER;
144 mib[1] = USER_BC_STRING_MAX;
145 break;
146 case _SC_COLL_WEIGHTS_MAX:
147 mib[0] = CTL_USER;
148 mib[1] = USER_COLL_WEIGHTS_MAX;
149 break;
150 case _SC_EXPR_NEST_MAX:
151 mib[0] = CTL_USER;
152 mib[1] = USER_EXPR_NEST_MAX;
153 break;
154 case _SC_LINE_MAX:
155 mib[0] = CTL_USER;
156 mib[1] = USER_LINE_MAX;
157 break;
158 case _SC_PAGESIZE:
159 mib[0] = CTL_HW;
160 mib[1] = HW_PAGESIZE;
161 break;
162 case _SC_RE_DUP_MAX:
163 mib[0] = CTL_USER;
164 mib[1] = USER_RE_DUP_MAX;
165 break;
166 case _SC_2_VERSION:
167 mib[0] = CTL_USER;
168 mib[1] = USER_POSIX2_VERSION;
169 break;
170 case _SC_2_C_BIND:
171 mib[0] = CTL_USER;
172 mib[1] = USER_POSIX2_C_BIND;
173 goto yesno;
174 case _SC_2_C_DEV:
175 mib[0] = CTL_USER;
176 mib[1] = USER_POSIX2_C_DEV;
177 goto yesno;
178 case _SC_2_CHAR_TERM:
179 mib[0] = CTL_USER;
180 mib[1] = USER_POSIX2_CHAR_TERM;
181 goto yesno;
182 case _SC_2_FORT_DEV:
183 mib[0] = CTL_USER;
184 mib[1] = USER_POSIX2_FORT_DEV;
185 goto yesno;
186 case _SC_2_FORT_RUN:
187 mib[0] = CTL_USER;
188 mib[1] = USER_POSIX2_FORT_RUN;
189 goto yesno;
190 case _SC_2_LOCALEDEF:
191 mib[0] = CTL_USER;
192 mib[1] = USER_POSIX2_LOCALEDEF;
193 goto yesno;
194 case _SC_2_SW_DEV:
195 mib[0] = CTL_USER;
196 mib[1] = USER_POSIX2_SW_DEV;
197 goto yesno;
198 case _SC_2_UPE:
199 mib[0] = CTL_USER;
200 mib[1] = USER_POSIX2_UPE;
201 yesno: if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
202 return (-1);
203 if (value == 0)
204 return (-1);
205 return (value);
206 break;
207 default:
208 errno = EINVAL;
209 return (-1);
210 }
211 return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
212 }