mybash

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>
#include<pwd.h>//获取用户名

#define PATH "/home/stu/c215/mybin/"
#define MAX_ARG 10

void print_info()
{
    //printf("\033[1;32mstu@stu-virtual-machine:\033[0m\033[1;34m~/c215/mybash$\033[0m ");
    //fflush(stdout);
    char* s="$";
    //char* username="root";
    int id=getuid();//得到当前进程用户的id
    if(0 == id)
    {
        s="#";
    }

    struct passwd* pw=getpwuid(id);
    if( NULL == pw)
    {
        printf("mybash>>$");
        fflush(stdout);
        return ;
    }
    char hostname[128]={0};
    if( gethostname(hostname,128) == -1 )
    {
        printf("mybash>>$");
        fflush(stdout);
        return ;  
    }

    char pwd_str[256] = {0};
    if(NULL == getcwd(pwd_str,256) )
    {
        printf("mybash>>$");
        fflush(stdout);
        return ;
    }
    printf("\033[1;32m%s@%s\033[0m \033[1;34m%s%s\033[0m ",pw->pw_name,hostname,pwd_str,s);
}


char* get_cmd(char* p,char* myargv[])
{
    if( p == NULL || myargv == NULL)
    {
        return NULL;
    }
    int i=0;
    char* s=strtok( p ," ");
    while(s != NULL)
    {
        myargv[i++] = s;
        s = strtok(NULL, " ");//字符串分割函数第一个参数为需要分割的字符串,第二个参数为分割条件
    }
    return myargv[0];
}

void change_dirent(char* dir)
{

    if(dir == NULL)
    {
         return ;
    }
    if( chdir(dir) == -1)//改变工作路径
    {
        perror("cd err");
    }
}
int main()
{
    while(1)
    {
        print_info();

        char buff[128]={0};
        fgets(buff,128,stdin);
        buff[strlen(buff)-1]=0;

        char* myargv[MAX_ARG]={0};

        char* cmd=get_cmd(buff,myargv);
        if( cmd == NULL)
        {
            continue;
        }
        else if(strcmp(cmd,"cd") == 0 )//字符串比较函数
        {
            change_dirent(myargv[1]);
        }
        
        else if ( strcmp(cmd,"exit") == 0 )
        {
            break;
        }
        else
        {
            pid_t pid =fork();
            if( pid ==-1 )
            {
                printf("fork err\n");
                continue;
            }
            if ( pid == 0 )
            {
                char cmdpath[128] ={0};
                if(strncmp(cmd,"./",2) != 0 && strncmp(cmd,"/",1) != 0 )
                {
                    strcpy(cmdpath,PATH);
                    strcat(cmdpath,cmd);
                }
                else
                {
                     strcpy(cmdpath,cmd);
                }
                execv(cmdpath,myargv);

                //execvp( cmd, myargv);//替换进程函数
                perror("execvp err");//打印错误和原因函数
                exit(0);
            }
            wait(NULL);
            
        }
        

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值