1/2 xargs add
This commit is contained in:
parent
8b274b13a4
commit
f04874487c
|
@ -1,7 +1,7 @@
|
||||||
# micro-utils
|
# micro-utils
|
||||||
Utils with minimal functionality for creating unix-like OS
|
Utils with minimal functionality for creating unix-like OS
|
||||||
|
|
||||||
Systems useing micro-utils:
|
Systems using micro-utils:
|
||||||
[PlainOs](https://nlight.dimension.sh)
|
[PlainOs](https://nlight.dimension.sh)
|
||||||
|
|
||||||
License: wtfpl
|
License: wtfpl
|
||||||
|
|
2
TODO
2
TODO
|
@ -54,7 +54,7 @@ Modutils (linux only):
|
||||||
Findutils:
|
Findutils:
|
||||||
grep
|
grep
|
||||||
find
|
find
|
||||||
xargs
|
*xargs (stdin)
|
||||||
|
|
||||||
Shell:
|
Shell:
|
||||||
rc - run command (1 2 3 ~ | <> <<>> & * " parsing) (sig handler)
|
rc - run command (1 2 3 ~ | <> <<>> & * " parsing) (sig handler)
|
||||||
|
|
|
@ -39,17 +39,9 @@ void Compile(const char *src, const char *output_dir) {
|
||||||
printf(CC_FMT, src, path);
|
printf(CC_FMT, src, path);
|
||||||
|
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
if ((pid = fork()) == 0) {
|
if ((pid = fork()) == 0)
|
||||||
if (pid == 1)
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
execlp(CC, CC, CFLAGS, src, "-o", path, NULL);
|
execlp(CC, CC, CFLAGS, src, "-o", path, NULL);
|
||||||
|
|
||||||
/* If compiler return 1 */
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (pid == -1) {
|
else if (pid == -1) {
|
||||||
fprintf(stderr, "builder: fork failed\n");
|
fprintf(stderr, "builder: fork failed\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -60,7 +52,7 @@ void Compile(const char *src, const char *output_dir) {
|
||||||
int status = 0;
|
int status = 0;
|
||||||
waitpid(pid, &status, 0);
|
waitpid(pid, &status, 0);
|
||||||
if (status)
|
if (status)
|
||||||
exit(1);
|
exit(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListAndCompile(const char *dir, const char *output_dir) {
|
void ListAndCompile(const char *dir, const char *output_dir) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ const char *objects[] = {
|
||||||
"networking",
|
"networking",
|
||||||
"procps",
|
"procps",
|
||||||
"sysutils",
|
"sysutils",
|
||||||
|
"findutils",
|
||||||
"sysutils-linux",
|
"sysutils-linux",
|
||||||
"miscutils",
|
"miscutils",
|
||||||
"shell"
|
"shell"
|
||||||
|
@ -15,7 +16,9 @@ const char *objects[] = {
|
||||||
#define CHDIR_FMT "Making \033[32m%s\033[0m\n"
|
#define CHDIR_FMT "Making \033[32m%s\033[0m\n"
|
||||||
#define CC_FMT "CC %10s -> %s\n"
|
#define CC_FMT "CC %10s -> %s\n"
|
||||||
|
|
||||||
|
/* Need by spark */
|
||||||
#define LDFLAGS "-lm"
|
#define LDFLAGS "-lm"
|
||||||
|
|
||||||
#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", LDFLAGS
|
#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", LDFLAGS
|
||||||
#define CC "cc"
|
#define CC "cc"
|
||||||
#endif
|
#endif
|
||||||
|
|
6
config.h
6
config.h
|
@ -1,7 +1,7 @@
|
||||||
#ifndef _CONFIG_H
|
#ifndef _CONFIG_H
|
||||||
#define _CONFIG_H
|
#define _CONFIG_H
|
||||||
|
|
||||||
/* (cat tee wc) */
|
/* (cat tee wc xargs) */
|
||||||
#define BUF_SIZE 4096
|
#define BUF_SIZE 4096
|
||||||
|
|
||||||
/* Random source (shred) */
|
/* Random source (shred) */
|
||||||
|
@ -26,6 +26,10 @@
|
||||||
/* RunComm prompt */
|
/* RunComm prompt */
|
||||||
#define RC_PS "> "
|
#define RC_PS "> "
|
||||||
|
|
||||||
|
/* Args count (xargs) */
|
||||||
|
#define NARGS 10000
|
||||||
|
#define ECHO_CMD "echo"
|
||||||
|
|
||||||
/* Options: To disable, comment line */
|
/* Options: To disable, comment line */
|
||||||
/* Add escape-char support in echo */
|
/* Add escape-char support in echo */
|
||||||
#define ECHO_FANCY
|
#define ECHO_FANCY
|
||||||
|
|
|
@ -21,7 +21,9 @@ int cat(const char *path) {
|
||||||
while ((len = read(fd, buf, sizeof(buf))) > 0)
|
while ((len = read(fd, buf, sizeof(buf))) > 0)
|
||||||
write(STDOUT_FILENO, buf, len);
|
write(STDOUT_FILENO, buf, len);
|
||||||
|
|
||||||
|
if (strcmp(path, "-"))
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -15,8 +15,8 @@ long parse_long(const char *str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
long min;
|
long min = 0;
|
||||||
long max;
|
long max = 0;
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
|
@ -42,7 +42,7 @@ int main(int argc, char **argv) {
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
putchar('\xe2');
|
putchar('\xe2');
|
||||||
putchar('\x96');
|
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');
|
putchar('\n');
|
||||||
|
|
Loading…
Reference in New Issue