Skip to content

Commit 4003fd8

Browse files
committed
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>
1 parent 01b5cbe commit 4003fd8

File tree

1 file changed

+20
-55
lines changed

1 file changed

+20
-55
lines changed

scripts/basic/fixdep.c

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -264,42 +264,36 @@ static int strrcmp(const char *s, const char *sub)
264264
return memcmp(s + slen - sublen, sub, sublen);
265265
}
266266

267-
static void do_config_file(const char *filename)
267+
static void *read_file(const char *filename)
268268
{
269269
struct stat st;
270270
int fd;
271-
char *map;
271+
char *buf;
272272

273273
fd = open(filename, O_RDONLY);
274274
if (fd < 0) {
275-
fprintf(stderr, "fixdep: error opening config file: ");
275+
fprintf(stderr, "fixdep: error opening file: ");
276276
perror(filename);
277277
exit(2);
278278
}
279279
if (fstat(fd, &st) < 0) {
280-
fprintf(stderr, "fixdep: error fstat'ing config file: ");
280+
fprintf(stderr, "fixdep: error fstat'ing file: ");
281281
perror(filename);
282282
exit(2);
283283
}
284-
if (st.st_size == 0) {
285-
close(fd);
286-
return;
287-
}
288-
map = malloc(st.st_size + 1);
289-
if (!map) {
284+
buf = malloc(st.st_size + 1);
285+
if (!buf) {
290286
perror("fixdep: malloc");
291287
exit(2);
292288
}
293-
if (read(fd, map, st.st_size) != st.st_size) {
289+
if (read(fd, buf, st.st_size) != st.st_size) {
294290
perror("fixdep: read");
295291
exit(2);
296292
}
297-
map[st.st_size] = '\0';
293+
buf[st.st_size] = '\0';
298294
close(fd);
299295

300-
parse_config_file(map);
301-
302-
free(map);
296+
return buf;
303297
}
304298

305299
/*
@@ -314,6 +308,7 @@ static void parse_dep_file(char *m)
314308
int is_last, is_target;
315309
int saw_any_target = 0;
316310
int is_first_dep = 0;
311+
void *buf;
317312

318313
while (1) {
319314
/* Skip any "white space" */
@@ -372,7 +367,10 @@ static void parse_dep_file(char *m)
372367
is_first_dep = 0;
373368
} else
374369
printf(" %s \\\n", s);
375-
do_config_file(s);
370+
371+
buf = read_file(s);
372+
parse_config_file(buf);
373+
free(buf);
376374
}
377375
}
378376

@@ -397,46 +395,10 @@ static void parse_dep_file(char *m)
397395
printf("$(deps_%s):\n", target);
398396
}
399397

400-
static 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-
438398
int main(int argc, char *argv[])
439399
{
400+
void *buf;
401+
440402
if (argc == 5 && !strcmp(argv[1], "-e")) {
441403
insert_extra_deps = 1;
442404
argv++;
@@ -448,7 +410,10 @@ int main(int argc, char *argv[])
448410
cmdline = argv[3];
449411

450412
print_cmdline();
451-
print_deps(depfile);
413+
414+
buf = read_file(depfile);
415+
parse_dep_file(buf);
416+
free(buf);
452417

453418
return 0;
454419
}

0 commit comments

Comments
 (0)