diff options
Diffstat (limited to 'src/util/shared.c')
-rw-r--r-- | src/util/shared.c | 106 |
1 files changed, 85 insertions, 21 deletions
diff --git a/src/util/shared.c b/src/util/shared.c index 600a967..8f2fe93 100644 --- a/src/util/shared.c +++ b/src/util/shared.c @@ -1,5 +1,6 @@ #include "shared.h" +#include <ctype.h> #include <errno.h> #include <limits.h> #include <stdarg.h> @@ -10,15 +11,36 @@ #include <sys/stat.h> #include <paths.h> -void error(const char* format, ...) { +extern char* cmd; + +void error_s(const char *format, ...) { va_list list; va_start(list, format); + fprintf(stderr, "%s: ", cmd); + vfprintf(stderr, format, list); + fprintf(stderr, "\n"); +} + +void error(const char *format, ...) { + va_list list; + va_start(list, format); + + fprintf(stderr, "%s: ", cmd); vfprintf(stderr, format, list); fprintf(stderr, "\n"); exit(EXIT_FAILURE); } +void output(const char *format, ...) { + va_list list; + va_start(list, format); + + fprintf(stdout, "%s: ", cmd); + vfprintf(stdout, format, list); + fprintf(stdout, "\n"); +} + FILE* get_file_s(const char* path, const char* type) { struct stat s; if (streql("-", path) && type[0] == 'r') { @@ -28,11 +50,11 @@ FILE* get_file_s(const char* path, const char* type) { } if (lstat(path, &s) < 0) { if (type[0] != 'r') goto read; - fprintf(stderr, "error: failed to read %s: %s\n", path, strerror(errno)); + error_s("failed to read %s: %s", path, strerror(errno)); return NULL; } if (S_ISDIR(s.st_mode)) { - fprintf(stderr, "error: %s is a directory\n", path); + error_s("%s is a directory", path); return NULL; } @@ -40,7 +62,7 @@ FILE* get_file_s(const char* path, const char* type) { read: file = fopen(path, type); if (file == NULL) { - fprintf(stderr, "error: failed to %s file %s: %s\n", type[0] == 'r' ? "read" : "write", path, strerror(errno)); + error_s("failed to %s file %s: %s", type[0] == 'r' ? "read" : "write", path, strerror(errno)); } return file; } @@ -55,7 +77,7 @@ long int get_number(const char* text) { char* end; long int n = strtol(text, &end, 10); if (text == end) { - error("error: %s is not a valid number", text); + error("%s is not a valid number", text); } return n; } @@ -64,7 +86,7 @@ long int get_blkm(const char* text) { char* end; long int n = strtol(text, &end, 10); if (text == end) { - error("error: %s is not a valid bkm", text); + error("%s is not a valid bkm", text); } if (*end == '\0') return n; switch (*end) { @@ -78,12 +100,23 @@ long int get_blkm(const char* text) { case 'm': return n * 1024 * 1204; default: - error("error: invalid bkm type %c", *end); + error("invalid bkm type %c", *end); } // shouldnt get here anyways return 0; } +mode_t get_mode(const char* next) { + char* end = NULL; + mode_t mode = (mode_t)strtol(next, &end, 8); + if (!end) return 0; + while(isspace(*end)) end++; + if (*end != '\0' || (unsigned) mode < 010000) { + error("invalid file mode: `%s`", next); + } + return mode; +} + bool streql(const char* a, const char* b) { if (*a != *b) return false; int n = 0; @@ -151,11 +184,11 @@ void print_date_time(time_t mills, char buf[13]) { void check_arg (char* arg) { if (arg == NULL) { - error("error: expected another argument after option"); + error("expected another argument after option"); } } -static void global_help(void (*help)(void)) { +void global_help(void (*help)(void)) { printf("LazySphere v%d.%d.%d multi-call binary.\n\n", MAJOR, MINOR, PATCH); help(); exit(EXIT_SUCCESS); @@ -196,6 +229,8 @@ int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char, start++; } else if (r == ARG_IGNORE) { goto exit; + } else if (r == ARG_INVALID) { + error("invalid argument %s", argv[current]); } } else { if (short_arg == NULL) { @@ -208,6 +243,8 @@ int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char, start++; } else if (r == ARG_IGNORE) { goto exit; + } else if (r == ARG_INVALID) { + error("invalid argument -%c", argv[current][j]); } } } @@ -220,7 +257,7 @@ exit: int get_tty() { int fd = open(_PATH_TTY, O_RDONLY); - if (fd < 0) error("error: failed to get tty: %s", strerror(errno)); + if (fd < 0) error("failed to get tty: %s", strerror(errno)); return fd; } @@ -228,11 +265,27 @@ FILE* get_tty_stream(char* type) { int fd = get_tty(); FILE* file = fdopen(fd, type); if (file == NULL) { - error("error: failed to open tty stream: %s", strerror(errno)); + error("failed to open tty stream: %s", strerror(errno)); } return file; } +static int push_path_buffer_b(char* buf, int* index, const char* string) { + int save = *index; + if (*index > 1 || (*index == 1 && buf[0] != '/')) { + buf[(*index)++] = '/'; + } + int string_len = strlen(string); + memcpy(buf + *index, string, string_len + 1); + *index += string_len; + return save; +} + +static void pop_path_buffer_b(char* buf, int* index, int i) { + *index = i; + buf[*index] = '\0'; +} + static char path_buffer[PATH_MAX + 1]; static int path_buffer_index = 0; @@ -241,17 +294,28 @@ char* get_path_buffer() { } int push_path_buffer(const char* string) { - int save = path_buffer_index; - if (path_buffer_index > 1 || (path_buffer_index == 1 && path_buffer[0] != '/')) { - path_buffer[path_buffer_index++] = '/'; - } - int string_len = strlen(string); - memcpy(path_buffer + path_buffer_index, string, string_len + 1); - path_buffer_index += string_len; - return save; + return push_path_buffer_b(path_buffer, &path_buffer_index, string); } void pop_path_buffer(int i) { - path_buffer_index = i; - path_buffer[path_buffer_index] = '\0'; + pop_path_buffer_b(path_buffer, &path_buffer_index, i); +} + +static char path_buffer_2[PATH_MAX + 1]; +static int path_buffer_index_2 = 0; + +char* get_path_buffer_2() { + return path_buffer_2; +} + +int push_path_buffer_2(const char* string) { + return push_path_buffer_b(path_buffer_2, &path_buffer_index_2, string); +} + +void pop_path_buffer_2(int i) { + pop_path_buffer_b(path_buffer_2, &path_buffer_index_2, i); +} + +bool is_dot_dir(const char* path) { + return streql(path, ".") || streql(path, ".."); } |