This commit is contained in:
overcuriousity 2025-07-05 14:02:25 +02:00
parent 287fed9795
commit 98e6aa4d51
2 changed files with 38 additions and 28 deletions

BIN
bin/main

Binary file not shown.

View File

@ -18,6 +18,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <regex.h> #include <regex.h>
#include <stdarg.h>
#define BUF 1024 #define BUF 1024
#define DEBUG true #define DEBUG true
@ -53,9 +54,19 @@ struct access_log_entry_t {
char *http_user_agent; char *http_user_agent;
}; };
void debugmsg(char *msg) { struct access_log_table_t {
struct *access_log_entry_t;
}
void debugmsg(const char *format, ...) {
if(DEBUG == true) { if(DEBUG == true) {
printf("DEBUG: %s\n", msg); va_list args;
va_start(args, format);
printf("DEBUG: ");
vprintf(format, args);
printf("\n");
va_end(args);
} }
} }
@ -66,7 +77,7 @@ int checkparams(int *argc, char *argv[]) {
}; };
}; };
struct access_log_entry_t parseLogline(char *accessLogLine) { struct access_log_entry_t parse_access_log(char *accessLogLine) {
struct access_log_entry_t logEntry; struct access_log_entry_t logEntry;
//logEntry.remote_addr = parse_remote_address(accessLogLine); //logEntry.remote_addr = parse_remote_address(accessLogLine);
return logEntry; return logEntry;
@ -85,53 +96,52 @@ unsigned int count_lines(FILE *filepointer) {
lines += 1; lines += 1;
} }
} }
char msg[BUF]; debugmsg( "Detected %d Lines in filestream\n", lines);
sprintf(msg, "Detected %d Lines in filestream\n", lines);
debugmsg(msg);
return lines; return lines;
} }
unsigned int calculate_filesize(FILE *filepointer){
rewind(filepointer);
fseek(filepointer, 0L, SEEK_END);
int filesize = ftell(filepointer);
return filesize;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
debugmsg("START OF PROGRAM"); debugmsg("START OF PROGRAM");
int i = 0; int i = 0;
while(i<argc){ while(i<argc){
char msg[BUF]; debugmsg("Argument %d: %s, Speicheradresse: %p\n", i, argv[i], &argv[i]);
sprintf(msg,"Argument %d: %s, Speicheradresse: %p\n", i, argv[i], &argv[i]);
debugmsg(msg);
i++; i++;
} }
checkparams(&argc, argv); checkparams(&argc, argv);
const char *filepath = argv[1]; const char *filepath = argv[1];
FILE *fp = fopen(filepath, "r"); FILE *fp = fopen(filepath, "r"); //malloc wird innerhalb fopen durchgeführt
if(fp == NULL) { if(fp == NULL) {
perror("fopen"); perror("fopen");
printf("Scheisse gelaufen mit der Datei...\n"); printf("Datei konnte nicht geöffnet werden.\n");
return -1; return -1;
}else{ }else{
char msg[BUF]; unsigned int filesize = calculate_filesize(fp);
fseek(fp, 0L, SEEK_END); unsigned int linecount = count_lines(fp);
int filesize = ftell(fp); debugmsg("Datei %s geöffnet. Speicheradresse %p, Speichergröße %d Byte\n", filepath, &fp, filesize);
sprintf(msg, "Datei %s geöffnet. Speicheradresse %p, Speichergröße %d Byte\n", filepath, &fp, filesize); struct access_log_entry_t *access_logentry = malloc(sizeof(struct access_log_entry_t)*BUF);
debugmsg(msg); debugmsg("Speicher erfolgreich für access_logentry alloziert: %d Byte\n", sizeof(access_logentry));
count_lines(fp); struct access_log_table_t *access_log_table = malloc(sizeof(access_logentry_t)*linecount);
char *access_log_buffer = malloc(BUF); debugmsg("Speicher erfolgreich für _log_tablaccess_log_table alloziert: %d Byte\n", sizeof(access_log_table));
struct access_log_entry_t *access_log_table = malloc(sizeof(struct access_log_entry_t)*BUF); if(access_log_table==NULL){
if(access_log_buffer==NULL){
perror("Fuck\n");
return -1; return -1;
}; };
rewind(fp); rewind(fp);
for (int counter = 0; counter < 10 && fgets(access_log_buffer, filesize, fp); counter++) { for (int counter = 0; counter < 10 && fgets(access_log_table, filesize, fp); counter++) {
//debugmsg("entered loop");
char outputline[BUF]; char outputline[BUF];
sprintf(outputline, "LINE %d: %s", counter+1, access_log_buffer); sprintf(outputline, "LINE %d: %s", counter+1, access_log_table);
fputs(outputline, stdout); fputs(outputline, stdout);
//debugmsg("endloop");
} }
free(access_log_buffer);
free(access_log_table); free(access_log_table);
//access_log_buffer = NULL; free(access_logentry);
access_log_table = NULL; access_log_table = NULL;
access_logentry = NULL;
} }
debugmsg("END OF PROGRAM"); debugmsg("END OF PROGRAM");
return 0; return 0;