So, I wrote the next function:
void actualizetime(){
puts("actualizetime called"); //debugging purposes.
time_t rawtime;
puts("static time_t defined"); //debugging purposes.
struct tm *timeinfo;
puts("struct tm * defined"); //debugging purposes.
time(&rawtime);
puts("time function called"); //debugging purposes.
timeinfo = localtime(&rawtime);
puts("timeinfo has localtime"); //debugging purposes.
actualtime.year = timeinfo->tm_year + 1900;
actualtime.month = timeinfo->tm_mon;
actualtime.day = timeinfo->tm_mday;
actualtime.hour = timeinfo->tm_hour;
actualtime.min = timeinfo->tm_min;
actualtime.sec = timeinfo->tm_sec;
}
The thing is, this function works great only once. The second time the function is called, timeinfo = localtime(&rawtime);
crashes. What's going wrong? and, how can I fix it?
I don't think the actualtime
struct is important but, anyhow:
struct {
int year;
int month;
int day;
int hour;
int min;
int sec;
} actualtime;
EDIT:
The complete program (linux only):
WARNING: To run this program you have to be root, and have on root file system a dir called /pragma/regfiles/
#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void actualizetime(void);
void openregfile(void);
void closeregfile(void);
void addreg(char *,char *);
void terminate(void);
char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
FILE *regfile;
struct {
int year;
int month;
int day;
int hour;
int min;
int sec;
} actualtime;
void actualizetime(){
puts("-actualizetime called"); //debugging purposes.
time_t rawtime;
puts("-static time_t defined"); //debugging purposes.
struct tm *timeinfo;
puts("-struct tm * defined"); //debugging purposes.
time(&rawtime);
puts("-time function called"); //debugging purposes.
timeinfo = localtime(&rawtime);
puts("-timeinfo has localtime"); //debugging purposes.
actualtime.year = timeinfo->tm_year + 1900;
actualtime.month = timeinfo->tm_mon;
actualtime.day = timeinfo->tm_mday;
actualtime.hour = timeinfo->tm_hour;
actualtime.min = timeinfo->tm_min;
actualtime.sec = timeinfo->tm_sec;
}
void openregfile(void){
char route[64];
struct stat *st;
if(!route){
puts("Error initializing files");
exit(1);
}
actualizetime();
sprintf(route, "/pragma/regfiles/%04d/", actualtime.year);
if(stat(route, st) < 0){
if(mkdir(route, 0600) < 0){
puts("ERROR: cannot create directory for regs files! (1)");
exit(1);
}
}
sprintf(route, "/pragma/regfiles/%04d/%s", actualtime.year, months[actualtime.month]);
if(stat(route, st) < 0){
if(mkdir(route, 0600) < 0){
puts("ERROR: cannot create directory for regs files! (2)");
exit(1);
}
}
sprintf(route, "/pragma/regfiles/%04d/%s/%02d.reg", actualtime.year, months[actualtime.month], actualtime.day);
if((regfile = fopen(route, "a")) == NULL){
puts("ERROR: cannot create reg file!");
exit(1);
}
addreg("log", "reg start");
}
void closeregfile(){
addreg("log", "reg stop");
fclose(regfile);
}
void addreg(char *label,char *msg){
puts("-before actualize time"); //debugging purposes.
actualizetime();
puts("-after actualize time"); //debugging purposes.
printf("%d%d%d", actualtime.hour, actualtime.min, actualtime.sec);
fprintf(regfile, "[%s]:%04d%s%02d_%02d%02d%02d %s\n", label, actualtime.year, months[actualtime.month], actualtime.day, actualtime.hour, actualtime.min, actualtime.sec, msg);
fflush(regfile);
}
int main(void){
openregfile();
//Do something
terminate();
return 0;
}
void terminate(void){
closeregfile();
puts("Terminating...");
}
This causes undefined behaviour:
struct stat *st;
if(stat(route, st) < 0){
You are passing an uninitialized variable st
to a library function, which causes undefined behaviour.
Reading the manual page for stat
would tell you that you are supposed to supply a pointer to a buffer that already exists, and the stat
function will fill in.
For example:
struct stat st = { 0 }; // good practice to zero-initialize
if ( stat(route, &st) < 0 ) {