2 * (c) Copyright 1998-2001 by Rob Braun
3 * All rights reserved. The file named COPYRIGHT specifies the terms
4 * and conditions for redistribution.
20 #include "includedir.h"
25 #if !defined(NAME_MAX)
27 #define NAME_MAX FILENAME_MAX
33 static int compfunc( const void *_a, const void *_b )
35 char **a = (char **)_a;
36 char **b = (char **)_b;
38 if( a == NULL || a[0] == NULL )
40 if( b == NULL || b[0] == NULL )
42 return strcmp(a[0], b[0]);
45 void handle_includedir(const char *service_name, struct configuration *confp)
50 struct dirent *direntry;
54 const char *func = "handle_includedir";
56 if( service_name == NULL )
59 dir_list = pset_create(0, 0);
60 if( dir_list == NULL )
63 len_sn = strlen(service_name);
64 filename = (char *)malloc(len_sn + NAME_MAX + 2);
66 parsemsg( LOG_ERR, func, ES_NOMEM );
70 dirfp = opendir(service_name);
72 parsemsg( LOG_ERR, func, "Unable to read included directory: %s", service_name);
76 /* Get the list of files in the directory */
77 while ((direntry = readdir(dirfp)) != 0) {
78 storename = new_string(direntry->d_name);
79 if( storename == NULL ) {
80 parsemsg( LOG_ERR, func, ES_NOMEM );
84 pset_add(dir_list, storename);
88 /* Sort the list using "compfunc" */
89 pset_sort(dir_list, compfunc);
91 /* Now, traverse the list in alphabetic order
92 * (as determined by strcmp).
94 for( u = 0; (unsigned)u < pset_count(dir_list); u++ ) {
95 storename = pset_pointer(dir_list, u);
97 /* Don't try to parse any files containing a dot ('.')
98 * or ending with a tilde ('~'). This catches the case of
99 * '.' and '..', as well as preventing the parsing of
100 * many editor files, temporary files and those saved by RPM
103 if ( !storename[0] /* Shouldn't happen */ ||
104 strchr(storename, '.') ||
105 storename[strlen(storename)-1] == '~') {
106 pset_remove(dir_list, storename);
112 strx_sprint(filename, len_sn+NAME_MAX+1, "%s/%s",
113 service_name, storename);
115 if( stat(filename, &sb) < 0 ) {
116 parsemsg( LOG_ERR, func, "Unable to stat includedir file %s", filename);
117 pset_remove(dir_list, storename);
123 /* Only open it if it's a regular file. */
124 if( !S_ISREG(sb.st_mode) ) {
126 "%s is not a regular file. It is being skipped.",
128 pset_remove(dir_list, storename);
133 incfd = open(filename, O_RDONLY);
135 parsemsg( LOG_ERR, func, "Unable to open included configuration file: %s", filename);
136 pset_remove(dir_list, storename);
141 parsemsg( LOG_DEBUG,func,"Reading included configuration file: %s",filename);
142 parse_conf_file(incfd, confp, filename);
145 * parse_conf_file eventually calls Srdline, try Sclosing to
149 pset_remove(dir_list, storename);
154 parsemsg( LOG_ERR, func, "Error reading included directory: %s", service_name);
156 pset_destroy(dir_list);