Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write and implement shell terminal code

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to write shell terminal code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write and implement shell terminal code.

The code is 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 [iTunes +] = 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, "* [% slots% s% s] #"

Pw- > pw_name,hostname,usercwd)

}

Else {

Sprintf (buf, "* [% slots% 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 (ptrackers 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

}

At this point, I believe you "how to write shell terminal code" have a deeper understanding, might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report