From dd6b6e771b6211833d13533daa2e48b2a6dc8ac6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 5 Nov 2023 18:31:17 +0300 Subject: [PATCH] cmp --- README.md | 4 +- TODO | 1 - build.sh | 4 +- builder.c => builder/builder.c | 17 +++-- config.h => builder/config.h | 4 ++ coreutils/cmp.c | 114 +++++++++++++++++++++++++++++++++ coreutils/du.c | 6 +- obj/.gitignore | 0 8 files changed, 136 insertions(+), 14 deletions(-) mode change 100755 => 100644 build.sh rename builder.c => builder/builder.c (73%) rename config.h => builder/config.h (84%) create mode 100644 coreutils/cmp.c create mode 100644 obj/.gitignore diff --git a/README.md b/README.md index 71cc180..de64498 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # micro-utils [Compile] - cc builder.c -Ilibmu -obuilder - ./builder + cc builder/builder.c -Ilibmu -obuild + ./build [or] sh build.sh diff --git a/TODO b/TODO index 232d09d..08aaddf 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,6 @@ PlainOs With "micro-" prefix *Todo: -cmp tail uniq head diff --git a/build.sh b/build.sh old mode 100755 new mode 100644 index 0e5300b..e8160f5 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/sh -cc builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuilder -./builder && rm builder +cc builder/builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuild +./build && rm build diff --git a/builder.c b/builder/builder.c similarity index 73% rename from builder.c rename to builder/builder.c index 4c4f12a..d4da446 100644 --- a/builder.c +++ b/builder/builder.c @@ -13,7 +13,7 @@ void remove_suffix(char *base, const char *suffix) { *ptr = '\0'; } -char *MakePath(char *src) { +char *MakePath(const char *src, const char *output_dir) { char *dup = strdup(src); if (dup == NULL) { fprintf(stderr, "builder: strdup failed"); @@ -21,7 +21,7 @@ char *MakePath(char *src) { } remove_suffix(dup, ".c"); - char *new_path = mu_make_path("builder", "../bin", dup); + char *new_path = mu_make_path("builder", output_dir, dup); if (new_path == NULL) { free(dup); exit(1); @@ -31,8 +31,8 @@ char *MakePath(char *src) { return new_path; } -void Compile(char *src) { - char *path = MakePath(src); +void Compile(const char *src, const char *output_dir) { + char *path = MakePath(src, output_dir); size_t len = strlen(CC) + strlen(CFLAGS) + strlen(src) + strlen(path) + 7; char *arg = malloc(len + 1); @@ -49,7 +49,7 @@ void Compile(char *src) { free(path); } -void ListAndCompile(const char *dir) { +void ListAndCompile(const char *dir, const char *output_dir) { chdir(dir); DIR *dp = opendir("."); if (dp == NULL) { @@ -63,7 +63,7 @@ void ListAndCompile(const char *dir) { continue; printf("[INFO] Building %s\n", ep->d_name); - Compile(ep->d_name); + Compile(ep->d_name, output_dir); } @@ -72,8 +72,11 @@ void ListAndCompile(const char *dir) { } int main(void) { + /* for (size_t i = 0; i < sizeof(libs) / sizeof(char *); i++) + ListAndCompile(objects[i], "../obj"); */ + for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++) - ListAndCompile(objects[i]); + ListAndCompile(objects[i], "../bin"); return 0; } diff --git a/config.h b/builder/config.h similarity index 84% rename from config.h rename to builder/config.h index 1e7913a..05eeb48 100644 --- a/config.h +++ b/builder/config.h @@ -9,6 +9,10 @@ const char *objects[] = { "shell" }; +const char *libs[] = { + "readline" +}; + #define CFLAGS "-Wall -Wextra -pedantic -Os -s -I ../libmu" #define CC "cc" #endif diff --git a/coreutils/cmp.c b/coreutils/cmp.c new file mode 100644 index 0000000..c1af7d9 --- /dev/null +++ b/coreutils/cmp.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include +unsigned int s_flag; + +int compare(FILE *fp1, FILE *fp2) { + if (fp1 == fp2) + return 0; + + int ch1; + int ch2; + + size_t byte = 0; + do { + ch1 = getc(fp1); + ch2 = getc(fp2); + + if (ch1 != ch2) { + if (!s_flag) + printf("files differ at byte %lu\n", byte); + + return 1; + } + + byte++; + } while(ch1 != EOF); + + return 0; +} + +long parse_int(const char *str) { + char *ptr; + long val = strtol(str, &ptr, 0); + if (*ptr || val < 0) { + fprintf(stderr, "cmp: invalid offset: %s\n", str); + exit(1); + } + + return val; +} + +FILE *file_open(const char *path) { + if (!strcmp(path, "-")) + return stdin; + + FILE *fp = fopen(path, "r"); + if (fp == NULL) { + fprintf(stderr, "cmp: %s\n", strerror(errno)); + exit(1); + } + + return fp; +} + +int main(const int argc, const char **argv) { + int i; + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + break; + + else if (!strcmp(argv[i], "-s")) + s_flag = 1; + + else if (!strcmp(argv[i], "--help")) { + printf("cmp [-s silent] FILE1 FILE2 [SKIP1] [SKIP2]\n"); + return 0; + } + } + + + long skip1 = 0; + long skip2 = 0; + FILE *fp1 = NULL; + FILE *fp2 = stdin; + + switch (argc - i) { + case 4: + skip2 = parse_int(argv[i + 3]); + + case 3: + skip1 = parse_int(argv[i + 2]); + + case 2: + fp2 = file_open(argv[i + 1]); + + case 1: + fp1 = file_open(argv[i]); + break; + + default: + return 1; + } + + if (skip1 && fseek(fp1, skip1, SEEK_SET) < 0) { + if (!s_flag) + fprintf(stderr, "cmp: %s\n", strerror(errno)); + + return 1; + } + + if (skip2 && fseek(fp2, skip2, SEEK_SET) < 0) { + if (!s_flag) + fprintf(stderr, "cmp: %s\n", strerror(errno)); + + return 1; + } + + int ret = compare(fp1, fp2); + fclose(fp1); + fclose(fp2); + + return ret; +} diff --git a/coreutils/du.c b/coreutils/du.c index 8eda331..a8f191d 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -16,8 +16,10 @@ void print(double size, const char *filename) { char c = 0; if (h_flag) { - if (size < 1048576) + if (size < 1048576) { + size = size / 1024; c = 'K'; + } else if (size < 1073741824) { size = size / 1048576; @@ -38,7 +40,7 @@ void print(double size, const char *filename) { else if (m_flag) size = size / 1048576; - printf("%.1f%c\t%s\n", size, c, filename); + printf("%.1f%c %s\n", size, c, filename); } double du(const char *path, int recurs_flag) { diff --git a/obj/.gitignore b/obj/.gitignore new file mode 100644 index 0000000..e69de29