Implemented init command
h3rald h3rald@h3rald.com
Wed, 10 Jun 2026 06:37:27 +0200
6 files changed,
63 insertions(+),
29 deletions(-)
M
src/conver.c
→
src/conver.c
@@ -8,38 +8,24 @@ # endif
# include <windows.h> #else # define FS_POSIX +# include <errno.h> # include <sys/stat.h> #endif -void print_error(conver_result_t result) +void print_result(conver_result_t result) { - switch (result) + if (result == 0) { - case CONVER_OK: - break; - case CONVER_ERR_OPEN: - fprintf(stderr, "ERROR: Unable to open file.\n"); - break; - case CONVER_ERR_WRITE: - fprintf(stderr, "ERROR: Unable to write file.\n"); - break; - case CONVER_ERR_READ: - fprintf(stderr, "ERROR: Unable to read file.\n"); - break; - case CONVER_ERR_ALLOC: - fprintf(stderr, "ERROR: Unable to allocate memory for file.\n"); - break; - case CONVER_ERR_STAT: - fprintf(stderr, "ERROR: Unable to get file/directory information.\n"); - break; + fprintf(stdout, "-> %s\n", conver_errors[result]); + return; } + fprintf(stderr, "ERROR: %s\n", conver_errors[result]); } int fs_dir_exists(const char *path) { #ifdef FS_WINDOWS DWORD attr = GetFileAttributesA(path); - return (attr != INVALID_FILE_ATTRIBUTES) && - (attr & FILE_ATTRIBUTE_DIRECTORY); + return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY); #else struct stat st; return (stat(path, &st) == 0) && S_ISDIR(st.st_mode);@@ -56,6 +42,29 @@ struct stat st;
return (stat(path, &st) == 0) && S_ISREG(st.st_mode); #endif } + +conver_result_t fs_dir_create(const char *path, int ok_if_exists) { +#ifdef FS_WINDOWS + if (CreateDirectoryA(path, NULL)) { + return CONVER_OK; + } + if (ok_if_exists && GetLastError() == ERROR_ALREADY_EXISTS && fs_dir_exists(path)) + { + return CONVER_OK; + } + return CONVER_ERR_MKDIR; +#else + if (mkdir(path, 0755) == 0) { + return CONVER_OK; + } + if (ok_if_exists && errno == EEXIST && fs_dir_exists(path)) + { + return CONVER_OK; + } + return CONVER_ERR_MKDIR; +#endif +} + static conver_result_t fs_open_write(const char *path, const char *mode, const void *data, size_t size) {@@ -156,11 +165,27 @@ int write_all = 0;
if (!fs_dir_exists(CONVER_DIR)) { write_all = 1; + fs_dir_create(CONVER_DIR, 1); } if (write_all || !fs_file_exists(CONVER_DRAFT_FILE)) { printf("Creating %s...\n", CONVER_DRAFT_FILE); - print_error(fs_file_create(CONVER_DRAFT_FILE)); + print_result(fs_file_create(CONVER_DRAFT_FILE)); + } + if (write_all || !fs_file_exists(CONVER_RELEASE_FILE)) + { + printf("Creating %s...\n", CONVER_RELEASE_FILE); + print_result(fs_file_create(CONVER_RELEASE_FILE)); + } + if (write_all || !fs_file_exists(CONVER_LEGACY_FILE)) + { + printf("Creating %s...\n", CONVER_LEGACY_FILE); + print_result(fs_file_create(CONVER_LEGACY_FILE)); + } + if (write_all || !fs_file_exists(CONVER_HISTORY_FILE)) + { + printf("Creating %s...\n", CONVER_HISTORY_FILE); + print_result(fs_file_create(CONVER_HISTORY_FILE)); } }
M
src/conver.h
→
src/conver.h
@@ -17,14 +17,23 @@ #include <string.h>
typedef enum { - CONVER_OK = 0, - CONVER_ERR_OPEN = -1, - CONVER_ERR_WRITE = -2, - CONVER_ERR_READ = -3, - CONVER_ERR_ALLOC = -4, - CONVER_ERR_STAT = -5 + CONVER_OK = 0, + CONVER_ERR_OPEN = 1, + CONVER_ERR_WRITE = 2, + CONVER_ERR_READ = 3, + CONVER_ERR_ALLOC = 4, + CONVER_ERR_STAT = 5, + CONVER_ERR_MKDIR = 6 } conver_result_t; - +char * conver_errors[7] = { + "Success", + "Unable to open file.", + "Unable to write file", + "Unable to read file", + "Unable to allocate memory for file", + "Unable to get file/directory information", + "Unable to create directory" +}; #endif // CONVER_H