linux c 编程之一 passwd文档用户分离
一思路:
对于想分离lnux系统passwd文档里面的用户.如何实现
其实方法是很多.不过这里都是介绍自己的理解和做法.先看看passwd
文档里面的结构:
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/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/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
那怎样分离比较好呢.
如果能把第一个冒号前面的保存下来不就可以了.那如何实现?
二.函数介绍:
其实正好就有这样一个函数可以利用:
如果你在linux 下,可以立刻man strtok 来查看这个函数的manual:
NAME
strtok, strtok_r - extract tokens from strings
SYNOPSIS
#include <string.h>
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 are
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 for
each 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.
我贴出其中一些:
这个函数包含在库文件<string.h>:
#include<string.h>
结构是:
char *strtok(char * str1,const char * str2);
这个函数strtok()返回一个指针,指向串str1中的一个词法单位.组成串str2的字符是确定词法单位的间隔符.无词法单位时,函数返回空指针.
第一次调用strtok()时,实际使用str1;后继调用,必须在第一变元处使用空指针.这样整个串就被缩减为它的词法单位.对它的每次调用,都可能使用不同的一组间隔符串.
三.程序设计:
在window下也可用tc来编译
现在在linux 下.则:
建立一个文件.并命名为handleuser.c
nightcat@nightcat$vi handleuser.c
编辑其中内容:
/*username split version1.0
the small tools can slpit username from passwd and other passwd file
To Compile:
nightcat@nightcat$gcc -o handluser handleuser.c
Usage:
./handleuser <inputfile> <outputfile>
Code By Nihgtcat
Aprile 2003
*/
#include<string.h>
#include<stdio.h>
main(int argc,char *argv[])
{
FILE *in ;
FILE *out;
char buffer[1024];
char *username;
if(argc<3)
printf("usage:%s <inputfile> <outputfiles>/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(in);
fclose(out);
return 0;}
保存文档:
按一下esc 退出编辑模式
:wq 保存退出
编译:
nightcat@nightcat$gcc -o handleuser handleuser.c
执行:
nightcat@nightcat$./handleuser /etc/passwd /nightcat/username.txt
nightcat@nightcat$cat /nightcat/username.txtroot
bin
daemon
adm
lpsync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
ntp
..
..
..
..
四.后言:
这个程序里面也有好几个基础的东西:
int argc ,char *argv[] 的含义:argc 是指出有几个参数.argv是参数存放的地方.是一个指针的指针 char *argv[]
fopen ,fscanf ,fprintf,fclose.是文件打开,读入,写入,关闭.
feof是判断文件是否读完.
详细的资料看相关的man manual.