C语言fgetgrent()函数:读取组格式函数
头文件:
1
|
#include <grp.h> #include <stdio.h> #include <sys/types.h> |
定义函数:
1
|
struct group * getgrent( FILE * stream); |
函数说明:fgetgrent()会从参数stream 指定的文件读取一行数据, 然后以group 结构将该数据返回. 参数stream 所指定的文件必须和、etc/group 相同的格式. group 结构定义请参考getgrent().
返回值:返回 group 结构数据, 如果返回NULL 则表示已无数据, 或有错误发生.
范例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <grp.h> #include <sys/types.h> #include <stdio.h> main() { struct group *data; FILE *stream; int i; stream = fopen ( "/etc/group" , "r" ); while ((data = fgetgrent(stream)) != 0) { i = 0; printf ( "%s :%s:%d :" , data->gr_name, data->gr_passwd, data->gr_gid); while (data->gr_mem[i]) printf ( "%s, " , data->gr_mem[i++]); printf ( "\n" ); } fclose (stream); } |
执行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
root:x:0:root, bin:x:1:root, bin, daemon daemon:x:2:root, bin, daemon sys:x:3:root, bin, adm adm:x:4:root, adm, daemon tty:x:5 disk:x:6:root lp:x:7:daemon, lp mem:x:8 kmem:x:9 wheel:x:10:root mail:x:12:mail news:x:13:news uucp:x:14:uucp man:x:15 games:x:20 gopher:x:30 dip:x:40: ftp:x:50 nobody:x:99: |
C语言fgetpwent()函数:读取密码格式
头文件:
1
|
#include <pwd.h> #include <stdio.h> #include <sys/types.h> |
定义函数:
1
|
struct passwd * fgetpwent( FILE * stream); |
函数说明:fgetpwent()会从参数stream 指定的文件读取一行数据, 然后以passwd 结构将该数据返回. 参数stream 所指定的文件必须和/etc/passwd 相同的格式. passwd 结构定义请参考getpwent().
返回值:返回 passwd 结构数据, 如果返回NULL 则表示已无数据, 或有错误发生.
范例
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <pwd.h> #include <sys/types.h> main() { struct passwd *user; FILE *stream; stream = fopen ( "/etc/passwd" , "r" ); while ((user = fgetpwent(stream)) != 0) { printf ( "%s:%d:%d:%s:%s:%s\n" , user->pw_name, user->pw_uid, user->pw_gid, user->pw_gecos, user->pw_dir, user->pw_shell); } } |
执行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
root:0:0:root:/root:/bin/bash bin:1:1:bin:/bin: daemon:2:2:daemon:/sbin: adm:3:4:adm:/var/adm: lp:4:7:lp:/var/spool/lpd: sync:5:0:sync:/sbin:/bin/sync shutdown:6:0:shutdown:/sbin:/sbin/shutdown halt:7:0:halt:/sbin:/sbin/halt mail:8:12:mail:/var/spool/mail: news:9:13:news:var/spool/news uucp:10:14:uucp:/var/spool/uucp: operator:11:0:operator :/root: games:12:100:games:/usr/games: gopher:13:30:gopher:/usr/lib/gopher-data: ftp:14:50:FTP User:/home/ftp: nobody:99:99:Nobody:/: xfs:100:101:X Font Server: /etc/Xll/fs:/bin/false gdm:42:42:/home/gdm:/bin/bash kids:500:500: : /home/kids:/bin/bash |