This commit is contained in:
Your Name 2023-11-23 15:02:49 +03:00
parent 65f837f830
commit 1eb916455b
4 changed files with 64 additions and 41 deletions

5
TODO
View File

@ -2,10 +2,11 @@ With "micro-" prefix
*Todo: *Todo:
**dmesg (portable) **dmesg (portable)
**head
**cat
tail tail
expr expr
uniq uniq
head
od od
nice nice
renice renice
@ -62,7 +63,7 @@ Findutils:
xargs xargs
Shell: Shell:
rc - run command (pipe, ~ | <> <<>> & " parsing) rc - run command (1 2 3 ~ | <> <<>> & * " parsing)
Init: Init:
sinit - Simple init sinit - Simple init

View File

@ -1,7 +1,7 @@
#ifndef _CONFIG_H #ifndef _CONFIG_H
#define _CONFIG_H #define _CONFIG_H
/* (cat tee wc) */ /* (tee wc) */
#define BUF_SIZE 4096 #define BUF_SIZE 4096
/* Random source (shred) */ /* Random source (shred) */

View File

@ -3,51 +3,66 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "config.h"
int cat(const int fd) { unsigned int n_flag;
char buf[BUF_SIZE + 1];
off_t rbytes;
while ((rbytes = read(fd, buf, sizeof(buf))) > 0) int cat(const char *path) {
if (write(STDOUT_FILENO, buf, rbytes) != rbytes) { FILE *fp = NULL;
fprintf(stderr, "cat: write error copying\n");
return 1;
}
if (!strcmp(path, "-"))
fp = stdin;
else
fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "cat: %s: %s\n", path, strerror(errno));
return 1;
}
char *buf = NULL;
size_t len = 0;
size_t line = 0;
while (getline(&buf, &len, fp) != EOF) {
line++;
if (n_flag)
printf("\t%zu\t%s", line, buf);
else
printf("%s", buf);
}
free(buf);
return 0; return 0;
} }
int main(const int argc, const char **argv) { int main(int argc, char **argv) {
if (argc == 1) int opt;
return cat(STDIN_FILENO); while ((opt = getopt(argc, argv, "n")) != -1) {
switch (opt) {
case 'n':
n_flag = 1;
break;
default:
printf("cat [file1 file2]\n\t[-n number all output lines]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 0)
return cat("-");
else { else {
int ret = 0; int ret = 0;
for (int i = 1; i < argc; i++) { for (int i = 0; i < argc; i++)
if (argv[i][0] == '-') { if (cat(argv[i]))
if (argv[i][1])
continue;
else
if (cat(STDIN_FILENO))
ret = 1;
continue;
}
int fd = open(argv[i], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno));
return 1;
}
if (cat(fd))
ret = 1; ret = 1;
close(fd);
}
return ret; return ret;
} }
} }

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include <sys/wait.h> #include <sys/wait.h>
#include "config.h" #include "config.h"
#include "unused.h" #include "unused.h"
@ -15,7 +16,7 @@ int rc_cd(char **args, char **envp);
int rc_exit(char **args, char **envp); int rc_exit(char **args, char **envp);
/* Builtin commands */ /* Builtin commands */
char *cmd[] = {"cd", "exit", "which"}; char *cmd[] = {"cd", "exit"};
int (*func[])(char **args, char **envp) = {rc_cd, rc_exit}; int (*func[])(char **args, char **envp) = {rc_cd, rc_exit};
int rc_exit(char **args, char **envp) { int rc_exit(char **args, char **envp) {
@ -52,14 +53,16 @@ int execute(char **tok, char **envp) {
if (!strcmp(tok[0], cmd[i])) if (!strcmp(tok[0], cmd[i]))
return func[i](tok + 1, envp); return func[i](tok + 1, envp);
/* TODO: pipe */ /* TODO: pars res */
/* If its not builtin cmd */ /* If its not builtin cmd */
pid_t pid; pid_t pid;
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
if (pid == 1) if (pid == 1)
exit(1); exit(1);
execvp(tok[0], tok); if (execvp(tok[0], tok) == -1)
fprintf(stderr, "rc: %s: unknow command\n", tok[0]);
exit(1); exit(1);
} }
@ -101,13 +104,14 @@ char **tokenize(char *str) {
} }
char *readline(FILE *fp, char *prompt) { char *readline(FILE *fp, char *prompt) {
puts(prompt); printf("%s", prompt);
char *str = NULL; char *str = NULL;
size_t len = 0; size_t len = 0;
if (getline(&str, &len, fp) == -1) if (getline(&str, &len, fp) == -1)
return NULL; return NULL;
putchar('\n');
return str; return str;
} }
@ -115,6 +119,9 @@ int main(int argc, char **argv, char **envp) {
UNUSED(argc); UNUSED(argc);
UNUSED(argv); UNUSED(argv);
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
char *prompt = RC_PS; char *prompt = RC_PS;
if (!isatty(STDIN_FILENO)) if (!isatty(STDIN_FILENO))
prompt = ""; prompt = "";