diff --git a/README.md b/README.md index a92161c..71cc180 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # micro-utils [Compile] - cc builder.c -obuilder + cc builder.c -Ilibmu -obuilder ./builder [or] diff --git a/build.sh b/build.sh index b214bcf..0e5300b 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/sh -cc builder.c -Wall -Wextra -Os -s -pedantic -obuilder +cc builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuilder ./builder && rm builder diff --git a/builder.c b/builder.c index ceb0dba..4c4f12a 100644 --- a/builder.c +++ b/builder.c @@ -1,7 +1,79 @@ #include +#include #include +#include +#include +#include +#include "make_path.h" #include "config.h" +void remove_suffix(char *base, const char *suffix) { + char *ptr = base + strlen(base) - strlen(suffix); + if (!strcmp(ptr, suffix)) + *ptr = '\0'; +} + +char *MakePath(char *src) { + char *dup = strdup(src); + if (dup == NULL) { + fprintf(stderr, "builder: strdup failed"); + exit(1); + } + + remove_suffix(dup, ".c"); + char *new_path = mu_make_path("builder", "../bin", dup); + if (new_path == NULL) { + free(dup); + exit(1); + } + + free(dup); + return new_path; +} + +void Compile(char *src) { + char *path = MakePath(src); + + size_t len = strlen(CC) + strlen(CFLAGS) + strlen(src) + strlen(path) + 7; + char *arg = malloc(len + 1); + if (arg == NULL) { + free(path); + fprintf(stderr, "builder: malloc failed"); + exit(1); + } + + snprintf(arg, len, "%s %s %s -o %s", CC, CFLAGS, src, path); + system(arg); + + free(arg); + free(path); +} + +void ListAndCompile(const char *dir) { + chdir(dir); + DIR *dp = opendir("."); + if (dp == NULL) { + fprintf(stderr, "builder: %s\n", strerror(errno)); + exit(1); + } + + struct dirent *ep; + while ((ep = readdir(dp)) != NULL) { + if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) + continue; + + printf("[INFO] Building %s\n", ep->d_name); + Compile(ep->d_name); + + } + + closedir(dp); + chdir(".."); +} + int main(void) { + for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++) + ListAndCompile(objects[i]); + return 0; } diff --git a/config.h b/config.h index d863928..1e7913a 100644 --- a/config.h +++ b/config.h @@ -9,6 +9,6 @@ const char *objects[] = { "shell" }; -#define CFLAGS "-Wall -Wextra -pedantic -Os -s" +#define CFLAGS "-Wall -Wextra -pedantic -Os -s -I ../libmu" #define CC "cc" #endif diff --git a/coreutils/shred.c b/coreutils/shred.c index 07f2699..59d45f9 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -59,7 +59,7 @@ int main(const int argc, const char **argv) { else if (!strcmp(argv[i], "-z")) z_flag = 1; - else if (!strncmp(argv[i], "-n=", 3) + else if (!strncmp(argv[i], "-n=", 3)) n_loops = atoi(argv[i] + 3); else if (!strcmp(argv[i], "--help")) {