added a write function, and a questionable read function

This commit is contained in:
randomuser 2020-11-02 03:03:53 +00:00
parent 3f2968789b
commit 6d3cb6b3bd

104
main.c
View File

@ -2,6 +2,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <signal.h> #include <signal.h>
#include <string.h>
#define DATFILE "/home/randomuser/datfile"
time_t buf;
struct tm bt;
struct tm et;
int flg = 0;
struct time { struct time {
int hour; int hour;
@ -14,42 +22,88 @@ struct date {
}; };
struct session { struct session {
struct date date; struct date date;
struct time time; int offset;
int minutes; char name[4096];
char *name;
}; };
time_t buf; void sighnd() { flg = 1; }
struct time buf2; void flgwt() { while(flg == 0) { continue; } }
struct tm bt;
struct tm et;
int flg = 1;
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;
}
void sighnd() { flg = 0; } struct tm getTime() {
void flgwt() { while(flg) { continue; } } time_t buf;
struct tm b;
buf = time(NULL);
b = *localtime(&buf);
return b;
}
int writeData(int fd, struct session session); void writeSession(FILE *fd, struct session session) {
fprintf(fd, "P%02d%02d%04d%d %s\n",
session.date.month, session.date.day, session.date.year,
session.offset, session.name);
}
struct time timediff(struct tm f, struct tm l) { struct session dataToSession(struct tm time, int offset, char *name) {
struct time tmp; struct session session;
tmp.hour = f.tm_hour - l.tm_hour; session.date.year = time.tm_year + 1900;
tmp.min = f.tm_min - l.tm_min; session.date.month = time.tm_mon;
return tmp; session.date.day = time.tm_mday;
session.offset = offset;
strcpy(session.name, name);
return session;
}
struct session lineToSession(char *line) {
struct session session;
char *buf = line;
char *parameters = strtok(buf, " ");
strcpy(session.name, strtok(NULL, ""));
session.date.month = atoi(concat(&parameters[1], &parameters[2]));
session.date.day = atoi(concat(&parameters[3], &parameters[4]));
session.date.year = atoi(concat(
concat(&parameters[5], &parameters[6]),
concat(&parameters[7], &parameters[8])));
char charbuf[8];
for(int i = 9; parameters[i] == " "; i++) {
strcat(&charbuf, &parameters[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;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
buf = time(NULL); if (argc != 2) {
bt = *localtime(&buf); FILE *f = fopen(DATFILE, "a");
printf("%d:%02d:%02d\n", bt.tm_hour, bt.tm_min, bt.tm_year + 1900); char *line;
struct session buffer;
while(fgets(line, sizeof(line), f)) {
buffer = lineToSession(&line);
writeSession(f, buffer);
}
return 1;
}
bt = getTime();
signal(SIGINT, sighnd); signal(SIGINT, sighnd);
flgwt(); flgwt();
buf = time(NULL); int t = timediff(getTime(), bt);
et = *localtime(&buf); struct session data = dataToSession(bt, t, argv[1]);
printf("%d:%02d:%02d:%02d\n", et.tm_hour, et.tm_min, et.tm_sec, et.tm_year + 1900); FILE *f = fopen(DATFILE, "a");
buf2 = timediff(et, bt); writeSession(f, data);
printf("%i:%i difference\n", buf2.hour, buf2.min); fclose(f);
printf("exiting lol\n");
return 0; return 0;
} }