nwho - enhanced "who" program

sources-request at panda.UUCP sources-request at panda.UUCP
Wed Dec 25 00:45:52 AEST 1985


Mod.sources:  Volume 3, Issue 69
Submitted by: genrad!decvax!linus!rayssd!gmp (Gregory M. Paris)


The following shell archive is source and manual page for an enhanced
"who" program, called "nwho".  This program expands aliases for login
names, sorts alphabetically by name, and outputs in a space efficient
multi-column format.  Binary size is nearly the same as the standard
/bin/who, as is speed of execution.  It will compile and run on 2.9,
4.1, and 4.2 BSD UNIX systems.  It does not do "who am i" and it is
not intended to be used on "wtmp" files (it is not meant to be a
replacement for /bin/who).

++---------------------------------------------------------------------------++
||  Greg Paris             {allegra,linus,raybed2,ccice5,brunix}!rayssd!gmp  ||
++---------------------------------------------------------------------------++
---------------------------------cut here--------------------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	README
#	nwho.1
#	nwho.c
# This archive created: Tue Dec 24 09:43:29 1985
export PATH; PATH=/bin:$PATH
echo shar: extracting "'README'" '(1501 characters)'
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
cat << \SHAR_EOF > 'README'
This is an enhanced who program.  Features:
	1)  expands aliases for login names
	2)  sorts output alphabetically by name
	3)  space-efficient multi-column output
	4)  speed comparable to /bin/who
	5)  size comparable to /bin/who

To compile:
	This program is used regularly on 2.9, 4.1 and 4.2 BSD UNIX
	systems (PDP's, VAXen, SUN-2's, and Pyramid 90x's) here at rayssd.
	(We used to have a V7 system that an earlier version worked on, so
	I think that with "#define void int", it should work fine.)
	If you're on a 2.9 or 4.2 system, #define bsd42.  Else, if you're
	on a system that has a ut_host field in the utmp structure, you should
	#define hashosts.  If your system has tty id's that are other than 2
	letters long (the "xx" in "ttyxx"), you'll need to #define TTYLEN to
	that length.  You'll need getopt() to compile this program.  If you
	don't have termcap, you can take out the guts of termlen() or adapt
	the program to use terminfo (send me the diffs!).

	A good example is a vanilla Pyramid 90x system running Berkley init.

		cc -O -Dbsd42 -DTTYLEN=3 -o nwho nwho.c getopt.o -ltermcap

Problems:
	If you have any problems, I'd like to know about them.  If you get
	this program running on any type of system not mentioned above, I'd be
	interested in the diffs to make it work.  If you have any useful
	enhancements, I'd like to know about them too.

		Greg Paris          { allegra, linus, raybed2 }!rayssd!gmp
		PO Box 360/MS 330
		Portsmouth, RI  02871
		(401) 847-8000 ext. 3839

SHAR_EOF
if test 1501 -ne "`wc -c < 'README'`"
then
	echo shar: error transmitting "'README'" '(should have been 1501 characters)'
fi
fi
echo shar: extracting "'nwho.1'" '(3269 characters)'
if test -f 'nwho.1'
then
	echo shar: will not over-write existing file "'nwho.1'"
else
cat << \SHAR_EOF > 'nwho.1'
.TH NWHO 1  "Raytheon SSD SGF"
.SH NAME
nwho \- column sorted, aliased list of who's logged-on
.SH SYNOPSIS
.B nwho
.B [ -qht ]
.B [ -l length ]
.B [ -c columns ]
.B [ -a file ]
.SH DESCRIPTION
The
.I nwho
program is an updated version of the standard
.IR who (1)
program.
Enhancements are multi-column formatting,
login-alias expansion,
and output in alphabetical order by name,
rather than terminal order.
The program's output format is more economical
as it does not print the (usually redundant) date
and "tty" strings of
.IR who .
.PP
For each user logged-in,
.I nwho
produces an output entry with format
.nf
.in +4
.sp 1
.B tt hh:mm name
.sp 1
.in -4
.fi
where
.I tt
is the last two letters of the login tty
("co" if it's the console),
and
.IR hh : mm
is the login time
(24-hour format, no leading zero),
and
.I name
is either the login name of the user,
or an alias for that login name.
By default, output is multi-column.
The number of columns is the maximum which
will allow equal-width columns,
as wide as the widest output entry,
to fit within the output line length (default 79 characters).
If any output entries are longer than the line length,
the output will be single-column, and those entries will
be truncated.
Sorting is done down columns
rather than across rows.
.PP
Aliases for login names are specified for the
.I nwho
program in a ".nwhorc" file in the invoker's home directory
(as named by the "HOME" environment variable).
If the file exists,
.I nwho
will replace matched login names with their respective aliases.
The file consists of one entry per line, each line
consisting of a login name,
followed by white space (spaces and tabs),
followed by the alias for the login name.
The alias is terminated by the end of line,
therefore may contain embedded white space.
The contents of the .nwhorc file
.I must
.I be
.I sorted
.I alphabetically.
Alias expansion will not be done properly if the file is not sorted.
.PP
Options to
.I nwho
are as listed below.
(This program uses
.IR getopt(3)
to parse its arguments.)
.nf
.in +4
.sp 1
.ta 1.6i
-a file	use "file" for aliases instead of .nwhorc
-c columns	use "columns" as maximum number of columns
-h	output remote host field (if supported)
-l length	use "length" as line length, or
-l t	look up line length in termcap using $TERM, or
-l t=term	look up line length in termcap using "term"
-q	quick -- don't do alias expansion
-t	output in terminal order (don't alphabetize)
.sp 1
.in -4
.fi
.SH "DIAGNOSTICS"
If the line length is smaller than that needed to print
the terminal id, the time, and one character of the name,
.I nwho
will complain then exit.
.sp 1
Other diagnostics are self-explanatory.
.sp 1
Exits with status 0 if all goes well,
1 on a user-generated error (e.g., bad option),
and 2 on internal error.
.SH "BUGS"
Doesn't complain if .nwhorc is not sorted.
.sp 1
Avoids problems with premature line wrapping by using
a default line length of 79 rather than 80.
If the -lt option is used,
.I nwho
will use a line length of one less than that listed
in termcap if the terminal wraps (automatic margins).
This cheats many terminals out of one space per line.
.SH "AUTHOR"
Greg Paris, Raytheon Submarine Signal Div., Portsmouth, RI
.SH "SEE ALSO"
who(1), getopt(3), termcap(5), utmp(5)
SHAR_EOF
if test 3269 -ne "`wc -c < 'nwho.1'`"
then
	echo shar: error transmitting "'nwho.1'" '(should have been 3269 characters)'
fi
fi
echo shar: extracting "'nwho.c'" '(17037 characters)'
if test -f 'nwho.c'
then
	echo shar: will not over-write existing file "'nwho.c'"
else
cat << \SHAR_EOF > 'nwho.c'
/* vi:set sw=4 ts=4: */
#ifndef	lint
static char	   *sccsid = "@(#)nwho.c	3.12 rayssd!gmp 12/22/85";
#endif	lint
/*
**	nwho.c (version 3.12): written by
**		Greg Paris
**		Raytheon Submarine Signal Division
**		Portsmouth, RI  02871
*/

/*
**	NOTE: correct operation of this program depends
**		on the alias file being sorted by login name.
**
**	NOTE: the "-u utmp" option is undocumented.  This
**		program is quite memory intensive -- great for
**		/etc/utmp use, but a real hog when used on large
**		/usr/adm/wtmp files.
*/

#include	<sys/types.h>
#include	<utmp.h>
#include	<sys/stat.h>

#ifdef	bsd42
#define		hashosts
#include	<sys/time.h>
#else	bsd42
#include	<time.h>
#include	<sys/timeb.h>
#endif	bsd42

#ifdef	pyr
/*
**	compile in ucb universe
**	works with ucb init being run
*/
char		utmpfl[]	= "/etc/.ucbutmp";		/* for att users */
#else	pyr
char		utmpfl[]	= "/etc/utmp";			/* default utmp file */
#endif	pyr

char		aliasfl[]	= ".nwhorc";			/* default alias file */

char		*progname;							/* program name */
int			proglen;							/* program name length */
char		*requtmpfl;							/* requested utmp file */
char		*reqaliasfl;						/* requested alias file */

extern int	optind;
extern char	*optarg;
extern int	getopt();
#ifndef	EOF
#define		EOF		-1
#endif	EOF

int			logsort(), alsort(), termsort();
char		*strcpy(), *strncpy(), *index(), *timefmt();
char		*getenv(), *malloc(), *getaliases(), *logalias();
long		gmtoffset();

/*
**	plus: structure for aliasing login names
*/
struct plus {
	struct utmp		*pu;			/* pointer to utmp entry */
	char			*al;			/* pointer to alias */
};

struct utmp	dumutmp;							/* for SZ defines */

#ifndef	TTYLEN
#define	TTYLEN		2							/* assume ttyxx */
#endif	TTYLEN
#define	MINENTLEN	(TTYLEN+7)					/* space for term and time */
#define	MINSPACES	2							/* min between columns */
#define	WHITESPACE	case ' ': case '\t'			/* white space in alias file */
#define	DEFLINELEN	79							/* avoids premature wrapping */

#define	SZ_UTMP		sizeof(struct utmp)
#define	SZ_PLUS		sizeof(struct plus)
#define	SZ_UTNAME	sizeof(dumutmp.ut_name)

#ifdef	hashosts
#define	SZ_UTHOST	sizeof(dumutmp.ut_host)
#endif	hashosts

/*
**	add characters to output buffer
*/
#define	bufadd(str)	for(fr = str;*fr;*pb++ = *fr++)
#define	bufaddn(st, nn)	for(fr = st, i = nn;*fr && i--;*pb++ = *fr++)

/*
**	add TTYLEN-letter abbreviation for terminal name
**	loses if non-tty devices named t* exist
*/
#define	buftty(tty)	if(tty[0] == 't') bufadd(tty+3); \
		else bufaddn(tty, TTYLEN)

/*
**	add " hh:mm "
*/
#define	buftime(tm)	bufadd(timefmt(tm -= offset))

/*
**	add alias or login name
*/
#define	bufname(ppp)	\
		if(ppp->al) bufadd(ppp->al); \
		else bufaddn(ppp->pu->ut_name, SZ_UTNAME)

#ifdef	hashosts
/*
**	add " (hostname)"
*/
#define	bufhost(ppp)	\
		if(hosts && ppp->pu->ut_host[0]) { \
			*pb++ = ' '; *pb++ = '('; \
			bufaddn(ppp->pu->ut_host, SZ_UTHOST); \
			*pb++ = ')'; }
#endif	hashosts

#ifdef	NOERRMSGS
#define	errexit(code,msg)	exit(code)
#else	NOERRMSGS
#define	errexit(code,msg)	\
		static char errmsg[] = msg; \
		(void) write(2, progname, proglen); \
		(void) write(2, errmsg, sizeof(errmsg) - 1); \
		exit(code)
#endif	NOERRMSGS
		
main(ac, av)
int		ac;
char	*av[];
{
	register struct plus	*pp, *ppp;
	register int			entries;
	register char			*pb, *fr;
	register int			i;
	struct plus				*ppmax;
	struct utmp				*pu, *puu;
	int						size;
	int						linelen, entrylen, spaces, cols;
	int						termord, quick, colreq;
	char					*linebuf, *aliasbuf, *entryend;
	long					offset;
	int						hosts;

	proglen = strlen(progname = av[0]);
	requtmpfl = reqaliasfl = (char *) 0;
	hosts = termord = colreq = linelen = quick = 0;

	while((i = getopt(ac, av, "hqta:l:c:u:")) != EOF) {
		switch(i) {
		case 'h':	/* show remote host field */
			hosts = 1;
			break;
		case 'a':	/* alias file request */
			reqaliasfl = optarg;
			break;
		case 'u':	/* utmp file request */
			requtmpfl = optarg;
			break;
		case 'q':	/* don't expand */
			quick = 1;
			break;
		case 'l':	/* line length */
			if(*optarg == 't') {
				linelen = termlen(optarg);
			} else if((linelen = atoi(optarg)) < MINENTLEN + 2) {
				errexit(1, ": improper line length\n");
			}
			break;
		case 'c':	/* set columns */
			if((colreq = atoi(optarg)) < 1) {
				errexit(1, ": improper column count\n");
			}
			break;
		case 't':	/* terminal order output */
			termord = 1;
			break;
		case '?':	/* bad option */
		default:	/* error */
			usage();
		}
	}

	offset = gmtoffset();
	linelen = linelen ? linelen : DEFLINELEN;

	if((i = open(requtmpfl ? requtmpfl : utmpfl, 0)) < 0) {
		errexit(1, ": can't open utmp file\n");
	}

	if((entries = (size = fdsize(i))/SZ_UTMP) == 0) {
		/*
		**	no users
		*/
		exit(0);
	}
	if(size != entries * SZ_UTMP) {
		errexit(2, ": bad utmp file\n");
	}

	puu = pu = (struct utmp *) malloc((unsigned) size);
	ppp = pp = (struct plus *) malloc((unsigned) (entries * SZ_PLUS));
	if(!pu || !pp) {
		errexit(2, ": no memory for utmp entries\n");
	}
	if(read(i, (char *) pu, size) != size) {
		errexit(2, ": read error on utmp file\n");
	}
	(void) close(i);

	/*
	**	initialize plus entries, ignoring null utmp entries, then sort
	*/
	puu += entries;						/* start at end */
	entries = 0;						/* forget... */
	while(puu-- > pu) {					/* easier to go backwards */
		if(puu->ut_name[0]) {			/* active entry */
			ppp->al = (char *) 0;		/* no alias yet */
			(ppp++)->pu = puu;			/* set utmp pointer */
			++entries;					/* active count */
		}
	}
	ppmax = ppp;						/* at end */

	if(entries == 0) {
		/*
		**	there were inactive utmp entries,
		**	but no active entries (yes, it happens)
		*/
		exit(0);
	}

	if(!quick || !termord) {
		/*
		**	sort by login necessary for alias expansion
		**	default if terminal order not requested
		*/
		qsort((char *) pp, entries, SZ_PLUS, logsort);
	}

	/*
	**	if alias file, expand aliases
	*/
	if(!quick && (aliasbuf = getaliases())) {
		/*
		**	expand aliases for login names
		**	found in the alias file
		*/
		expand(pp, ppmax, aliasbuf);

		if(termord) {
			/*
			**	put back in terminal order
			*/
			qsort((char *) pp, entries, SZ_PLUS, termsort);
		} else {
			/*
			**	sort by name to be printed
			**	(alias if one, login otherwise)
			*/
			qsort((char *) pp, entries, SZ_PLUS, alsort);
		}
	}

	/*
	**	take extreme pains to format the output
	**		variable number of columns
	**		ragged right (Raytheon standard)
	**		uses as much of line as possible
	**		minimum character output (no tabs)
	**		at least MINSPACES spaces between columns
	**		sorted alphabetically by user name down columns
	**		single write
	*/
	entrylen = 0;
	for(ppp = pp;ppp < ppmax;++ppp) {
		if(ppp->al) {
			/*
			**	check alias length
			*/
			i = strlen(ppp->al);
		} else {
			/*
			**	check login length
			*/
			pb = ppp->pu->ut_name;
			for(i = 0;*pb++ && i < SZ_UTNAME;++i)
				;
		}

#ifdef	hashosts
		/*
		**	add room for remote host
		*/
		if(hosts && ppp->pu->ut_host[0]) {
			size = i + 3;	/* room for parens and space */
			pb = ppp->pu->ut_host;
			for(i = 0;*pb++ && i < SZ_UTHOST;++i)
				;
			i += size;
		}
#endif	hashosts

		entrylen = (entrylen < i) ? i : entrylen;
	}

	if(colreq == 1 || entries == 1) {
		/*
		**	single-column is always valid
		*/
		cols = 1;
	} else {
		/*
		**	here's the basic equation for formatting
		**		linelen = (cols - 1) * (entrylen + spaces) + entrylen
		**	solve for columns using
		**		entrylen = maxnamelen + MINENTLEN
		**		spaces = MINSPACES
		**	then solve for entrylen with
		**		spaces = MINSPACES
		**	then solve for spaces
		*/
		spaces = MINSPACES;
		entrylen += MINENTLEN;

		if(linelen <= entrylen) {
			cols = 1;
		} else {
			cols = 1 + (linelen - entrylen) / (entrylen + spaces);
		}

		/*
		**	if there is a column request
		**	check it's validity
		*/
		if(colreq && cols > colreq) {
			/*
			**	use number of columns requested 
			*/
			cols = colreq;
		}
	}

	if(cols == 1) {
		/*
		**	simpler processing for single-column mode
		*/
		if(!(pb = linebuf = malloc((unsigned)(entries * entrylen)))) {
			errexit(2, ": no memory for output buffer\n");
		}

		for(ppp = pp;ppp < ppmax;++ppp) {
			entryend = pb + linelen;

			buftty(ppp->pu->ut_line);
			buftime(ppp->pu->ut_time);
			bufname(ppp);
#ifdef	hashosts
			bufhost(ppp);
#endif	hashosts

			/*
			**	truncate if necessary, add newline
			*/
			pb = (pb >= entryend) ? (entryend - 1) : pb;
			*pb++ = '\n';
		}
	} else {
		int		lines, linenum, colnum, lastcol, maxcols;

		/*
		**	multi-column mode
		**	(can't have more columns than entries)
		*/
		cols = (cols > entries) ? entries : cols;
		entrylen = (linelen - (cols - 1) * spaces) / cols;
		spaces = (linelen - cols * entrylen) / (cols - 1);
		lines = entries / cols;
		if(lastcol = entries % cols) {
			++lines;
		} else {
			lastcol = cols;
		}

		if(!(pb = linebuf = malloc((unsigned) (lines * linelen)))) {
			errexit(2, ": no memory for output buffer\n");
		}

		maxcols = cols;
		for(linenum = 0;linenum < lines;) {
			ppp = pp + linenum;
			for(colnum = 0;;) {
				entryend = pb + spaces + entrylen;

				buftty(ppp->pu->ut_line);
				buftime(ppp->pu->ut_time);
				bufname(ppp);
#ifdef	hashosts
				bufhost(ppp);
#endif	hashosts

				if(++colnum < maxcols) {
					ppp += (colnum <= lastcol) ? lines : (lines - 1);
					while(pb < entryend) {
						*pb++ = ' ';
					}
					continue;
				}
				break;
			}

			*pb++ = '\n';
			if(++linenum == lines - 1) {
				maxcols = lastcol;
			}
		}
	}

	size = (int) (pb - linebuf);
	if(write(1, linebuf, size) != size) {
		errexit(2, ": output write error\n");
	}

	exit(0);
}

/*
**	logsort: sort plus entries by login first, time second
*/
logsort(pp0, pp1)
struct plus	   *pp0;
struct plus	   *pp1;
{
	int			val;

	/*
	**	string compare
	*/
	if(val = strncmp(pp0->pu->ut_name, pp1->pu->ut_name, SZ_UTNAME)) {
		return(val);
	}

	/*
	**	chronological
	*/
	return((int) (pp0->pu->ut_time - pp1->pu->ut_time));
}

/*
**	alsort: sort plus entries by alias or ut_name first, time second
*/
alsort(pp0, pp1)
struct plus	   *pp0;
struct plus	   *pp1;
{
	int			val;

	/*
	**	string compare
	*/
	if(pp0->al && pp1->al) {
		/*
		**	both have aliases -- easy
		*/
		if(val = strcmp(pp0->al, pp1->al)) {
			return(val);
		}
	} else {
		/*
		**	one or both missing alias
		*/
		if(val = strncmp(
			(pp0->al ? pp0->al : pp0->pu->ut_name),			/* arg0 */
			(pp1->al ? pp1->al : pp1->pu->ut_name),			/* arg1 */
			SZ_UTNAME)
		) {
			return(val);
		}

		/*
		**	different lengths?
		**	assume one with alias is longer
		**	(fast but possibly incorrect?)
		*/
		if(val = (int) (pp0->al - pp1->al)) {
			return(val);
		}
	}

	/*
	**	chronological
	*/
	return((int) (pp0->pu->ut_time - pp1->pu->ut_time));
}

/*
**	termsort: sort plus entries by terminal order
*/
termsort(pp0, pp1)
struct plus	   *pp0;
struct plus	   *pp1;
{
	return((int)(pp0->pu - pp1->pu));
}

/*
**	getaliases: read alias file into dynamic buffer
*/
char *
getaliases()
{
	char	   *s;
	char	   *b;
	int			fd;
	int			size;

	if(reqaliasfl) {
		/*
		**	open requested alias file -- must be readable
		*/
		if((fd = open(reqaliasfl, 0)) < 0) {
			errexit(1, ": can't open alias file\n");
		}
	} else {
		/*
		**	try to open default alias file
		*/
		if(!(b = getenv("HOME"))) {
			return((char *) 0);
		}

		if(!(s = malloc(
			(unsigned) ((size = strlen(b)) + 1 + sizeof(aliasfl))))
			) {
			errexit(2, ": no memory for path name expansion\n");
		}

		(void) strcpy(s, b);
		*(s+size++) = '/';
		(void) strcpy(s+size, aliasfl);

		if((fd = open(s, 0)) < 0) {
			/*
			**	can't open for read -- not error
			*/
			free(s);
			return((char *) 0);
		}
		free(s);
	}

	if((size = fdsize(fd)) < 4) {
		/*
		**	smaller than smallest possible alias -- ignore
		*/
		(void) close(fd);
		return((char *) 0);
	}

	/*
	**	get null-terminated buffer
	*/
	if(!(b = malloc((unsigned) (size + 1)))) {
		errexit(2, ": no memory for alias buffer\n");
	}
	*(b+size) = '\0';

	if(read(fd, b, size) != size) {
		errexit(2, ": read error on alias file\n");
	}
	(void) close(fd);

	return(b);
}

/*
**	fdsize: return size of file corresponding to fd
*/
fdsize(fd)
int		fd;
{
	struct stat		st;

	if(fstat(fd, &st) < 0) {
		errexit(2, ": fstat failed in fdsize\n");
	}

	return((int) st.st_size);
}

/*
**	expand: expand alias names
*/
expand(ppp, ppmax, buf)
register struct plus	*ppp;
register struct plus	*ppmax;
register char			*buf;
{
	register int	next;
	register int	cmp;
	char			*login;
	char			*alias;

	next = 1;
	while(ppp < ppmax) {
		if(next) {
			if(!(buf = logalias(buf, &login, &alias))) {
				/*
				**	no more list entries
				*/
				return;
			}
		}

		if((cmp = strncmp(login, ppp->pu->ut_name, SZ_UTNAME)) < 0) {
			/*
			**	no match, keep utmp entry, continue in list
			*/
			next = 1;
			continue;
		}

		if(cmp > 0) {
			/*
			**	no match, next utmp entry, too far in list
			*/
			++ppp;
			next = 0;
			continue;
		}

		/*
		**	matched, next utmp entry, try to match this list entry again
		*/
		(ppp++)->al = alias;
		next = 0;
	}
}

/*
**	logalias: get pointer to login and alias
**		return NULL if end of buffer, new position otherwise
**		buf must point to first character in line
*/
char *
logalias(buf, plogin, palias)
register char	*buf;
char			**plogin;
char			**palias;
{
	register int	inword;

	inword = 0;
	while(!inword) {					/* find beginning of login */
		switch(*buf) {
		case '\r':						/* empty lines -- OK */
		case '\n':						/* empty lines -- OK */
		WHITESPACE:						/* white space at beginning */
			++buf;
			break;
		case '\0':						/* end of buffer */
			return((char *) 0);
		default:						/* found login */
			inword = 1;
			*plogin = buf;				/* save login */
			break;
		}
	}

	while(inword) {						/* find end of login */
		switch(*buf) {
		WHITESPACE:						/* end of login */
			inword = 0;
			*buf++ = '\0';				/* null terminate */
			break;
		case '\0':						/* end of buffer */
			return((char *) 0);
		default:						/* still in login */
			++buf;
			break;
		}
	}

	while(!inword) {					/* find beginning of alias */
		switch(*buf) {
		WHITESPACE:						/* still in white space */
			++buf;
			break;
		case '\0':						/* end of buffer */
			return((char *) 0);
		default:						/* beginning of alias */
			inword = 1;
			*palias = buf;				/* save alias */
			break;
		}
	}

	while(inword) {						/* find end of alias */
		switch(*buf) {
		case '\r':						/* end of line */
		case '\n':						/* end of line */
			inword = 0;					/* end of alias */
			*buf++ = '\0';				/* null terminate */
			break;
		case '\0':						/* end of buffer */
			inword = 0;					/* end of alias */
			break;						/* leave buffer here (OK) */
		default:						/* still in alias */
			if(*buf < ' ' || *buf > '~') {	/* ASCII */
				/*
				**	replace control characters
				**	(they screw up output format)
				*/
				*buf = ' ';				/* replace with space */
			}
			++buf;
			break;
		}
	}

	return(buf);						/* return buffer position */
}

/*
**	usage: print a usage message and exit
*/
usage()
{
	static char	preamble[]		= "usage -- ";
	static char	postamble[]		=
		" [-q] [-t] [-c columns] [-l length] [-a aliasfl]\n";

#ifdef	hashosts
	static char	hostamble[]	= " [-h]";
#endif	hashosts

	(void) write(2, preamble, sizeof(preamble) - 1);
	(void) write(2, progname, proglen);
#ifdef	hashosts
	(void) write(2, hostamble, sizeof(hostamble) - 1);
#endif	hashosts
	(void) write(2, postamble, sizeof(postamble) - 1);

	exit(1);
}

/*
**	gmtoffset: get seconds offset from gmt
*/
long
gmtoffset()
{
#ifdef	bsd42

	struct timeval		tv;
	struct timezone		tz;
	struct tm		   *pt;
	struct tm		   *localtime();

	(void) gettimeofday(&tv, &tz);
	pt = localtime((time_t *) &tv.tv_sec);

	return(tz.tz_minuteswest * 60 - (pt->tm_isdst ? 3600 : 0));

#else	bsd42

	struct timeb		tb;
	struct tm		   *pt;
	struct tm		   *localtime();

	(void) ftime(&tb);
	pt = localtime(&tb.time);

	return((long) tb.timezone * 60 - (pt->tm_isdst ? 3600 : 0));

#endif	bsd42
}

/*
**	timefmt: format the time string
**		this routine is ASCII dependent
*/
char *
timefmt(tim)
long	tim;
{
	static char	tbf[8];
	int			t;
	int			tens;

	(void) strcpy(tbf, " 00:00 ");			/* initialize */

	if((t = (tim / 3600) % 24) > 9) {		/* hours */
		tbf[1] = (tens = t / 10) + '0';
		tbf[2] = t - (tens * 10) + '0';		/* mult faster than mod */
	} else {
		tbf[1] = ' ';						/* leading space */
		tbf[2] = t + '0';
	}

	if((t = (tim / 60) % 60) > 9) {			/* minutes */
		tbf[4] = (tens = t / 10) + '0';
		tbf[5] = t - (tens * 10) + '0';		/* mult faster than mod */
	} else {
		tbf[4] = '0';						/* leading zero */
		tbf[5] = t + '0';
	}

	return(tbf);
}

/*
**	termlen: get line length from termcap
**		if arg contains '=' use following as
**		terminal name, else use $TERM
*/
termlen(arg)
char   *arg;
{
	char   *name;
	char	tbuf[1024];
	int		len;

	if((name = index(arg, '=')) == (char *)0) {
		name = getenv("TERM");
	} else {
		++name;
	}

	if(name != (char *)0) {
		if(tgetent(tbuf, name) == 1) {
			if((len = tgetnum("co")) > 0) {
				return(tgetflag("am") ? len - 1 : len);		/* avoid wrap */
			}
		}
	}

	return(DEFLINELEN);
}
SHAR_EOF
if test 17037 -ne "`wc -c < 'nwho.c'`"
then
	echo shar: error transmitting "'nwho.c'" '(should have been 17037 characters)'
fi
fi
exit 0
#	End of shell archive



More information about the Mod.sources mailing list