最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

實現(xiàn)shell終端代碼分享(可用戶登錄 實現(xiàn)系統(tǒng)命令)

 更新時間:2013年12月19日 15:12:45   作者:  
實現(xiàn)shell終端代碼分享,實現(xiàn)用戶登錄,系統(tǒng)命令使用,大家參考實現(xiàn)吧

復(fù)制代碼 代碼如下:

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

}

相關(guān)文章

  • shell腳本中取消重定向的方法實例

    shell腳本中取消重定向的方法實例

    這篇文章主要介紹了shell腳本中取消重定向的方法實例,本文直接給出代碼實例,需要的朋友可以參考下
    2015-03-03
  • Linux?syslog命令使用教程

    Linux?syslog命令使用教程

    syslog?是?Linux?和類?Unix?系統(tǒng)中用于記錄系統(tǒng)消息的標準協(xié)議,它允許應(yīng)用程序、守護進程和內(nèi)核將日志消息發(fā)送到集中式日志管理系統(tǒng),本文給大家介紹了Linux?syslog命令使用教程,需要的朋友可以參考下
    2025-03-03
  • shell的比較運算介紹

    shell的比較運算介紹

    shell中的比較運算介紹,想學(xué)習linux shell編程的朋友可以參考下
    2013-01-01
  • shell腳本4種執(zhí)行方式

    shell腳本4種執(zhí)行方式

    Linux中shell腳本的執(zhí)行通常有4種方式,分別為工作目錄執(zhí)行,絕對路徑執(zhí)行,sh執(zhí)行,shell環(huán)境執(zhí)行。這篇文章主要介紹了shell腳本4種執(zhí)行方式 ,需要的朋友可以參考下
    2019-05-05
  • Linux Shell腳本的編程之正則表達式

    Linux Shell腳本的編程之正則表達式

    這篇文章主要介紹了Linux Shell腳本的編程之正則表達式的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • 開發(fā)者常用及實用Linux Shell命令備忘錄(小結(jié))

    開發(fā)者常用及實用Linux Shell命令備忘錄(小結(jié))

    這篇文章主要介紹了開發(fā)者常用及實用Linux Shell命令備忘錄(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2019-11-11
  • Linux Bash腳本中的IFS的作用

    Linux Bash腳本中的IFS的作用

    ??在處理文本數(shù)據(jù)時,Bash 將文本分割成多個字段,這些字段之間由 IFS 指定的字符進行分隔,本文主要介紹了Linux Bash腳本中的IFS的作用,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • 一張圖讓你學(xué)會Vim編輯器快捷鍵

    一張圖讓你學(xué)會Vim編輯器快捷鍵

    今天小編就為大家分享一篇一張圖讓你學(xué)會Vim編輯器快捷鍵,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Shell 編程:Bash空格的那點事

    Shell 編程:Bash空格的那點事

    空格,一個看不見的字符,很不起眼,也正由于不起眼,很多人經(jīng)常忽略它,導(dǎo)致代碼出錯,卻還找不著北。這里,我們來聊聊bash中空格的那點事
    2015-01-01
  • sed使用正則表達式進行替換例子以及注意事項

    sed使用正則表達式進行替換例子以及注意事項

    在使用sed替換時,可以使用正則表達式分組來保留部分匹配內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于sed使用正則表達式進行替換例子以及注意事項的相關(guān)資料,需要的朋友可以參考下
    2024-03-03

最新評論

乌兰察布市| 浪卡子县| 吕梁市| 栾城县| 临桂县| 和林格尔县| 达拉特旗| 铜川市| 浪卡子县| 深水埗区| 鄢陵县| 台前县| 民勤县| 西宁市| 普兰县| 肥西县| 云梦县| 蒙阴县| 丰城市| 宝山区| 湾仔区| 东城区| 广安市| 卫辉市| 威信县| 新和县| 东安县| 烟台市| 信宜市| 松滋市| 尖扎县| 常宁市| 南江县| 上饶市| 平顺县| 丰原市| 芦溪县| 凤城市| 正宁县| 出国| 长治市|