fix: missing \n (added perror too)

This commit is contained in:
javalsai 2024-07-26 14:25:22 +02:00
parent 93f14f398e
commit 844687809b
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8

View File

@ -40,6 +40,7 @@ static u_int16_t used_size = 0;
static struct session *sessions = NULL; static struct session *sessions = NULL;
static struct sessions_list *__sessions_list = NULL; static struct sessions_list *__sessions_list = NULL;
// NOTE: commented printf's here would be nice to have debug logs if I ever implement it
static enum session_type session_type; static enum session_type session_type;
static int fn(const char *fpath, const struct stat *sb, int typeflag) { static int fn(const char *fpath, const struct stat *sb, int typeflag) {
// practically impossible to reach this // practically impossible to reach this
@ -47,12 +48,14 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
if (used_size == 0xffff) if (used_size == 0xffff)
return 0; return 0;
if (!S_ISREG(sb->st_mode)) if (sb == NULL || !S_ISREG(sb->st_mode))
return 0; return 0;
/*printf("gonna open %s\n", fpath);*/
FILE *fd = fopen(fpath, "r"); FILE *fd = fopen(fpath, "r");
if (fd == NULL) { if (fd == NULL) {
fprintf(stderr, "error opening file (r) %s", fpath); perror("fopen");
fprintf(stderr, "error opening file (r) %s\n", fpath);
return 0; return 0;
} }
@ -63,6 +66,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
char *exec_buf = NULL; char *exec_buf = NULL;
char *tryexec_buf = NULL; char *tryexec_buf = NULL;
while (true) { while (true) {
/*printf(".");*/
char *buf = malloc(sb->st_blksize); char *buf = malloc(sb->st_blksize);
ssize_t read_size = getline(&buf, &alloc_size, fd); ssize_t read_size = getline(&buf, &alloc_size, fd);
if (read_size == -1) { if (read_size == -1) {
@ -91,11 +95,13 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
if (found == 0b111) if (found == 0b111)
break; break;
} }
/*printf("\nend parsing...\n");*/
fclose(fd); fclose(fd);
// just add this to the list // just add this to the list
if (name_buf != NULL && exec_buf != NULL) { if (name_buf != NULL && exec_buf != NULL) {
/*printf("gonna add to session list\n");*/
if (used_size >= alloc_size) { if (used_size >= alloc_size) {
alloc_size += bs; alloc_size += bs;
sessions = realloc(sessions, alloc_size * unit_size); sessions = realloc(sessions, alloc_size * unit_size);
@ -128,6 +134,7 @@ struct sessions_list *get_avaliable_sessions() {
for (uint i = 0; i < sources_size; i++) { for (uint i = 0; i < sources_size; i++) {
session_type = sources[i].type; session_type = sources[i].type;
/*printf("recurring into %s\n", sources[i].dir);*/
ftw(sources[i].dir, &fn, 1); ftw(sources[i].dir, &fn, 1);
} }