Linux C programming one Passwd document user separation

xiaoxiao2021-03-06  65

Linux C programming one Passwd document user separation

One idea:

Users who want to separate the PASSWD document in the LNUX system. How to implement

Actually, there are many ways. But here is introducing your own understanding and practices. First look at the structure inside the passwd document:

Root: x: 0: 0: root: / root: / bin / bash

BIN: x: 1: 1: bin: / bin: / sbin / nologin

Daemon: x: 2: 2: daem: / sbin: / sbin / nologin

ADM: X: 3: 4: ADM: / VAR / ADM: / SBIN / NOLOGIN

LP: x: 4: 7: lp: / var / spool / lpd: / sbin / nologin

Sync: x: 5: 0: sync: / sbin: / bin / sync

Shutdown: x: 6: 0: shutdown: / sbin: / sbin / shutdown

Halt: x: 7: 0: Halt: / sbin: / sbin / halt

Mail: x: 8: 12: Mail: / var / spool / mail: / sbin / nologin

News: x: 9: 13: News: / etc / news:

UUCP: X: 10: 14: UUCP: / VAR / SPOOL / UUCP: / SBIN / NOLOGINOPERATOR: X: 11: 0: Operator: / root: / sbin / nologingames: x: 12: 100: Games: / usr / games : / sbin / nologingopher: x: 13: 30: Gopher: / var / gopher: / sbin / nologin

FTP: X: 14: 50: FTP User: / Var / ftp: / sbin / nologin

Nobody: x: 99: 99: NoDy: /: / sbin / nologin

NTP: x: 38: 38 :: / etc / ntp: / sbin / nologin

How is it better?

If you can save the first colon, you can do it. How do you implement it?

II. Function introduction:

In fact, there is such a function to use:

If you are under Linux, you can immediately view the Manual: MANUAL:

Name STRTOK, STRTOK_R - EXTRACT TOKENS from Strings

Synopsis #include

Char * STRTOK (Char * S, Const Char * Delim);

Char * STRTOK_R (Char * S, Const Char * Delim, Char ** PTRPTR);

Description a `token 'is a nonempty string of characters Not Occurring In The String Delim, Followed by / 0 or by a character occurring in Delim.

The strtok () function can be used to parse the string s into tokens. The first call to strtok () should have s as its first argument. Subse- quent calls should have the first argument set to NULL. Each call returns a pointer to The next token, or null when no more tokens is found.

If a token ends with a delimiter, this delimiting character is over-ritten with a / 0 and a pointer to the next character is saved for the next call to strtok (). The delimiter string delim may be different foreach call.The strtok_r ( ) function is a reentrant version of the strtok () function, which instead of using its own static buffer, requires a pointer to a user allocated char *. This pointer, the ptrptr parameter, must be the same while parsing the same string.

I posted some:

This function is included in the library file :

#include

The structure is:

Char * Strtok (Char * STR1, CONST Char * STR2);

This function strtok () returns a pointer to a lexical unit in string STR1. The characters that make up the string STR2 are the interval of determining the lexical units. When no terms are units, the function returns an empty pointer.

When Strtok () is called for the first time, STR1 is actually used. After the successor, the empty pointer must be used at the first variable element. This whole string is reduced to its lexical unit. Each call for it is possible to use A set of interlocking strings.

3. Programming:

TC can also be compiled with TC under Window

Now under Linux.

Create a file. And name it Handleuser.c

Nightcat @ Nightcat $ VI HANDEUSER.C

Edit it:

/ * Username split version1.0the small tools can slpit username from passwd and other passwd fileTo Compile: nightcat @ nightcat $ gcc -o handluser handleuser.cUsage: ./ handleuser Code By NihgtcatAprile 2003 * / # include < String.h> #include

Main (int Argc, char * argv []) {

File * in; file * out;

Char buffer [1024]; char * usrname;

IF (ARGC <3) Printf ("USAGE:% s / n", argv [0]);

IF ((IN = FOPEN (Argv [1], "R")) == NULL) Printf ("File Could Not Be Opened / N); IF ((out = fopen (Argv [2]," W ") ) == NULL) Printf ("File Could Not Be Opened / N);

While (! Feof (in) {fscanf (in, "% s", buffer; username = strtok (buffer, ":"); fprintf (out, "% s / n", username;} fclose ); Fclose (out); RETURN 0;}

Save documentation:

Press ESC to exit editing mode

: WQ Save Exit

Compile:

Nightcat @ nightcat $ gcc -o handleuser handleuser.c

Execution: Nightcat @ nightcat $. / HANDEUSER / ETC / Passwd / Nightcat/Username.txtnightcat@nightcat /cat / nightcat/Username.txt

root

bin

Daemon

ADMLP

Sync

Shutdown

Halt

Mail

news

UUCP

Operator

Games

gopher

FTP

NoBody

NTP

.

.

.

.

I'm later:

There are also several foundations in this program:

INT ARGC, CHAR * ARGV [] The meaning of: Argc is a few parameters .argv is where the parameter is stored. It is a pointer char * argv []

Fopen, fscanf, fprintf, fclose. It is a file to open, read, write, and turn it off.

FeOf is the judgment of whether the file is finished.

Detailed information related to Manual.

Five. Contact me: Nickname: Nightcat

E-mail: ncnynl@hotmail.com

QQ: 1043931

ICQ: 153436005

转载请注明原文地址:https://www.9cbs.com/read-119314.html

New Post(0)