aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/remembering.c211
1 files changed, 63 insertions, 148 deletions
diff --git a/src/remembering.c b/src/remembering.c
index 596c69d..a2f57e8 100644
--- a/src/remembering.c
+++ b/src/remembering.c
@@ -3,20 +3,18 @@
#endif
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
+#include <stdlib.h>
#include <unistd.h>
-// #include <tuple.h>
-/*
-#include "vendor/vector.h"
+#ifdef TEST
#include <assert.h>
-#include <libgen.h>
-#include <stdbool.h>
-#include <sys/stat.h>
-*/
+#endif
+
+#ifdef FALLIBLE
+#include <fallible/alloc.h>
+#endif
-/*
static void print_usage(FILE *stream, char *given_name) {
char *name = "remembering";
if (given_name && given_name[0]) {
@@ -42,7 +40,12 @@ static void print_missing(FILE *stream, char *text) {
fprintf(stream, "Missing option: %s\n", text);
}
-static struct Tuple *get_options(int argc, char *argv[]) {
+struct Pair {
+ char *first;
+ char *second;
+};
+
+static int get_options(int argc, char *argv[], struct Pair *pair) {
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "--help")) {
print_usage(stdout, argv[0]);
@@ -75,36 +78,27 @@ static struct Tuple *get_options(int argc, char *argv[]) {
if (commandarg == NULL) {
print_missing(stderr, "-c 'COMMAND'");
print_usage(stderr, argv[0]);
- exit(2);
+ return 2;
}
if (profilearg == NULL) {
print_missing(stderr, "-p 'PROFILE'");
print_usage(stderr, argv[0]);
- exit(2);
- }
-
- struct Tuple *options = tuple_new();
- if (!options) {
- return NULL;
+ return 2;
}
- options->first = malloc(strlen(commandarg) + 1);
- options->second = malloc(strlen(profilearg) + 1);
- if (!options->first || !options->second) {
- tuple_free(options);
- return NULL;
+ pair->first = malloc(strlen(commandarg) + 1);
+ pair->second = malloc(strlen(profilearg) + 1);
+ if (!pair->first || !pair->second) {
+ return 1;
}
- strcpy(options->first, commandarg);
- strcpy(options->second, profilearg);
+ strcpy(pair->first, commandarg);
+ strcpy(pair->second, profilearg);
- return options;
+ return 0;
}
-*/
-
-/*
-static char *expand_profile_name(char *profile_name) {
+static char *expand_profile_name(const char *const profile_name) {
char *prefix = NULL;
char *env_prefix = getenv("XDG_DATA_HOME");
if (env_prefix) {
@@ -139,72 +133,52 @@ static char *expand_profile_name(char *profile_name) {
}
#ifdef TEST
-static char *rand_str(size_t size) {
- static const char CHARSET[] = "0123456789";
- "abcdefghijklmnopqrstuvwxyz"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- static const int CEILING = sizeof CHARSET - 1;
- static const int DEFAULT_LENGTH = 24;
-
- size = size == 0 ? DEFAULT_LENGTH : size;
-
- char *str = malloc(size + 1);
- assert(str);
-
- size_t cur = size;
- while (cur--) {
- str[cur] = CHARSET[rand() % CEILING];
- }
- str[size] = '\0';
- return str;
-}
-
-const char *set_test_home(const char *new_home) {
+char *set_test_home(const char *new_home) {
size_t size = _PC_PATH_MAX == -1 ? 4096 : _PC_PATH_MAX;
char *buf = malloc(size + 1);
- assert(buf);
+ if (!buf) {
+ return NULL;
+ }
+
char *cwd = getcwd(buf, size);
- assert(buf);
+ if (!cwd) {
+ free(buf);
+ return NULL;
+ }
+
const char *suffix = "/tests/test-profiles/";
char *home = malloc(strlen(cwd) + strlen(suffix) + 1);
- assert(home);
- strcpy(home, cwd);
- strcat(home, suffix);
- // free(buf);
- // free(cwd);
+ if (!new_home) {
+ free(buf);
+ return NULL;
+ }
+
+ strcpy(new_home, buf);
+ strcat(new_home, suffix);
+
+ char *previous_home = getenv("HOME");
+ setenv("HOME", new_home, 1);
+ return previous_home;
}
-static void test_expand_profile_name() {
+void expand_profile_name_test() {
{
testing("expanding profile name without setting $XDG_DATA_HOME");
- char *r1 = rand_str(0);
- printf("\n\nrand_str() is: %s----\n", r1);
- char *r2 = rand_str(10);
- printf("\n\nrand_str() is: %s----\n", r2);
- free(r1);
- free(r2);
- // exit(1);
- // const char *new_home = random_string("test-expand-profile-name-");
- // const char *previous_home = set_test_home(new_home);
-
- // int ret = setenv("HOME", previous_home, 1);
- // assert(ret == 0);
- test_ok();
- }
-}
-#endif
+ // FIXME: this can fail
+ const char *new_home = sharedc_random_string("test-expand-profile-name-");
+ const char *previous_home = set_test_home(new_home);
-static void mkdir_p(char *dir, mode_t mode) { mkdir(dir, mode); }
+ int ret = setenv("HOME", previous_home, 1);
+ assert(ret == 0);
-#ifdef TEST
-static void test_mkdir_p() {
- {
- testing("xablau");
+ char *n = expand_profile_name(new_home);
+ assert(n);
test_ok();
}
}
#endif
+/*
static void ensure_profile(char *profile) {
char *prefix = dirname(profile);
struct stat sb;
@@ -411,83 +385,24 @@ int merge_stdin_with_profile(char *profile_name, struct Rankings *s_rankings,
}
*/
-/*
#ifdef TEST
-void test_libeuandreh() {
- time_t t;
- srand((unsigned)time(&t));
-}
void unit_tests() {
- test_libeuandreh();
-
- test_vector();
- test_tuple();
- test_expand_profile_name();
- test_mkdir_p();
- test_ensure_profile();
- test_rankings();
}
#endif
-*/
-int main() { return 0; }
-/*
int main(int argc, char *argv[]) {
#ifdef TEST
-unit_tests();
-return 0;
+ unit_tests();
+ return 0;
#endif
-struct Tuple *options = get_options(argc, argv);
-if (!options) {
- goto oom;
-}
+ int ret;
+ struct Pair p;
+ ret = get_options(argc, argv, &p);
+ if (ret) {
+ goto err;
+ }
-char *profile = expand_profile_name(options->second);
-if (!profile) {
- tuple_free(options);
- goto oom;
+err:
+ return ret;
}
-free(options->second);
-options->second = profile;
-ensure_profile(options->second);
-
-tuple_free(options);
-return 0;
-oom:
-fprintf(stderr, "Unable to allocate memory, exitting.\n");
-return 1;
-// exit(1);
-
- struct Tuple options;
- struct Rankings s_rankings;
- struct Rankings p_rankings;
- rankings_init(&s_rankings);
- rankings_init(&p_rankings);
-
- int ret = 0;
-
- get_options(&options, argc, argv);
- if (process_options(&options)) {
- ret = 1;
- goto cleanup;
- }
- // char *command = options.first;
- char *profile = options.second;
- merge_stdin_with_profile(profile, &s_rankings, &p_rankings);
-
-cleanup:
- options_free(&options);
- rankings_free(&s_rankings);
- rankings_free(&p_rankings);
- return ret;
-}
-*/
-
-/*
- char *err_msg = "malloc failed: unable to allocate memory while processing "
- "command-line options.\n";
-
-
-
-*/