1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <ctype.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32
33#define ERROR_EXIT(strExit) \
34 { \
35 const int errnoSave = errno; \
36 fprintf(stderr, "%s: ", str_my_name); \
37 errno = errnoSave; \
38 perror((strExit)); \
39 exit(1); \
40 }
41
42
43
44int main(int argc, const char * argv [])
45{
46 const char * str_my_name;
47 const char * str_file_autoconf;
48 const char * str_dir_config;
49
50 FILE * fp_config;
51 FILE * fp_target;
52 FILE * fp_find;
53
54 int buffer_size;
55
56 char * line;
57 char * old_line;
58 char * list_target;
59 char * ptarget;
60
61 struct stat stat_buf;
62
63
64 if (argc != 3)
65 {
66 fprintf(stderr, "%s: wrong number of arguments.\n", argv[0]);
67 exit(1);
68 }
69
70 str_my_name = argv[0];
71 str_file_autoconf = argv[1];
72 str_dir_config = argv[2];
73
74
75 if (stat(str_file_autoconf, &stat_buf) != 0)
76 ERROR_EXIT(str_file_autoconf);
77 buffer_size = 2 * stat_buf.st_size + 4096;
78
79
80 if ( (line = malloc(buffer_size)) == NULL
81 || (old_line = malloc(buffer_size)) == NULL
82 || (list_target = malloc(buffer_size)) == NULL )
83 ERROR_EXIT(str_file_autoconf);
84
85
86 if ((fp_config = fopen(str_file_autoconf, "r")) == NULL)
87 ERROR_EXIT(str_file_autoconf);
88
89
90 if (stat(str_dir_config, &stat_buf) != 0)
91 {
92 if (mkdir(str_dir_config, 0755) != 0)
93 ERROR_EXIT(str_dir_config);
94 }
95
96
97 if (chdir(str_dir_config) != 0)
98 ERROR_EXIT(str_dir_config);
99
100
101 ptarget = list_target;
102 *ptarget++ = '\n';
103
104
105 while (fgets(line, buffer_size, fp_config))
106 {
107 const char * str_config;
108 int is_same;
109 int itarget;
110
111 if (line[0] != '#')
112 continue;
113 if ((str_config = strstr(line, " CONFIG_")) == NULL)
114 continue;
115
116
117
118 str_config += sizeof(" CONFIG_") - 1;
119 for (itarget = 0; !isspace(str_config[itarget]); itarget++)
120 {
121 int c = (unsigned char) str_config[itarget];
122 if (isupper(c)) c = tolower(c);
123 if (c == '_') c = '/';
124 ptarget[itarget] = c;
125 }
126 ptarget[itarget++] = '.';
127 ptarget[itarget++] = 'h';
128 ptarget[itarget++] = '\0';
129
130
131 is_same = 0;
132 if ((fp_target = fopen(ptarget, "r")) != NULL)
133 {
134 fgets(old_line, buffer_size, fp_target);
135 if (fclose(fp_target) != 0)
136 ERROR_EXIT(ptarget);
137 if (!strcmp(line, old_line))
138 is_same = 1;
139 }
140
141 if (!is_same)
142 {
143
144 int islash;
145 for (islash = 0; islash < itarget; islash++)
146 {
147 if (ptarget[islash] == '/')
148 {
149 ptarget[islash] = '\0';
150 if (stat(ptarget, &stat_buf) != 0
151 && mkdir(ptarget, 0755) != 0)
152 ERROR_EXIT( ptarget );
153 ptarget[islash] = '/';
154 }
155 }
156
157
158 if ((fp_target = fopen(ptarget, "w")) == NULL)
159 ERROR_EXIT(ptarget);
160 fputs(line, fp_target);
161 if (ferror(fp_target) || fclose(fp_target) != 0)
162 ERROR_EXIT(ptarget);
163 }
164
165
166 ptarget += itarget;
167 *(ptarget-1) = '\n';
168 }
169
170
171
172
173
174 if (fclose(fp_config) != 0)
175 ERROR_EXIT(str_file_autoconf);
176 *ptarget = '\0';
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 fp_find = popen("find * -type f -name \"*.h\" -print", "r");
193 if (fp_find == 0)
194 ERROR_EXIT( "find" );
195
196 line[0] = '\n';
197 while (fgets(line+1, buffer_size, fp_find))
198 {
199 if (strstr(list_target, line) == NULL)
200 {
201
202
203
204
205
206 line[strlen(line)-1] = '\0';
207
208
209 if (stat(line+1, &stat_buf) != 0)
210 ERROR_EXIT(line);
211
212
213 if (stat_buf.st_size != 0)
214 {
215 if ((fp_target = fopen(line+1, "w")) == NULL)
216 ERROR_EXIT(line);
217 if (fclose(fp_target) != 0)
218 ERROR_EXIT(line);
219 }
220 }
221 }
222
223 if (pclose(fp_find) != 0)
224 ERROR_EXIT("find");
225
226 return 0;
227}
228