toaruos/userspace/core/whoami.c

44 lines
838 B
C
Raw Normal View History

/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2014 Kevin Lange
*/
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* Who Am I?
*
*/
#include <unistd.h>
#include <stdio.h>
2012-01-29 08:27:37 +04:00
#include <string.h>
#include <stdlib.h>
#define LINE_LEN 4096
int main(int argc, char ** argv) {
FILE * passwd = fopen("/etc/passwd", "r");
char line[LINE_LEN];
int uid = 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;
}