чување f798b7975018cbe191e2234280256a1aa2bbd5f9
родитељ 2033ac42133f0e4d009e4c65fe2d80acaa604ba5
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Tue, 23 Jul 2024 08:39:35 +0200
slweb.c: Set return types for functions not returning anything to void;
make perror output consistent and initialize errno to zero everywhere
Diffstat:
| M | slweb.c | | | 88 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
измењених датотека: 1, додавања: 44(+), брисања: 44(-)
diff --git a/slweb.c b/slweb.c
@@ -70,15 +70,15 @@ static u8* get_value(KeyValue* list, const size_t list_count, const u8* key,
KeyValue** cursor);
static int print_meta_var(FILE* output, u8** tsv_header, u8** tsv_register);
static int print_output(FILE* output, const char* fmt, ...);
-static int process_horizontal_rule(FILE* output);
-static int process_incdir_subdir(FILE* output, const char* subdirname,
+static void process_horizontal_rule(FILE* output);
+static void process_incdir_subdir(FILE* output, const char* subdirname,
const u8* link_prefix, const int details_open, const u8* macro_body,
const u8* footer_permalink_text, const char* permalink_url,
const int ext_in_permalink, const int list_only);
-static int process_inline_image(FILE* output, const u8* image_text,
+static void process_inline_image(FILE* output, const u8* image_text,
const u8* image_file_prefix, const u8* link_prefix, const u8* image_url,
const int add_link, const int add_figcaption);
-static int process_inline_stylesheet(FILE* output, const u8* css_filename,
+static void process_inline_stylesheet(FILE* output, const u8* css_filename,
int output_yaml);
static void process_list_end(FILE* output);
static void process_list_item_end(FILE* output);
@@ -88,9 +88,9 @@ static void process_stylesheet(FILE* output, const u8* css_filename,
static void process_timestamp(FILE* output, const u8* link_prefix,
const char* link, const u8* permalink_macro, const u8* date,
const int ext_in_permalink);
-static int process_tsv(FILE* output, const u8* arg_token,
+static void process_tsv(FILE* output, const u8* arg_token,
const int read_yaml_macros_and_links, const int end_tag);
-static int process_tsv_count(FILE* output, const u8* arg_token,
+static void process_tsv_count(FILE* output, const u8* arg_token,
const int read_yaml_macros_and_links, const int is_tsv);
static void read_csv(FILE* output, const char* filename,
csv_callback_t callback);
@@ -303,7 +303,6 @@ cleanup(void)
free(links);
free(macros);
free(vars);
-
return 0;
}
@@ -329,7 +328,7 @@ end_footnotes(FILE* output, int add_footnote_div)
output_firstcol = 1;
}
- (void)process_horizontal_rule(output);
+ process_horizontal_rule(output);
for (footnote = 0; footnote < inline_footnote_count; footnote++)
{
@@ -578,13 +577,14 @@ print_command(FILE* output, const char* command, const u8* pass_arguments[],
/* Parent */
close(arg_pipe_fds[PIPE_READ_INDEX]);
close(output_pipe_fds[PIPE_WRITE_INDEX]);
+ errno = 0;
cmd_input = fdopen(arg_pipe_fds[PIPE_WRITE_INDEX], "w");
- cmd_output = fdopen(output_pipe_fds[PIPE_READ_INDEX], "r");
-
+ if (cmd_input)
+ cmd_output = fdopen(output_pipe_fds[PIPE_READ_INDEX], "r");
if (!cmd_input || !cmd_output)
{
- perror("fdopen");
- exit(error(1, __FILE__, __func__, __LINE__, "fdopen failed"));
+ perror(PROGRAMNAME ": fdopen");
+ exit(error(errno, __FILE__, __func__, __LINE__, "fdopen failed"));
}
if (pipe_arguments)
@@ -805,12 +805,13 @@ print_image_dimensions(FILE* output, const u8* image_file_prefix, const u8* path
if (stat(image_path, &sb) < 0)
{
perror(PROGRAMNAME ": stat");
- return 1;
+ return errno;
}
snprintf(command, BUFSIZE - 1, CMD_IDENTIFY, image_path);
cmd_output = popen(command, "r");
+ /* popen(3): "The popen() function does not reliably set errno." */
if (!cmd_output)
- return error(errno, __FILE__, __func__, __LINE__,
+ return error(1, __FILE__, __func__, __LINE__,
"popen failed");
if (!fgets(cmd_output_line, BUFSIZE, cmd_output))
goto print_dimensions_cleanup;
@@ -1278,23 +1279,22 @@ process_heading_start(FILE* output, const unsigned char heading_level,
return 0;
}
-static int
+static void
process_horizontal_rule(FILE* output)
{
print_output(output, "<hr>\n");
if (IN(state, ST_PARA_OPEN))
print_output(output, "<p>\n");
output_firstcol = 1;
- return 0;
}
-static int
+static void
process_image(FILE* output, const u8* image_text, const u8* image_file_prefix,
const u8* link_prefix, const u8* image_id, const int add_link,
const int add_figcaption)
{
u8* url = get_value(links, links_count, image_id, NULL);
- return process_inline_image(output, image_text, image_file_prefix,
+ process_inline_image(output, image_text, image_file_prefix,
link_prefix, url, add_link, add_figcaption);
}
@@ -1365,14 +1365,12 @@ incdir_next_arg:
incdir_done_args:
print_output(output, "<ul class=\"incdir\">\n");
+ /* scandir(3) does not set errno */
if ((names_total = scandir(incdir, &namelist, &filter_subdirs,
&reverse_alphacompare))
< 0)
- {
- perror(PROGRAMNAME ": scandir");
- exit(error(errno, __FILE__, __func__, __LINE__,
+ exit(error(1, __FILE__, __func__, __LINE__,
"scandir '%s' failed", incdir));
- }
pnamelist = namelist;
names_output = 0;
while (names_output < MIN(names_total, num))
@@ -1398,7 +1396,7 @@ incdir_done_args:
return 0;
}
-static int
+static void
process_incdir_subdir(FILE* output, const char* subdirname,
const u8* link_prefix, const int details_open, const u8* macro_body,
const u8* footer_permalink_text, const char* permalink_url,
@@ -1448,8 +1446,7 @@ process_incdir_subdir(FILE* output, const char* subdirname,
&reverse_alphacompare))
< 0)
{
- perror(PROGRAMNAME ": scandir");
- exit(error(errno, __FILE__, __func__, __LINE__,
+ exit(error(1, __FILE__, __func__, __LINE__,
"scandir '%s' failed", abs_subdirname));
}
@@ -1623,7 +1620,6 @@ process_incdir_subdir(FILE* output, const char* subdirname,
print_output(output, "</div>\n</details>\n</li>\n");
output_firstcol = 0;
- return 0;
}
static int
@@ -1664,12 +1660,14 @@ process_include(FILE* output, const u8* token,
child_output_line = NULL;
close(arg_pipe_fds[PIPE_READ_INDEX]);
close(output_pipe_fds[PIPE_WRITE_INDEX]);
+ errno = 0;
if (!(child_output
= fdopen(output_pipe_fds[PIPE_READ_INDEX], "r")))
{
- wait(&pstatus);
+ int save_errno = errno;
perror(PROGRAMNAME ": fdopen");
- exit(error(1, __FILE__, __func__, __LINE__,
+ wait(&pstatus);
+ exit(error(save_errno, __FILE__, __func__, __LINE__,
"fdopen failed"));
}
CALLOC(child_output_line, u8, BUFSIZE);
@@ -1693,11 +1691,12 @@ process_include(FILE* output, const u8* token,
{
close(arg_pipe_fds[PIPE_WRITE_INDEX]);
close(output_pipe_fds[PIPE_READ_INDEX]);
+ errno = 0;
if (!(child_output
= fdopen(output_pipe_fds[PIPE_WRITE_INDEX], "w")))
{
- perror("fdopen");
- exit(error(1, __FILE__, __func__, __LINE__,
+ perror(PROGRAMNAME ": fdopen");
+ exit(error(errno, __FILE__, __func__, __LINE__,
"fdopen failed"));
}
CALLOC(include_filename, char, BUFSIZE);
@@ -1806,7 +1805,7 @@ process_inline_footnote(const u8* token, const int read_yaml_macros_and_links,
return 0;
}
-static int
+static void
process_inline_image(FILE* output, const u8* image_text,
const u8* image_file_prefix, const u8* link_prefix, const u8* image_url,
const int add_link, const int add_figcaption)
@@ -1837,7 +1836,6 @@ process_inline_image(FILE* output, const u8* image_text,
image_text);
output_firstcol = 1;
}
- return 0;
}
static int
@@ -1853,7 +1851,7 @@ process_inline_link(FILE* output, const u8* link_text, const u8* link_prefix,
return 0;
}
-static int
+static void
process_inline_stylesheet(FILE* output, const u8* css_filename, int output_yaml)
{
FILE* css = NULL;
@@ -1871,6 +1869,7 @@ process_inline_stylesheet(FILE* output, const u8* css_filename, int output_yaml)
css_filename)
> css_pathname_size)
warning(1, (u8*)"snprintf:%d: Overflow", __LINE__);
+ errno = 0;
if (!(css = fopen((const char*)css_pathname, "rt")))
{
perror(PROGRAMNAME ": fopen");
@@ -1900,7 +1899,6 @@ process_inline_stylesheet_cleanup:
free(css_pathname);
fclose(css);
free(bufline);
- return 0;
}
static void
@@ -2328,7 +2326,7 @@ process_timestamp_cleanup:
free(in_filename);
}
-static int
+static void
process_tsv(FILE* output, const u8* arg_token,
const int read_yaml_macros_and_links, const int end_tag)
{
@@ -2342,7 +2340,7 @@ process_tsv(FILE* output, const u8* arg_token,
{
CLEAR(state, ST_TSV_BODY);
if (read_yaml_macros_and_links)
- return 0;
+ return;
read_tsv(output, tsv_filename, &print_tsv_row);
free(tsv_filename);
tsv_filename = NULL;
@@ -2357,7 +2355,7 @@ process_tsv(FILE* output, const u8* arg_token,
"Can't nest csv/tsv directives"));
SET(state, ST_TSV_BODY);
if (read_yaml_macros_and_links)
- return 0;
+ return;
tsv_iter = 0;
(void)strtok_r((char*)arg_token, " ", (char**)&saveptr);
args = (u8*)strtok_r(NULL, " ", (char**)&saveptr);
@@ -2385,10 +2383,9 @@ process_tsv(FILE* output, const u8* arg_token,
"Invalid argument '%s'", args));
}
}
- return 0;
}
-static int
+static void
process_tsv_count(FILE* output, const u8* arg_token,
const int read_yaml_macros_and_links, const int is_tsv)
{
@@ -2403,7 +2400,7 @@ process_tsv_count(FILE* output, const u8* arg_token,
assert((output != NULL) && (arg_token != NULL));
if (read_yaml_macros_and_links)
- return 0;
+ return;
tsv_iter = 0;
(void)strtok_r((char*)arg_token, " ", (char**)&saveptr);
args = (u8*)strtok_r(NULL, " ", (char**)&saveptr);
@@ -2421,10 +2418,11 @@ process_tsv_count(FILE* output, const u8* arg_token,
snprintf(tsv_filename, BUFSIZE, "%s/%s.%csv", input_dirname,
(char*)args_base, is_tsv ? 't' : 'c');
free(args_base);
+ errno = 0;
if (!(tsv = fopen(tsv_filename, "rt")))
{
- perror("fopen");
- exit(error(ENOENT, __FILE__, __func__, __LINE__,
+ perror(PROGRAMNAME ": fopen");
+ exit(error(errno, __FILE__, __func__, __LINE__,
"fopen failed: %s", tsv_filename));
}
CALLOC(bufline, char, BUFSIZE);
@@ -2448,7 +2446,6 @@ process_tsv_count(FILE* output, const u8* arg_token,
free(bufline);
free(tsv_filename);
tsv_filename = NULL;
- return 0;
}
static void
@@ -2473,9 +2470,10 @@ read_csv(FILE* output, const char* filename, csv_callback_t callback)
assert((output != NULL) && (filename != NULL) && (callback != NULL));
csv_delimiter = get_value(vars, vars_count, (u8*)"csv-delimiter", NULL);
+ errno = 0;
if (!(csv = fopen(filename, "rt")))
{
- perror("fopen");
+ perror(PROGRAMNAME ": fopen");
exit(error(errno, __FILE__, __func__, __LINE__,
"fopen failed: %s", filename));
}
@@ -2670,6 +2668,7 @@ read_tsv(FILE* output, const char* filename, tsv_callback_t callback)
unsigned char i;
assert((output != NULL) && (filename != NULL) && (callback != NULL));
+ errno = 0;
if (!(tsv = fopen(filename, "rt")))
{
perror(PROGRAMNAME ": fopen");
@@ -2911,6 +2910,7 @@ strip_ext(const char* fn, const size_t fn_size)
dot = strrchr(fn, '.');
if (!dot)
return NULL;
+ errno = 0;
newname = strndup(fn, fn_size);
if (!newname)
{