Wed Dec 20 18:23:10 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / posix / getconf.c
blob7423a7722505b41c5d0ef295fcf1046dea4ec4e7
1 /* Copyright (C) 1991, 1992, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
26 struct conf
28 CONST char *name;
29 CONST int call_name;
30 CONST enum { SYSCONF, CONFSTR, PATHCONF } call;
33 static struct conf vars[] =
35 { "LINK_MAX", _PC_LINK_MAX, PATHCONF },
36 { "MAX_CANON", _PC_MAX_CANON, PATHCONF },
37 { "MAX_INPUT", _PC_MAX_INPUT, PATHCONF },
38 { "NAME_MAX", _PC_NAME_MAX, PATHCONF },
39 { "PATH_MAX", _PC_PATH_MAX, PATHCONF },
40 { "PIPE_BUF", _PC_PIPE_BUF, PATHCONF },
41 { "_POSIX_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED, PATHCONF },
42 { "_POSIX_NO_TRUNC", _PC_NO_TRUNC, PATHCONF },
43 { "_POSIX_VDISABLE", _PC_VDISABLE, PATHCONF },
45 { "ARG_MAX", _SC_ARG_MAX, SYSCONF },
46 { "CHILD_MAX", _SC_CHILD_MAX, SYSCONF },
47 { "CLK_TCK", _SC_CLK_TCK, SYSCONF },
48 { "NGROUPS_MAX", _SC_NGROUPS_MAX, SYSCONF },
49 { "OPEN_MAX", _SC_OPEN_MAX, SYSCONF },
50 { "_POSIX_JOB_CONTROL", _SC_JOB_CONTROL, SYSCONF },
51 { "_POSIX_SAVED_IDS", _SC_SAVED_IDS, SYSCONF },
52 { "_POSIX_VERSION", _SC_VERSION, SYSCONF },
54 { "PATH", _CS_PATH, CONFSTR },
56 { NULL, 0, SYSCONF }
59 static CONST char *program;
61 static void
62 DEFUN_VOID(usage)
64 fprintf (stderr, _("Usage: %s variable_name [pathname]\n"), program);
65 exit (2);
68 int
69 DEFUN(main, (argc, argv), int argc AND char **argv)
71 register CONST struct conf *c;
73 program = strrchr (argv[0], '/');
74 if (program == NULL)
75 program = argv[0];
76 else
77 ++program;
79 if (argc < 2 || argc > 3)
80 usage ();
82 for (c = vars; c->name != NULL; ++c)
83 if (!strcmp (c->name, argv[1]))
85 long int value;
86 size_t clen;
87 char *cvalue;
88 switch (c->call)
90 case PATHCONF:
91 if (argc < 3)
92 usage ();
93 value = pathconf (argv[2], c->call_name);
94 if (value == -1)
96 fprintf (stderr, "%s: pathconf: %s: %s\n",
97 program, argv[2], strerror (errno));
98 exit (3);
100 printf ("%ld\n", value);
101 exit (0);
103 case SYSCONF:
104 if (argc > 2)
105 usage ();
106 value = sysconf (c->call_name);
107 printf ("%ld\n", value);
108 exit (0);
110 case CONFSTR:
111 if (argc > 2)
112 usage ();
113 clen = confstr (c->call_name, (char *) NULL, 0);
114 cvalue = (char *) malloc (clen);
115 if (cvalue == NULL)
117 fprintf (stderr, "%s: malloc: %s\n",
118 program, strerror (errno));
119 exit (3);
121 if (confstr (c->call_name, cvalue, clen) != clen)
123 fprintf (stderr, "%s: confstr: %s\n",
124 program, strerror (errno));
125 exit (3);
127 printf ("%.*s\n", (int) clen, cvalue);
128 exit (0);
132 fprintf (stderr, _("%s: Unrecognized variable `%s'\n"), program, argv[1]);
133 exit (2);