Skip to content

Commit

Permalink
fixing reallocs again
Browse files Browse the repository at this point in the history
  • Loading branch information
vinissou committed Oct 5, 2024
1 parent 77c764e commit 1e0bce9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/modules/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,29 @@ short int OUTPUT_STDOUT = 0;
short int OUTPUT_RETURN = 0; //implement return option

//TODO VVV does it need to be static???
static char *output_buffer = NULL;
//static char *output_buffer = NULL;




/* Make this work with buffered chunks*/
//TODO this is used in copy.h, FIX THIS >>> copy it there
char *ReturnOutput(FILE *file, const char *fmt, ...) //eliminate it?
void ReturnOutput(FILE *file, const char *fmt, ...) //eliminate it?
{
unsigned long long output_size = 0;
va_list arg;

output_buffer = realloc(output_buffer, OUTPUT_MAX);
char *output_buffer = malloc(OUTPUT_MAX);

va_start(arg, fmt);
output_size = vsnprintf(output_buffer, OUTPUT_MAX, fmt, arg);
va_end(arg);

if(output_size < OUTPUT_MAX){
output_buffer = realloc(output_buffer, output_size);
if (output_buffer == NULL){
free(output_buffer);
}
}

//TODO fix this VVV change it to a flag??
Expand All @@ -61,7 +64,7 @@ char *ReturnOutput(FILE *file, const char *fmt, ...) //eliminate it?
fprintf(file ,"%s" ,output_buffer);
}

return output_buffer;
free(output_buffer);
}


Expand Down
2 changes: 1 addition & 1 deletion src/modules/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern short int OUTPUT_RETURN;


/* FUNCTIONS */
extern char *ReturnOutput(FILE *file, const char *fmt, ...);
extern void ReturnOutput(FILE *file, const char *fmt, ...);
extern void NotFound(FILE *file, const char *term);
extern void ReturnResults(const long long position, const long long find_count);
extern void ReturnTotal(FILE *file, const long long find_count);

0 comments on commit 1e0bce9

Please sign in to comment.