main journal function rewritten in zig #1

Manually merged
luna merged 9 commits from zig-translation into master 2019-08-11 14:20:16 +00:00
2 changed files with 0 additions and 57 deletions
Showing only changes of commit 007cd0e22a - Show all commits

View file

View file

@ -1,57 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
// functions to deal with the journal file
FILE *journal_open(char *topic)
{
char *home_path = getenv("HOME");
// combine w/ home path
char *journal_dir = (char*)malloc(1024);
snprintf(journal_dir, 1024, "%s/.lunabot", home_path);
// construct a 744
mkdir(journal_dir, S_IRWXU | S_IRGRP | S_IROTH);
char *journal_path = (char*)malloc(1024);
snprintf(journal_path, 1024, "%s/%s", journal_dir, topic);
FILE* res = fopen(journal_path, "a");
free(journal_path);
free(journal_dir);
return res;
}
void journal_write(FILE* journal_fd, char* message)
{
// TODO: strlen()?
fwrite(message, strlen(message), 1, journal_fd);
}
void journal_close(FILE* journal_fd)
{
fclose(journal_fd);
}
void journal_write_topic(FILE *journal_fd, char *topic, char *message)
{
char *tstamp = malloc(128 * sizeof(char));
char fmt_msg[512];
time_t rawtime;
time(&rawtime);
const struct tm *cur_time = gmtime(&rawtime);
strftime(tstamp, 128, "%c", cur_time);
sprintf(fmt_msg, "[%s] [%s]: %s\n", tstamp, topic, message);
free(tstamp);
journal_write(journal_fd, fmt_msg);
}