e42b909ed929d2e63c5df8af5bbcf4d6cddc02bf
[platform/upstream/glibc.git] / posix / getconf.c
1 /* Copyright (C) 1991, 1992 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 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.
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 Library General Public License for more details.
13
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.  */
18
19 #include <ansidecl.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 struct conf
27   {
28     CONST char *name;
29     CONST int call_name;
30     CONST enum { SYSCONF, CONFSTR, PATHCONF } call;
31   };
32
33 static struct conf vars[] =
34   {
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 },
44
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 },
53
54     { "PATH", _CS_PATH, CONFSTR },
55
56     { NULL, 0, SYSCONF }
57   };
58
59 static CONST char *program;
60
61 static void
62 DEFUN_VOID(usage)
63 {
64   fprintf (stderr, "Usage: %s variable_name [pathname]\n", program);
65   exit (2);
66 }
67
68 int
69 DEFUN(main, (argc, argv), int argc AND char **argv)
70 {
71   register CONST struct conf *c;
72
73   program = strrchr (argv[0], '/');
74   if (program == NULL)
75     program = argv[0];
76   else
77     ++program;
78
79   if (argc < 2 || argc > 3)
80     usage ();
81
82   for (c = vars; c->name != NULL; ++c)
83     if (!strcmp (c->name, argv[1]))
84       {
85         long int value;
86         size_t clen;
87         char *cvalue;
88         switch (c->call)
89           {
90           case PATHCONF:
91             if (argc < 3)
92               usage ();
93             value = pathconf (argv[2], c->call_name);
94             if (value == -1)
95               {
96                 fprintf (stderr, "%s: pathconf: %s: %s\n",
97                          program, argv[2], strerror (errno));
98                 exit (3);
99               }
100             printf ("%ld\n", value);
101             exit (0);
102
103           case SYSCONF:
104             if (argc > 2)
105               usage ();
106             value = sysconf (c->call_name);
107             printf ("%ld\n", value);
108             exit (0);
109
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)
116               {
117                 fprintf (stderr, "%s: malloc: %s\n",
118                          program, strerror (errno));
119                 exit (3);
120               }
121             if (confstr (c->call_name, cvalue, clen) != clen)
122               {
123                 fprintf (stderr, "%s: confstr: %s\n",
124                          program, strerror (errno));
125                 exit (3);
126               }
127             printf ("%.*s\n", (int) clen, cvalue);
128             exit (0);
129           }
130       }
131
132   fprintf (stderr, "%s: Unrecognized variable `%s'\n", program, argv[1]);
133   exit (2);
134 }