Skip to content

Commit

Permalink
Use double pointers when passing FILE pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Mar 2, 2024
1 parent b41ce60 commit a0ae475
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
18 changes: 9 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ process_mrl(st_waste *waste_head,
fprintf(stderr, "while reading %s\n", mrl_file);
clearerr(fp);
}
close_file(fp, mrl_file, __func__);
close_file(&fp, mrl_file, __func__);
// This fixes a coverity warning, and may be a good idea anyway, since the data read
// by fread may not be null-terminated.
contents[f_size] = '\0';
Expand Down Expand Up @@ -132,16 +132,16 @@ add_removal(st_removed *removals, const char *file)
static void
create_undo_file(st_removed *removals_head, const char *mrl_file)
{
FILE *fd = fopen(mrl_file, "w");
if (fd)
FILE *fp = fopen(mrl_file, "w");
if (fp)
{
st_removed *st_removals_list = removals_head;
while (st_removals_list != NULL)
{
fprintf(fd, "%s\n", st_removals_list->file);
fprintf(fp, "%s\n", st_removals_list->file);
st_removals_list = st_removals_list->next_node;
}
close_file(fd, mrl_file, __func__);
close_file(&fp, mrl_file, __func__);
}
else
open_err(mrl_file, __func__);
Expand Down Expand Up @@ -522,14 +522,14 @@ get_locations(const char *alt_config_file)

if ((p_state = check_pathname_state(x.config_file)) == P_STATE_ENOENT)
{
FILE *fd = fopen(x.config_file, "w");
if (fd)
FILE *fp = fopen(x.config_file, "w");
if (fp)
{
puts(_("Creating default configuration file:"));
printf(" %s\n\n", x.config_file);

print_config(fd);
close_file(fd, x.config_file, __func__);
print_config(fp);
close_file(&fp, x.config_file, __func__);
}
else
{
Expand Down
7 changes: 4 additions & 3 deletions src/messages_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ function: <%s>\n\
* @return an error number, 0 if no error
*/
int
close_file(FILE *file_ptr, const char *filename, const char *function_name)
close_file(FILE **fp, const char *filename, const char *function_name)
{
if (file_ptr == NULL)
if (*fp == NULL)
return -1;

if (fclose(file_ptr) != EOF)
if (fclose(*fp) != EOF)
return 0;
else
{
Expand All @@ -84,6 +84,7 @@ function: <%s>\n\
}
}


void
display_dot_trashinfo_error(const char *dti)
{
Expand Down
3 changes: 1 addition & 2 deletions src/messages_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ void print_msg_warn(void);

void open_err(const char *filename, const char *function_name);

int
close_file(FILE * file_ptr, const char *filename, const char *function_name);
int close_file(FILE **fp, const char *filename, const char *function_name);

void display_dot_trashinfo_error(const char *dti);

Expand Down
6 changes: 3 additions & 3 deletions src/purging_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ is_time_to_purge(st_time *st_time_var, const char *file)
print_msg_error();
printf("while getting line from %s\n", file);
perror(__func__);
close_file(fp, file, __func__);
close_file(&fp, file, __func__);
exit(EXIT_FAILURE);
}

trim_whitespace(time_prev);
close_file(fp, file, __func__);
close_file(&fp, file, __func__);

if ((st_time_var->now - atoi(time_prev)) < SECONDS_IN_A_DAY)
return false;
Expand All @@ -69,7 +69,7 @@ is_time_to_purge(st_time *st_time_var, const char *file)
if (fp)
{
fprintf(fp, "%ld\n", st_time_var->now);
close_file(fp, file, __func__);
close_file(&fp, file, __func__);

/*
* if this is the first time the file got created, it's very likely
Expand Down
8 changes: 4 additions & 4 deletions src/restore_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,11 @@ undo_last_rmw(st_time *st_time_var, const char *mrl_file, const
{
if (cli_user_options->want_dry_run == false)
{
FILE *fd = fopen(mrl_file, "w");
if (fd != NULL)
FILE *fp = fopen(mrl_file, "w");
if (fp != NULL)
{
fprintf(fd, "%s", mrl_is_empty);
close_file(fd, mrl_file, __func__);
fprintf(fp, "%s", mrl_is_empty);
close_file(&fp, mrl_file, __func__);
}
else
open_err(mrl_file, __func__);
Expand Down
6 changes: 3 additions & 3 deletions src/trashinfo_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ create_trashinfo(rmw_target *st_f_props, st_waste *waste_curr,
**/
char *escaped_path = escape_url(st_f_props->real_path);
if (escaped_path == NULL)
return close_file(fp, final_info_dest, __func__);
return close_file(&fp, final_info_dest, __func__);

char *escaped_path_ptr = escaped_path;
if (waste_curr->media_root != NULL)
Expand All @@ -108,7 +108,7 @@ create_trashinfo(rmw_target *st_f_props, st_waste *waste_curr,
fprintf(fp, "%s%s\n", st_trashinfo_template[TI_DATE_LINE].str,
st_time_var->deletion_date);

return close_file(fp, final_info_dest, __func__);
return close_file(&fp, final_info_dest, __func__);
}
else
{
Expand Down Expand Up @@ -191,7 +191,7 @@ parse_trashinfo_file(const char *file, const char *req_value)
}
line_no++;
}
close_file(fp, file, __func__);
close_file(&fp, file, __func__);

if (res && line_no == TI_LINE_COUNT)
return trashinfo_field.value;
Expand Down
2 changes: 1 addition & 1 deletion test/test_restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fi
# /dev/sda7 on /mnt/sda7 type ext4 (rw,relatime)
# /dev/sda7 on /home/andy/src type ext4 (rw,relatime)

if test -d /mnt/e2f95a91; then
if test -d /mnt/918375c2; then
test_file="media_root_test"
PREV_RMW_FAKE_HOME=${RMW_FAKE_HOME}
# needs to be unset so rmw will use $HOME instead
Expand Down

0 comments on commit a0ae475

Please sign in to comment.