Compare commits
No commits in common. "07dff4f2d2ca7e33bef3a2f2000c397220b52016" and "699a66a75ac6c9b61abda0561728abbd544a90be" have entirely different histories.
07dff4f2d2
...
699a66a75a
6 changed files with 9 additions and 176 deletions
17
makefile
17
makefile
|
@ -1,14 +1,7 @@
|
||||||
.PHONY: debug release run
|
.PHONY: all debug
|
||||||
|
all: src/wta.c
|
||||||
|
gcc -o build/wta -O2 -Wall -Wextra -pedantic -Werror src/wta.c
|
||||||
|
|
||||||
release: src/*.c
|
debug: src/wta.c
|
||||||
gcc -o build/wte -O2 -Wall -Wextra -pedantic -Werror src/*.c
|
gcc -o build/wta -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wta.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
61
src/file.c
|
@ -1,61 +0,0 @@
|
||||||
#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
14
src/manage.c
|
@ -1,14 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
4
src/wta.c
Normal file
4
src/wta.c
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
50
src/wte.c
50
src/wte.c
|
@ -1,50 +0,0 @@
|
||||||
#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
39
src/wte.h
|
@ -1,39 +0,0 @@
|
||||||
#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