#include #include #include #include "config.h" config_t g_config; int load_config(const char *path) { FILE *f = fopen(path, "r"); if (!f) return -1; char line[512]; char current_section[64] = ""; while (fgets(line, sizeof(line), f)) { char *p = line; while (*p == ' ' || *p == '\t') p++; if (*p == '\0' || *p == '#' || *p == ';') continue; if (*p == '[') { p++; char *end = strchr(p, ']'); if (end) { *end = '\0'; strncpy(current_section, p, sizeof(current_section) - 1); current_section[sizeof(current_section) - 1] = '\0'; } continue; } char *eq = strchr(p, '='); if (!eq) continue; *eq = '\0'; char *key = p; char *value = eq + 1; char *end_key = key + strlen(key) - 1; while (end_key > key && (*end_key == ' ' || *end_key == '\t')) { *end_key = '\0'; end_key--; } while (*value == ' ' || *value == '\t') value++; char *end_val = value + strlen(value) - 1; while (end_val > value && (*end_val == ' ' || *end_val == '\t' || *end_val == '\r' || *end_val == '\n')) { *end_val = '\0'; end_val--; } if (strcmp(current_section, "paths") == 0) { if (strcmp(key, "base_path") == 0) strncpy(g_config.base_path, value, sizeof(g_config.base_path) - 1); else if (strcmp(key, "template_path") == 0) strncpy(g_config.template_path, value, sizeof(g_config.template_path) - 1); else if (strcmp(key, "passwd_file") == 0) strncpy(g_config.passwd_file, value, sizeof(g_config.passwd_file) - 1); } else if (strcmp(current_section, "limits") == 0) { if (strcmp(key, "max_entries") == 0) g_config.max_entries = atoi(value); } else if (strcmp(current_section, "hidden") == 0) { if (strcmp(key, "dirs") == 0) { char *token = strtok(value, ","); g_config.hidden_dirs_count = 0; while (token && g_config.hidden_dirs_count < 10) { while (*token == ' ') token++; strncpy(g_config.hidden_dirs[g_config.hidden_dirs_count], token, 255); g_config.hidden_dirs[g_config.hidden_dirs_count][255] = '\0'; g_config.hidden_dirs_count++; token = strtok(NULL, ","); } } else if (strcmp(key, "files") == 0) { char *token = strtok(value, ","); g_config.hidden_files_count = 0; while (token && g_config.hidden_files_count < 10) { while (*token == ' ') token++; strncpy(g_config.hidden_files[g_config.hidden_files_count], token, 255); g_config.hidden_files[g_config.hidden_files_count][255] = '\0'; g_config.hidden_files_count++; token = strtok(NULL, ","); } } } else if (strcmp(current_section, "preview") == 0) { if (strcmp(key, "image") == 0) strncpy(g_config.preview_image, value, sizeof(g_config.preview_image) - 1); else if (strcmp(key, "text") == 0) strncpy(g_config.preview_text, value, sizeof(g_config.preview_text) - 1); else if (strcmp(key, "audio") == 0) strncpy(g_config.preview_audio, value, sizeof(g_config.preview_audio) - 1); else if (strcmp(key, "video") == 0) strncpy(g_config.preview_video, value, sizeof(g_config.preview_video) - 1); else if (strcmp(key, "pdf") == 0) strncpy(g_config.preview_pdf, value, sizeof(g_config.preview_pdf) - 1); } } fclose(f); return 0; } void set_default_config(void) { strcpy(g_config.base_path, "/home/romkazvo/www"); strcpy(g_config.template_path, "/home/romkazvo/www/cgi-bin/template.html"); strcpy(g_config.passwd_file, "/home/romkazvo/www/cgi-bin/.htpasswd"); g_config.max_entries = 1000; g_config.hidden_dirs_count = 0; g_config.hidden_files_count = 0; strcpy(g_config.preview_image, "jpg,jpeg,png,gif,webp,bmp,svg,ico"); strcpy(g_config.preview_text, "txt,md,html,htm,css,js,json,xml,csv"); strcpy(g_config.preview_audio, "mp3,wav,ogg,flac,m4a,aac"); strcpy(g_config.preview_video, "mp4,webm,ogv,mov,avi,mkv"); strcpy(g_config.preview_pdf, "pdf"); } int is_string_in_list(const char *str, const char *list) { if (!list || !*list) return 0; char temp[512]; strncpy(temp, list, sizeof(temp) - 1); temp[sizeof(temp) - 1] = '\0'; char *token = strtok(temp, ","); while (token) { while (*token == ' ') token++; if (strcasecmp(token, str) == 0) return 1; token = strtok(NULL, ","); } return 0; }