xref: /csrg-svn/usr.bin/finger/util.c (revision 37660)
1*37660Sbostic /*
2*37660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
3*37660Sbostic  * All rights reserved.
4*37660Sbostic  *
5*37660Sbostic  * This code is derived from software contributed to Berkeley by
6*37660Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7*37660Sbostic  *
8*37660Sbostic  * Redistribution and use in source and binary forms are permitted
9*37660Sbostic  * provided that the above copyright notice and this paragraph are
10*37660Sbostic  * duplicated in all such forms and that any documentation,
11*37660Sbostic  * advertising materials, and other materials related to such
12*37660Sbostic  * distribution and use acknowledge that the software was developed
13*37660Sbostic  * by the University of California, Berkeley.  The name of the
14*37660Sbostic  * University may not be used to endorse or promote products derived
15*37660Sbostic  * from this software without specific prior written permission.
16*37660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*37660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*37660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*37660Sbostic  */
20*37660Sbostic 
21*37660Sbostic #ifndef lint
22*37660Sbostic static char sccsid[] = "@(#)util.c	5.1 (Berkeley) 05/06/89";
23*37660Sbostic #endif /* not lint */
24*37660Sbostic 
25*37660Sbostic #include <sys/param.h>
26*37660Sbostic #include <sys/stat.h>
27*37660Sbostic #include <sys/file.h>
28*37660Sbostic #include <stdio.h>
29*37660Sbostic #include <ctype.h>
30*37660Sbostic #include <strings.h>
31*37660Sbostic #include "finger.h"
32*37660Sbostic #include "pathnames.h"
33*37660Sbostic 
34*37660Sbostic find_idle_and_ttywrite(pn)
35*37660Sbostic 	register PERSON *pn;
36*37660Sbostic {
37*37660Sbostic 	extern time_t now;
38*37660Sbostic 	extern int errno;
39*37660Sbostic 	struct stat sb;
40*37660Sbostic 	char *strerror();
41*37660Sbostic 
42*37660Sbostic 	(void)sprintf(tbuf, "%s/%s", _PATH_DEV, pn->tty);
43*37660Sbostic 	if (stat(tbuf, &sb) < 0) {
44*37660Sbostic 		(void)fprintf(stderr,
45*37660Sbostic 		    "finger: %s: %s\n", tbuf, strerror(errno));
46*37660Sbostic 		exit(1);
47*37660Sbostic 	}
48*37660Sbostic 	pn->idletime = now < sb.st_atime ? 0 : now - sb.st_atime;
49*37660Sbostic 
50*37660Sbostic #define	TALKABLE	0220		/* tty is writable if 220 mode */
51*37660Sbostic 	pn->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
52*37660Sbostic }
53*37660Sbostic 
54*37660Sbostic userinfo(pn, pw)
55*37660Sbostic 	register PERSON *pn;
56*37660Sbostic 	register struct passwd *pw;
57*37660Sbostic {
58*37660Sbostic 	register char *p, *t;
59*37660Sbostic 	char name[256];
60*37660Sbostic 
61*37660Sbostic 	pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
62*37660Sbostic 
63*37660Sbostic 	pn->uid = pw->pw_uid;
64*37660Sbostic 	pn->name = strdup(pw->pw_name);
65*37660Sbostic 	pn->dir = strdup(pw->pw_dir);
66*37660Sbostic 	pn->shell = strdup(pw->pw_shell);
67*37660Sbostic 
68*37660Sbostic 	/* why do we skip asterisks!?!? */
69*37660Sbostic 	(void)strcpy(p = tbuf, pw->pw_gecos);
70*37660Sbostic 	if (*p == '*')
71*37660Sbostic 		++p;
72*37660Sbostic 
73*37660Sbostic 	/* ampersands get replaced by the login name */
74*37660Sbostic 	if (!(p = strsep(p, ",")))
75*37660Sbostic 		return;
76*37660Sbostic 	for (t = name; *t = *p; ++p)
77*37660Sbostic 		if (*t == '&') {
78*37660Sbostic 			(void)strcpy(t, pw->pw_name);
79*37660Sbostic 			if (islower(*t))
80*37660Sbostic 				*t = toupper(*t);
81*37660Sbostic 			while (*++t);
82*37660Sbostic 		}
83*37660Sbostic 		else
84*37660Sbostic 			++t;
85*37660Sbostic 	pn->realname = strdup(name);
86*37660Sbostic 	pn->office = (p = strsep((char *)NULL, ",")) ? strdup(p) : NULL;
87*37660Sbostic 	pn->officephone = (p = strsep((char *)NULL, ",")) ? strdup(p) : NULL;
88*37660Sbostic 	pn->homephone = (p = strsep((char *)NULL, ",")) ? strdup(p) : NULL;
89*37660Sbostic }
90*37660Sbostic 
91*37660Sbostic match(pw, user)
92*37660Sbostic 	struct passwd *pw;
93*37660Sbostic 	char *user;
94*37660Sbostic {
95*37660Sbostic 	register char *p, *t;
96*37660Sbostic 	char name[256];
97*37660Sbostic 
98*37660Sbostic 	/* why do we skip asterisks!?!? */
99*37660Sbostic 	(void)strcpy(p = tbuf, pw->pw_gecos);
100*37660Sbostic 	if (*p == '*')
101*37660Sbostic 		++p;
102*37660Sbostic 
103*37660Sbostic 	/* ampersands get replaced by the login name */
104*37660Sbostic 	if (!(p = strtok(p, ",")))
105*37660Sbostic 		return(0);
106*37660Sbostic 	for (t = name; *t = *p; ++p)
107*37660Sbostic 		if (*t == '&') {
108*37660Sbostic 			(void)strcpy(t, pw->pw_name);
109*37660Sbostic 			while (*++t);
110*37660Sbostic 		}
111*37660Sbostic 		else
112*37660Sbostic 			++t;
113*37660Sbostic 	for (t = name; p = strtok(t, "\t "); t = (char *)NULL)
114*37660Sbostic 		if (!strcasecmp(p, user))
115*37660Sbostic 			return(1);
116*37660Sbostic 	return(0);
117*37660Sbostic }
118*37660Sbostic 
119*37660Sbostic find_when(pn)
120*37660Sbostic 	register PERSON *pn;
121*37660Sbostic {
122*37660Sbostic 	static int fd;
123*37660Sbostic 	struct lastlog ll;
124*37660Sbostic 	off_t lseek();
125*37660Sbostic 
126*37660Sbostic 	if (!fd && (fd = open(_PATH_LASTLOG, O_RDONLY, 0)) < 0) {
127*37660Sbostic 		(void)fprintf(stderr,
128*37660Sbostic 		    "finger: %s: open error\n", _PATH_LASTLOG);
129*37660Sbostic 		exit(1);
130*37660Sbostic 	}
131*37660Sbostic 	(void)lseek(fd, (long)(pn->uid * (sizeof(ll))), L_SET);
132*37660Sbostic 	if (read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
133*37660Sbostic 		(void)fprintf(stderr,
134*37660Sbostic 		    "finger: %s: read error\n", _PATH_LASTLOG);
135*37660Sbostic 		exit(1);
136*37660Sbostic 	}
137*37660Sbostic 	bcopy(ll.ll_line, pn->tty, UT_LINESIZE);
138*37660Sbostic 	pn->tty[UT_LINESIZE] = NULL;
139*37660Sbostic 	bcopy(ll.ll_host, pn->host, UT_HOSTSIZE);
140*37660Sbostic 	pn->host[UT_HOSTSIZE] = NULL;
141*37660Sbostic 	pn->loginat = ll.ll_time;
142*37660Sbostic }
143*37660Sbostic 
144*37660Sbostic utcopy(ut, pn)
145*37660Sbostic 	struct utmp *ut;
146*37660Sbostic 	PERSON *pn;
147*37660Sbostic {
148*37660Sbostic 	bcopy(ut->ut_line, pn->tty, UT_LINESIZE);
149*37660Sbostic 	pn->tty[UT_LINESIZE] = 0;
150*37660Sbostic 	bcopy(ut->ut_host, pn->host, UT_HOSTSIZE);
151*37660Sbostic 	pn->host[UT_HOSTSIZE] = 0;
152*37660Sbostic 	pn->loginat = (time_t)ut->ut_time;
153*37660Sbostic }
154