--- a/src/c2html.c Fri Aug 26 13:49:19 2016 +0200 +++ b/src/c2html.c Fri Aug 26 14:15:29 2016 +0200 @@ -34,7 +34,24 @@ #include "ucx/list.h" -void printhelp() { +static int appendfile(const char *filename, FILE *fout, + char *copybuf, size_t copybuflen, const char *errmsg) { + FILE *headerfile = fopen(filename, "r"); + if (!headerfile) { + perror(errmsg); + if (fout != stdout) { + fclose(fout); + } + return 1; + } + ucx_stream_copy(headerfile, fout, + (read_func) fread, (write_func) fwrite, + copybuf, copybuflen, (size_t)-1); + fclose(headerfile); + return 0; +} + +static void printhelp() { printf("Formats source code using HTML.\n\nUsage:\n" " c2html [Options] FILE\n\n" " Options:\n" @@ -49,16 +66,14 @@ "\n"); } -void formatlines(highlighter_func highlighter, +static void formatlines(highlighter_func highlighter, UcxList *in, write_func out, void *stream, int showlineno) { /* compute width of line numbering */ - int lnw; + int lnw = 0; if (showlineno) { size_t lines = ucx_list_size(in); - lnw = 1; - int p = 1; - while ((p*=10) < lines) lnw++; + for (size_t p = 1; p < lines ; p*=10) lnw++; } /* start monospace formatting */ @@ -198,19 +213,9 @@ } /* Prepend header file */ - { - FILE *headerfile = fopen(settings.headerfile, "r"); - if (!headerfile) { - perror("Error opening header file"); - if (fout != stdout) { - fclose(fout); - } - return EXIT_FAILURE; - } - ucx_stream_copy(headerfile, fout, - (read_func) fread, (write_func) fwrite, - filebuf, FILEBUF_SIZE, (size_t)-1); - fclose(headerfile); + if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, + "Error opening header file")) { + return EXIT_FAILURE; } /* Process input file */ @@ -248,21 +253,10 @@ } /* Append footer file */ - { - FILE *footerfile = fopen(settings.footerfile, "r"); - if (!footerfile) { - perror("Error opening footer file"); - if (fout != stdout) { - fclose(fout); - } - return EXIT_FAILURE; - } - ucx_stream_copy(footerfile, fout, - (read_func) fread, (write_func) fwrite, - filebuf, FILEBUF_SIZE, (size_t)-1); - fclose(footerfile); + if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, + "Error opening footer file")) { + return EXIT_FAILURE; } - free(filebuf);