diff options
author | lamp | 2023-03-05 21:48:37 +0000 |
---|---|---|
committer | lamp | 2023-03-05 21:48:37 +0000 |
commit | 998b06d89b869b9587830ec767c6e30f851fff29 (patch) | |
tree | 08b4a4478dc224abec092611fed40b79769cb216 /common.h |
initmain
Diffstat (limited to 'common.h')
-rw-r--r-- | common.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/common.h b/common.h new file mode 100644 index 0000000..28b5242 --- /dev/null +++ b/common.h @@ -0,0 +1,40 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#define BUF_SIZE 512 +#define DEFAULT_SOCK_FNAME "/pass-cache.sock" + +bool socket_path_did_alloc = false; + +void die(char* message) { + fprintf(stderr, "%s\n", message); + exit(EXIT_FAILURE); +} + +char* get_socket_path(void) { + char* explicit_path = getenv("PASS_CACHE_SOCK"); + if (explicit_path) { + return explicit_path; + } + char* base_path = getenv("XDG_RUNTIME_DIR"); + if (!base_path) { + base_path = "/tmp"; + } + char* final_path = (char*)malloc(strlen(base_path) + strlen(DEFAULT_SOCK_FNAME)); + socket_path_did_alloc = true; + strcpy(final_path, base_path); + strcat(final_path, DEFAULT_SOCK_FNAME); + return final_path; +} + +void free_socket_path(char* path) { + if (socket_path_did_alloc) { + free(path); + } +} + +#endif //ifndef _COMMON_H_ |