configury: improve dlsym underscore detection.
[m4.git] / modules / stdlib.c
blob163bdcabcf00e0d3ef3ae32eb8582b467afdd816
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1999-2001, 2006-2010, 2013-2014 Free Software
3 Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include <pwd.h>
24 #if TM_IN_SYS_TIME
25 # include <sys/time.h>
26 #else
27 # include <time.h>
28 #endif
29 #include <sys/utsname.h>
31 /* Build using only the exported interfaces, unless NDEBUG is set, in
32 which case use private symbols to speed things up as much as possible. */
33 #ifndef NDEBUG
34 # include <m4/m4module.h>
35 #else
36 # include "m4private.h"
37 #endif
39 /* function macros blind side minargs maxargs */
40 #define builtin_functions \
41 BUILTIN (getcwd, false, false, false, 0, 0 ) \
42 BUILTIN (getenv, false, true, false, 1, 1 ) \
43 BUILTIN (getlogin, false, false, false, 0, 0 ) \
44 BUILTIN (getpid, false, false, false, 0, 0 ) \
45 BUILTIN (getppid, false, false, false, 0, 0 ) \
46 BUILTIN (getuid, false, false, false, 0, 0 ) \
47 BUILTIN (getpwnam, false, true, false, 1, 1 ) \
48 BUILTIN (getpwuid, false, true, false, 1, 1 ) \
49 BUILTIN (hostname, false, false, false, 0, 0 ) \
50 BUILTIN (rand, false, false, false, 0, 0 ) \
51 BUILTIN (srand, false, false, false, 0, 1 ) \
52 BUILTIN (setenv, false, true, false, 2, 3 ) \
53 BUILTIN (unsetenv, false, true, false, 1, 1 ) \
54 BUILTIN (uname, false, false, false, 0, 0 ) \
57 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler);
58 builtin_functions
59 #undef BUILTIN
61 static const m4_builtin m4_builtin_table[] =
63 #define BUILTIN(handler, macros, blind, side, min, max) \
64 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
66 builtin_functions
67 #undef BUILTIN
69 { NULL, NULL, 0, 0, 0 },
73 void
74 include_stdlib (m4 *context, m4_module *module, m4_obstack *obs)
76 m4_install_builtins (context, module, m4_builtin_table);
80 /**
81 * getcwd()
82 **/
83 M4BUILTIN_HANDLER (getcwd)
85 /* FIXME - Use gnulib module for arbitrary-length cwd. */
86 char buf[1024];
87 char *bp;
89 bp = getcwd (buf, sizeof buf);
91 if (bp != NULL) /* in case of error return null string */
92 m4_shipout_string (context, obs, buf, SIZE_MAX, false);
95 /**
96 * getenv(NAME)
97 **/
98 M4BUILTIN_HANDLER (getenv)
100 char *env;
102 env = getenv (M4ARG (1));
104 if (env != NULL)
105 m4_shipout_string (context, obs, env, SIZE_MAX, false);
109 * setenv(NAME, VALUE, [OVERWRITE])
111 M4BUILTIN_HANDLER (setenv)
113 int overwrite = 1;
115 if (argc >= 4)
116 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (3), M4ARGLEN (3),
117 &overwrite))
118 return;
120 /* TODO - error checking. */
121 setenv (M4ARG (1), M4ARG (2), overwrite);
125 * unsetenv(NAME)
127 M4BUILTIN_HANDLER (unsetenv)
129 /* TODO - error checking. */
130 unsetenv (M4ARG (1));
134 * getlogin()
136 M4BUILTIN_HANDLER (getlogin)
138 char *login;
140 login = getlogin ();
142 if (login != NULL)
143 m4_shipout_string (context, obs, login, SIZE_MAX, false);
147 * getpid()
149 M4BUILTIN_HANDLER (getpid)
151 m4_shipout_int (obs, getpid ());
155 * getppid()
157 M4BUILTIN_HANDLER (getppid)
159 m4_shipout_int (obs, getppid ());
163 * getpwnam(NAME)
165 M4BUILTIN_HANDLER (getpwnam)
167 struct passwd *pw;
169 pw = getpwnam (M4ARG (1));
171 if (pw != NULL)
173 m4_shipout_string (context, obs, pw->pw_name, SIZE_MAX, true);
174 obstack_1grow (obs, ',');
175 m4_shipout_string (context, obs, pw->pw_passwd, SIZE_MAX, true);
176 obstack_1grow (obs, ',');
177 m4_shipout_int (obs, pw->pw_uid);
178 obstack_1grow (obs, ',');
179 m4_shipout_int (obs, pw->pw_gid);
180 obstack_1grow (obs, ',');
181 m4_shipout_string (context, obs, pw->pw_gecos, SIZE_MAX, true);
182 obstack_1grow (obs, ',');
183 m4_shipout_string (context, obs, pw->pw_dir, SIZE_MAX, true);
184 obstack_1grow (obs, ',');
185 m4_shipout_string (context, obs, pw->pw_shell, SIZE_MAX, true);
190 * getpwuid(UID)
192 M4BUILTIN_HANDLER (getpwuid)
194 struct passwd *pw;
195 int uid;
197 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
198 &uid))
199 return;
201 pw = getpwuid (uid);
203 if (pw != NULL)
205 m4_shipout_string (context, obs, pw->pw_name, SIZE_MAX, true);
206 obstack_1grow (obs, ',');
207 m4_shipout_string (context, obs, pw->pw_passwd, SIZE_MAX, true);
208 obstack_1grow (obs, ',');
209 m4_shipout_int (obs, pw->pw_uid);
210 obstack_1grow (obs, ',');
211 m4_shipout_int (obs, pw->pw_gid);
212 obstack_1grow (obs, ',');
213 m4_shipout_string (context, obs, pw->pw_gecos, SIZE_MAX, true);
214 obstack_1grow (obs, ',');
215 m4_shipout_string (context, obs, pw->pw_dir, SIZE_MAX, true);
216 obstack_1grow (obs, ',');
217 m4_shipout_string (context, obs, pw->pw_shell, SIZE_MAX, true);
222 * hostname()
224 M4BUILTIN_HANDLER (hostname)
226 char buf[1024];
228 if (gethostname (buf, sizeof buf) < 0)
229 return;
231 m4_shipout_string (context, obs, buf, SIZE_MAX, false);
235 * rand()
237 M4BUILTIN_HANDLER (rand)
239 m4_shipout_int (obs, rand ());
243 * srand()
245 M4BUILTIN_HANDLER (srand)
247 int seed;
249 if (argc == 1)
250 seed = time (0L) * getpid ();
251 else
253 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1),
254 M4ARGLEN (1), &seed))
255 return;
258 srand (seed);
262 * uname()
264 M4BUILTIN_HANDLER (uname)
266 struct utsname ut;
268 if (uname (&ut) == 0)
270 m4_shipout_string (context, obs, ut.sysname, SIZE_MAX, true);
271 obstack_1grow (obs, ',');
272 m4_shipout_string (context, obs, ut.nodename, SIZE_MAX, true);
273 obstack_1grow (obs, ',');
274 m4_shipout_string (context, obs, ut.release, SIZE_MAX, true);
275 obstack_1grow (obs, ',');
276 m4_shipout_string (context, obs, ut.version, SIZE_MAX, true);
277 obstack_1grow (obs, ',');
278 m4_shipout_string (context, obs, ut.machine, SIZE_MAX, true);
283 * getuid()
285 M4BUILTIN_HANDLER (getuid)
287 m4_shipout_int (obs, getuid ());