Implement shell terminal code sharing (user login to implement system commands) _linux Shell

Source: Internet
Author: User
Tags crypt sprintf strcmp

Copy Code code as follows:

#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 ' symbol ' # '
Else ' $ '
and the current work path are truncated from the "last"/"to" 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] = ';
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;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.