婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 實現shell終端代碼分享(可用戶登錄 實現系統命令)

實現shell終端代碼分享(可用戶登錄 實現系統命令)

熱門標簽:百度地圖標注錯了有責任嗎 打電話機器人接我是他的秘書 地圖標注員都是年輕人 揭陽智能電話機器人推薦 華鋒e路航港口地圖標注 江蘇云電銷機器人公司 如果做線上地圖標注 河南信譽好的不封卡電話外呼系統 客服外呼系統怎么樣

復制代碼 代碼如下:

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

}

標簽:婁底 馬鞍山 赤峰 淘寶邀評 巴彥淖爾 許昌 金昌 邵陽

巨人網絡通訊聲明:本文標題《實現shell終端代碼分享(可用戶登錄 實現系統命令)》,本文關鍵詞  實現,shell,終端,代碼,分享,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《實現shell終端代碼分享(可用戶登錄 實現系統命令)》相關的同類信息!
  • 本頁收集關于實現shell終端代碼分享(可用戶登錄 實現系統命令)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 靖宇县| 双桥区| 嘉黎县| 孟村| 阜新| 突泉县| 应城市| 佛山市| 宜州市| 卓资县| 崇明县| 永平县| 柘城县| 瓦房店市| 莒南县| 崇信县| 巍山| 靖西县| 芷江| 五峰| 冀州市| 玉溪市| 方城县| 莒南县| 丹凤县| 来凤县| 邹平县| 九台市| 东安县| 民和| 龙岩市| 阿拉善盟| 怀集县| 昌江| 沙田区| 陇川县| 韶山市| 西华县| 澄迈县| 陆丰市| 肥乡县|