国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

【Linux】第一個簡單shell

2019-11-08 18:51:30
字體:
來源:轉載
供稿:網(wǎng)友

使用c/c++完成一個簡單shell,具有基本功能

#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<wait.h>#include<pwd.h>#include<string.h>void GetLoginName(){	struct passwd* pwd;	pwd=getpwuid(getuid());	PRintf("[%s@",pwd->pw_name);}void GetHostName(){char name[128]={0};gethostname(name,sizeof(name)-1);printf("%s",name);}void GetDir(){char pwd[128]={0};getcwd(pwd,sizeof(pwd)-1);int len=strlen(pwd);char* p=pwd+len;while(*p!='/'&&len--){p--;}p++;printf(" %s]&",p);}int main(){	while(1)	{	GetLoginName();	GetHostName();	GetDir();	fflush(stdout);	char line[1024];	ssize_t _s=read(0,line,1024);	char* myargv[10];	char* start=line;    myargv[0]=start;	int  i=1;	if(_s>0)	{		while(*start)		{			if(isspace(*start))			{				while(isspace(*start))				{					*start='/0';								start++;				}							myargv[i++]=start;			}			else			{				start++;			}		}	}	else	{      continue;	}	myargv[i-1]=NULL;	pid_t id=vfork();	if(id==0)	{		execvp(myargv[0],myargv);		perror("error");	}	else{		sleep(3);		wait(NULL);	}	printf("/n");	}運行結果

也可以直接將命令頭輸出,不用函數(shù)獲取

#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<wait.h>#include<string.h>int main(){	while(1)	{	printf("[root@localhost 2.13]&");	fflush(stdout);	char line[1024];	ssize_t _s=read(0,line,1024);	char* myargv[10];	char* start=line;    myargv[0]=start;	int  i=1;	if(_s>0)	{		while(*start)		{			if(isspace(*start))			{				while(isspace(*start))				{					*start='/0';								start++;				}							myargv[i++]=start;			}			else			{				start++;			}		}	}	else	{      continue;	}	myargv[i-1]=NULL;	pid_t id=vfork();	if(id==0)	{		execvp(myargv[0],myargv);		perror("error");	}	else{		sleep(3);		wait(NULL);	}	printf("/n");	}	return 0;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巢湖市| 德格县| 巴里| 肥城市| 乌兰县| 安岳县| 银川市| 双辽市| 中江县| 兴海县| 蓬莱市| 清新县| 吉水县| 大英县| 泗水县| 双鸭山市| 盘山县| 囊谦县| 南安市| 江山市| 通州区| 巴青县| 淮北市| 松阳县| 房产| 舞钢市| 长葛市| 永德县| 井陉县| 抚松县| 安新县| 凌海市| 新巴尔虎左旗| 郓城县| 濮阳市| 门源| 长垣县| 建始县| 越西县| 华亭县| 彰化县|