實現shell終端代碼分享(可使用者登入 實現系統命令)_linux shell

來源:互聯網
上載者:User

複製代碼 代碼如下:

#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;

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.