source: trunk/minix/commands/simple/who.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 1.4 KB
RevLine 
[9]1/* who 1.5 - tell who is currently logged in Author: Kees J. Bot
2 * 9 Jul 1989
3 */
4#define nil 0
5#include <sys/types.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <sys/stat.h>
10#include <utmp.h>
11#include <time.h>
12#include <string.h>
13#include <minix/paths.h>
14
15char PATH_UTMP[] = _PATH_UTMP;
16
17char day[] = "SunMonTueWedThuFriSat";
18char month[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
19
20int main(int argc, char **argv)
21{
22 char *tmp= PATH_UTMP;
23 FILE *f;
24 struct utmp ut;
25 struct tm *tm;
26 int slot, wtmp= 0, once= 0;
27
28 if (argc > 3) {
29 fprintf(stderr, "Usage: who <account-file> | who am i\n");
30 exit(1);
31 }
32 if (argc == 2) {
33 tmp= argv[1];
34 wtmp= 1;
35 }
36
37 if ((f= fopen(tmp, "r")) == nil) {
38 fprintf(stderr, "who: can't open %s\n", tmp);
39 exit(1);
40 }
41 if (argc == 3) {
42 if ((slot= ttyslot()) < 0) {
43 fprintf(stderr, "who: no access to terminal.\n");
44 exit(1);
45 }
46 fseek(f, (off_t) sizeof(ut) * slot, 0);
47 once= 1;
48 }
49
50 while (fread((char *) &ut, sizeof(ut), 1, f) == 1) {
51 if (!wtmp && ut.ut_name[0] == 0) continue;
52
53 tm= localtime(&ut.ut_time);
54
55 printf("%-9.8s %-9.8s %.3s %.3s %2d %02d:%02d",
56 ut.ut_name,
57 ut.ut_line,
58 day + (3 * tm->tm_wday),
59 month + (3 * tm->tm_mon),
60 tm->tm_mday,
61 tm->tm_hour,
62 tm->tm_min
63 );
64
65 if (ut.ut_host[0] != 0) printf(" (%.*s)",
66 (int) sizeof(ut.ut_host), ut.ut_host);
67
68 printf("\n");
69 if (once) break;
70 }
71 exit(0);
72}
Note: See TracBrowser for help on using the repository browser.