10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include "acctdef.h"
350Sstevel@tonic-gate #include <utmpx.h>
360Sstevel@tonic-gate #include <locale.h>
37*428Ssl108498 #include <stdlib.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate struct utmpx Ut;
400Sstevel@tonic-gate static char time_buf[50];
410Sstevel@tonic-gate
420Sstevel@tonic-gate static int inp(FILE *, struct utmpx *);
430Sstevel@tonic-gate
440Sstevel@tonic-gate int
main(int c,char ** v)450Sstevel@tonic-gate main(int c, char **v)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate
480Sstevel@tonic-gate int iflg, cflg;
490Sstevel@tonic-gate
500Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
510Sstevel@tonic-gate
520Sstevel@tonic-gate iflg = cflg = 0;
530Sstevel@tonic-gate
540Sstevel@tonic-gate while (--c > 0) {
550Sstevel@tonic-gate if (**++v == '-')
560Sstevel@tonic-gate while (*++*v)
570Sstevel@tonic-gate switch (**v) {
580Sstevel@tonic-gate case 'c':
590Sstevel@tonic-gate cflg++;
600Sstevel@tonic-gate continue;
610Sstevel@tonic-gate case 'i':
620Sstevel@tonic-gate iflg++;
630Sstevel@tonic-gate continue;
640Sstevel@tonic-gate }
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate for (;;) {
680Sstevel@tonic-gate if (iflg) {
690Sstevel@tonic-gate if (inp(stdin, &Ut) == EOF)
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate } else {
720Sstevel@tonic-gate if (fread(&Ut, sizeof (Ut), 1, stdin) != 1)
730Sstevel@tonic-gate break;
740Sstevel@tonic-gate }
750Sstevel@tonic-gate if (cflg)
760Sstevel@tonic-gate fwrite(&Ut, sizeof (Ut), 1, stdout);
770Sstevel@tonic-gate else {
780Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &Ut.ut_xtime);
790Sstevel@tonic-gate printf("%-*.*s %-4.4s %-*.*s %9d %2hd "
800Sstevel@tonic-gate "%4.4ho %4.4ho %ld %ld %d %hd %-.*s %s",
810Sstevel@tonic-gate NSZ,
820Sstevel@tonic-gate NSZ,
830Sstevel@tonic-gate Ut.ut_name,
840Sstevel@tonic-gate Ut.ut_id,
850Sstevel@tonic-gate LINESZ,
860Sstevel@tonic-gate LINESZ,
870Sstevel@tonic-gate Ut.ut_line,
880Sstevel@tonic-gate Ut.ut_pid,
890Sstevel@tonic-gate Ut.ut_type,
900Sstevel@tonic-gate Ut.ut_exit.e_termination,
910Sstevel@tonic-gate Ut.ut_exit.e_exit,
920Sstevel@tonic-gate Ut.ut_xtime,
930Sstevel@tonic-gate Ut.ut_tv.tv_usec,
940Sstevel@tonic-gate Ut.ut_session,
950Sstevel@tonic-gate Ut.ut_syslen,
960Sstevel@tonic-gate Ut.ut_syslen,
970Sstevel@tonic-gate Ut.ut_host,
980Sstevel@tonic-gate time_buf);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate exit(0);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static int
inp(FILE * file,struct utmpx * u)1050Sstevel@tonic-gate inp(FILE *file, struct utmpx *u)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate char buf[BUFSIZ];
1090Sstevel@tonic-gate char *p;
1100Sstevel@tonic-gate int i;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (fgets((p = buf), BUFSIZ, file) == NULL)
1130Sstevel@tonic-gate return (EOF);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate for (i = 0; i < NSZ; i++) /* Allow a space in name field */
1160Sstevel@tonic-gate u->ut_name[i] = *p++;
1170Sstevel@tonic-gate for (i = NSZ-1; i >= 0; i--) {
1180Sstevel@tonic-gate if (u->ut_name[i] == ' ')
1190Sstevel@tonic-gate u->ut_name[i] = '\0';
1200Sstevel@tonic-gate else
1210Sstevel@tonic-gate break;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate p++;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate for (i = 0; i < 4; i++)
1260Sstevel@tonic-gate if ((u->ut_id[i] = *p++) == ' ')
1270Sstevel@tonic-gate u->ut_id[i] = '\0';
1280Sstevel@tonic-gate p++;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate for (i = 0; i < LINESZ; i++) /* Allow a space in line field */
1310Sstevel@tonic-gate u->ut_line[i] = *p++;
1320Sstevel@tonic-gate for (i = LINESZ-1; i >= 0; i--) {
1330Sstevel@tonic-gate if (u->ut_line[i] == ' ')
1340Sstevel@tonic-gate u->ut_line[i] = '\0';
1350Sstevel@tonic-gate else
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate sscanf(p, "%d %hd %ho %ho %ld %ld %d %hd",
1400Sstevel@tonic-gate &u->ut_pid,
1410Sstevel@tonic-gate &u->ut_type,
1420Sstevel@tonic-gate &u->ut_exit.e_termination,
1430Sstevel@tonic-gate &u->ut_exit.e_exit,
1440Sstevel@tonic-gate &u->ut_xtime,
1450Sstevel@tonic-gate &u->ut_tv.tv_usec,
1460Sstevel@tonic-gate &u->ut_session,
1470Sstevel@tonic-gate &u->ut_syslen);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate if (u->ut_syslen > 1)
1500Sstevel@tonic-gate sscanf(p, "%*d %*hd %*ho %*ho %*ld %*ld %*d %*hd %s",
1510Sstevel@tonic-gate u->ut_host);
1520Sstevel@tonic-gate else
1530Sstevel@tonic-gate u->ut_host[0] = '\0';
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate return (1);
1560Sstevel@tonic-gate }
157