toaruos/userspace/whoami.c

42 lines
703 B
C
Raw Normal View History

/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* Who Am I?
*
*/
#include <stdio.h>
#include <syscall.h>
2012-01-29 08:27:37 +04:00
#include <string.h>
#include <stdlib.h>
DEFN_SYSCALL0(getuid, 23);
#define LINE_LEN 4096
int main(int argc, char ** argv) {
FILE * passwd = fopen("/etc/passwd", "r");
char line[LINE_LEN];
int uid = syscall_getuid();
while (fgets(line, LINE_LEN, passwd) != NULL) {
line[strlen(line)-1] = '\0';
char *p, *tokens[10], *last;
int i = 0;
for ((p = strtok_r(line, ":", &last)); p;
(p = strtok_r(NULL, ":", &last)), i++) {
if (i < 511) tokens[i] = p;
}
tokens[i] = NULL;
if (atoi(tokens[2]) == uid) {
printf("%s\n", tokens[0]);
}
}
fclose(passwd);
return 0;
}