2 Copyright (C) 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by jla; revised by djm */
21 name [state] line time [idle] host
23 name, line, time: not -q
27 -m Same as 'who am i', for POSIX.
28 -q Only user names and # logged on; overrides all other options.
29 -s Name, line, time (default).
30 -i, -u Idle hours and minutes; '.' means active in last minute;
31 'old' means idle for >24 hours.
32 -H Print column headings at top.
33 -w, -T -s plus mesg (+ or -, or ? if bad line). */
36 #include <sys/types.h>
41 #include <sys/param.h>
46 #ifdef _PATH_UTMP /* 4.4BSD. */
47 #define UTMP_FILE _PATH_UTMP
48 #else /* !_PATH_UTMP */
49 #define UTMP_FILE "/etc/utmp"
50 #endif /* !_PATH_UTMP */
51 #endif /* !UTMP_FILE */
53 #ifndef MAXHOSTNAMELEN
54 #define MAXHOSTNAMELEN 64
57 #define MESG_BIT 020 /* Group write bit. */
63 struct utmp *search_entries ();
67 void print_heading ();
73 /* The name this program was run with. */
76 /* If nonzero, display only a list of usernames and count of
78 Ignored for `who am i'. */
79 static int short_list;
81 /* If nonzero, display the hours:minutes since each user has touched
82 the keyboard, or "." if within the last minute, or "old" if
83 not within the last day. */
84 static int include_idle;
86 /* If nonzero, display a line at the top describing each field. */
87 static int include_heading;
89 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
90 or a `?' if their tty cannot be statted. */
91 static int include_mesg;
93 static struct option longopts[] =
95 {"count", 0, NULL, 'q'},
96 {"idle", 0, NULL, 'u'},
97 {"heading", 0, NULL, 'H'},
98 {"message", 0, NULL, 'T'},
99 {"mesg", 0, NULL, 'T'},
100 {"writable", 0, NULL, 'T'},
110 int my_line_only = 0;
112 program_name = argv[0];
114 while ((optc = getopt_long (argc, argv, "imqsuwHT", longopts, &longind))
150 error (1, errno, "cannot change directory to /dev");
152 switch (argc - optind)
156 who_am_i (UTMP_FILE);
161 case 1: /* who <utmp file> */
163 who_am_i (argv[optind]);
168 case 2: /* who <blurf> <glop> */
169 who_am_i (UTMP_FILE);
179 static struct utmp *utmp_contents;
181 /* Display a list of who is on the system, according to utmp file FILENAME. */
189 users = read_utmp (filename);
191 list_entries (users);
193 scan_entries (users);
196 /* Read the utmp file FILENAME into UTMP_CONTENTS and return the
197 number of entries it contains. */
204 struct stat file_stats;
206 desc = open (filename, O_RDONLY, 0);
208 error (1, errno, "%s", filename);
210 fstat (desc, &file_stats);
211 if (file_stats.st_size > 0)
212 utmp_contents = (struct utmp *) xmalloc ((unsigned) file_stats.st_size);
219 /* Use < instead of != in case the utmp just grew. */
220 if (read (desc, utmp_contents, file_stats.st_size) < file_stats.st_size)
221 error (1, errno, "%s", filename);
224 error (1, errno, "%s", filename);
226 return file_stats.st_size / sizeof (struct utmp);
229 /* Display a line of information about entry THIS. */
238 char line[sizeof (this->ut_line) + 1];
240 strncpy (line, this->ut_line, sizeof (this->ut_line));
241 line[sizeof (this->ut_line)] = 0;
242 if (stat (line, &stats) == 0)
244 mesg = (stats.st_mode & MESG_BIT) ? '+' : '-';
245 last_change = stats.st_atime;
254 sizeof (this->ut_name), sizeof (this->ut_name),
257 printf (" %c ", mesg);
259 sizeof (this->ut_line), sizeof (this->ut_line),
261 printf (" %-12.12s", ctime (&this->ut_time) + 4);
265 printf (" %s", idle_string (last_change));
270 if (this->ut_host[0])
271 printf (" (%-.*s)", sizeof (this->ut_host), this->ut_host);
277 /* Print the username of each valid entry and the number of valid entries
278 in `utmp_contents', which should have N elements. */
284 register struct utmp *this = utmp_contents;
285 register int entries = 0;
291 && this->ut_type == USER_PROCESS
295 printf ("%s ", this->ut_name);
300 printf ("\n# users=%u\n", entries);
308 printf ("%-*s ", sizeof (ut->ut_name), "USER");
311 printf ("%-*s ", sizeof (ut->ut_line), "LINE");
312 printf ("LOGIN-TIME ");
318 /* Display `utmp_contents', which should have N entries. */
324 register struct utmp *this = utmp_contents;
333 && this->ut_type == USER_PROCESS
341 /* Search `utmp_contents', which should have N entries, for
342 an entry with a `ut_line' field identical to LINE.
343 Return the first matching entry found, or NULL if there
344 is no matching entry. */
347 search_entries (n, line)
351 register struct utmp *this = utmp_contents;
357 && this->ut_type == USER_PROCESS
359 && !strncmp (line, this->ut_line, sizeof (this->ut_line)))
366 /* Display the entry in utmp file FILENAME for this tty on standard input,
367 or nothing if there is no entry for it. */
373 register struct utmp *utmp_entry;
374 char hostname[MAXHOSTNAMELEN + 1];
377 if (gethostname (hostname, MAXHOSTNAMELEN + 1))
382 printf ("%*s ", strlen (hostname), " ");
389 tty += 5; /* Remove "/dev/". */
391 utmp_entry = search_entries (read_utmp (filename), tty);
392 if (utmp_entry == NULL)
395 printf ("%s!", hostname);
396 print_entry (utmp_entry);
399 /* Return a string representing the time between WHEN and the time
400 that this function is first run. */
406 static time_t now = 0;
407 static char idle[10];
413 seconds_idle = now - when;
414 if (seconds_idle < 60) /* One minute. */
416 if (seconds_idle < (24 * 60 * 60)) /* One day. */
418 sprintf (idle, "%02d:%02d",
419 seconds_idle / (60 * 60),
420 (seconds_idle % (60 * 60)) / 60);
430 Usage: %s [-imqsuwHT] [--count] [--idle] [--heading] [--message] [--mesg]\n\
431 [--writable] [file] [am i]\n",