fixed
This commit is contained in:
parent
d1cf396bec
commit
4cf8171790
5
AUTHORS
5
AUTHORS
|
@ -1,5 +0,0 @@
|
|||
micro-utils:
|
||||
8nlight (8nlight @ disroot <dot> org)
|
||||
|
||||
Thanks:
|
||||
builder, echo: Kind_Foxie (fedi: KindFoxie @ lamp <dot> leemon <dot> network)
|
|
@ -1,8 +1,4 @@
|
|||
# micro-utils
|
||||
Utils with minimal functionality for creating unix-like OS
|
||||
|
||||
Systems using micro-utils:
|
||||
[PlainOs](https://nlight.dimension.sh)
|
||||
|
||||
Own implementation of *nix utils
|
||||
License: wtfpl
|
||||
|
||||
|
|
5
TODO
5
TODO
|
@ -54,3 +54,8 @@ Findutils:
|
|||
BUGS:
|
||||
ls (unicode strlen)
|
||||
xargs (getopt with glibc)
|
||||
|
||||
FIX:
|
||||
ps
|
||||
echo
|
||||
head
|
||||
|
|
14
config.h
14
config.h
|
@ -1,7 +1,7 @@
|
|||
#ifndef _CONFIG_H
|
||||
#define _CONFIG_H
|
||||
|
||||
/* (cat tee wc xargs rev split) */
|
||||
/* (cat tee wc xargs rev split) text buffer */
|
||||
#define BUF_SIZE 2048
|
||||
|
||||
/* Random source (shred) */
|
||||
|
@ -34,18 +34,6 @@ char *INIT_START[] = {"/etc/rc.init", NULL};
|
|||
#define INIT_MSG "Starting micro-init..."
|
||||
#endif
|
||||
|
||||
/* Max args (xargs) */
|
||||
#ifdef XARGS
|
||||
#include <limits.h>
|
||||
#define NARGS ARG_MAX
|
||||
|
||||
/* Arg size (xargs) */
|
||||
#define ARG_SIZE 1024
|
||||
#endif
|
||||
|
||||
/* Default cmd (xargs) */
|
||||
#define ECHO_CMD "echo"
|
||||
|
||||
/* Os name for uname */
|
||||
/* #define OS_NAME "unknow" */
|
||||
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
echo "[*] Umounting..."
|
||||
umount -a
|
||||
|
||||
echo "[*] Syncing..."
|
||||
sync
|
||||
|
||||
echo "[*] Closing processes..."
|
||||
kill -a -s TERM
|
||||
sleep 5
|
||||
|
|
|
@ -80,10 +80,6 @@ int main(int argc, char **argv) {
|
|||
|
||||
default:
|
||||
printf("date [rsdu] [+\"fmt\"]\n\t-s DATE Set new date\n\t-d DATE Print new date\n\t-u Work in UTC\n\t-r FILE Display last modification time of FILE\n");
|
||||
printf("\nFormats:\n");
|
||||
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++)
|
||||
printf("\t%s\n", fmts[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -111,4 +107,3 @@ int main(int argc, char **argv) {
|
|||
puts(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -7,59 +8,54 @@ char n_flag = 0;
|
|||
char e_flag = 0;
|
||||
|
||||
void format(char *str) {
|
||||
if (e_flag) {
|
||||
for (size_t i = 0; i < strlen(str); i++) {
|
||||
unsigned int c = str[i];
|
||||
if (c == '\\') {
|
||||
switch (str[i + 1]) {
|
||||
case 'a':
|
||||
c = '\a';
|
||||
break;
|
||||
for (size_t i = 0; i < strlen(str); i++) {
|
||||
unsigned int c = str[i];
|
||||
if (c == '\\') {
|
||||
switch (str[i + 1]) {
|
||||
case 'a':
|
||||
c = '\a';
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
c = '\n';
|
||||
break;
|
||||
case 'n':
|
||||
c = '\n';
|
||||
break;
|
||||
|
||||
case 't':
|
||||
c = '\t';
|
||||
break;
|
||||
case 't':
|
||||
c = '\t';
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
exit(0);
|
||||
case 'c':
|
||||
exit(0);
|
||||
|
||||
case 'v':
|
||||
c = '\v';
|
||||
break;
|
||||
case 'v':
|
||||
c = '\v';
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
c = '\r';
|
||||
break;
|
||||
case 'r':
|
||||
c = '\r';
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
c = '\f';
|
||||
break;
|
||||
case 'f':
|
||||
c = '\f';
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
c = '\033';
|
||||
break;
|
||||
case 'e':
|
||||
c = '\033';
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
c = '\b';
|
||||
break;
|
||||
case 'b':
|
||||
c = '\b';
|
||||
break;
|
||||
|
||||
default:
|
||||
c = '\\';
|
||||
}
|
||||
|
||||
i++;
|
||||
default:
|
||||
c = '\\';
|
||||
}
|
||||
|
||||
putchar(c);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
fputs(str, stdout);
|
||||
putchar(c);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
@ -81,7 +77,12 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
format(argv[i]);
|
||||
if (e_flag)
|
||||
format(argv[i]);
|
||||
|
||||
else
|
||||
fputs(argv[i], stdout);
|
||||
|
||||
if (i < argc - 1)
|
||||
putchar(' ');
|
||||
}
|
||||
|
|
|
@ -6,21 +6,29 @@
|
|||
|
||||
char s_flag;
|
||||
char f_flag;
|
||||
char v_flag;
|
||||
|
||||
int ln(const char *src, const char *dst) {
|
||||
if (f_flag)
|
||||
unlink(dst);
|
||||
if (unlink(dst) && v_flag)
|
||||
fprintf(stderr, "ln: removed %s\n", dst);
|
||||
|
||||
int ret = 0;
|
||||
if (s_flag)
|
||||
return symlink(src, dst);
|
||||
ret = symlink(src, dst);
|
||||
|
||||
else
|
||||
return link(src, dst);
|
||||
ret = link(src, dst);
|
||||
|
||||
if (!ret && v_flag)
|
||||
fprintf(stderr, "ln: linked %s to %s\n", src, dst);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "sf")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "sfv")) != -1) {
|
||||
switch (opt) {
|
||||
case 's':
|
||||
s_flag = 1;
|
||||
|
@ -30,8 +38,12 @@ int main(int argc, char **argv) {
|
|||
f_flag = 1;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
v_flag = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("ln [sf] [src] [dst]\n\t-f Force\n\t-s Symbolic\n");
|
||||
printf("ln [sfv] [src] [dst]\n\t-f Force\n\t-s Symbolic\n\t-v Verbose\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -332,7 +332,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
|
|||
qsort(dir, files, sizeof(struct d_node *), sorter);
|
||||
|
||||
if ((label || R_flag) && !d_flag && !its_file)
|
||||
printf("%s:\n", dir_name);
|
||||
printf("\n%s:\n", dir_name);
|
||||
|
||||
/* pipe print */
|
||||
if (!p_flag || l_flag || O_flag) {
|
||||
|
|
|
@ -14,6 +14,7 @@ char z_flag;
|
|||
int n_loops = 3;
|
||||
|
||||
int shred(int rand_fd, int fd) {
|
||||
|
||||
/* Get size */
|
||||
off_t size = lseek(fd, 0, SEEK_END);
|
||||
if (size <= 0)
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#define XARGS
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
@ -6,8 +5,11 @@
|
|||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <limits.h>
|
||||
#include <ctype.h>
|
||||
#include "config.h"
|
||||
|
||||
int args;
|
||||
char *cmd[ARG_MAX + 1];
|
||||
|
||||
char *I_flag;
|
||||
char t_flag;
|
||||
|
@ -15,16 +17,6 @@ char r_flag;
|
|||
char nl_flag;
|
||||
int n_flag;
|
||||
int s_flag;
|
||||
int P_flag;
|
||||
int P_flag2;
|
||||
|
||||
int args;
|
||||
char *cmd[NARGS + 1];
|
||||
|
||||
enum {
|
||||
NONE,
|
||||
QUOTE
|
||||
};
|
||||
|
||||
enum {
|
||||
NORMAL,
|
||||
|
@ -33,6 +25,11 @@ enum {
|
|||
I_FLAG
|
||||
};
|
||||
|
||||
enum {
|
||||
NONE,
|
||||
QUOTE
|
||||
};
|
||||
|
||||
void clear_cmd(void) {
|
||||
for (int i = 0; i < args; i++)
|
||||
if (cmd[i] != NULL)
|
||||
|
@ -42,10 +39,10 @@ void clear_cmd(void) {
|
|||
}
|
||||
|
||||
int add_arg(const char *str, int chars, int flag) {
|
||||
if (args >= NARGS)
|
||||
if (args >= ARG_MAX)
|
||||
return ERROR;
|
||||
|
||||
else if (n_flag > 0 && args > n_flag)
|
||||
else if (n_flag > 0 && args > n_flag && I_flag == NULL)
|
||||
return ERROR;
|
||||
|
||||
else if (s_flag > 0 && chars > s_flag)
|
||||
|
@ -53,7 +50,7 @@ int add_arg(const char *str, int chars, int flag) {
|
|||
|
||||
if (!flag && I_flag) {
|
||||
for (int i = 0; i < args; i++)
|
||||
if (!strcmp(cmd[i], I_flag))
|
||||
if (strstr(cmd[i], I_flag))
|
||||
cmd[i] = strdup(str);
|
||||
|
||||
return I_FLAG;
|
||||
|
@ -73,50 +70,60 @@ int is_correct(char c) {
|
|||
return isspace(c);
|
||||
}
|
||||
|
||||
int stdin_read(void) {
|
||||
char arg[ARG_SIZE + 1];
|
||||
|
||||
/* Word start */
|
||||
char *p = arg;
|
||||
int xargs(void) {
|
||||
size_t arg_size = 0;
|
||||
char *arg = malloc(1);
|
||||
if (arg == NULL) {
|
||||
fprintf(stderr, "xargs: malloc failed\n");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
int flag = NONE;
|
||||
int ret = NORMAL;
|
||||
int chars = 0;
|
||||
int args_passed = 0;
|
||||
|
||||
int ret = NORMAL;
|
||||
char flag = NONE;
|
||||
while (1) {
|
||||
int c = getchar();
|
||||
if (c == EOF) {
|
||||
if (flag == QUOTE) {
|
||||
fprintf(stderr, "xargs: unterminated double quote\n");
|
||||
fprintf(stderr, "xargs: unterminated quote\n");
|
||||
ret = ERROR;
|
||||
}
|
||||
|
||||
if (p != arg)
|
||||
if (index != arg_size)
|
||||
goto PUSH;
|
||||
|
||||
ret = CEOF;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Place str */
|
||||
else if ((flag == NONE && is_correct(c) && strlen(arg) > 0) || p == arg + sizeof(arg) - 1) {
|
||||
if (flag == NONE && is_correct(c) && strlen(arg) > 0) {
|
||||
int r = 0;
|
||||
PUSH:
|
||||
|
||||
*p = '\0';
|
||||
p = arg;
|
||||
|
||||
int r = add_arg(arg, chars, 0);
|
||||
r = add_arg(arg, chars, 0);
|
||||
if (r == ERROR)
|
||||
break;
|
||||
|
||||
else if (r == I_FLAG)
|
||||
else if (r == I_FLAG) {
|
||||
free(arg);
|
||||
return NORMAL;
|
||||
}
|
||||
|
||||
index = 0;
|
||||
arg_size = 0;
|
||||
args_passed++;
|
||||
|
||||
free(arg);
|
||||
arg = malloc(1);
|
||||
if (arg == NULL) {
|
||||
fprintf(stderr, "xargs: malloc failed\n");
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add char to str */
|
||||
else {
|
||||
if (c == '"' || c == '\'') {
|
||||
if (flag == QUOTE)
|
||||
|
@ -128,11 +135,17 @@ PUSH:
|
|||
continue;
|
||||
}
|
||||
|
||||
*p = c;
|
||||
if (p + 1 == arg + sizeof(arg))
|
||||
continue;
|
||||
arg = realloc(arg, (arg_size++) + 1);
|
||||
if (arg == NULL) {
|
||||
fprintf(stderr, "xargs: realloc failed\n");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
arg[index] = c;
|
||||
|
||||
index++;
|
||||
arg[index] = '\0';
|
||||
|
||||
p++;
|
||||
chars++;
|
||||
}
|
||||
}
|
||||
|
@ -140,17 +153,11 @@ PUSH:
|
|||
if (args_passed == 0)
|
||||
ret = CEOF;
|
||||
|
||||
free(arg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Run */
|
||||
void sig_handler(int sig) {
|
||||
if (sig == SIGUSR1)
|
||||
if (P_flag2)
|
||||
P_flag2--;
|
||||
}
|
||||
|
||||
int run(void) {
|
||||
int spawn(void) {
|
||||
if (t_flag) {
|
||||
for (int i = 0; i < args; i++)
|
||||
fprintf(stderr, "%s ", cmd[i]);
|
||||
|
@ -177,13 +184,6 @@ int run(void) {
|
|||
return status;
|
||||
}
|
||||
|
||||
int spawn(void) {
|
||||
if (P_flag == 0)
|
||||
return run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void check(int val) {
|
||||
if (val <= 0) {
|
||||
fprintf(stderr, "xargs: invalid number: %s\n", optarg);
|
||||
|
@ -192,8 +192,6 @@ void check(int val) {
|
|||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
signal(SIGUSR1, sig_handler);
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "tn:s:rP:0I:")) != -1) {
|
||||
switch (opt) {
|
||||
|
@ -215,11 +213,6 @@ int main(int argc, char **argv) {
|
|||
r_flag = 1;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
P_flag = atoi(optarg);
|
||||
check(P_flag);
|
||||
break;
|
||||
|
||||
case '0':
|
||||
nl_flag = 1;
|
||||
break;
|
||||
|
@ -229,7 +222,7 @@ int main(int argc, char **argv) {
|
|||
break;
|
||||
|
||||
default:
|
||||
printf("xargs [tnsrP0I] [cmd [arg1] [arg2...]\n\t-t Print the command before start\n\t-n Pass no more than N args\n\t-r Don't run command if input is empty\n\t-s Pass command line of no more than N bytes\n\t-P Run up to N PROGs in parallel\n\t-0 NUL terminated input\n\t-I STR Replace STR within PROG ARGS with input line\n");
|
||||
printf("xargs [tnsrP0I] [cmd [arg1] [arg2...]\n\t-t Print the command before start\n\t-n Pass no more than N args\n\t-r Don't run command if input is empty\n\t-s Pass command line of no more than N bytes\n\t-0 NUL terminated input\n\t-I STR Replace STR within PROG ARGS with input line\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -248,10 +241,9 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
|
||||
else
|
||||
add_arg(ECHO_CMD, 0, 1);
|
||||
add_arg("echo", 0, 1);
|
||||
|
||||
/* Stdin */
|
||||
int stdin_stat = stdin_read();
|
||||
int stdin_stat = xargs();
|
||||
|
||||
/* Check NULL */
|
||||
for (int i = 0; i < args; i++) {
|
||||
|
@ -271,7 +263,6 @@ int main(int argc, char **argv) {
|
|||
else if (stdin_stat == NORMAL)
|
||||
ret = spawn();
|
||||
|
||||
/* Clear */
|
||||
clear_cmd();
|
||||
if (stdin_stat)
|
||||
break;
|
||||
|
|
|
@ -26,8 +26,9 @@ int main(int argc, char **argv) {
|
|||
int config = 0;
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "c:")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "c:F:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'F':
|
||||
case 'c':
|
||||
readcfg(optarg, hostname, sizeof(hostname));
|
||||
|
||||
|
@ -36,7 +37,7 @@ int main(int argc, char **argv) {
|
|||
break;
|
||||
|
||||
default:
|
||||
printf("hostname [c] [hostname]\n\t-c PATH Use config file\n");
|
||||
printf("hostname [cF] [hostname]\n\t-c/-F PATH Use config file\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
int open_file(const char *path) {
|
||||
|
|
Loading…
Reference in New Issue