From 952dc3ae323a5e39ffce19252784ace1f17d24ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Tue, 28 Nov 2017 19:57:09 +0000 Subject: make-dissectors: Use g_file_set_contents() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also add some more errors checks, we only pass valid files to make-dissectors. Change-Id: I9c068e47f35ee6c3da0112ee9ce905af35030475 Reviewed-on: https://code.wireshark.org/review/24625 Reviewed-by: João Valverde Petri-Dish: João Valverde Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- epan/dissectors/make-dissectors.c | 64 ++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/epan/dissectors/make-dissectors.c b/epan/dissectors/make-dissectors.c index ffc35a4366..bca8ce1e38 100644 --- a/epan/dissectors/make-dissectors.c +++ b/epan/dissectors/make-dissectors.c @@ -16,6 +16,7 @@ #include #define ARRAY_RESERVED_SIZE 2048 +#define STRING_RESERVED_SIZE (300 * 1024) #ifdef _WIN32 #define SEP "\r\n" @@ -50,21 +51,27 @@ static void scan_file(const char *file, GPtrArray *protos, GPtrArray *handoffs) { char *contents; + GError *err = NULL; - if (!g_file_get_contents(file, &contents, NULL, NULL)) - return; + if (!g_file_get_contents(file, &contents, NULL, &err)) { + fprintf(stderr, "%s: %s\n", file, err->message); + exit(1); + } scan_matches(protos_regex, contents, protos); scan_matches(handoffs_regex, contents, handoffs); g_free(contents); } static void -scan_list(const char *list, GPtrArray *protos, GPtrArray *handoffs) +scan_list(const char *file, GPtrArray *protos, GPtrArray *handoffs) { char *contents, *arg; + GError *err = NULL; - if (!g_file_get_contents(list, &contents, NULL, NULL)) - return; + if (!g_file_get_contents(file, &contents, NULL, NULL)) { + fprintf(stderr, "%s: %s\n", file, err->message); + exit(1); + } for (arg = strtok(contents, SEP); arg != NULL; arg = strtok(NULL, SEP)) { scan_file(arg, protos, handoffs); } @@ -76,7 +83,9 @@ int main(int argc, char **argv) GPtrArray *protos = NULL, *handoffs = NULL; GError *err = NULL; guint i; - FILE *out; + GString *s; + const char *outfile; + guint count_protos, count_handoffs; if (argc < 3) { fprintf(stderr, "Usage: %s \n", argv[0]); @@ -99,6 +108,7 @@ int main(int argc, char **argv) exit(1); } + outfile = argv[1]; for (int arg = 2; arg < argc; arg++) { if (argv[arg][0] == '@') { scan_list(&argv[arg][1], protos, handoffs); @@ -113,16 +123,12 @@ int main(int argc, char **argv) exit(1); } - out = fopen(argv[1], "w"); - if (out == NULL) { - fprintf(stderr, "Error opening file: %s: %s\n", argv[1], strerror(errno)); - exit(1); - } - g_ptr_array_sort(protos, compare_symbols); g_ptr_array_sort(handoffs, compare_symbols); - fprintf(out, + s = g_string_sized_new(STRING_RESERVED_SIZE); + + g_string_append(s, "/*\n" " * Do not modify this file. Changes will be overwritten.\n" " *\n" @@ -130,52 +136,60 @@ int main(int argc, char **argv) " * \"make-dissectors\".\n" " */\n" "\n" - "#include \n" "#include \n" "\n"); - fprintf(out, + g_string_append_printf(s, "const gulong dissector_reg_proto_count = %d;\n" "const gulong dissector_reg_handoff_count = %d;\n" "\n", protos->len, handoffs->len); for (i = 0; i < protos->len; i++) { - fprintf(out, "void %s(void);\n", (char *)protos->pdata[i]); + g_string_append_printf(s, "void %s(void);\n", (char *)protos->pdata[i]); } - fprintf(out, + g_string_append(s, "\n" "dissector_reg_t dissector_reg_proto[] = {\n"); for (i = 0; i < protos->len; i++) { - fprintf(out, " { \"%s\", %s },\n", (char *)protos->pdata[i], (char *)protos->pdata[i]); + g_string_append_printf(s, " { \"%s\", %s },\n", (char *)protos->pdata[i], (char *)protos->pdata[i]); } - fprintf(out, + g_string_append(s, " { NULL, NULL }\n" "};\n" "\n"); for (i = 0; i < handoffs->len; i++) { - fprintf(out, "void %s(void);\n", (char *)handoffs->pdata[i]); + g_string_append_printf(s, "void %s(void);\n", (char *)handoffs->pdata[i]); } - fprintf(out, + g_string_append(s, "\n" "dissector_reg_t dissector_reg_handoff[] = {\n"); for (i = 0; i < handoffs->len; i++) { - fprintf(out, " { \"%s\", %s },\n", (char *)handoffs->pdata[i], (char *)handoffs->pdata[i]); + g_string_append_printf(s, " { \"%s\", %s },\n", (char *)handoffs->pdata[i], (char *)handoffs->pdata[i]); } - fprintf(out, + g_string_append(s, " { NULL, NULL }\n" "};\n"); - fclose(out); + if (!g_file_set_contents(outfile, s->str, s->len, &err)) { + fprintf(stderr, "%s: %s\n", outfile, err->message); + exit(1); + } + + count_protos = protos->len; + count_handoffs = handoffs->len; - printf("Found %u registrations and %u handoffs.\n", protos->len, handoffs->len); + g_string_free(s, TRUE); g_regex_unref(protos_regex); g_regex_unref(handoffs_regex); g_ptr_array_free(protos, TRUE); g_ptr_array_free(handoffs, TRUE); + + printf("Found %u registrations and %u handoffs.\n", + count_protos, count_handoffs); } /* -- cgit v1.2.3