[444] | 1 | #ifndef _NO_GETPWENT |
---|
| 2 | |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <sys/types.h> |
---|
| 5 | #include <pwd.h> |
---|
| 6 | #include <errno.h> |
---|
| 7 | #include <unistd.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | #include <string.h> |
---|
| 10 | #include <limits.h> |
---|
| 11 | |
---|
| 12 | static struct passwd pw_passwd; /* password structure */ |
---|
| 13 | static FILE *passwd_fp; |
---|
| 14 | |
---|
| 15 | static char logname[8]; |
---|
| 16 | static char password[1024]; |
---|
| 17 | static char gecos[1024]; |
---|
| 18 | static char dir[1024]; |
---|
| 19 | static char shell[1024]; |
---|
| 20 | |
---|
| 21 | struct passwd * |
---|
| 22 | getpwnam (name) |
---|
| 23 | const char *name; |
---|
| 24 | { |
---|
| 25 | FILE *fp; |
---|
| 26 | char buf[1024]; |
---|
| 27 | |
---|
| 28 | if ((fp = fopen ("/etc/passwd", "r")) == NULL) |
---|
| 29 | { |
---|
| 30 | return NULL; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | while (fgets (buf, sizeof (buf), fp)) |
---|
| 34 | { |
---|
| 35 | sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", |
---|
| 36 | logname, password, &pw_passwd.pw_uid, |
---|
| 37 | &pw_passwd.pw_gid, gecos, |
---|
| 38 | dir, shell); |
---|
| 39 | pw_passwd.pw_name = logname; |
---|
| 40 | pw_passwd.pw_passwd = password; |
---|
| 41 | pw_passwd.pw_comment = ""; |
---|
| 42 | pw_passwd.pw_gecos = gecos; |
---|
| 43 | pw_passwd.pw_dir = dir; |
---|
| 44 | pw_passwd.pw_shell = shell; |
---|
| 45 | |
---|
| 46 | if (!strcmp (logname, name)) |
---|
| 47 | { |
---|
| 48 | fclose (fp); |
---|
| 49 | return &pw_passwd; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | fclose (fp); |
---|
| 53 | return NULL; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | struct passwd * |
---|
| 57 | getpwuid (uid_t uid) |
---|
| 58 | { |
---|
| 59 | FILE *fp; |
---|
| 60 | char buf[1024]; |
---|
| 61 | |
---|
| 62 | if ((fp = fopen ("/etc/passwd", "r")) == NULL) |
---|
| 63 | { |
---|
| 64 | return NULL; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | while (fgets (buf, sizeof (buf), fp)) |
---|
| 68 | { |
---|
| 69 | sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", |
---|
| 70 | logname, password, &pw_passwd.pw_uid, |
---|
| 71 | &pw_passwd.pw_gid, gecos, |
---|
| 72 | dir, shell); |
---|
| 73 | pw_passwd.pw_name = logname; |
---|
| 74 | pw_passwd.pw_passwd = password; |
---|
| 75 | pw_passwd.pw_comment = ""; |
---|
| 76 | pw_passwd.pw_gecos = gecos; |
---|
| 77 | pw_passwd.pw_dir = dir; |
---|
| 78 | pw_passwd.pw_shell = shell; |
---|
| 79 | |
---|
| 80 | if (uid == pw_passwd.pw_uid) |
---|
| 81 | { |
---|
| 82 | fclose (fp); |
---|
| 83 | return &pw_passwd; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | fclose (fp); |
---|
| 87 | return NULL; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | struct passwd * |
---|
| 91 | getpwent () |
---|
| 92 | { |
---|
| 93 | char buf[1024]; |
---|
| 94 | |
---|
| 95 | if (passwd_fp == NULL) |
---|
| 96 | return NULL; |
---|
| 97 | |
---|
| 98 | if (fgets (buf, sizeof (buf), passwd_fp) == NULL) |
---|
| 99 | return NULL; |
---|
| 100 | |
---|
| 101 | sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", |
---|
| 102 | logname, password, &pw_passwd.pw_uid, |
---|
| 103 | &pw_passwd.pw_gid, gecos, |
---|
| 104 | dir, shell); |
---|
| 105 | pw_passwd.pw_name = logname; |
---|
| 106 | pw_passwd.pw_passwd = password; |
---|
| 107 | pw_passwd.pw_comment = ""; |
---|
| 108 | pw_passwd.pw_gecos = gecos; |
---|
| 109 | pw_passwd.pw_dir = dir; |
---|
| 110 | pw_passwd.pw_shell = shell; |
---|
| 111 | |
---|
| 112 | return &pw_passwd; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void |
---|
| 116 | setpwent () |
---|
| 117 | { |
---|
| 118 | if (passwd_fp != NULL) |
---|
| 119 | fclose (passwd_fp); |
---|
| 120 | |
---|
| 121 | passwd_fp = fopen ("/etc/passwd", "r"); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void |
---|
| 125 | endpwent () |
---|
| 126 | { |
---|
| 127 | if (passwd_fp != NULL) |
---|
| 128 | fclose (passwd_fp); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | #endif /* !_NO_GETPWENT */ |
---|