自制shell
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<sys/wait.h>
#define ERR_EXIT(msg)\
do{
printf("[%s][%d] %s:%s\n", __FILE__, __LINE__, msg, strerror(errno)); \
exit(EXIT_FAILURE);\
}while(0)
int get_input(char* buf) {
memset(buf, 0x00, sizeof(buf));
if (scanf("%[^\n]%*c", buf) != 1) {
int c;
do c = getchar(); while (c != '\n');
}
return strlen(buf);
}
void do_action(int argc, char* argv[]) {
pid_t pid;
switch (pid = fork()) {
case -1:
printf("当前shell出现故障,正在维护中...\n");
return;
case 0: {
int ret = execvp(argv[0], argv);
printf("%s : command not found\n", argv[0]);
exit(1);
}
break;
default: {
int status;
waitpid(pid, &status, 0);
}
break;
}
}
void do_parse(char* buf) {
int argc = 0;
char* argv[10] = {};
#define OUT 0
#define IN 1
int status = OUT;
int i;
for (i = 0; buf[i] != '\0'; i++) {
if (status == OUT && !isspace(buf[i])) {
argv[argc++] = buf + i;
status = IN;
}
else if (isspace(buf[i])) {
status = OUT;
buf[i] = '\0';
}
}
argv[argc] = NULL;
do_action(argc, argv);
}
int main(void) {
char buf[512];
while (1) {
printf("[taoge shell] # ");
if (get_input(buf) == 0)
continue;
do_parse(buf);
}
}