| build: | build: | ||||
| gcc -o $(appname) src/*.c -std=c99 | gcc -o $(appname) src/*.c -std=c99 | ||||
| debug: | |||||
| gcc -o $(appname) src/*.c -std=c99 -DDEBUG=1 | |||||
| install: | install: | ||||
| mv $(appname) /usr/bin/$(appname) | mv $(appname) /usr/bin/$(appname) | ||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. |
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. | |||||
| This is placeholder text. |
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <errno.h> | #include <errno.h> | ||||
| static char* get_message(cms_err err) | |||||
| #ifdef DEBUG | |||||
| #include <error.h> | |||||
| #endif | |||||
| static char* get_error_message(cms_err err) | |||||
| { | { | ||||
| switch (err) | switch (err) | ||||
| { | { | ||||
| return "Memory allocation failed."; | return "Memory allocation failed."; | ||||
| case CMS_ERR_PARSE: | case CMS_ERR_PARSE: | ||||
| return "Parse error."; | return "Parse error."; | ||||
| case CMS_ERR_FILENOENT: | |||||
| return "File doesn't exist."; | |||||
| case CMS_ERR_DIRNOENT: | |||||
| return "Directory doesn't exist."; | |||||
| case CMS_ERR_NOENT: | |||||
| return "No such file or directory."; | |||||
| case CMS_ERR_NOTFILE: | case CMS_ERR_NOTFILE: | ||||
| return "Not a file."; | return "Not a file."; | ||||
| case CMS_ERR_NOTDIR: | case CMS_ERR_NOTDIR: | ||||
| return "Not a directory."; | return "Not a directory."; | ||||
| case CMS_ERR_FILEEXISTS: | |||||
| return "File exists."; | |||||
| case CMS_ERR_DIREXISTS: | |||||
| return "Directory exists."; | |||||
| case CMS_ERR_PERM: | case CMS_ERR_PERM: | ||||
| return "Permission denied."; | return "Permission denied."; | ||||
| case CMS_ERR_INITED: | case CMS_ERR_INITED: | ||||
| } | } | ||||
| } | } | ||||
| void cms_err_panic(cms_err err, char* msg) | |||||
| void _cms_err_panic(cms_err err, char* msg, const char* file, int line) | |||||
| { | { | ||||
| if (err == CMS_ERR_NONE) | if (err == CMS_ERR_NONE) | ||||
| return; | return; | ||||
| #ifdef DEBUG | |||||
| fprintf(stderr, "File %s, line %i:\n\t", file, line); | |||||
| #endif | |||||
| if (msg == NULL) | if (msg == NULL) | ||||
| fprintf(stderr, "Error: %s\n", get_message(err)); | |||||
| fprintf(stderr, "Error: %s\n", get_error_message(err)); | |||||
| else | else | ||||
| fprintf(stderr, "Error: %s (%s)\n", get_message(err), msg); | |||||
| fprintf(stderr, "Error: %s: %s\n", msg, get_error_message(err)); | |||||
| exit(1); | exit(1); | ||||
| } | } | ||||
| cms_err cms_err_from_std_err(int err) | cms_err cms_err_from_std_err(int err) | ||||
| { | { | ||||
| #ifdef DEBUG | |||||
| error(0, err, "converting to cms_err"); | |||||
| #endif | |||||
| switch (err) | switch (err) | ||||
| { | { | ||||
| case EACCES: | case EACCES: | ||||
| case EISDIR: | case EISDIR: | ||||
| return CMS_ERR_NOTFILE; | return CMS_ERR_NOTFILE; | ||||
| case ENOENT: | case ENOENT: | ||||
| return CMS_ERR_FILENOENT; | |||||
| return CMS_ERR_NOENT; | |||||
| case ENOMEM: | case ENOMEM: | ||||
| return CMS_ERR_ALLOC; | return CMS_ERR_ALLOC; | ||||
| case ENOTDIR: | case ENOTDIR: |
| CMS_ERR_UNKNOWN, | CMS_ERR_UNKNOWN, | ||||
| CMS_ERR_ALLOC, | CMS_ERR_ALLOC, | ||||
| CMS_ERR_PARSE, | CMS_ERR_PARSE, | ||||
| CMS_ERR_FILENOENT, | |||||
| CMS_ERR_DIRNOENT, | |||||
| CMS_ERR_NOENT, | |||||
| CMS_ERR_NOTFILE, | CMS_ERR_NOTFILE, | ||||
| CMS_ERR_NOTDIR, | CMS_ERR_NOTDIR, | ||||
| CMS_ERR_FILEEXISTS, | CMS_ERR_FILEEXISTS, | ||||
| CMS_ERR_NOTINITED | CMS_ERR_NOTINITED | ||||
| } cms_err; | } cms_err; | ||||
| void cms_err_panic(cms_err err, char* msg); | |||||
| void _cms_err_panic(cms_err err, char* msg, const char* file, int line); | |||||
| #define cms_err_panic(err, msg) _cms_err_panic(err, msg, __FILE__, __LINE__) | |||||
| cms_err cms_err_from_std_err(int err); | cms_err cms_err_from_std_err(int err); | ||||
| #endif | #endif | ||||
| cms_err cms_util_file_create(char* fname) | cms_err cms_util_file_create(char* fname) | ||||
| { | { | ||||
| int f = open(fname, O_CREAT); | |||||
| int f = open(fname, O_CREAT, 0777); | |||||
| close(f); | close(f); | ||||
| if (f == -1) | if (f == -1) | ||||
| cms_err cms_util_file_copy(char* fname1, char* fname2) | cms_err cms_util_file_copy(char* fname1, char* fname2) | ||||
| { | { | ||||
| int f1 = open(fname1, O_RDONLY); | int f1 = open(fname1, O_RDONLY); | ||||
| int f2 = open(fname2, O_WRONLY); | |||||
| if (f1 == -1) | |||||
| return cms_err_from_std_err(errno); | |||||
| int f2 = open(fname2, O_WRONLY | O_CREAT, 0777); | |||||
| if (f1 == -1) | |||||
| return cms_err_from_std_err(errno); | |||||
| struct stat* s = malloc(sizeof(struct stat)); | struct stat* s = malloc(sizeof(struct stat)); | ||||
| if (s == NULL) | if (s == NULL) | ||||
| cms_err cms_util_dir_copy_recursive(char* dir1, char* dir2) | cms_err cms_util_dir_copy_recursive(char* dir1, char* dir2) | ||||
| { | { | ||||
| if (mkdir(dir2, 0777) == -1) | |||||
| if (mkdir(dir2, 0777) == -1 && errno != EEXIST) | |||||
| return cms_err_from_std_err(errno); | return cms_err_from_std_err(errno); | ||||
| DIR* dp = opendir(dir1); | DIR* dp = opendir(dir1); | ||||
| while (ep = readdir(dp)) | while (ep = readdir(dp)) | ||||
| { | { | ||||
| if (ep->d_name[0] == '.') | |||||
| continue; | |||||
| char* path1 = cms_util_path_join(dir1, ep->d_name); | char* path1 = cms_util_path_join(dir1, ep->d_name); | ||||
| char* path2 = cms_util_path_join(dir2, ep->d_name); | char* path2 = cms_util_path_join(dir2, ep->d_name); | ||||
| stat(path1, st); | |||||
| if (stat(path1, st) == -1) | |||||
| return cms_err_from_std_err(errno); | |||||
| if (S_ISDIR(st->st_mode)) | if (S_ISDIR(st->st_mode)) | ||||
| { | { | ||||
| } | } | ||||
| } | } | ||||
| closedir(dp); | |||||
| free(st); | free(st); | ||||
| return 0; | |||||
| return CMS_ERR_NONE; | |||||
| } | } | ||||
| char* cms_util_path_join(char* str1, char* str2) | char* cms_util_path_join(char* str1, char* str2) |
| { | { | ||||
| if (argc < 2) | if (argc < 2) | ||||
| { | { | ||||
| fprintf(stderr, "Usage: %s <init|build>\n", argv[0]); | |||||
| fprintf(stderr, "Usage: %s <init|deinit|build>\n", argv[0]); | |||||
| return 1; | return 1; | ||||
| } | } | ||||
| if (cms_util_file_exists(initedPath)) | if (cms_util_file_exists(initedPath)) | ||||
| cms_err_panic(CMS_ERR_INITED, NULL); | cms_err_panic(CMS_ERR_INITED, NULL); | ||||
| //Copy files from resources | |||||
| cms_err_panic( | |||||
| cms_util_dir_copy_recursive(CMS_FILE_RESOURCES, dirname), | |||||
| dirname | |||||
| ); | |||||
| //Create .cmsinited file | //Create .cmsinited file | ||||
| cms_util_file_create(initedPath); | |||||
| cms_err_panic(cms_util_file_create(initedPath), initedPath); | |||||
| } | } | ||||
| //Build | //Build |