cat fix
This commit is contained in:
parent
65f837f830
commit
1eb916455b
5
TODO
5
TODO
|
@ -2,10 +2,11 @@ With "micro-" prefix
|
|||
|
||||
*Todo:
|
||||
**dmesg (portable)
|
||||
**head
|
||||
**cat
|
||||
tail
|
||||
expr
|
||||
uniq
|
||||
head
|
||||
od
|
||||
nice
|
||||
renice
|
||||
|
@ -62,7 +63,7 @@ Findutils:
|
|||
xargs
|
||||
|
||||
Shell:
|
||||
rc - run command (pipe, ~ | <> <<>> & " parsing)
|
||||
rc - run command (1 2 3 ~ | <> <<>> & * " parsing)
|
||||
|
||||
Init:
|
||||
sinit - Simple init
|
||||
|
|
2
config.h
2
config.h
|
@ -1,7 +1,7 @@
|
|||
#ifndef _CONFIG_H
|
||||
#define _CONFIG_H
|
||||
|
||||
/* (cat tee wc) */
|
||||
/* (tee wc) */
|
||||
#define BUF_SIZE 4096
|
||||
|
||||
/* Random source (shred) */
|
||||
|
|
|
@ -3,51 +3,66 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "config.h"
|
||||
|
||||
int cat(const int fd) {
|
||||
char buf[BUF_SIZE + 1];
|
||||
off_t rbytes;
|
||||
unsigned int n_flag;
|
||||
|
||||
while ((rbytes = read(fd, buf, sizeof(buf))) > 0)
|
||||
if (write(STDOUT_FILENO, buf, rbytes) != rbytes) {
|
||||
fprintf(stderr, "cat: write error copying\n");
|
||||
int cat(const char *path) {
|
||||
FILE *fp = NULL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int main(const int argc, const char **argv) {
|
||||
if (argc == 1)
|
||||
return cat(STDIN_FILENO);
|
||||
int main(int argc, char **argv) {
|
||||
int opt;
|
||||
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 {
|
||||
int ret = 0;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (argv[i][0] == '-') {
|
||||
if (argv[i][1])
|
||||
continue;
|
||||
|
||||
else
|
||||
if (cat(STDIN_FILENO))
|
||||
for (int i = 0; i < argc; i++)
|
||||
if (cat(argv[i]))
|
||||
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;
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
15
shell/rc.c
15
shell/rc.c
|
@ -4,6 +4,7 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include "config.h"
|
||||
#include "unused.h"
|
||||
|
@ -15,7 +16,7 @@ int rc_cd(char **args, char **envp);
|
|||
int rc_exit(char **args, char **envp);
|
||||
|
||||
/* Builtin commands */
|
||||
char *cmd[] = {"cd", "exit", "which"};
|
||||
char *cmd[] = {"cd", "exit"};
|
||||
int (*func[])(char **args, char **envp) = {rc_cd, rc_exit};
|
||||
|
||||
int rc_exit(char **args, char **envp) {
|
||||
|
@ -52,14 +53,16 @@ int execute(char **tok, char **envp) {
|
|||
if (!strcmp(tok[0], cmd[i]))
|
||||
return func[i](tok + 1, envp);
|
||||
|
||||
/* TODO: pipe */
|
||||
/* TODO: pars res */
|
||||
/* If its not builtin cmd */
|
||||
pid_t pid;
|
||||
if ((pid = fork()) == 0) {
|
||||
if (pid == 1)
|
||||
exit(1);
|
||||
|
||||
execvp(tok[0], tok);
|
||||
if (execvp(tok[0], tok) == -1)
|
||||
fprintf(stderr, "rc: %s: unknow command\n", tok[0]);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -101,13 +104,14 @@ char **tokenize(char *str) {
|
|||
}
|
||||
|
||||
char *readline(FILE *fp, char *prompt) {
|
||||
puts(prompt);
|
||||
printf("%s", prompt);
|
||||
|
||||
char *str = NULL;
|
||||
size_t len = 0;
|
||||
if (getline(&str, &len, fp) == -1)
|
||||
return NULL;
|
||||
|
||||
putchar('\n');
|
||||
return str;
|
||||
}
|
||||
|
||||
|
@ -115,6 +119,9 @@ int main(int argc, char **argv, char **envp) {
|
|||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
|
||||
signal(SIGINT, SIG_IGN);
|
||||
signal(SIGTSTP, SIG_IGN);
|
||||
|
||||
char *prompt = RC_PS;
|
||||
if (!isatty(STDIN_FILENO))
|
||||
prompt = "";
|
||||
|
|
Loading…
Reference in New Issue