#include #include #include int main(int argc, char * argv[]) { if (argc != 2) { fprintf(stderr, "usage: a.out uid\n"); exit(1); } int wanted = atoi(argv[1]); FILE * f = fopen("/etc/passwd", "r"); if (f == NULL) { fprintf(stderr, "Can't open /etc/passwd\n"); exit(1); } while (1) { char line[1000]; char * s = fgets(line, 999, f); if (s == NULL) break; if (line[0] == '#') continue; char * uname = strtok(line, ":\n"); char * star = strtok(NULL, ":\n"); int uid = atoi(strtok(NULL, ":\n")); char * gid = strtok(NULL, ":\n"); char * name = strtok(NULL, ":\n"); char * home = strtok(NULL, ":\n"); char * shell = strtok(NULL, ":\n"); if (uid == wanted) { printf("%s '%s' %s\n", uname, name, shell); break; } } fclose(f); }