| appname = cms | |||||
| build: | build: | ||||
| gcc -o cms src/*.c -std=c99 | |||||
| gcc -o $(appname) src/*.c -std=c99 | |||||
| install: | |||||
| mv $(appname) /usr/bin/$(appname) | |||||
| if [ -d /usr/share/$(appname) ]; then rm -r /usr/share/$(appname); fi | |||||
| mkdir /usr/share/$(appname) | |||||
| cp -r resources/* /usr/share/$(appname) |
| #ifndef CMS_FILES_H | |||||
| #define CMS_FILES_H | |||||
| //Directory for all resources, like default themes and such. | |||||
| #define CMS_FILE_RESOURCES "/usr/share/cms" | |||||
| //File which tells us if a directory is already initiated. | |||||
| #define CMS_FILE_INITED ".cmsinited" | |||||
| #endif |
| #include <stdio.h> | |||||
| #include <stdlib.h> | |||||
| #include "cms_log.h" | |||||
| void cms_log_panic(char* msg) | |||||
| { | |||||
| fprintf(stderr, "Fatal error: %s\n", msg); | |||||
| exit(1); | |||||
| } | |||||
| void cms_log_error(char* msg) | |||||
| { | |||||
| fprintf(stderr, "Error: %s\n", msg); | |||||
| exit(1); | |||||
| } |
| #ifndef CMS_LOG_H | |||||
| #define CMS_LOG_H | |||||
| //Panic and halt execution. | |||||
| void cms_log_panic(char* msg); | |||||
| //Halt execution. | |||||
| void cms_log_error(char* msg); | |||||
| #endif |
| #include <stdio.h> | |||||
| #include <stddef.h> | |||||
| #include <string.h> | |||||
| #include <stdlib.h> | |||||
| #include "cms_util.h" | |||||
| int cms_util_file_exists(char* fname) | |||||
| { | |||||
| FILE* f; | |||||
| if (f = fopen(fname, "r")) | |||||
| { | |||||
| fclose(f); | |||||
| return 1; | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| void cms_util_file_create(char* fname) | |||||
| { | |||||
| FILE* f = fopen(fname, "ab+"); | |||||
| fclose(f); | |||||
| } | |||||
| char* cms_util_join_paths(char* str1, char* str2) | |||||
| { | |||||
| size_t len1 = strlen(str1); | |||||
| size_t len2 = strlen(str2); | |||||
| if (str1[len1 - 1] == '/') | |||||
| { | |||||
| len1 -= 1; | |||||
| } | |||||
| char* path = malloc((len1 + len2 + 1) * sizeof(char)); | |||||
| if (path == NULL) | |||||
| return NULL; | |||||
| memcpy(path, str1, len1); //First part | |||||
| path[len1] = '/'; //Separator | |||||
| memcpy(path + len1 + 1, str2, len2); //Second part | |||||
| path[len1 + len2 + 1] = '\0'; //Null terminator | |||||
| return path; | |||||
| } |
| #ifndef CMS_UTIL_H | |||||
| #define CMS_UTIL_H | |||||
| //Returns 1 if a file exists, 0 if it doesn't. | |||||
| int cms_util_file_exists(char* fname); | |||||
| //Create a file | |||||
| void cms_util_file_create(char* fname); | |||||
| //Joins together two paths. | |||||
| char* cms_util_join_paths(char* str1, char* str2); | |||||
| #endif |
| int main(int argv, char** argc) | |||||
| #include <string.h> | |||||
| #include <stdio.h> | |||||
| #include "cms_util.h" | |||||
| #include "cms_log.h" | |||||
| #include "cms_files.h" | |||||
| int main(int argc, char** argv) | |||||
| { | { | ||||
| if (argc < 2) | |||||
| { | |||||
| fprintf(stderr, "Usage: %s <init|build>\n", argv[0]); | |||||
| return 1; | |||||
| } | |||||
| if (strcmp(argv[1], "init") == 0) //Initiate directory | |||||
| { | |||||
| if (argc != 3) | |||||
| { | |||||
| fprintf(stderr, "Usage: %s init <directory>\n", argv[0]); | |||||
| return 1; | |||||
| } | |||||
| char* dirname = argv[2]; | |||||
| //Get the path of .cmsinited, which tells us | |||||
| //whether or not the directory is already inited | |||||
| char* initedPath = cms_util_join_paths(dirname, CMS_FILE_INITED); | |||||
| if (initedPath == NULL) | |||||
| cms_log_panic("Memory allocation failed."); | |||||
| //Check if the .cmsinited file exists | |||||
| if (cms_util_file_exists(initedPath)) | |||||
| cms_log_error("Directory already initiated."); | |||||
| //Create .cmsinited file | |||||
| cms_util_file_create(initedPath); | |||||
| } | |||||
| else if (strcmp(argv[1], "build") == 0) //Build | |||||
| { | |||||
| } | |||||
| } | } |