主页 > 知识库 > 实现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终端代码分享(可用户登录 实现系统命令)的相关信息资讯供网民参考!
  • 推荐文章