benchtests: Add workload name for cosf
[glibc.git] / iconv / gconv_parseconfdir.h
blobc0de548833bee4e146806aa23af6a44438dd9915
1 /* Handle configuration data.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <dirent.h>
20 #include <libc-symbols.h>
21 #include <locale.h>
22 #include <sys/types.h>
24 #if IS_IN (libc)
25 # include <libio/libioP.h>
26 # define __getdelim(line, len, c, fp) __getdelim (line, len, c, fp)
28 # undef isspace
29 # define isspace(__c) __isspace_l ((__c), _nl_C_locobj_ptr)
30 # define asprintf __asprintf
31 # define opendir __opendir
32 # define readdir __readdir
33 # define closedir __closedir
34 # define mempcpy __mempcpy
35 # define lstat64 __lstat64
36 # define feof_unlocked __feof_unlocked
37 #endif
39 /* Name of the file containing the module information in the directories
40 along the path. */
41 static const char gconv_conf_filename[] = "gconv-modules";
43 static void add_alias (char *);
44 static void add_module (char *, const char *, size_t, int);
46 /* Read the next configuration file. */
47 static bool
48 read_conf_file (const char *filename, const char *directory, size_t dir_len)
50 /* Note the file is opened with cancellation in the I/O functions
51 disabled. */
52 FILE *fp = fopen (filename, "rce");
53 char *line = NULL;
54 size_t line_len = 0;
55 static int modcounter;
57 /* Don't complain if a file is not present or readable, simply silently
58 ignore it. */
59 if (fp == NULL)
60 return false;
62 /* No threads reading from this stream. */
63 __fsetlocking (fp, FSETLOCKING_BYCALLER);
65 /* Process the known entries of the file. Comments start with `#' and
66 end with the end of the line. Empty lines are ignored. */
67 while (!feof_unlocked (fp))
69 char *rp, *endp, *word;
70 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
71 if (n < 0)
72 /* An error occurred. */
73 break;
75 rp = line;
76 /* Terminate the line (excluding comments or newline) by an NUL byte
77 to simplify the following code. */
78 endp = strchr (rp, '#');
79 if (endp != NULL)
80 *endp = '\0';
81 else
82 if (rp[n - 1] == '\n')
83 rp[n - 1] = '\0';
85 while (isspace (*rp))
86 ++rp;
88 /* If this is an empty line go on with the next one. */
89 if (rp == endp)
90 continue;
92 word = rp;
93 while (*rp != '\0' && !isspace (*rp))
94 ++rp;
96 if (rp - word == sizeof ("alias") - 1
97 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
98 add_alias (rp);
99 else if (rp - word == sizeof ("module") - 1
100 && memcmp (word, "module", sizeof ("module") - 1) == 0)
101 add_module (rp, directory, dir_len, modcounter++);
102 /* else */
103 /* Otherwise ignore the line. */
106 free (line);
108 fclose (fp);
109 return true;
112 /* Prefix DIR (with length DIR_LEN) with PREFIX if the latter is non-NULL and
113 parse configuration in it. */
115 static __always_inline bool
116 gconv_parseconfdir (const char *prefix, const char *dir, size_t dir_len)
118 /* No slash needs to be inserted between dir and gconv_conf_filename; dir
119 already ends in a slash. The additional 2 is to accommodate the ".d"
120 when looking for configuration files in gconv-modules.d. */
121 size_t buflen = dir_len + sizeof (gconv_conf_filename) + 2;
122 char *buf = malloc (buflen + (prefix != NULL ? strlen (prefix) : 0));
123 char *cp = buf;
124 bool found = false;
126 if (buf == NULL)
127 return false;
129 if (prefix != NULL)
130 cp = stpcpy (cp, prefix);
132 cp = mempcpy (mempcpy (cp, dir, dir_len), gconv_conf_filename,
133 sizeof (gconv_conf_filename));
135 /* Read the gconv-modules configuration file first. */
136 found = read_conf_file (buf, dir, dir_len);
138 /* Next, see if there is a gconv-modules.d directory containing
139 configuration files and if it is non-empty. */
140 cp--;
141 cp[0] = '.';
142 cp[1] = 'd';
143 cp[2] = '\0';
145 DIR *confdir = opendir (buf);
146 if (confdir != NULL)
148 struct dirent *ent;
149 while ((ent = readdir (confdir)) != NULL)
151 if (ent->d_type != DT_REG && ent->d_type != DT_UNKNOWN)
152 continue;
154 size_t len = strlen (ent->d_name);
155 const char *suffix = ".conf";
157 if (len > strlen (suffix)
158 && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
160 char *conf;
161 struct stat64 st;
162 if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
163 continue;
165 if (ent->d_type != DT_UNKNOWN
166 || (lstat64 (conf, &st) != -1 && S_ISREG (st.st_mode)))
167 found |= read_conf_file (conf, dir, dir_len);
169 free (conf);
172 closedir (confdir);
174 free (buf);
175 return found;