From 998b06d89b869b9587830ec767c6e30f851fff29 Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:48:37 +0000 Subject: init --- common.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 common.h (limited to 'common.h') 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 +#include +#include +#include + +#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_ -- cgit v1.2.3