这个程序可以实现简单的shell功能:输入命令,处理命令。参考了《Linux C 编程实战》的shell例程。
代码如下:
/*************************************************************************
> File Name: myshell.c
> Author:
> Note:
> Created Time: 2015年07月29日 星期三 14时52分32秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<dirent.h>
#define normal 0 //一般的命令
#define in_redirect 1 //输入重定向
#define out_redirect 2 //输出重定向
#define have_pipe 3 //有管道
int flag = 0;
void print_prefix() //打印提示符
{
int number;
srand((unsigned)time(NULL) );
number = rand() % 100;
if(flag != 1){
if(number>=0 && number < 20) {
printf("myshell (っ・Д・)っ $$:");
flag = 1;
} else if( number >= 20 && number < 40 ) {
printf("myshell Σ(*゚д゚ノ)ノ $$:");
flag = 1;
} else if( number >= 40 && number < 60 ) {
printf("myshell ԅ(¯﹃¯ԅ) $$:");
flag = 1;
} else if( number >= 60 && number < 65 ) {
printf("myshell ─=≡Σ((( つ•̀ω•́)つ $$:");
flag = 1;
} else if( number >= 65 && number < 80 ) {
printf("myshell (ง๑ •̀_•́)ง $$:");
flag = 1;
} else {
printf("myshell ρ(・ω・、) $$:");
flag = 1;
}
}
else {
printf("myshell ------------ $$:");
flag = 0;
}
}
void get_cmd( char *buf ) //得到输入的命令
{
int len = 0;
char ch;
ch = getchar();
while( len < 256 && ch != '\n'