This function does similar for auto.conf and autoconf.h Create __conf_write_autoconf() helper to factor out the common code.
Unified · Split
| @@ -1058,13 +1058,53 @@ static int conf_touch_deps(void) · 1057 unmodified lines | ||
| 1058 | 1058 | return 0; |
| 1059 | 1059 | } |
| 1060 | 1060 | |
| 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 | + | |
| 1061 | 1103 | int conf_write_autoconf(int overwrite) |
| 1062 | 1104 | { |
| 1063 | 1105 | struct symbol *sym; |
| 1064 | − const char *name; | |
| 1065 | 1106 | const char *autoconf_name = conf_get_autoconfig_name(); |
| 1066 | − FILE *out, *out_h; | |
| 1067 | − int i; | |
| 1107 | + int ret, i; | |
| 1068 | 1108 | |
| 1069 | 1109 | if (!overwrite && is_present(autoconf_name)) |
| 1070 | 1110 | return 0; |
| @@ -1074,45 +1114,25 @@ int conf_write_autoconf(int overwrite) · 3 unmodified lines | ||
| 1074 | 1114 | if (conf_touch_deps()) |
| 1075 | 1115 | return 1; |
| 1076 | 1116 | |
| 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) | |
| 1091 | 1118 | sym_calc_value(sym); |
| 1092 | − if (!(sym->flags & SYMBOL_WRITE) || !sym->name) | |
| 1093 | − continue; | |
| 1094 | 1119 | |
| 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; | |
| 1107 | 1125 | |
| 1108 | − if (make_parent_dir(autoconf_name)) | |
| 1109 | − return 1; | |
| 1110 | 1126 | /* |
| 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. | |
| 1113 | 1130 | */ |
| 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; | |
| 1116 | 1136 | |
| 1117 | 1137 | return 0; |
| 1118 | 1138 | } |