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
1 changed files with 80 additions and 26 deletions

104
main.c
View File

@ -2,6 +2,14 @@
#include <stdlib.h>
#include <time.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 {
int hour;
@ -14,42 +22,88 @@ struct date {
};
struct session {
struct date date;
struct time time;
int minutes;
char *name;
int offset;
char name[4096];
};
time_t buf;
struct time buf2;
struct tm bt;
struct tm et;
int flg = 1;
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;
}
void sighnd() { flg = 0; }
void flgwt() { while(flg) { continue; } }
struct tm getTime() {
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 time tmp;
tmp.hour = f.tm_hour - l.tm_hour;
tmp.min = f.tm_min - l.tm_min;
return tmp;
struct session dataToSession(struct tm time, int offset, char *name) {
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;
}
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) {
buf = time(NULL);
bt = *localtime(&buf);
printf("%d:%02d:%02d\n", bt.tm_hour, bt.tm_min, bt.tm_year + 1900);
if (argc != 2) {
FILE *f = fopen(DATFILE, "a");
char *line;
struct session buffer;
while(fgets(line, sizeof(line), f)) {
buffer = lineToSession(&line);
writeSession(f, buffer);
}
return 1;
}
bt = getTime();
signal(SIGINT, sighnd);
flgwt();
buf = time(NULL);
et = *localtime(&buf);
printf("%d:%02d:%02d:%02d\n", et.tm_hour, et.tm_min, et.tm_sec, et.tm_year + 1900);
buf2 = timediff(et, bt);
printf("%i:%i difference\n", buf2.hour, buf2.min);
printf("exiting lol\n");
int t = timediff(getTime(), bt);
struct session data = dataToSession(bt, t, argv[1]);
FILE *f = fopen(DATFILE, "a");
writeSession(f, data);
fclose(f);
return 0;
}