From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_USING_C/06.1.c | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 reference/C/CONTRIB/OR_USING_C/06.1.c (limited to 'reference/C/CONTRIB/OR_USING_C/06.1.c') diff --git a/reference/C/CONTRIB/OR_USING_C/06.1.c b/reference/C/CONTRIB/OR_USING_C/06.1.c new file mode 100644 index 0000000..2efc718 --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/06.1.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#define UTMP "/etc/utmp" +#define NAMELEN 8 + +main() +{ + FILE *fp; + struct utmp u; + struct passwd *p; + char tmp[NAMELEN+1]; + struct passwd *getpwnam(); + + if ((fp = fopen(UTMP, "r")) == NULL) { + perror(UTMP); + exit(1); + } + + /* + * For each entry... + */ + while (fread(&u, sizeof(u), 1, fp) != NULL) { + /* + * Skip non-logged in ports. + */ + if (u.ut_name[0] == NULL) + continue; + + /* + * Make sure name is null-terminated. + */ + strncpy(tmp, u.ut_name, NAMELEN); + + /* + * Skip non-existent users (shouldn't + * be any). + */ + if ((p = getpwnam(tmp)) == NULL) + continue; + + /* + * Print the line. ctime() converts the time + * to ASCII format, it is described in Chapter + * 7, Telling Time and Timing Things. We + * ignore the format of the gecos field and + * just print the first 30 characters; in real + * life we would stop at a comma or some such. + */ + printf("%-10.8s %-10.8s %-30.30s %s", u.ut_name, + u.ut_line, p->pw_gecos, ctime(&u.ut_time)); + } + + fclose(fp); + exit(0); +} + -- cgit v1.2.3-54-g00ecf