#ifndef __uid_t_defined
typedef __uid_t uid_t;
# define __uid_t_defined
#endif
# define __STD_TYPE typedef
#include <bits/typesizes.h> /* Defines __*_T_TYPE macros. */
__STD_TYPE __UID_T_TYPE __uid_t; /* Type of user identifications. */
#define __UID_T_TYPE __U32_TYPE
#define __S32_TYPE unsigned int
#include <sys/types.h>
#include <unistd.h>
uid_t getuid(void);
char *getlogin(void);
beyes:x:500:500:beyes:/home/beyes:/bin/bash
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
01 #include <sys/types.h>
02 #include <pwd.h>
03 #include <stdio.h>
04 #include <unistd.h>
05 #include <stdlib.h>
06
07
08 int main()
09 {
10 uid_t uid;
11 gid_t gid;
12 struct passwd *pw;
13
14 uid = getuid();
15 gid = getgid();
16
17 printf("User is %s\n", getlogin());
18
19 printf("User IDs: uid=%d, gid=%d\n", uid, gid);
20
21 pw = getpwuid(uid);
22
23 pw = getpwuid(uid);
24 printf("UID passwd entry:\n name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
25 pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
26
27
28 pw = getpwnam("root");
29 printf("root passwd entry:\n");
30
31 printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
32 pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
33
34 exit(0);
35 }
[root@localhost C]# ./user.exe
User is root
User IDs: uid=0, gid=0
UID passwd entry:
name=root, uid=0, gid=0, home=/root, shell=/bin/bash
root passwd entry:
name=root, uid=0, gid=0, home=/root, shell=/bin/bash
#include <pwd.h>
#include <sys/types.h>
void endpwent(void);
struct passwd *getpwent(void);
void setpwent(void);
#include <sys/types.h>
#include <unistd.h>
uid_t geteuid(void);
gid_t getgid(void);
gid_t getegid(void);
int setuid(uid_t uid);
int setgid(gid_t gid);
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |