源代码
#include<stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#define LEN 1024
char* trim(char*);
int main() {
char buff[LEN];
while (1) {
printf("kaikeba$");
fgets(buff, LEN, stdin);
buff[strlen(buff) - 1] = 0;
printf("cmd:[%s]\n", buff);
if (!strcmp(buff, "exit")) {
printf("exit~~\n");
break;
}
pid_t pid = fork();
if (pid < 0) {
perror("fork error");
exit(1);
}
if (pid) {
wait(NULL);
continue;
}
int redfd = -1;
if (strstr(buff, "<")) {
redfd = 0;
}
if (strstr(buff, ">")) {
redfd = 1;
}
char* cmd = NULL;
if (redfd != -1) {
char* token = strtok(buff, "<>");
printf("token:[%s]\n", token);
cmd = token;
token = strtok(NULL, "<>");
printf("token:[%s]\n", token);
token = trim(token);
int fd;
if (redfd) {
fd = open(token, O_RDWR | O_CREAT, 0644);
}
else {
fd = open(token, O_RDWR);
}
if(fd < 0) {
perror(token);
exit(1);
}
printf("open %s successfully~\n", token);
dup2(fd, redfd);
}
else {
cmd = buff;
}
int i = 0;
char* argarr[20];
char* tk = strtok(cmd, " ");
while (tk) {
argarr[i++] = tk;
tk = strtok(NULL, " ");
}
argarr[i] = tk;
execvp(argarr[0], argarr);
perror(argarr[0]);
exit(1);
}
return 0;
}
char* trim(char* str) {
int head = 0;
int tail = strlen(str) - 1;
while (isspace(str[head])) {
head++;
}
while (isspace(str[tail])) {
str[tail--] = 0;
}
return str + head;
}
执行程序
lh@iZwz9hl4wc7w8vb0fryco4Z ~ %gcc kkbshell.c [0]
lh@iZwz9hl4wc7w8vb0fryco4Z ~ %./a.out [0]
kaikeba$ls
cmd:[ls]
a.out euler index.txt leetcode Project ttt.txt
c fs kegg.csv libevent-2.1.12-stable python
c++ hello.txt kepp.py libevent-2.1.12-stable.tar.gz shelledit
data http kkbshell.c oj tencent
kaikeba$exit
cmd:[exit]
exit~~
|