Remove Wundef warnings for specification macros
[platform/upstream/glibc.git] / posix / confstr.c
1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <stddef.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <confstr.h>
23 #include "../version.h"
24 #include <posix-conf-vars.h>
25
26 /* If BUF is not NULL and LEN > 0, fill in at most LEN - 1 bytes
27    of BUF with the value corresponding to NAME and zero-terminate BUF.
28    Return the number of bytes required to hold NAME's entire value.  */
29 size_t
30 confstr (name, buf, len)
31      int name;
32      char *buf;
33      size_t len;
34 {
35   const char *string = "";
36   size_t string_len = 1;
37
38   /* Note that this buffer must be large enough for the longest strings
39      used below.  */
40   char restenvs[4 * sizeof "POSIX_V7_LPBIG_OFFBIG"];
41
42   switch (name)
43     {
44     case _CS_PATH:
45       {
46         static const char cs_path[] = CS_PATH;
47         string = cs_path;
48         string_len = sizeof (cs_path);
49       }
50       break;
51
52       /* For _CS_V7_WIDTH_RESTRICTED_ENVS, _CS_V6_WIDTH_RESTRICTED_ENVS
53          and _CS_V5_WIDTH_RESTRICTED_ENVS:
54
55          We have to return a newline-separated list of names of
56          programming environments in which the widths of blksize_t,
57          cc_t, mode_t, nfds_t, pid_t, ptrdiff_t, size_t, speed_t,
58          ssize_t, suseconds_t, tcflag_t, useconds_t, wchar_t, and
59          wint_t types are no greater than the width of type long.
60
61          Currently this means all environments that the system allows.  */
62
63 #define START_ENV_GROUP(VERSION)                \
64     case _CS_##VERSION##_WIDTH_RESTRICTED_ENVS: \
65       string_len = 0;
66
67 #define END_ENV_GROUP(VERSION)                  \
68       restenvs[string_len++] = '\0';            \
69       string = restenvs;                        \
70       break;
71
72 #define KNOWN_ABSENT_ENVIRONMENT(SC_PREFIX, ENV_PREFIX, SUFFIX) \
73       /* Empty.  */
74
75 #define KNOWN_PRESENT_ENV_STRING(STR)           \
76       if (string_len > 0)                       \
77         restenvs[string_len++] = '\n';          \
78       memcpy (restenvs + string_len, STR,       \
79               sizeof STR - 1);                  \
80       string_len += sizeof STR - 1;
81
82 #define KNOWN_PRESENT_ENVIRONMENT(SC_PREFIX, ENV_PREFIX, SUFFIX)        \
83       KNOWN_PRESENT_ENV_STRING (#ENV_PREFIX "_" #SUFFIX)
84
85 #define UNKNOWN_ENVIRONMENT(SC_PREFIX, ENV_PREFIX, SUFFIX)              \
86       if (__sysconf (_SC_##SC_PREFIX##_##SUFFIX) > 0)                   \
87         {                                                               \
88           KNOWN_PRESENT_ENVIRONMENT (SC_PREFIX, ENV_PREFIX, SUFFIX)     \
89         }
90
91 #include "posix-envs.def"
92
93 #undef START_ENV_GROUP
94 #undef END_ENV_GROUP
95 #undef KNOWN_ABSENT_ENVIRONMENT
96 #undef KNOWN_PRESENT_ENV_STRING
97 #undef KNOWN_PRESENT_ENVIRONMENT
98 #undef UNKNOWN_ENVIRONMENT
99
100     case _CS_XBS5_ILP32_OFF32_CFLAGS:
101     case _CS_POSIX_V6_ILP32_OFF32_CFLAGS:
102     case _CS_POSIX_V7_ILP32_OFF32_CFLAGS:
103 #ifdef __ILP32_OFF32_CFLAGS
104 # if CONF_IS_DEFINED_UNSET (_POSIX_V7_ILP32_OFF32)
105 #  error "__ILP32_OFF32_CFLAGS should not be defined"
106 # elif CONF_IS_UNDEFINED (_POSIX_V7_ILP32_OFF32)
107       if (__sysconf (_SC_V7_ILP32_OFF32) < 0)
108         break;
109 # endif
110       string = __ILP32_OFF32_CFLAGS;
111       string_len = sizeof (__ILP32_OFF32_CFLAGS);
112 #endif
113       break;
114
115     case _CS_XBS5_ILP32_OFFBIG_CFLAGS:
116     case _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS:
117     case _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS:
118 #ifdef __ILP32_OFFBIG_CFLAGS
119 # if CONF_IS_DEFINED_UNSET (_POSIX_V7_ILP32_OFFBIG)
120 #  error "__ILP32_OFFBIG_CFLAGS should not be defined"
121 # elif CONF_IS_UNDEFINED (_POSIX_V7_ILP32_OFFBIG)
122       if (__sysconf (_SC_V7_ILP32_OFFBIG) < 0)
123         break;
124 # endif
125       string = __ILP32_OFFBIG_CFLAGS;
126       string_len = sizeof (__ILP32_OFFBIG_CFLAGS);
127 #endif
128       break;
129
130     case _CS_XBS5_LP64_OFF64_CFLAGS:
131     case _CS_POSIX_V6_LP64_OFF64_CFLAGS:
132     case _CS_POSIX_V7_LP64_OFF64_CFLAGS:
133 #ifdef __LP64_OFF64_CFLAGS
134 # if CONF_IS_DEFINED_UNSET (_POSIX_V7_LP64_OFF64)
135 #  error "__LP64_OFF64_CFLAGS should not be defined"
136 # elif CONF_IS_UNDEFINED (_POSIX_V7_LP64_OFF64)
137       if (__sysconf (_SC_V7_LP64_OFF64) < 0)
138         break;
139 # endif
140       string = __LP64_OFF64_CFLAGS;
141       string_len = sizeof (__LP64_OFF64_CFLAGS);
142 #endif
143       break;
144
145     case _CS_XBS5_ILP32_OFF32_LDFLAGS:
146     case _CS_POSIX_V6_ILP32_OFF32_LDFLAGS:
147     case _CS_POSIX_V7_ILP32_OFF32_LDFLAGS:
148 #ifdef __ILP32_OFF32_LDFLAGS
149 # if CONF_IS_DEFINED_UNSET (_POSIX_V7_ILP32_OFF32 )
150 #  error "__ILP32_OFF32_LDFLAGS should not be defined"
151 # elif CONF_IS_UNDEFINED (_POSIX_V7_ILP32_OFF32)
152       if (__sysconf (_SC_V7_ILP32_OFF32) < 0)
153         break;
154 # endif
155       string = __ILP32_OFF32_LDFLAGS;
156       string_len = sizeof (__ILP32_OFF32_LDFLAGS);
157 #endif
158       break;
159
160     case _CS_XBS5_ILP32_OFFBIG_LDFLAGS:
161     case _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS:
162     case _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS:
163 #ifdef __ILP32_OFFBIG_LDFLAGS
164 # if CONF_IS_DEFINED_UNSET (_POSIX_V7_ILP32_OFFBIG)
165 #  error "__ILP32_OFFBIG_LDFLAGS should not be defined"
166 # elif CONF_IS_UNDEFINED (_POSIX_V7_ILP32_OFFBIG)
167       if (__sysconf (_SC_V7_ILP32_OFFBIG) < 0)
168         break;
169 # endif
170       string = __ILP32_OFFBIG_LDFLAGS;
171       string_len = sizeof (__ILP32_OFFBIG_LDFLAGS);
172 #endif
173       break;
174
175     case _CS_XBS5_LP64_OFF64_LDFLAGS:
176     case _CS_POSIX_V6_LP64_OFF64_LDFLAGS:
177     case _CS_POSIX_V7_LP64_OFF64_LDFLAGS:
178 #ifdef __LP64_OFF64_LDFLAGS
179 # if CONF_IS_DEFINED_UNSET (_POSIX_V7_LP64_OFF64)
180 #  error "__LP64_OFF64_LDFLAGS should not be defined"
181 # elif CONF_IS_UNDEFINED (_POSIX_V7_LP64_OFF64)
182       if (__sysconf (_SC_V7_LP64_OFF64) < 0)
183         break;
184 # endif
185       string = __LP64_OFF64_LDFLAGS;
186       string_len = sizeof (__LP64_OFF64_LDFLAGS);
187 #endif
188       break;
189
190     case _CS_LFS_CFLAGS:
191     case _CS_LFS_LINTFLAGS:
192 #if (CONF_IS_DEFINED_SET (_POSIX_V6_ILP32_OFF32) \
193      && CONF_IS_DEFINED_SET (_POSIX_V6_ILP32_OFFBIG))
194 # define __LFS_CFLAGS "-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
195       /* Signal that we want the new ABI.  */
196       string = __LFS_CFLAGS;
197       string_len = sizeof (__LFS_CFLAGS);
198 #endif
199       break;
200
201     case _CS_LFS_LDFLAGS:
202     case _CS_LFS_LIBS:
203       /* No special libraries or linker flags needed.  */
204       break;
205
206     case _CS_LFS64_CFLAGS:
207     case _CS_LFS64_LINTFLAGS:
208 #define __LFS64_CFLAGS "-D_LARGEFILE64_SOURCE"
209       string = __LFS64_CFLAGS;
210       string_len = sizeof (__LFS64_CFLAGS);
211       break;
212
213     case _CS_LFS64_LDFLAGS:
214     case _CS_LFS64_LIBS:
215       /* No special libraries or linker flags needed.  */
216       break;
217
218     case _CS_XBS5_ILP32_OFF32_LIBS:
219     case _CS_XBS5_ILP32_OFF32_LINTFLAGS:
220     case _CS_XBS5_ILP32_OFFBIG_LIBS:
221     case _CS_XBS5_ILP32_OFFBIG_LINTFLAGS:
222     case _CS_XBS5_LP64_OFF64_LIBS:
223     case _CS_XBS5_LP64_OFF64_LINTFLAGS:
224     case _CS_XBS5_LPBIG_OFFBIG_CFLAGS:
225     case _CS_XBS5_LPBIG_OFFBIG_LDFLAGS:
226     case _CS_XBS5_LPBIG_OFFBIG_LIBS:
227     case _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS:
228
229     case _CS_POSIX_V6_ILP32_OFF32_LIBS:
230     case _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS:
231     case _CS_POSIX_V6_ILP32_OFFBIG_LIBS:
232     case _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS:
233     case _CS_POSIX_V6_LP64_OFF64_LIBS:
234     case _CS_POSIX_V6_LP64_OFF64_LINTFLAGS:
235     case _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS:
236     case _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS:
237     case _CS_POSIX_V6_LPBIG_OFFBIG_LIBS:
238     case _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS:
239
240     case _CS_POSIX_V7_ILP32_OFF32_LIBS:
241     case _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS:
242     case _CS_POSIX_V7_ILP32_OFFBIG_LIBS:
243     case _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS:
244     case _CS_POSIX_V7_LP64_OFF64_LIBS:
245     case _CS_POSIX_V7_LP64_OFF64_LINTFLAGS:
246     case _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS:
247     case _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS:
248     case _CS_POSIX_V7_LPBIG_OFFBIG_LIBS:
249     case _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS:
250       /* GNU libc does not require special actions to use LFS functions.  */
251       break;
252
253     case _CS_GNU_LIBC_VERSION:
254       string = "glibc " VERSION;
255       string_len = sizeof ("glibc " VERSION);
256       break;
257
258     case _CS_GNU_LIBPTHREAD_VERSION:
259 #ifdef LIBPTHREAD_VERSION
260       string = LIBPTHREAD_VERSION;
261       string_len = sizeof LIBPTHREAD_VERSION;
262       break;
263 #else
264       /* No thread library.  */
265       __set_errno (EINVAL);
266       return 0;
267 #endif
268
269     case _CS_V6_ENV:
270     case _CS_V7_ENV:
271       /* Maybe something else is needed in future.  */
272       string = "POSIXLY_CORRECT=1";
273       string_len = sizeof ("POSIXLY_CORRECT=1");
274       break;
275
276     default:
277       __set_errno (EINVAL);
278       return 0;
279     }
280
281   if (len > 0 && buf != NULL)
282     {
283       if (string_len <= len)
284         memcpy (buf, string, string_len);
285       else
286         {
287           memcpy (buf, string, len - 1);
288           buf[len - 1] = '\0';
289         }
290     }
291   return string_len;
292 }
293 libc_hidden_def (confstr)