Fix cexpl when compiled with latest GCC
[glibc.git] / nis / nss-default.c
blob092e12110747a3e18d42cae178be343e748403a2
1 /* Copyright (C) 1996-2017 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 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.
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.
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/>. */
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdio_ext.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <libc-lock.h>
26 #include <libnsl.h>
29 /* Path of the file. */
30 static const char default_nss[] = "/etc/default/nss";
32 /* Flags once read from the file. */
33 static int default_nss_flags;
35 /* Code to make sure we call 'init' once. */
36 __libc_once_define (static, once);
38 /* Table of the recognized variables. */
39 static const struct
41 char name[23];
42 unsigned int len;
43 int flag;
44 } vars[] =
46 #define STRNLEN(s) s, sizeof (s) - 1
47 { STRNLEN ("NETID_AUTHORITATIVE"), NSS_FLAG_NETID_AUTHORITATIVE },
48 { STRNLEN ("SERVICES_AUTHORITATIVE"), NSS_FLAG_SERVICES_AUTHORITATIVE },
49 { STRNLEN ("SETENT_BATCH_READ"), NSS_FLAG_SETENT_BATCH_READ },
50 { STRNLEN ("ADJUNCT_AS_SHADOW"), NSS_FLAG_ADJUNCT_AS_SHADOW },
52 #define nvars (sizeof (vars) / sizeof (vars[0]))
55 static void
56 init (void)
58 int saved_errno = errno;
59 FILE *fp = fopen (default_nss, "rce");
60 if (fp != NULL)
62 char *line = NULL;
63 size_t linelen = 0;
65 __fsetlocking (fp, FSETLOCKING_BYCALLER);
67 while (!feof_unlocked (fp))
69 ssize_t n = getline (&line, &linelen, fp);
70 if (n <= 0)
71 break;
73 /* Recognize only
75 <THE-VARIABLE> = TRUE
77 with arbitrary white spaces. */
78 char *cp = line;
79 while (isspace (*cp))
80 ++cp;
82 /* Recognize comment lines. */
83 if (*cp == '#')
84 continue;
86 int idx;
87 for (idx = 0; idx < nvars; ++idx)
88 if (strncmp (cp, vars[idx].name, vars[idx].len) == 0)
89 break;
90 if (idx == nvars)
91 continue;
93 cp += vars[idx].len;
94 while (isspace (*cp))
95 ++cp;
96 if (*cp++ != '=')
97 continue;
98 while (isspace (*cp))
99 ++cp;
101 if (strncmp (cp, "TRUE", 4) != 0)
102 continue;
103 cp += 4;
105 while (isspace (*cp))
106 ++cp;
108 if (*cp == '\0')
109 default_nss_flags |= vars[idx].flag;
112 free (line);
114 fclose (fp);
116 __set_errno (saved_errno);
121 _nsl_default_nss (void)
123 /* If we have not yet read the file yet do it now. */
124 __libc_once (once, init);
126 return default_nss_flags;