* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)who.c 5.2 (Berkeley) %G%";
#define NMAX sizeof(utmp.ut_name)
#define LMAX sizeof(utmp.ut_line)
#define HMAX sizeof(utmp.ut_host)
static struct utmp utmp
; /* read buffer */
register FILE *fp
; /* utmp file pointer */
register char *tp
, /* tty name */
*fname
; /* utmp file name */
struct passwd
*pw
, /* user passwd structure */
char hostname
[MAXHOSTNAMELEN
], /* host name */
if (!(tp
= ttyname(0))) {
* no tty -- use best guess from passwd file.
* next line is a kludge, but as of now getuid
* returns a "uid_t" and getpwuid takes an int.
pw
= getpwuid((int)getuid());
strncpy(utmp
.ut_name
,pw
? pw
->pw_name
: "?",NMAX
);
strcpy(utmp
.ut_line
,"tty??");
if (gethostname(hostname
,sizeof(hostname
)) == -1) {
fputs("usage: who [ utmp_file ]\nor who am i\n",stderr
);
if (!(fp
= fopen(fname
,"r"))) {
while (fread((char *)&utmp
,sizeof(utmp
),1,fp
) == 1)
if (!strcmp(utmp
.ut_line
,tp
)) {
else if (argc
!= 1 || *utmp
.ut_name
)
cbuf
= ctime(&utmp
.ut_time
) + 4;
printf("%-*.*s %-*.*s%.12s",NMAX
,NMAX
,utmp
.ut_name
,LMAX
,LMAX
,utmp
.ut_line
,cbuf
);
printf("\t(%.*s)",HMAX
,utmp
.ut_host
);