您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何編寫實(shí)現(xiàn)shell終端代碼”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何編寫實(shí)現(xiàn)shell終端代碼”吧!
代碼如下:
#include"apue.h"
int userlogin(struct passwd **);
int main()
{
int ret,i=0;
long ret_cwd;
struct passwd *pw;
char buf[128],hostname[16],usercwd[128];
char *ptmp1;
do{
ret = userlogin(&pw);
}while(ret != 1);
getchar();
while(1){
//to gethostname
if(gethostname(buf,128)!=0){
perror("gethostname()");
return 0;
}
//truncate hostname untill '.'
while(buf[i]!='.'){
hostname[i++] = buf[i];
}
i = 0;
//if the user is 'root' the symbol is '#'
//else '$'
//and the current work path is truncated from the last '/' to the end
ret_cwd = (long)getcwd(usercwd,128);
if(strcmp(usercwd,pw->pw_dir)==0){
ret = sprintf(usercwd,"~\0",NULL);
}
else if(strcmp(usercwd,"/")!=0){
ptmp1 = strrchr(usercwd,'/');
sprintf(usercwd,"%s",ptmp1+1);
}
if(strcmp(pw->pw_name,"root")==0){
sprintf(buf,"*[%s@%s %s]# ",
pw->pw_name,hostname,usercwd);
}
else{
sprintf(buf,"*[%s@%s %s]$ ",
pw->pw_name,hostname,usercwd);
}
//command
ssize_t g_ret,len;
char *line = NULL,*s_ret,*ptr=NULL;
int con_cd=0;
fprintf(stderr,"%s",buf);
g_ret = getline(&line,&len,stdin);
line[strlen(line)-1] = '\0';
if(strcmp(line,"exit")==0){
exit(-1);
}
ptr = line;
s_ret = line;
system(line);
while(ptr!=NULL){
s_ret = (char *)strsep(&ptr," ");
if(strcmp(s_ret,"cd") == 0){
con_cd=1;
continue;
}
else if(con_cd == 1){
chdir(s_ret);
}
}
}
return 0;
}
//login function
int userlogin(struct passwd **pw)
{
char name[32],*passwd,*pret;
struct spwd *sp;
printf("login:");
fflush(stdout);
scanf("%s",name);
passwd = getpass("password:");
sp = getspnam(name);
if(sp == NULL){
fprintf(stdout,"no is user![%s]\n",name);
return 0;
}
pret = crypt(passwd,sp->sp_pwdp);
if(pret == NULL){
fprintf(stdout,"crypt(%s)\n",name);
return 0;
}
if(strcmp(pret,sp->sp_pwdp) == 0){
printf("login successful!\n");
}
else{
fprintf(stdout,"passwd is error!\n");
return 0;
}
*pw = getpwnam(name);
if(*pw == NULL){
printf("getpwnam(%s) error\n",name);
return 0;
}
return 1;
}
到此,相信大家對(duì)“如何編寫實(shí)現(xiàn)shell終端代碼”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。