Reworked date function to take buffer as an argument

This commit is contained in:
solusipse
2017-09-05 22:04:21 +02:00
parent 99b45b93f2
commit 10cb524891

44
fiche.c
View File

@@ -175,7 +175,7 @@ static void log_entry(const Fiche_Settings *s, const char *ip,
* @brief Returns string containing current date * @brief Returns string containing current date
* @warning Output has to be freed! * @warning Output has to be freed!
*/ */
static char *get_date(); static void get_date(char *buf);
/** /**
@@ -223,9 +223,9 @@ int fiche_run(Fiche_Settings settings) {
// Display welcome message // Display welcome message
{ {
char *date = get_date(); char date[64];
get_date(date);
print_status("Starting fiche on %s...", date); print_status("Starting fiche on %s...", date);
free(date);
} }
// Try to set requested user // Try to set requested user
@@ -329,34 +329,32 @@ static void log_entry(const Fiche_Settings *s, const char *ip,
return; return;
} }
char *date = get_date(); char date[64];
get_date(date);
// Write entry to file // Write entry to file
fprintf(f, "%s -- %s -- %s (%s)\n", slug, date, ip, hostname); fprintf(f, "%s -- %s -- %s (%s)\n", slug, date, ip, hostname);
fclose(f); fclose(f);
free(date);
} }
static char *get_date() { static void get_date(char *buf) {
struct tm curtime; struct tm curtime;
time_t ltime; time_t ltime;
ltime=time(&ltime); ltime=time(&ltime);
localtime_r(&ltime, &curtime); localtime_r(&ltime, &curtime);
// Much more than required, date string is usually about 25 chars // Save data to provided buffer
char buf[128]; if (asctime_r(&curtime, buf) == 0) {
asctime_r(&curtime, buf); // Couldn't get date, setting first byte of the
// buffer to zero so it won't be displayed
char *out = malloc(strlen(buf) + 1); buf[0] = 0;
strcpy(out, buf); return;
}
// Remove newline char // Remove newline char
out[strlen(buf)-1] = 0; buf[strlen(buf)-1] = 0;
return out;
} }
@@ -366,7 +364,7 @@ static int set_domain_name(Fiche_Settings *settings) {
const int len = strlen(settings->domain) + strlen(prefix) + 1; const int len = strlen(settings->domain) + strlen(prefix) + 1;
char *b = malloc(len); char *b = malloc(len);
if (b == NULL) { if (!b) {
return -1; return -1;
} }
@@ -501,7 +499,6 @@ static void dispatch_connection(int socket, Fiche_Settings *settings) {
} }
// Create an argument for the thread function // Create an argument for the thread function
//struct fiche_connection c = { s, address, settings };
struct fiche_connection *c = malloc(sizeof(*c)); struct fiche_connection *c = malloc(sizeof(*c));
if (!c) { if (!c) {
print_error("Couldn't allocate memory!"); print_error("Couldn't allocate memory!");
@@ -546,9 +543,9 @@ static void *handle_connection(void *args) {
// Print status on this connection // Print status on this connection
{ {
char *date = get_date(); char date[64];
get_date(date);
print_status("%s", date); print_status("%s", date);
free(date);
print_status("Incoming connection from: %s (%s).", ip, hostname); print_status("Incoming connection from: %s (%s).", ip, hostname);
} }
@@ -715,6 +712,9 @@ static int create_directory(char *output_dir, char *slug) {
// Generate a path // Generate a path
char *path = malloc(len); char *path = malloc(len);
if (!path) {
return -1;
}
snprintf(path, len, "%s%s%s", output_dir, "/", slug); snprintf(path, len, "%s%s%s", output_dir, "/", slug);
// Create output directory, just in case // Create output directory, just in case
@@ -740,6 +740,10 @@ static int save_to_file(uint8_t *data, char *output_dir, char *slug) {
// Generate a path // Generate a path
char *path = malloc(len); char *path = malloc(len);
if (!path) {
return -1;
}
snprintf(path, len, "%s%s%s%s%s", output_dir, "/", slug, "/", file_name); snprintf(path, len, "%s%s%s%s%s", output_dir, "/", slug, "/", file_name);
// Attempt file saving // Attempt file saving