-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyid.c
More file actions
44 lines (39 loc) · 1.14 KB
/
Copy pathmyid.c
File metadata and controls
44 lines (39 loc) · 1.14 KB
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
40
41
42
43
44
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_user_info(char *username) {
FILE *fp = fopen("/etc/passwd", "r");
if (fp == NULL) {
perror("Cannot open /etc/passwd");
return;
}
char line[256];
int found = 0;
while (fgets(line, sizeof(line), fp)) {
// /etc/passwd: username:password:UID:GID:info:home:shell
char *token = strtok(line, ":");
if (token != NULL && strcmp(token, username) == 0) {
found = 1;
char *pass = strtok(NULL, ":");
char *uid = strtok(NULL, ":");
char *gid = strtok(NULL, ":");
char *info = strtok(NULL, ":");
char *home = strtok(NULL, ":");
char *shell = strtok(NULL, ":");
printf("User: %s\n", username);
printf("UID: %s\n", uid);
printf("GID: %s\n", gid);
printf("Home: %s\n", home);
break;
}
}
if (!found) printf("User not found!\n");
fclose(fp);
}
int main() {
char username[50];
printf("Enter username: ");
scanf("%s", username);
get_user_info(username);
return 0;
}