From 1cb0ba935dbbca166100b28c3eae10a546436a29 Mon Sep 17 00:00:00 2001 From: Laan Tungir Date: Thu, 14 Aug 2025 10:45:27 -0400 Subject: [PATCH] Version v0.2.70 - Added preferences system with default pad support --- otp.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/otp.c b/otp.c index 9ce67a8..99cdd2c 100644 --- a/otp.c +++ b/otp.c @@ -46,6 +46,9 @@ static const int base64_decode_table[256] = { // Global variable for current pads directory (can be local or OTP thumb drive) static char current_pads_dir[512] = DEFAULT_PADS_DIR; +// Global variable for default pad path from preferences +static char default_pad_path[1024] = ""; + // Function prototypes int main(int argc, char* argv[]); int interactive_mode(void); @@ -56,6 +59,14 @@ int pipe_mode(int argc, char* argv[], const char* piped_text); int has_stdin_data(void); char* read_stdin_text(void); +// Preferences management functions +int load_preferences(void); +int save_preferences(void); +char* get_preference(const char* key); +int set_preference(const char* key, const char* value); +char* get_default_pad_path(void); +int set_default_pad_path(const char* pad_path); + // OTP thumb drive detection function int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size); @@ -120,6 +131,9 @@ void get_directory_display(const char* file_path, char* result, size_t result_si void print_usage(const char* program_name); int main(int argc, char* argv[]) { + // Load preferences first + load_preferences(); + // Check for OTP thumb drive on startup char otp_drive_path[512]; if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) { @@ -2427,6 +2441,123 @@ int pipe_mode(int argc, char* argv[], const char* piped_text) { return encrypt_text(pad_input, piped_text); } +// Preferences management functions implementation +int load_preferences(void) { + char* home_dir = getenv("HOME"); + if (!home_dir) { + return 1; // No home directory + } + + char preferences_dir[1024]; + char preferences_file[1024]; + snprintf(preferences_dir, sizeof(preferences_dir), "%s/.otp", home_dir); + snprintf(preferences_file, sizeof(preferences_file), "%s/otp.conf", preferences_dir); + + FILE* file = fopen(preferences_file, "r"); + if (!file) { + return 0; // No preferences file, use defaults + } + + char line[1024]; + while (fgets(line, sizeof(line), file)) { + // Remove newline + line[strcspn(line, "\n")] = 0; + + // Skip empty lines and comments + if (strlen(line) == 0 || line[0] == '#') { + continue; + } + + // Parse key=value pairs + char* equals = strchr(line, '='); + if (equals) { + *equals = '\0'; + char* key = line; + char* value = equals + 1; + + // Trim whitespace + while (*key == ' ' || *key == '\t') key++; + while (*value == ' ' || *value == '\t') value++; + + if (strcmp(key, "default_pad") == 0) { + strncpy(default_pad_path, value, sizeof(default_pad_path) - 1); + default_pad_path[sizeof(default_pad_path) - 1] = '\0'; + } + } + } + + fclose(file); + return 0; +} + +int save_preferences(void) { + char* home_dir = getenv("HOME"); + if (!home_dir) { + return 1; + } + + char preferences_dir[1024]; + char preferences_file[1024]; + snprintf(preferences_dir, sizeof(preferences_dir), "%s/.otp", home_dir); + snprintf(preferences_file, sizeof(preferences_file), "%s/otp.conf", preferences_dir); + + // Create .otp directory if it doesn't exist + struct stat st = {0}; + if (stat(preferences_dir, &st) == -1) { + if (mkdir(preferences_dir, 0755) != 0) { + return 1; + } + } + + FILE* file = fopen(preferences_file, "w"); + if (!file) { + return 1; + } + + fprintf(file, "# OTP Preferences File\n"); + fprintf(file, "# This file is automatically generated and updated by the OTP program\n\n"); + + if (strlen(default_pad_path) > 0) { + fprintf(file, "default_pad=%s\n", default_pad_path); + } + + fclose(file); + return 0; +} + +char* get_preference(const char* key) { + if (strcmp(key, "default_pad") == 0) { + if (strlen(default_pad_path) > 0) { + return strdup(default_pad_path); + } + } + return NULL; +} + +int set_preference(const char* key, const char* value) { + if (strcmp(key, "default_pad") == 0) { + if (value) { + strncpy(default_pad_path, value, sizeof(default_pad_path) - 1); + default_pad_path[sizeof(default_pad_path) - 1] = '\0'; + } else { + default_pad_path[0] = '\0'; + } + return save_preferences(); + } + return 1; +} + +char* get_default_pad_path(void) { + if (strlen(default_pad_path) > 0) { + return strdup(default_pad_path); + } + return NULL; +} + +int set_default_pad_path(const char* pad_path) { + return set_preference("default_pad", pad_path); +} + // OTP thumb drive detection function implementation int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) { const char* mount_dirs[] = {"/media", "/run/media", "/mnt", NULL};