aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/basic/fixdep.c
diff options
context:
space:
mode:
authorMasahiro Yamada <yamada.masahiro@socionext.com>2018-01-11 08:05:42 -0500
committerMasahiro Yamada <yamada.masahiro@socionext.com>2018-01-17 19:37:38 -0500
commit4003fd80cba967e4044ebac96f13746153e87c4d (patch)
tree006b763982e18bb1625de91fd02ddd030d93a545 /scripts/basic/fixdep.c
parent01b5cbe7012fb1eeffc5c143865569835bcd405e (diff)
fixdep: factor out common code for reading files
Now, do_config_files() and print_deps() are almost the same. Only the difference is the parser function called (parse_config_file vs parse_dep_file). We can reduce the code duplication by factoring out the common code into read_file() - this function allocates a buffer and loads a file to it. It returns the pointer to the allocated buffer. (As before, it bails out by exit(2) for any error.) The caller must free the buffer when done. Having empty source files is possible; fixdep should simply skip them. I deleted the "st.st_size == 0" check, so read_file() allocates 1-byte buffer for an empty file. strstr() will immediately return NULL, and this is what we expect. On the other hand, an empty dep_file should be treated as an error. In this case, parse_dep_file() will error out with "no targets found" and it is a correct error message. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Diffstat (limited to 'scripts/basic/fixdep.c')
-rw-r--r--scripts/basic/fixdep.c75
1 files changed, 20 insertions, 55 deletions
diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 91bb4c13f121..9f9238eaec19 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -264,42 +264,36 @@ static int strrcmp(const char *s, const char *sub)
264 return memcmp(s + slen - sublen, sub, sublen); 264 return memcmp(s + slen - sublen, sub, sublen);
265} 265}
266 266
267static void do_config_file(const char *filename) 267static void *read_file(const char *filename)
268{ 268{
269 struct stat st; 269 struct stat st;
270 int fd; 270 int fd;
271 char *map; 271 char *buf;
272 272
273 fd = open(filename, O_RDONLY); 273 fd = open(filename, O_RDONLY);
274 if (fd < 0) { 274 if (fd < 0) {
275 fprintf(stderr, "fixdep: error opening config file: "); 275 fprintf(stderr, "fixdep: error opening file: ");
276 perror(filename); 276 perror(filename);
277 exit(2); 277 exit(2);
278 } 278 }
279 if (fstat(fd, &st) < 0) { 279 if (fstat(fd, &st) < 0) {
280 fprintf(stderr, "fixdep: error fstat'ing config file: "); 280 fprintf(stderr, "fixdep: error fstat'ing file: ");
281 perror(filename); 281 perror(filename);
282 exit(2); 282 exit(2);
283 } 283 }
284 if (st.st_size == 0) { 284 buf = malloc(st.st_size + 1);
285 close(fd); 285 if (!buf) {
286 return;
287 }
288 map = malloc(st.st_size + 1);
289 if (!map) {
290 perror("fixdep: malloc"); 286 perror("fixdep: malloc");
291 exit(2); 287 exit(2);
292 } 288 }
293 if (read(fd, map, st.st_size) != st.st_size) { 289 if (read(fd, buf, st.st_size) != st.st_size) {
294 perror("fixdep: read"); 290 perror("fixdep: read");
295 exit(2); 291 exit(2);
296 } 292 }
297 map[st.st_size] = '\0'; 293 buf[st.st_size] = '\0';
298 close(fd); 294 close(fd);
299 295
300 parse_config_file(map); 296 return buf;
301
302 free(map);
303} 297}
304 298
305/* 299/*
@@ -314,6 +308,7 @@ static void parse_dep_file(char *m)
314 int is_last, is_target; 308 int is_last, is_target;
315 int saw_any_target = 0; 309 int saw_any_target = 0;
316 int is_first_dep = 0; 310 int is_first_dep = 0;
311 void *buf;
317 312
318 while (1) { 313 while (1) {
319 /* Skip any "white space" */ 314 /* Skip any "white space" */
@@ -372,7 +367,10 @@ static void parse_dep_file(char *m)
372 is_first_dep = 0; 367 is_first_dep = 0;
373 } else 368 } else
374 printf(" %s \\\n", s); 369 printf(" %s \\\n", s);
375 do_config_file(s); 370
371 buf = read_file(s);
372 parse_config_file(buf);
373 free(buf);
376 } 374 }
377 } 375 }
378 376
@@ -397,46 +395,10 @@ static void parse_dep_file(char *m)
397 printf("$(deps_%s):\n", target); 395 printf("$(deps_%s):\n", target);
398} 396}
399 397
400static void print_deps(const char *filename)
401{
402 struct stat st;
403 int fd;
404 char *buf;
405
406 fd = open(filename, O_RDONLY);
407 if (fd < 0) {
408 fprintf(stderr, "fixdep: error opening depfile: ");
409 perror(filename);
410 exit(2);
411 }
412 if (fstat(fd, &st) < 0) {
413 fprintf(stderr, "fixdep: error fstat'ing depfile: ");
414 perror(filename);
415 exit(2);
416 }
417 if (st.st_size == 0) {
418 close(fd);
419 return;
420 }
421 buf = malloc(st.st_size + 1);
422 if (!buf) {
423 perror("fixdep: malloc");
424 exit(2);
425 }
426 if (read(fd, buf, st.st_size) != st.st_size) {
427 perror("fixdep: read");
428 exit(2);
429 }
430 buf[st.st_size] = '\0';
431 close(fd);
432
433 parse_dep_file(buf);
434
435 free(buf);
436}
437
438int main(int argc, char *argv[]) 398int main(int argc, char *argv[])
439{ 399{
400 void *buf;
401
440 if (argc == 5 && !strcmp(argv[1], "-e")) { 402 if (argc == 5 && !strcmp(argv[1], "-e")) {
441 insert_extra_deps = 1; 403 insert_extra_deps = 1;
442 argv++; 404 argv++;
@@ -448,7 +410,10 @@ int main(int argc, char *argv[])
448 cmdline = argv[3]; 410 cmdline = argv[3];
449 411
450 print_cmdline(); 412 print_cmdline();
451 print_deps(depfile); 413
414 buf = read_file(depfile);
415 parse_dep_file(buf);
416 free(buf);
452 417
453 return 0; 418 return 0;
454} 419}