-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswd.c
More file actions
executable file
·39 lines (36 loc) · 805 Bytes
/
passwd.c
File metadata and controls
executable file
·39 lines (36 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* An implementation of getpwnam, using setpwent, getpwent, and endpwent.
*/
#include <stdio.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
struct passwd *mygetpwdnam(const char *name) {
struct passwd *pwd;
while ((pwd = getpwent()) != NULL) {
if (strcmp(pwd->pw_name, name) == 0) {
endpwent();
return pwd;
}
}
endpwent();
return NULL;
}
int main(int argc, char **argv) {
struct passwd *pwd;
printf("entry\n");
errno = 0;
pwd = mygetpwdnam("root");
printf("exit\n");
if (pwd != NULL) {
printf("found pwd entry! %s\n", pwd->pw_name);
printf("errno set to %ld\n", errno);
} else if (errno != 0) {
printf("null pwd, errno set! to %ld\n", errno);
exit(EXIT_FAILURE);
} else {
printf("pwd entry not found\n");
}
exit(EXIT_SUCCESS);
}