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