Skip to content

Commit 57ddd07

Browse files
committed
kconfig: refactor conf_write_autoconf()
This function does similar for auto.conf and autoconf.h Create __conf_write_autoconf() helper to factor out the common code. Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 8499f2d commit 57ddd07

File tree

1 file changed

+57
-37
lines changed

1 file changed

+57
-37
lines changed

scripts/kconfig/confdata.c

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,13 +1058,53 @@ static int conf_touch_deps(void)
10581058
return 0;
10591059
}
10601060

1061+
static int __conf_write_autoconf(const char *filename,
1062+
void (*print_symbol)(FILE *, struct symbol *),
1063+
const struct comment_style *comment_style)
1064+
{
1065+
char tmp[PATH_MAX];
1066+
FILE *file;
1067+
struct symbol *sym;
1068+
int ret, i;
1069+
1070+
if (make_parent_dir(filename))
1071+
return -1;
1072+
1073+
ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1074+
if (ret >= sizeof(tmp)) /* check truncation */
1075+
return -1;
1076+
1077+
file = fopen(tmp, "w");
1078+
if (!file) {
1079+
perror("fopen");
1080+
return -1;
1081+
}
1082+
1083+
conf_write_heading(file, comment_style);
1084+
1085+
for_all_symbols(i, sym)
1086+
if ((sym->flags & SYMBOL_WRITE) && sym->name)
1087+
print_symbol(file, sym);
1088+
1089+
/* check possible errors in conf_write_heading() and print_symbol() */
1090+
if (ferror(file))
1091+
return -1;
1092+
1093+
fclose(file);
1094+
1095+
if (rename(tmp, filename)) {
1096+
perror("rename");
1097+
return -1;
1098+
}
1099+
1100+
return 0;
1101+
}
1102+
10611103
int conf_write_autoconf(int overwrite)
10621104
{
10631105
struct symbol *sym;
1064-
const char *name;
10651106
const char *autoconf_name = conf_get_autoconfig_name();
1066-
FILE *out, *out_h;
1067-
int i;
1107+
int ret, i;
10681108

10691109
if (!overwrite && is_present(autoconf_name))
10701110
return 0;
@@ -1074,45 +1114,25 @@ int conf_write_autoconf(int overwrite)
10741114
if (conf_touch_deps())
10751115
return 1;
10761116

1077-
out = fopen(".tmpconfig", "w");
1078-
if (!out)
1079-
return 1;
1080-
1081-
out_h = fopen(".tmpconfig.h", "w");
1082-
if (!out_h) {
1083-
fclose(out);
1084-
return 1;
1085-
}
1086-
1087-
conf_write_heading(out, &comment_style_pound);
1088-
conf_write_heading(out_h, &comment_style_c);
1089-
1090-
for_all_symbols(i, sym) {
1117+
for_all_symbols(i, sym)
10911118
sym_calc_value(sym);
1092-
if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1093-
continue;
10941119

1095-
/* write symbols to auto.conf and autoconf.h */
1096-
print_symbol_for_autoconf(out, sym);
1097-
print_symbol_for_c(out_h, sym);
1098-
}
1099-
fclose(out);
1100-
fclose(out_h);
1101-
1102-
name = conf_get_autoheader_name();
1103-
if (make_parent_dir(name))
1104-
return 1;
1105-
if (rename(".tmpconfig.h", name))
1106-
return 1;
1120+
ret = __conf_write_autoconf(conf_get_autoheader_name(),
1121+
print_symbol_for_c,
1122+
&comment_style_c);
1123+
if (ret)
1124+
return ret;
11071125

1108-
if (make_parent_dir(autoconf_name))
1109-
return 1;
11101126
/*
1111-
* This must be the last step, kbuild has a dependency on auto.conf
1112-
* and this marks the successful completion of the previous steps.
1127+
* Create include/config/auto.conf. This must be the last step because
1128+
* Kbuild has a dependency on auto.conf and this marks the successful
1129+
* completion of the previous steps.
11131130
*/
1114-
if (rename(".tmpconfig", autoconf_name))
1115-
return 1;
1131+
ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1132+
print_symbol_for_autoconf,
1133+
&comment_style_pound);
1134+
if (ret)
1135+
return ret;
11161136

11171137
return 0;
11181138
}

0 commit comments

Comments
 (0)