Handle -- on getconf command line.
[platform/upstream/glibc.git] / posix / getconf.c
index 4ce4f8e..d9d90e4 100644 (file)
@@ -1,20 +1,19 @@
-/* Copyright (C) 1991, 92, 1995-2003, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 92, 1995-2008, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation; version 2 of the License, or
+   (at your option) any later version.
 
-   The GNU C Library is distributed in the hope that it will be useful,
+   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include <unistd.h>
 #include <errno.h>
@@ -356,6 +355,14 @@ static const struct conf vars[] =
 #ifdef _SC_THREAD_PRIO_PROTECT
     { "_POSIX_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT, SYSCONF },
 #endif
+#ifdef _SC_THREAD_ROBUST_PRIO_INHERIT
+    { "_POSIX_THREAD_ROBUST_PRIO_INHERIT", _SC_THREAD_ROBUST_PRIO_INHERIT,
+      SYSCONF },
+#endif
+#ifdef _SC_THREAD_ROBUST_PRIO_PROTECT
+    { "_POSIX_THREAD_ROBUST_PRIO_PROTECT", _SC_THREAD_ROBUST_PRIO_PROTECT,
+      SYSCONF },
+#endif
 #ifdef _SC_THREAD_PROCESS_SHARED
     { "_POSIX_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED, SYSCONF },
 #endif
@@ -477,6 +484,7 @@ static const struct conf vars[] =
 #endif
 #ifdef _SC_LINE_MAX
     { "_POSIX2_LINE_MAX", _SC_LINE_MAX, SYSCONF },
+    { "POSIX2_LINE_MAX", _SC_LINE_MAX, SYSCONF },
 #endif
 #ifdef _SC_2_LOCALEDEF
     { "POSIX2_LOCALEDEF", _SC_2_LOCALEDEF, SYSCONF },
@@ -529,6 +537,11 @@ static const struct conf vars[] =
 #endif
 
     /* Programming environments.  */
+#ifdef _CS_V5_WIDTH_RESTRICTED_ENVS
+    { "_XBS5_WIDTH_RESTRICTED_ENVS", _CS_V5_WIDTH_RESTRICTED_ENVS, CONFSTR },
+    { "XBS5_WIDTH_RESTRICTED_ENVS", _CS_V5_WIDTH_RESTRICTED_ENVS, CONFSTR },
+#endif
+
 #ifdef _SC_XBS5_ILP32_OFF32
     { "_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32, SYSCONF },
 #endif
@@ -611,6 +624,7 @@ static const struct conf vars[] =
 
 #ifdef _CS_V6_WIDTH_RESTRICTED_ENVS
     { "_POSIX_V6_WIDTH_RESTRICTED_ENVS", _CS_V6_WIDTH_RESTRICTED_ENVS, CONFSTR },
+    { "POSIX_V6_WIDTH_RESTRICTED_ENVS", _CS_V6_WIDTH_RESTRICTED_ENVS, CONFSTR },
 #endif
 
 #ifdef _SC_V6_ILP32_OFFBIG
@@ -661,6 +675,75 @@ static const struct conf vars[] =
     { "POSIX_V6_LPBIG_OFFBIG_LINTFLAGS", _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS, CONFSTR },
 #endif
 
+#ifdef _SC_V7_ILP32_OFF32
+    { "_POSIX_V7_ILP32_OFF32", _SC_V7_ILP32_OFF32, SYSCONF },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFF32_CFLAGS
+    { "POSIX_V7_ILP32_OFF32_CFLAGS", _CS_POSIX_V7_ILP32_OFF32_CFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFF32_LDFLAGS
+    { "POSIX_V7_ILP32_OFF32_LDFLAGS", _CS_POSIX_V7_ILP32_OFF32_LDFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFF32_LIBS
+    { "POSIX_V7_ILP32_OFF32_LIBS", _CS_POSIX_V7_ILP32_OFF32_LIBS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS
+    { "POSIX_V7_ILP32_OFF32_LINTFLAGS", _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS, CONFSTR },
+#endif
+
+#ifdef _CS_V7_WIDTH_RESTRICTED_ENVS
+    { "_POSIX_V7_WIDTH_RESTRICTED_ENVS", _CS_V7_WIDTH_RESTRICTED_ENVS, CONFSTR },
+    { "POSIX_V7_WIDTH_RESTRICTED_ENVS", _CS_V7_WIDTH_RESTRICTED_ENVS, CONFSTR },
+#endif
+
+#ifdef _SC_V7_ILP32_OFFBIG
+    { "_POSIX_V7_ILP32_OFFBIG", _SC_V7_ILP32_OFFBIG, SYSCONF },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS
+    { "POSIX_V7_ILP32_OFFBIG_CFLAGS", _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS
+    { "POSIX_V7_ILP32_OFFBIG_LDFLAGS", _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFFBIG_LIBS
+    { "POSIX_V7_ILP32_OFFBIG_LIBS", _CS_POSIX_V7_ILP32_OFFBIG_LIBS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS
+    { "POSIX_V7_ILP32_OFFBIG_LINTFLAGS", _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS, CONFSTR },
+#endif
+
+#ifdef _SC_V7_LP64_OFF64
+    { "_POSIX_V7_LP64_OFF64", _SC_V7_LP64_OFF64, SYSCONF },
+#endif
+#ifdef _CS_POSIX_V7_LP64_OFF64_CFLAGS
+    { "POSIX_V7_LP64_OFF64_CFLAGS", _CS_POSIX_V7_LP64_OFF64_CFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_LP64_OFF64_LDFLAGS
+    { "POSIX_V7_LP64_OFF64_LDFLAGS", _CS_POSIX_V7_LP64_OFF64_LDFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_LP64_OFF64_LIBS
+    { "POSIX_V7_LP64_OFF64_LIBS", _CS_POSIX_V7_LP64_OFF64_LIBS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_LP64_OFF64_LINTFLAGS
+    { "POSIX_V7_LP64_OFF64_LINTFLAGS", _CS_POSIX_V7_LP64_OFF64_LINTFLAGS, CONFSTR },
+#endif
+
+#ifdef _SC_V7_LPBIG_OFFBIG
+    { "_POSIX_V7_LPBIG_OFFBIG", _SC_V7_LPBIG_OFFBIG, SYSCONF },
+#endif
+#ifdef _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS
+    { "POSIX_V7_LPBIG_OFFBIG_CFLAGS", _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS
+    { "POSIX_V7_LPBIG_OFFBIG_LDFLAGS", _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_LPBIG_OFFBIG_LIBS
+    { "POSIX_V7_LPBIG_OFFBIG_LIBS", _CS_POSIX_V7_LPBIG_OFFBIG_LIBS, CONFSTR },
+#endif
+#ifdef _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS
+    { "POSIX_V7_LPBIG_OFFBIG_LINTFLAGS", _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS, CONFSTR },
+#endif
+
 #ifdef _SC_ADVISORY_INFO
     { "_POSIX_ADVISORY_INFO", _SC_ADVISORY_INFO, SYSCONF },
 #endif
@@ -911,6 +994,9 @@ static const struct conf vars[] =
 #ifdef _SC_LEVEL4_CACHE_ASSOC
     { "LEVEL4_CACHE_ASSOC", _SC_LEVEL4_CACHE_ASSOC, SYSCONF },
 #endif
+#ifdef _SC_LEVEL4_CACHE_LINESIZE
+    { "LEVEL4_CACHE_LINESIZE", _SC_LEVEL4_CACHE_LINESIZE, SYSCONF },
+#endif
 
 #ifdef _SC_IPV6
     { "IPV6", _SC_IPV6, SYSCONF },
@@ -923,12 +1009,20 @@ static const struct conf vars[] =
   };
 
 
-static struct { const char *name; int num; } specs[] =
+static const struct { const char *name; int num; } specs[] =
   {
+    { "XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32 },
+    { "XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG },
+    { "XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64 },
+    { "XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG },
     { "POSIX_V6_ILP32_OFF32", _SC_V6_ILP32_OFF32 },
     { "POSIX_V6_ILP32_OFFBIG", _SC_V6_ILP32_OFFBIG },
     { "POSIX_V6_LP64_OFF64", _SC_V6_LP64_OFF64 },
-    { "POSIX_V6_LPBIG_OFFBIG", _SC_V6_LPBIG_OFFBIG }
+    { "POSIX_V6_LPBIG_OFFBIG", _SC_V6_LPBIG_OFFBIG },
+    { "POSIX_V7_ILP32_OFF32", _SC_V7_ILP32_OFF32 },
+    { "POSIX_V7_ILP32_OFFBIG", _SC_V7_ILP32_OFFBIG },
+    { "POSIX_V7_LP64_OFF64", _SC_V7_LP64_OFF64 },
+    { "POSIX_V7_LPBIG_OFFBIG", _SC_V7_LPBIG_OFFBIG },
   };
 static const int nspecs = sizeof (specs) / sizeof (specs[0]);
 
@@ -941,9 +1035,56 @@ usage (void)
   fprintf (stderr,
           _("Usage: %s [-v specification] variable_name [pathname]\n"),
           __progname);
+  fprintf (stderr,
+          _("       %s -a [pathname]\n"), __progname);
   exit (2);
 }
 
+
+static void
+print_all (const char *path)
+{
+  register const struct conf *c;
+  size_t clen;
+  long int value;
+  char *cvalue;
+  for (c = vars; c->name != NULL; ++c) {
+    printf("%-35s", c->name);
+    switch (c->call) {
+      case PATHCONF:
+       value = pathconf (path, c->call_name);
+       if (value != -1) {
+         printf("%ld", value);
+       }
+       printf("\n");
+       break;
+      case SYSCONF:
+       value = sysconf (c->call_name);
+       if (value == -1l) {
+         if (c->call_name == _SC_UINT_MAX
+           || c->call_name == _SC_ULONG_MAX)
+           printf ("%lu", value);
+       }
+       else {
+         printf ("%ld", value);
+       }
+       printf ("\n");
+       break;
+      case CONFSTR:
+       clen = confstr (c->call_name, (char *) NULL, 0);
+       cvalue = (char *) malloc (clen);
+       if (cvalue == NULL)
+         error (3, 0, _("memory exhausted"));
+       if (confstr (c->call_name, cvalue, clen) != clen)
+         error (3, errno, "confstr");
+       printf ("%.*s\n", (int) clen, cvalue);
+       free (cvalue);
+       break;
+    }
+  }
+  exit (0);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -959,13 +1100,27 @@ main (int argc, char *argv[])
 
   if (argc > 1 && strcmp (argv[1], "--version") == 0)
     {
-      fprintf (stderr, "getconf (GNU %s) %s\n", PACKAGE, VERSION);
-      fprintf (stderr, gettext ("\
+      printf ("getconf (GNU %s) %s\n", PACKAGE, VERSION);
+      printf (gettext ("\
 Copyright (C) %s Free Software Foundation, Inc.\n\
 This is free software; see the source for copying conditions.  There is NO\n\
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
-"), "2004");
-      fprintf (stderr, gettext ("Written by %s.\n"), "Roland McGrath");
+"), "2009");
+      printf (gettext ("Written by %s.\n"), "Roland McGrath");
+      return 0;
+    }
+
+  if (argc > 1 && strcmp (argv[1], "--help") == 0)
+    {
+      printf (gettext ("\
+Usage: getconf [-v SPEC] VAR\n\
+  or:  getconf [-v SPEC] PATH_VAR PATH\n\
+\n\
+Get the configuration value for variable VAR, or for variable PATH_VAR\n\
+for path PATH.  If SPEC is given, give values for compilation\n\
+environment SPEC.\n\n"));
+      fputs (gettext ("For bug reporting instructions, please see:\n\
+<http://www.gnu.org/software/libc/bugs.html>.\n"), stdout);
       return 0;
     }
 
@@ -1019,6 +1174,18 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
 
       switch (specs[i].num)
        {
+#ifndef _XBS5_ILP32_OFF32
+         case _SC_XBS5_ILP32_OFF32:
+#endif
+#ifndef _XBS5_ILP32_OFFBIG
+         case _SC_XBS5_ILP32_OFFBIG:
+#endif
+#ifndef _XBS5_LP64_OFF64
+         case _SC_XBS5_LP64_OFF64:
+#endif
+#ifndef _XBS5_LPBIG_OFFBIG
+         case _SC_XBS5_LPBIG_OFFBIG:
+#endif
 #ifndef _POSIX_V6_ILP32_OFF32
          case _SC_V6_ILP32_OFF32:
 #endif
@@ -1031,6 +1198,18 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
 #ifndef _POSIX_V6_LPBIG_OFFBIG
          case _SC_V6_LPBIG_OFFBIG:
 #endif
+#ifndef _POSIX_V7_ILP32_OFF32
+         case _SC_V7_ILP32_OFF32:
+#endif
+#ifndef _POSIX_V7_ILP32_OFFBIG
+         case _SC_V7_ILP32_OFFBIG:
+#endif
+#ifndef _POSIX_V7_LP64_OFF64
+         case _SC_V7_LP64_OFF64:
+#endif
+#ifndef _POSIX_V7_LPBIG_OFFBIG
+         case _SC_V7_LPBIG_OFFBIG:
+#endif
            {
              const char *args[argc + 3];
              size_t spec_len = strlen (spec);
@@ -1050,13 +1229,27 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
        }
     }
 
-  if (argc < 2 || argc > 3)
+  if (argc > 1 && strcmp (argv[1], "-a") == 0)
+    {
+      if (argc == 2)
+       print_all ("/");
+      else if (argc == 3)
+       print_all (argv[2]);
+      else
+       usage ();
+    }
+
+  int ai = 1;
+  if (argc > ai && strcmp (argv[ai], "--") == 0)
+    ++ai;
+
+  if (argc - ai < 1 || argc - ai > 2)
     usage ();
 
   for (c = vars; c->name != NULL; ++c)
-    if (strcmp (c->name, argv[1]) == 0
+    if (strcmp (c->name, argv[ai]) == 0
        || (strncmp (c->name, "_POSIX_", 7) == 0
-           && strcmp (c->name + 7, argv[1]) == 0))
+           && strcmp (c->name + 7, argv[ai]) == 0))
       {
        long int value;
        size_t clen;
@@ -1064,14 +1257,14 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
        switch (c->call)
          {
          case PATHCONF:
-           if (argc < 3)
+           if (argc - ai < 2)
              usage ();
            errno = 0;
-           value = pathconf (argv[2], c->call_name);
+           value = pathconf (argv[ai + 1], c->call_name);
            if (value == -1)
              {
                if (errno)
-                 error (3, errno, "pathconf: %s", argv[2]);
+                 error (3, errno, "pathconf: %s", argv[ai + 1]);
                else
                  puts (_("undefined"));
              }
@@ -1080,7 +1273,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
            exit (0);
 
          case SYSCONF:
-           if (argc > 2)
+           if (argc - ai > 1)
              usage ();
            value = sysconf (c->call_name);
            if (value == -1l)
@@ -1096,7 +1289,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
            exit (0);
 
          case CONFSTR:
-           if (argc > 2)
+           if (argc - ai > 1)
              usage ();
            clen = confstr (c->call_name, (char *) NULL, 0);
            cvalue = (char *) malloc (clen);
@@ -1111,7 +1304,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
          }
       }
 
-  error (2, 0, _("Unrecognized variable `%s'"), argv[1]);
+  error (2, 0, _("Unrecognized variable `%s'"), argv[ai]);
   /* NOTREACHED */
   return 2;
 }