xref: /aosp_15_r20/external/libconfig/examples/c/example4.c (revision 2e9d491483b805f09ea864149eadd5680efcc72a)
1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    Copyright (C) 2005-2018  Mark A Lindner
4 
5    This file is part of libconfig.
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public License
9    as published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public
18    License along with this library; if not, see
19    <http://www.gnu.org/licenses/>.
20    ----------------------------------------------------------------------------
21 */
22 
23 #include <dirent.h>
24 #include <fnmatch.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 
31 #include <libconfig.h>
32 
33 /* This example reads the configuration file 'example.cfg' and displays
34  * some of its contents.
35  */
36 
include_func(config_t * config,const char * include_dir,const char * path,const char ** error)37 static const char **include_func(config_t *config,
38                                  const char *include_dir,
39                                  const char *path,
40                                  const char **error)
41 {
42   char *p;
43   DIR *dp;
44   struct dirent *dir_entry;
45   struct stat stat_buf;
46   char include_path[PATH_MAX + 1];
47   size_t include_path_len = 0;
48   char file_path[PATH_MAX + 1];
49   char **result = NULL;
50   char **result_next = result;
51   int result_count = 0;
52   int result_capacity = 0;
53 
54   *include_path = 0;
55 
56   if(*path != '/')
57   {
58     if(include_dir)
59     {
60       strcat(include_path, include_dir);
61       include_path_len += strlen(include_dir);
62     }
63   }
64 
65   p = strrchr(path, '/');
66   if(p > path)
67   {
68     int len = p - path;
69 
70     if((include_path_len > 0)
71        && (*(include_path + include_path_len - 1) != '/'))
72     {
73       strcat(include_path, "/");
74       ++include_path_len;
75     }
76 
77     strncat(include_path, path, len);
78     include_path_len += len;
79   }
80 
81   if(include_path_len == 0)
82   {
83     strcpy(include_path, ".");
84     include_path_len = 1;
85   }
86 
87   dp = opendir(include_path);
88   if(dp)
89   {
90     while((dir_entry = readdir(dp)) != NULL)
91     {
92       snprintf(file_path, PATH_MAX, "%s/%s", include_path, dir_entry->d_name);
93       if(lstat(file_path, &stat_buf) != 0) continue;
94       if(!S_ISREG(stat_buf.st_mode)) continue;
95       if(fnmatch(path, file_path, FNM_PATHNAME) != 0) continue;
96 
97       if(result_count == result_capacity)
98       {
99         result_capacity += 16;
100         result = (char **)realloc(result, (result_capacity + 1) * sizeof(char *));
101         result_next = result + result_count;
102       }
103 
104       *result_next = strdup(file_path);
105       ++result_next;
106       ++result_count;
107 
108       printf("file: %s\n", file_path);
109     }
110     closedir(dp);
111   }
112 
113   *result_next = NULL;
114 
115   return((const char **)result);
116 }
117 
main(int argc,char ** argv)118 int main(int argc, char **argv)
119 {
120   config_t cfg;
121 
122   config_init(&cfg);
123   config_set_include_func(&cfg, include_func);
124 
125   /* Read the file. If there is an error, report it and exit. */
126   if(! config_read_file(&cfg, "example4.cfg"))
127   {
128     fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
129             config_error_line(&cfg), config_error_text(&cfg));
130     config_destroy(&cfg);
131     return(EXIT_FAILURE);
132   }
133 
134   config_write(&cfg, stdout);
135 
136   return(EXIT_SUCCESS);
137 }
138