summaryrefslogtreecommitdiff
path: root/src/util/shared.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util/shared.c85
1 files changed, 64 insertions, 21 deletions
diff --git a/src/util/shared.c b/src/util/shared.c
index 5e08b8d..2e0c98a 100644
--- a/src/util/shared.c
+++ b/src/util/shared.c
@@ -43,27 +43,33 @@ void output(const char *format, ...) {
FILE* get_file_s(const char* path, const char* type) {
struct stat s;
+ FILE* file;
+
if (streql("-", path) && type[0] == 'r') {
clearerr(stdin);
fflush(stdin);
return stdin;
}
+
if (lstat(path, &s) < 0) {
if (type[0] != 'r') goto read;
error_s("failed to read %s: %s", path, strerror(errno));
return NULL;
}
+
if (S_ISDIR(s.st_mode)) {
error_s("%s is a directory", path);
return NULL;
}
- FILE* file;
read:
+
file = fopen(path, type);
+
if (file == NULL) {
error_s("failed to %s file %s: %s", type[0] == 'r' ? "read" : "write", path, strerror(errno));
}
+
return file;
}
@@ -102,7 +108,8 @@ long int get_blkm(const char* text) {
default:
error("invalid bkm type %c", *end);
}
- // shouldnt get here anyways
+
+ /* shouldnt get here anyways */
return 0;
}
@@ -118,8 +125,10 @@ mode_t get_mode(const char* next) {
}
bool streql(const char* a, const char* b) {
- if (*a != *b) return false;
int n = 0;
+
+ if (*a != *b) return false;
+
while (true) {
if (*(a+n) != *(b+n)) return false;
if (*(a+n) == '\0') return true;
@@ -133,25 +142,33 @@ bool prefix(const char* pre, const char* str) {
static char fs_types[5] = {'K','M','G','T','P'};
void print_file_size(size_t bytes, char buf[5]) {
- int index = 0;
- float next = bytes;
+ int index, n;
+ float next;
+
+ index = 0;
+ next = bytes;
+
while (true) {
if (next < 1000) {
break;
}
+
if (index == 5) {
printf("999P");
return;
- };
+ }
+
next /= 1024;
index++;
}
- int n = snprintf(buf, 4, "%u", (int)(next+.5));
+ n = snprintf(buf, 4, "%u", (int)(next+.5));
+
if (index > 0) {
buf[n] = (fs_types[index - 1]);
n++;
}
+
buf[n] = '\0';
}
@@ -159,8 +176,10 @@ static char* months[12] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
void print_date_time(time_t mills, char buf[13]) {
struct tm* info;
+ int n;
+
info = localtime(&mills);
- int n = snprintf(buf, 5, "%s ", months[info->tm_mon]);
+ n = snprintf(buf, 5, "%s ", months[info->tm_mon]);
if (info->tm_mday < 10) {
buf[n] = ' ';
@@ -203,35 +222,45 @@ void global_help(void (*help)(void)) {
}
void parse_help(int argc, char** argv, void (*help)(void)) {
+ int i;
+
if (argc < 1) return;
- for (int i = 0; i < argc; i++) {
+
+ for (i = 0; i < argc; i++) {
if (!prefix("-", argv[i]) || streql("-", argv[i])) break;
if (help != NULL && streql("--help", argv[i])) global_help(help);
}
}
int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*)) {
+ int start, i, current;
+ char* next_arg;
+
if (argc < 1) return 0;
- int start = 0;
- for (int i = 0; i < argc; i++) {
+ start = 0;
+ for (i = 0; i < argc; i++) {
+
if (!prefix("-", argv[i]) || streql("-", argv[i])) break;
if (help != NULL && streql("--help", argv[i])) global_help(help);
- char* next_arg;
if (i + 1 == argc) {
next_arg = NULL;
} else {
next_arg = argv[i+1];
}
- int current = i;
+ current = i;
if (prefix("--", argv[i])) {
+ int r;
+
if (long_arg == NULL) {
goto exit;
}
- int r = long_arg(argv[current], next_arg);
+
+ r = long_arg(argv[current], next_arg);
+
if (r == ARG_USED) {
i++;
start++;
@@ -239,13 +268,20 @@ int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char,
goto exit;
} else if (r == ARG_INVALID) {
error("invalid argument %s", argv[current]);
+
}
} else {
+ size_t j;
+ int r;
+
if (short_arg == NULL) {
goto exit;
}
- for (size_t j = 1; j < strlen(argv[current]); j++) {
- int r = short_arg(argv[current][j], next_arg);
+
+ for (j = 1; j < strlen(argv[current]); j++) {
+
+ r = short_arg(argv[current][j], next_arg);
+
if (r == ARG_USED) {
i++;
start++;
@@ -259,11 +295,13 @@ int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char,
start++;
}
+
exit:
+
return start;
}
-int get_tty() {
+int get_tty (void) {
int fd = open(_PATH_TTY, O_RDONLY);
if (fd < 0) error("failed to get tty: %s", strerror(errno));
return fd;
@@ -279,13 +317,18 @@ FILE* get_tty_stream(char* type) {
}
static int push_path_buffer_b(char* buf, int* index, const char* string) {
- int save = *index;
+ int save, string_len;
+
+ save = *index;
if (*index > 1 || (*index == 1 && buf[0] != '/')) {
buf[(*index)++] = '/';
}
- int string_len = strlen(string);
+
+ string_len = strlen(string);
memcpy(buf + *index, string, string_len + 1);
+
*index += string_len;
+
return save;
}
@@ -297,7 +340,7 @@ static void pop_path_buffer_b(char* buf, int* index, int i) {
static char path_buffer[PATH_MAX + 1];
static int path_buffer_index = 0;
-char* get_path_buffer() {
+char* get_path_buffer(void) {
return path_buffer;
}
@@ -312,7 +355,7 @@ void pop_path_buffer(int i) {
static char path_buffer_2[PATH_MAX + 1];
static int path_buffer_index_2 = 0;
-char* get_path_buffer_2() {
+char* get_path_buffer_2(void) {
return path_buffer_2;
}