2020-10-23 22:13:48 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <signal.h>
|
2020-11-01 21:03:53 -06:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DATFILE "/home/randomuser/datfile"
|
|
|
|
|
|
|
|
time_t buf;
|
|
|
|
struct tm bt;
|
|
|
|
struct tm et;
|
|
|
|
int flg = 0;
|
2020-10-23 22:13:48 -05:00
|
|
|
|
|
|
|
struct time {
|
|
|
|
int hour;
|
|
|
|
int min;
|
|
|
|
};
|
|
|
|
struct date {
|
|
|
|
int year;
|
|
|
|
int month;
|
|
|
|
int day;
|
|
|
|
};
|
2020-10-24 21:48:35 -05:00
|
|
|
struct session {
|
|
|
|
struct date date;
|
2020-11-01 21:03:53 -06:00
|
|
|
int offset;
|
|
|
|
char name[4096];
|
2020-10-24 21:48:35 -05:00
|
|
|
};
|
|
|
|
|
2020-11-01 21:03:53 -06:00
|
|
|
void sighnd() { flg = 1; }
|
|
|
|
void flgwt() { while(flg == 0) { continue; } }
|
|
|
|
|
|
|
|
char* concat(const char *s1, const char *s2) {
|
|
|
|
char *result = malloc(strlen(s1) + strlen(s2) + 1);
|
|
|
|
// let's hope malloc works correctly
|
|
|
|
strcpy(result, s1);
|
|
|
|
strcat(result, s2);
|
|
|
|
return result;
|
|
|
|
}
|
2020-10-24 21:48:35 -05:00
|
|
|
|
2020-11-01 21:08:10 -06:00
|
|
|
struct tm get_time() {
|
2020-11-01 21:03:53 -06:00
|
|
|
time_t buf;
|
|
|
|
struct tm b;
|
|
|
|
buf = time(NULL);
|
|
|
|
b = *localtime(&buf);
|
|
|
|
return b;
|
|
|
|
}
|
2020-10-23 22:13:48 -05:00
|
|
|
|
2020-11-01 21:08:10 -06:00
|
|
|
void write_session(FILE *fd, struct session session) {
|
2020-11-01 21:03:53 -06:00
|
|
|
fprintf(fd, "P%02d%02d%04d%d %s\n",
|
|
|
|
session.date.month, session.date.day, session.date.year,
|
|
|
|
session.offset, session.name);
|
|
|
|
}
|
2020-10-23 22:13:48 -05:00
|
|
|
|
2020-11-01 21:08:10 -06:00
|
|
|
struct session data_to_session(struct tm time, int offset, char *name) {
|
2020-11-01 21:03:53 -06:00
|
|
|
struct session session;
|
|
|
|
session.date.year = time.tm_year + 1900;
|
|
|
|
session.date.month = time.tm_mon;
|
|
|
|
session.date.day = time.tm_mday;
|
|
|
|
session.offset = offset;
|
|
|
|
strcpy(session.name, name);
|
|
|
|
return session;
|
|
|
|
}
|
2020-10-23 22:13:48 -05:00
|
|
|
|
2020-11-01 21:08:10 -06:00
|
|
|
struct session line_to_session(char *line) {
|
2020-11-01 21:03:53 -06:00
|
|
|
struct session session;
|
|
|
|
char *buf = line;
|
|
|
|
char *parameters = strtok(buf, " ");
|
|
|
|
strcpy(session.name, strtok(NULL, ""));
|
|
|
|
session.date.month = atoi(concat(¶meters[1], ¶meters[2]));
|
|
|
|
session.date.day = atoi(concat(¶meters[3], ¶meters[4]));
|
|
|
|
session.date.year = atoi(concat(
|
|
|
|
concat(¶meters[5], ¶meters[6]),
|
|
|
|
concat(¶meters[7], ¶meters[8])));
|
|
|
|
char charbuf[8];
|
|
|
|
for(int i = 9; parameters[i] == " "; i++) {
|
|
|
|
strcat(&charbuf, ¶meters[i]);
|
|
|
|
}
|
|
|
|
session.offset = atoi(&charbuf);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
int timediff(struct tm f, struct tm l) {
|
|
|
|
int fm = f.tm_min + (f.tm_hour * 60);
|
|
|
|
int lm = l.tm_min + (l.tm_hour * 60);
|
|
|
|
return fm - lm;
|
2020-10-23 22:13:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2020-11-01 21:03:53 -06:00
|
|
|
if (argc != 2) {
|
|
|
|
FILE *f = fopen(DATFILE, "a");
|
|
|
|
char *line;
|
|
|
|
struct session buffer;
|
|
|
|
|
|
|
|
while(fgets(line, sizeof(line), f)) {
|
2020-11-01 21:08:10 -06:00
|
|
|
buffer = line_to_session(&line);
|
|
|
|
write_session(f, buffer);
|
2020-11-01 21:03:53 -06:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2020-11-01 21:08:10 -06:00
|
|
|
bt = get_time();
|
2020-10-23 22:13:48 -05:00
|
|
|
signal(SIGINT, sighnd);
|
|
|
|
flgwt();
|
2020-11-02 20:10:56 -06:00
|
|
|
int t = timediff(get_time(), bt);
|
2020-11-01 21:08:10 -06:00
|
|
|
struct session data = data_to_session(bt, t, argv[1]);
|
2020-11-01 21:03:53 -06:00
|
|
|
FILE *f = fopen(DATFILE, "a");
|
2020-11-01 21:08:10 -06:00
|
|
|
write_session(f, data);
|
2020-11-01 21:03:53 -06:00
|
|
|
fclose(f);
|
2020-10-23 22:13:48 -05:00
|
|
|
return 0;
|
|
|
|
}
|