1/2 xargs add

This commit is contained in:
Your Name 2023-11-29 14:05:59 +03:00
parent 8b274b13a4
commit f04874487c
9 changed files with 104 additions and 17 deletions

View File

@ -1,7 +1,7 @@
# micro-utils
Utils with minimal functionality for creating unix-like OS
Systems useing micro-utils:
Systems using micro-utils:
[PlainOs](https://nlight.dimension.sh)
License: wtfpl

2
TODO
View File

@ -54,7 +54,7 @@ Modutils (linux only):
Findutils:
grep
find
xargs
*xargs (stdin)
Shell:
rc - run command (1 2 3 ~ | <> <<>> & * " parsing) (sig handler)

0
build.sh Executable file → Normal file
View File

View File

@ -39,17 +39,9 @@ void Compile(const char *src, const char *output_dir) {
printf(CC_FMT, src, path);
pid_t pid;
if ((pid = fork()) == 0) {
if (pid == 1)
exit(1);
if ((pid = fork()) == 0)
execlp(CC, CC, CFLAGS, src, "-o", path, NULL);
/* If compiler return 1 */
exit(1);
}
else if (pid == -1) {
fprintf(stderr, "builder: fork failed\n");
exit(1);
@ -60,7 +52,7 @@ void Compile(const char *src, const char *output_dir) {
int status = 0;
waitpid(pid, &status, 0);
if (status)
exit(1);
exit(status);
}
void ListAndCompile(const char *dir, const char *output_dir) {

View File

@ -7,6 +7,7 @@ const char *objects[] = {
"networking",
"procps",
"sysutils",
"findutils",
"sysutils-linux",
"miscutils",
"shell"
@ -15,7 +16,9 @@ const char *objects[] = {
#define CHDIR_FMT "Making \033[32m%s\033[0m\n"
#define CC_FMT "CC %10s -> %s\n"
/* Need by spark */
#define LDFLAGS "-lm"
#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", LDFLAGS
#define CC "cc"
#endif

View File

@ -1,7 +1,7 @@
#ifndef _CONFIG_H
#define _CONFIG_H
/* (cat tee wc) */
/* (cat tee wc xargs) */
#define BUF_SIZE 4096
/* Random source (shred) */
@ -26,6 +26,10 @@
/* RunComm prompt */
#define RC_PS "> "
/* Args count (xargs) */
#define NARGS 10000
#define ECHO_CMD "echo"
/* Options: To disable, comment line */
/* Add escape-char support in echo */
#define ECHO_FANCY

View File

@ -21,7 +21,9 @@ int cat(const char *path) {
while ((len = read(fd, buf, sizeof(buf))) > 0)
write(STDOUT_FILENO, buf, len);
close(fd);
if (strcmp(path, "-"))
close(fd);
return 0;
}

86
findutils/xargs.c Normal file
View File

@ -0,0 +1,86 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "config.h"
unsigned int t_flag;
char *cmd[NARGS + 1];
void clear_cmd(int count) {
if (count < 1)
return;
for (int i = 0; i < count; i++)
free(cmd[i]);
}
char *estrdup(const char *arg, int i) {
char *val = strdup(arg);
if (!val) {
clear_cmd(i - 1);
fprintf(stderr, "xargs: %s\n", strerror(errno));
return NULL;
}
return val;
}
int run(int count) {
if (t_flag) {
for (int i = 0; i < count; i++)
fprintf(stderr, "%s ", cmd[i]);
fputc('\n', stderr);
}
pid_t pid;
if ((pid = fork()) == 0) {
execvp(*cmd, cmd);
fprintf(stderr, "xargs: %s\n", strerror(errno));
exit(1);
}
int status = 0;
waitpid(pid, &status, 0);
return status;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "t")) != -1) {
switch (opt) {
case 't':
t_flag = 1;
break;
default:
printf("xargs [cmd] [arg1] [arg2]\n\t[-t Print the command on stderr before execution]\n");
return 0;
}
}
argv += optind;
argc -= optind;
/* Argv */
int i = 0;
if (argc) {
for (i = 0; i < argc; i++)
if (i < NARGS)
cmd[i] = estrdup(argv[i], i);
}
else {
cmd[0] = estrdup(ECHO_CMD, 0);
i++;
}
/* Stdin */
int ret = run(i);
clear_cmd(i);
return ret;
}

View File

@ -15,8 +15,8 @@ long parse_long(const char *str) {
}
int main(int argc, char **argv) {
long min;
long max;
long min = 0;
long max = 0;
if (argc > 1) {
for (int i = 1; i < argc; i++) {
@ -42,7 +42,7 @@ int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
putchar('\xe2');
putchar('\x96');
putchar('\x82' + round(parse_long(argv[i]) - min + 1) / diff * 7);
putchar('\x82' + round(parse_long(argv[i]) - min - 1) / diff * 7);
}
putchar('\n');