| 1 | /* Utility functions and helpers */ |
| 2 | |
| 3 | #ifndef UTILS_H |
| 4 | #define UTILS_H |
| 5 | |
| 6 | #include <sys/types.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <time.h> |
| 9 | |
| 10 | #include "gitswitch.h" |
| 11 | |
| 12 | /* File permissions */ |
| 13 | #define PERM_USER_RWX 0700 |
| 14 | #define PERM_USER_RW 0600 |
| 15 | #define PERM_USER_R 0400 |
| 16 | |
| 17 | /* Path manipulation constants */ |
| 18 | #define PATH_SEPARATOR "/" |
| 19 | #define HOME_PREFIX "~/" |
| 20 | |
| 21 | /* Function prototypes */ |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * String utilities |
| 26 | */ |
| 27 | char *trim_whitespace(char *str); |
| 28 | bool string_empty(const char *str); |
| 29 | bool string_equals(const char *a, const char *b); |
| 30 | bool string_starts_with(const char *str, const char *prefix); |
| 31 | bool string_ends_with(const char *str, const char *suffix); |
| 32 | int string_replace(char *str, size_t str_size, const char *old, const char *new); |
| 33 | |
| 34 | /** |
| 35 | * Path utilities |
| 36 | */ |
| 37 | int expand_path(const char *path, char *expanded_path, size_t path_size); |
| 38 | int get_home_directory(char *home_path, size_t path_size); |
| 39 | int join_path(char *result, size_t result_size, const char *base, const char *component); |
| 40 | bool path_exists(const char *path); |
| 41 | bool is_directory(const char *path); |
| 42 | bool is_regular_file(const char *path); |
| 43 | int create_directory_recursive(const char *path, mode_t mode); |
| 44 | int get_file_permissions(const char *path, mode_t *mode); |
| 45 | int set_file_permissions(const char *path, mode_t mode); |
| 46 | |
| 47 | /** |
| 48 | * File utilities |
| 49 | */ |
| 50 | int read_file_to_string(const char *file_path, char *buffer, size_t buffer_size); |
| 51 | int write_string_to_file(const char *file_path, const char *content, mode_t mode); |
| 52 | int copy_file(const char *src_path, const char *dst_path); |
| 53 | int backup_file(const char *file_path, const char *backup_suffix); |
| 54 | bool file_is_readable(const char *file_path); |
| 55 | bool file_is_writable(const char *file_path); |
| 56 | size_t get_file_size(const char *file_path); |
| 57 | time_t get_file_mtime(const char *file_path); |
| 58 | |
| 59 | /** |
| 60 | * Process utilities |
| 61 | */ |
| 62 | int execute_command(const char *command, char *output, size_t output_size); |
| 63 | int execute_command_with_input(const char *command, const char *input, |
| 64 | char *output, size_t output_size); |
| 65 | bool command_exists(const char *command); |
| 66 | pid_t start_background_process(const char *command, char *pidfile_path); |
| 67 | int kill_process_by_pidfile(const char *pidfile_path); |
| 68 | bool process_is_running(pid_t pid); |
| 69 | |
| 70 | /** |
| 71 | * Environment utilities |
| 72 | */ |
| 73 | int get_env_var(const char *name, char *buffer, size_t buffer_size); |
| 74 | int set_env_var(const char *name, const char *value, bool overwrite); |
| 75 | int unset_env_var(const char *name); |
| 76 | |
| 77 | /** |
| 78 | * Validation utilities |
| 79 | */ |
| 80 | bool validate_email(const char *email); |
| 81 | bool validate_name(const char *name); |
| 82 | bool validate_key_id(const char *key_id); |
| 83 | bool validate_file_path(const char *path); |
| 84 | |
| 85 | /** |
| 86 | * Security utilities |
| 87 | */ |
| 88 | void secure_zero_memory(void *ptr, size_t size); |
| 89 | int generate_random_string(char *buffer, size_t buffer_size, const char *charset); |
| 90 | bool check_file_permissions_safe(const char *file_path, mode_t expected_mode); |
| 91 | |
| 92 | /** |
| 93 | * Configuration utilities |
| 94 | */ |
| 95 | int get_config_directory(char *config_dir, size_t dir_size); |
| 96 | int ensure_config_directory_exists(void); |
| 97 | |
| 98 | /** |
| 99 | * Terminal utilities |
| 100 | */ |
| 101 | bool is_terminal(int fd); |
| 102 | int get_terminal_size(int *width, int *height); |
| 103 | void disable_echo(void); |
| 104 | void enable_echo(void); |
| 105 | |
| 106 | /** |
| 107 | * Time utilities |
| 108 | */ |
| 109 | void get_current_time_string(char *buffer, size_t buffer_size); |
| 110 | void get_timestamp_string(char *buffer, size_t buffer_size); |
| 111 | bool is_timestamp_expired(time_t timestamp, int max_age_seconds); |
| 112 | |
| 113 | /** |
| 114 | * Comparison utilities |
| 115 | */ |
| 116 | int compare_strings(const void *a, const void *b); |
| 117 | int compare_accounts_by_id(const void *a, const void *b); |
| 118 | int compare_accounts_by_name(const void *a, const void *b); |
| 119 | |
| 120 | /** |
| 121 | * Array utilities |
| 122 | */ |
| 123 | void sort_accounts(account_t *accounts, size_t count, |
| 124 | int (*compare)(const void *, const void *)); |
| 125 | account_t *find_account_in_array(account_t *accounts, size_t count, |
| 126 | const char *identifier); |
| 127 | |
| 128 | /** |
| 129 | * Memory utilities |
| 130 | */ |
| 131 | void *safe_memset(void *ptr, int value, size_t size); |
| 132 | void *safe_memcpy(void *dest, const void *src, size_t size); |
| 133 | int safe_mlock(void *ptr, size_t size); |
| 134 | int safe_munlock(void *ptr, size_t size); |
| 135 | |
| 136 | /** |
| 137 | * Cleanup utilities |
| 138 | */ |
| 139 | void cleanup_temporary_files(void); |
| 140 | int register_cleanup_handler(void (*handler)(void)); |
| 141 | |
| 142 | /** |
| 143 | * Debug utilities |
| 144 | */ |
| 145 | void dump_account(const account_t *account); |
| 146 | void dump_config(const config_t *config); |
| 147 | void dump_context(const gitswitch_ctx_t *ctx); |
| 148 | |
| 149 | #endif /* UTILS_H */ |