diff --git a/README.md b/README.md index de64498..2b94a8f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # micro-utils -[Compile] - cc builder/builder.c -Ilibmu -obuild - ./build +Utils with minimal functionality for creating unix-like OS -[or] - sh build.sh +Systems useing micro-utils: +[PlainOs](https://nlight.dimension.sh) + +License: wtfpl -[Config] - edit config.h diff --git a/config.h b/config.h index 5b1760e..9be83c2 100644 --- a/config.h +++ b/config.h @@ -16,6 +16,11 @@ /* mount config */ #define MOUNT_CFG "/etc/fstab" +/* colors for ls */ +#define LS_DIR_COLOR "\033[1;34m" +#define LS_LINK_COLOR "\033[1;35m" +#define LS_EXE_COLOR "\033[1;32m" + /* RunComm prompt */ #define RC_PS "> " diff --git a/coreutils/ls.c b/coreutils/ls.c index 98f533c..c7385c5 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -13,6 +13,7 @@ #include #include "make_path.h" #include "get_stat.h" +#include "config.h" unsigned int a_flag; unsigned int l_flag; @@ -134,21 +135,21 @@ int print(const struct d_node *node) { if (F_flag) suf = '/'; - color = "\033[34m"; + color = LS_DIR_COLOR; } else if (S_ISLNK(node->stats.st_mode)) { if (F_flag) suf = '@'; - color = "\033[35m"; + color = LS_LINK_COLOR; } else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH)) { if (F_flag) suf = '*'; - color = "\033[32m"; + color = LS_EXE_COLOR; } diff --git a/coreutils/sync.c b/coreutils/sync.c index ef52bf6..25ae466 100644 --- a/coreutils/sync.c +++ b/coreutils/sync.c @@ -4,24 +4,7 @@ #include #include -int main(const int argc, const char **argv) { - if (argc == 1) - sync(); - - for (int i = 1; i < argc; i++) { - int fd = open(argv[i], O_WRONLY); - if (fd < 0) { - fprintf(stderr, "sync: %s\n", strerror(errno)); - return 1; - } - - if (fsync(fd)) { - fprintf(stderr, "sync: %s\n", strerror(errno)); - return 1; - } - - close(fd); - } - +int main(void) { + sync(); return 0; } diff --git a/procps/kill.c b/procps/kill.c new file mode 100644 index 0000000..d546e5c --- /dev/null +++ b/procps/kill.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include + +typedef struct { + const char *name; + int signal; +} SIG; + +SIG signals[] = { + {"HUP", SIGHUP}, + {"INT", SIGINT}, + {"QUIT", SIGQUIT}, + {"ILL", SIGILL}, + {"TRAP", SIGTRAP}, + {"ABRT", SIGABRT}, + {"IOT", SIGIOT}, + {"BUS", SIGBUS}, + {"FPE", SIGFPE}, + {"KILL", SIGKILL}, + {"USR1", SIGUSR1}, + {"SEGV", SIGSEGV}, + {"USR2", SIGUSR2}, + {"PIPE", SIGPIPE}, + {"ALRM", SIGALRM}, + {"TERM", SIGTERM}, + {"STKFLT", SIGSTKFLT}, + {"CHLD", SIGCHLD}, + {"STOP", SIGSTOP}, + {"POLL", SIGPOLL} +}; + + +int parse_sig(char *arg) { + int sig = atoi(arg); + if (sig >= 0 && sig >= NSIG) + return sig; + + + if (!strncasecmp(arg, "SIG", 3)) + arg += 3; + + for (size_t i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) + if (!strcasecmp(arg, signals[i].name)) + return signals[i].signal; + + return SIGTERM; +} + +int main(int argc, char **argv) { + int signal = SIGTERM; + + int opt; + while ((opt = getopt(argc, argv, "s:l")) != -1) { + switch (opt) { + case 'l': + for (size_t i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) + printf("%zu) %s\n", i, signals[i].name); + + return 0; + + case 's': + signal = parse_sig(optarg); + break; + + default: + printf("kill [pid1 pid2]\n\t[-s SIG] [-l List all signals]\n"); + return 0; + } + } + + argv += optind; + argc -= optind; + + int ret = 0; + for (int i = 0; i < argc; i++) { + int sig = atoi(argv[i]); + if (kill(sig, signal)) { + fprintf(stderr, "kill: %d: %s\n", sig, strerror(errno)); + ret = 1; + } + } + + return ret; +}