Compare commits
10 commits
699a66a75a
...
07dff4f2d2
Author | SHA1 | Date | |
---|---|---|---|
07dff4f2d2 | |||
49fe655474 | |||
94ad845dab | |||
05bc9d24d3 | |||
64d5fb6f89 | |||
20f4ae0581 | |||
67a044a3c9 | |||
c03c9eb9cf | |||
06f1fe37ad | |||
42cd075421 |
6 changed files with 176 additions and 9 deletions
17
makefile
17
makefile
|
@ -1,7 +1,14 @@
|
|||
.PHONY: all debug
|
||||
all: src/wta.c
|
||||
gcc -o build/wta -O2 -Wall -Wextra -pedantic -Werror src/wta.c
|
||||
.PHONY: debug release run
|
||||
|
||||
debug: src/wta.c
|
||||
gcc -o build/wta -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wta.c
|
||||
release: src/*.c
|
||||
gcc -o build/wte -O2 -Wall -Wextra -pedantic -Werror src/*.c
|
||||
|
||||
run: release
|
||||
./build/wte
|
||||
|
||||
debug: src/*.c
|
||||
gcc -o build/wte -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/*.c
|
||||
|
||||
clean:
|
||||
rm -f build/*
|
||||
|
||||
|
|
61
src/file.c
Normal file
61
src/file.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include "wte.h"
|
||||
|
||||
char *get_path()
|
||||
{
|
||||
const char *home = getenv("HOME");
|
||||
char *path = NULL;
|
||||
if (home) {
|
||||
size_t path_len = strlen(home) + sizeof(DATA_DIR);
|
||||
path = malloc(sizeof(char) * path_len);
|
||||
if (path) {
|
||||
snprintf(path, path_len, "%s" DATA_DIR, home);
|
||||
dbg("path: %s", path);
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
int create_data_dir()
|
||||
{
|
||||
int ret = 0;
|
||||
struct stat st;
|
||||
int dir_exists = stat(DATA_DIR, &st);
|
||||
if (-1 == dir_exists) {
|
||||
char *path = get_path();
|
||||
if (path && 0 != (ret = mkdir(path, 0700))) {
|
||||
error("%s", "failed to create directory");
|
||||
} else {
|
||||
info("%s", "created data directory");
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int remove_file()
|
||||
{
|
||||
int ret = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int remove_data_dir()
|
||||
{
|
||||
int ret = 0;
|
||||
ret = remove_file();
|
||||
char *path = get_path();
|
||||
if (path) {
|
||||
// here: for loop to delete all entries in directory
|
||||
ret = remove(path);
|
||||
if (0 == ret) {
|
||||
info("%s", "removed data directory");
|
||||
} else {
|
||||
error("%s", "failed to remove data directory");
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
dbg("ret: %d", ret);
|
||||
return ret;
|
||||
}
|
14
src/manage.c
Normal file
14
src/manage.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "wte.h"
|
||||
|
||||
int insert_choice()
|
||||
{
|
||||
int ret = 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int remove_choice()
|
||||
{
|
||||
int ret = 1;
|
||||
error("%s", "unable to remove choice");
|
||||
return ret;
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
50
src/wte.c
Normal file
50
src/wte.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include "wte.h"
|
||||
|
||||
static size_t get_entry_count()
|
||||
{
|
||||
size_t count = 0;
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
static int make_suggestion()
|
||||
{
|
||||
int ret = 0;
|
||||
srandom(time(NULL)); // seed random with current time
|
||||
size_t count = get_entry_count();
|
||||
if (count) {
|
||||
size_t choice = random() % count;
|
||||
printf("choice: %lu", choice);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(const int argc, const char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
if (2 == argc && '-' == argv[1][0]) {
|
||||
if ('h' == argv[1][1] && '\0' == argv[1][2]) {
|
||||
printf("wte - what to eat\na simple decision making tool for food\nuse without options\n");
|
||||
} else if ('a' == argv[1][1] && '\0' == argv[1][2]) {
|
||||
ret = insert_choice();
|
||||
} else if ('d' == argv[1][1] && '\0' == argv[1][2]) {
|
||||
ret = remove_choice();
|
||||
} else if ('c' == argv[1][1] && '\0' == argv[1][2]) {
|
||||
ret = create_data_dir();
|
||||
} else if ('x' == argv[1][1] && '\0' == argv[1][2]) {
|
||||
ret = remove_data_dir();
|
||||
} else {
|
||||
printf("unrecognized option, try %s -h\n", *argv);
|
||||
}
|
||||
goto exit_main;
|
||||
}
|
||||
|
||||
if (make_suggestion()) {
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
exit_main:
|
||||
return ret;
|
||||
}
|
39
src/wte.h
Normal file
39
src/wte.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#define error(format, ...) do {\
|
||||
fprintf(stderr, c_red format c_end"\n", __VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
#define info(format, ...) do {\
|
||||
fprintf(stdout, c_blue format c_end "\n", __VA_ARGS__); \
|
||||
} while(0);
|
||||
|
||||
#ifdef DEBUG
|
||||
#define dbg(format, ...) do { \
|
||||
printf(c_bold c_lila"[DEBUG] (func: %s) msg: " c_end c_lila format c_end "\n",__func__, __VA_ARGS__); \
|
||||
} while(0)
|
||||
#else
|
||||
#define dbg(format, ...)
|
||||
#endif
|
||||
|
||||
/* easy colors */
|
||||
#define c_red "\033[31m"
|
||||
#define c_green "\033[32m"
|
||||
#define c_yellow "\033[33m"
|
||||
#define c_blue "\033[34m"
|
||||
#define c_lila "\033[35m"
|
||||
#define c_bold "\033[1m"
|
||||
#define c_end "\033[0m"
|
||||
|
||||
/* path to data storage */
|
||||
#define DATA_DIR "/.local/share/wte"
|
||||
|
||||
/* get full path to data storage (with "/home/$USER/"-prefix) */
|
||||
char *get_path();
|
||||
|
||||
/* data storage creation / deletion */
|
||||
int create_data_dir();
|
||||
int remove_data_dir();
|
||||
|
||||
/* operating on data storage */
|
||||
int insert_choice();
|
||||
int remove_choice();
|
Loading…
Add table
Add a link
Reference in a new issue