summaryrefslogtreecommitdiff
path: root/lib/convert.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-05-06 00:39:44 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-05-06 00:39:44 -0400
commitd8f2c10b7108fff6b7e437291093a1cadc15ab9f (patch)
tree3fc50a19d6fbb9c94a8fe147cd2a6c4ba7f59b8d /lib/convert.c
parentansii c (diff)
downloadlazysphere-d8f2c10b7108fff6b7e437291093a1cadc15ab9f.tar.gz
lazysphere-d8f2c10b7108fff6b7e437291093a1cadc15ab9f.tar.bz2
lazysphere-d8f2c10b7108fff6b7e437291093a1cadc15ab9f.zip
refactor
Diffstat (limited to 'lib/convert.c')
-rw-r--r--lib/convert.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/lib/convert.c b/lib/convert.c
new file mode 100644
index 0000000..071268e
--- /dev/null
+++ b/lib/convert.c
@@ -0,0 +1,126 @@
+#include "convert.h"
+#include "error.h"
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+long int get_number(const char* text) {
+ char* end;
+ long int n = strtol(text, &end, 10);
+ if (text == end) {
+ error("%s is not a valid number", text);
+ }
+ return n;
+}
+
+long int get_blkm(const char* text) {
+ char* end;
+ long int n = strtol(text, &end, 10);
+ if (text == end) {
+ error("%s is not a valid bkm", text);
+ }
+ if (*end == '\0') return n;
+ switch (*end) {
+ case 'K':
+ case 'k':
+ return n * 1024;
+ case 'B':
+ case 'b':
+ return n * 512;
+ case 'M':
+ case 'm':
+ return n * 1024 * 1204;
+ default:
+ 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) {
+ int n = 0;
+
+ if (*a != *b) return false;
+
+ while (true) {
+ if (*(a+n) != *(b+n)) return false;
+ if (*(a+n) == '\0') return true;
+ ++n;
+ }
+}
+
+bool prefix(const char* pre, const char* str) {
+ return strncmp(pre, str, strlen(pre)) == 0;
+}
+
+static char fs_types[5] = {'K','M','G','T','P'};
+void print_file_size(size_t bytes, char buf[5]) {
+ int index, n;
+ float next;
+
+ index = 0;
+ next = bytes;
+
+ while (true) {
+ if (next < 1000) {
+ break;
+ }
+
+ if (index == 5) {
+ printf("999P");
+ return;
+ }
+
+ next /= 1024;
+ index++;
+ }
+
+ n = snprintf(buf, 4, "%u", (int)(next+.5));
+
+ if (index > 0) {
+ buf[n] = (fs_types[index - 1]);
+ n++;
+ }
+
+ buf[n] = '\0';
+}
+
+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);
+ n = snprintf(buf, 5, "%s ", months[info->tm_mon]);
+
+ if (info->tm_mday < 10) {
+ buf[n] = ' ';
+ n++;
+ }
+
+ snprintf(buf + n, 13 - n, "%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec);
+}
+
+void print_file_path(char* path) {
+ if (streql("-", path)) {
+ printf("(standard input)");
+ } else {
+ printf("%s", path);
+ }
+}