char* getLine(char* buf,int size){
fgets(buf,size,stdin);
int len = strlen(buf);
if(buf[len-1]=='\n'){
buf[len-1] = 0;
}
}
int main(int argc, const char *argv[])
{
while(1){
int retval = fork();
if(retval > 0){
wait(0);
}else{
char* username = getlogin();
char hostname[64] = {0};
char pwd[128] = {0};
gethostname(hostname,64);
getcwd(pwd,128);
printf("\033[1;32;10m%s@%s\033[0m:\033[1;34;10m%s\033[0m$ ",username,hostname,pwd);
fflush(stdout);
char* arg[20] = {0};
// find /usr/include -name stdio.h
// cd ..
char cmd[128] = {0};
getLine(cmd,128);
char* retval = NULL;
int i = 0;
do{
if(retval == NULL){
retval = strtok(cmd," ");
}else{
retval = strtok(NULL," ");
}
// printf("retval = %s\n",retval);
arg[i++] = retval;
}while(retval != NULL);
if(strcmp(arg[0],"cd")==0){
chdir(arg[1]);
}else{
execvp(arg[0],arg);
printf("没有找到该指令\n");
}
//return 0;
}
}
return 0;
}
伪终端c语言
于 2024-05-10 15:20:44 首次发布