=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/finger/util.c,v retrieving revision 1.4 retrieving revision 1.5 diff -c -r1.4 -r1.5 *** src/usr.bin/finger/util.c 1996/08/30 11:39:36 1.4 --- src/usr.bin/finger/util.c 1997/05/30 23:35:53 1.5 *************** *** 1,8 **** ! /* $OpenBSD: util.c,v 1.4 1996/08/30 11:39:36 deraadt Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. --- 1,9 ---- ! /* $OpenBSD: util.c,v 1.5 1997/05/30 23:35:53 kstailey Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. * All rights reserved. + * Portions Copyright (c) 1983, 1995, 1996 Eric P. Allman (woof!) * * This code is derived from software contributed to Berkeley by * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. *************** *** 38,46 **** #ifndef lint /*static char sccsid[] = "from: @(#)util.c 5.14 (Berkeley) 1/17/91";*/ ! static char rcsid[] = "$OpenBSD: util.c,v 1.4 1996/08/30 11:39:36 deraadt Exp $"; #endif /* not lint */ #include #include #include --- 39,49 ---- #ifndef lint /*static char sccsid[] = "from: @(#)util.c 5.14 (Berkeley) 1/17/91";*/ ! static char rcsid[] = "$OpenBSD: util.c,v 1.5 1997/05/30 23:35:53 kstailey Exp $"; #endif /* not lint */ + #include + #include #include #include #include *************** *** 50,65 **** #include #include #include #include "finger.h" find_idle_and_ttywrite(w) ! register WHERE *w; { - extern time_t now; - extern int errno; struct stat sb; ! (void)sprintf(tbuf, "%s%s", _PATH_DEV, w->tty); if (stat(tbuf, &sb) < 0) { /* Don't bitch about it, just handle it... */ w->idletime = 0; --- 53,69 ---- #include #include #include + #include #include "finger.h" + #include "extern.h" + void find_idle_and_ttywrite(w) ! WHERE *w; { struct stat sb; ! (void)snprintf(tbuf, sizeof(tbuf), "%s%s", _PATH_DEV, w->tty); if (stat(tbuf, &sb) < 0) { /* Don't bitch about it, just handle it... */ w->idletime = 0; *************** *** 73,88 **** w->writable = ((sb.st_mode & TALKABLE) == TALKABLE); } userinfo(pn, pw) ! register PERSON *pn; ! register struct passwd *pw; { ! extern time_t now; ! register char *p, *t; struct stat sb; - extern int errno; - char *mbp, *bp, *name; - int len; pn->realname = pn->office = pn->officephone = pn->homephone = NULL; --- 77,90 ---- w->writable = ((sb.st_mode & TALKABLE) == TALKABLE); } + void userinfo(pn, pw) ! PERSON *pn; ! struct passwd *pw; { ! char *p; ! char *bp, name[1024]; struct stat sb; pn->realname = pn->office = pn->officephone = pn->homephone = NULL; *************** *** 91,122 **** pn->dir = strdup(pw->pw_dir); pn->shell = strdup(pw->pw_shell); ! /* why do we skip asterisks!?!? */ ! mbp = bp = (char *)malloc(strlen(pw->pw_gecos)+1); ! strcpy(bp, pw->pw_gecos); ! if (*bp == '*') ! ++bp; /* ampersands get replaced by the login name */ ! if (!(p = strsep(&bp, ","))) { ! free(mbp); return; ! } ! for (len = 0, t = p; *p; ++p) { ! len++; ! if (*p == '&') ! len += strlen(pw->pw_name); ! } ! name = (char *)malloc(len + 1); ! for (p = t, t = name; *t = *p; ++p) ! if (*t == '&') { ! (void)strcpy(t, pw->pw_name); ! if (islower(*t)) ! *t = toupper(*t); ! while (*++t); ! } else ! ++t; ! *t = '\0'; pn->realname = strdup(name); pn->office = ((p = strsep(&bp, ",")) && *p) ? strdup(p) : NULL; --- 93,104 ---- pn->dir = strdup(pw->pw_dir); pn->shell = strdup(pw->pw_shell); ! (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf)); /* ampersands get replaced by the login name */ ! if (!(p = strsep(&bp, ","))) return; ! expandusername(p, pw->pw_name, name, sizeof(name)); pn->realname = strdup(name); pn->office = ((p = strsep(&bp, ",")) && *p) ? strdup(p) : NULL; *************** *** 124,179 **** strdup(p) : NULL; pn->homephone = ((p = strsep(&bp, ",")) && *p) ? strdup(p) : NULL; ! (void)sprintf(tbuf, "%s/%s", _PATH_MAILSPOOL, pw->pw_name); pn->mailrecv = -1; /* -1 == not_valid */ if (stat(tbuf, &sb) < 0) { if (errno != ENOENT) { (void)fprintf(stderr, "finger: %s: %s\n", tbuf, strerror(errno)); - free(name); - free(mbp); return; } } else if (sb.st_size != 0) { pn->mailrecv = sb.st_mtime; pn->mailread = sb.st_atime; } - free(name); - free(mbp); } match(pw, user) struct passwd *pw; char *user; { ! register char *p, *t; char name[1024]; ! /* why do we skip asterisks!?!? */ ! (void)strcpy(p = tbuf, pw->pw_gecos); ! if (*p == '*') ! ++p; /* ampersands get replaced by the login name */ if (!(p = strtok(p, ","))) return(0); ! for (t = name; *t = *p; ++p) ! if (*t == '&') { ! (void)strcpy(t, pw->pw_name); ! while (*++t); ! } ! else ! ++t; ! for (t = name; p = strtok(t, "\t "); t = (char *)NULL) if (!strcasecmp(p, user)) return(1); return(0); } enter_lastlog(pn) ! register PERSON *pn; { ! register WHERE *w; static int opened, fd; struct lastlog ll; char doit = 0; --- 106,186 ---- strdup(p) : NULL; pn->homephone = ((p = strsep(&bp, ",")) && *p) ? strdup(p) : NULL; ! (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILSPOOL, ! pw->pw_name); pn->mailrecv = -1; /* -1 == not_valid */ if (stat(tbuf, &sb) < 0) { if (errno != ENOENT) { (void)fprintf(stderr, "finger: %s: %s\n", tbuf, strerror(errno)); return; } } else if (sb.st_size != 0) { pn->mailrecv = sb.st_mtime; pn->mailread = sb.st_atime; } } + int match(pw, user) struct passwd *pw; char *user; { ! char *p, *t; char name[1024]; ! (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf)); /* ampersands get replaced by the login name */ if (!(p = strtok(p, ","))) return(0); ! expandusername(p, pw->pw_name, name, sizeof(name)); ! for (t = name; (p = strtok(t, "\t ")) != NULL; t = (char *)NULL) if (!strcasecmp(p, user)) return(1); return(0); } + /* inspired by usr.sbin/sendmail/util.c::buildfname */ + void + expandusername(gecos, login, buf, buflen) + char *gecos; + char *login; + char *buf; + int buflen; + { + char *p, *bp; + + /* why do we skip asterisks!?!? */ + if (*gecos == '*') + gecos++; + bp = buf; + + /* copy gecos, interpolating & to be full name */ + for (p = gecos; *p != '\0'; p++) { + if (bp >= &buf[buflen - 1]) { + /* buffer overflow - just use login name */ + snprintf(buf, buflen, "%s", login); + buf[buflen - 1] = '\0'; + return; + } + if (*p == '&') { + /* interpolate full name */ + snprintf(bp, buflen - (bp - buf), "%s", login); + *bp = toupper(*bp); + bp += strlen(bp); + } + else + *bp++ = *p; + } + *bp = '\0'; + } + + void enter_lastlog(pn) ! PERSON *pn; { ! WHERE *w; static int opened, fd; struct lastlog ll; char doit = 0; *************** *** 188,194 **** (long)pn->uid * sizeof(ll) || read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) { /* as if never logged in */ ! ll.ll_line[0] = ll.ll_host[0] = NULL; ll.ll_time = 0; } if ((w = pn->whead) == NULL) --- 195,201 ---- (long)pn->uid * sizeof(ll) || read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) { /* as if never logged in */ ! ll.ll_line[0] = ll.ll_host[0] = '\0'; ll.ll_time = 0; } if ((w = pn->whead) == NULL) *************** *** 219,229 **** } } enter_where(ut, pn) struct utmp *ut; PERSON *pn; { ! register WHERE *w = walloc(pn); w->info = LOGGEDIN; bcopy(ut->ut_line, w->tty, UT_LINESIZE); --- 226,237 ---- } } + void enter_where(ut, pn) struct utmp *ut; PERSON *pn; { ! WHERE *w = walloc(pn); w->info = LOGGEDIN; bcopy(ut->ut_line, w->tty, UT_LINESIZE); *************** *** 236,244 **** PERSON * enter_person(pw) ! register struct passwd *pw; { ! register PERSON *pn, **pp; for (pp = htab + hash(pw->pw_name); *pp != NULL && strcmp((*pp)->name, pw->pw_name) != 0; --- 244,252 ---- PERSON * enter_person(pw) ! struct passwd *pw; { ! PERSON *pn, **pp; for (pp = htab + hash(pw->pw_name); *pp != NULL && strcmp((*pp)->name, pw->pw_name) != 0; *************** *** 266,272 **** find_person(name) char *name; { ! register PERSON *pn; /* name may be only UT_NAMESIZE long and not terminated */ for (pn = htab[hash(name)]; --- 274,280 ---- find_person(name) char *name; { ! PERSON *pn; /* name may be only UT_NAMESIZE long and not terminated */ for (pn = htab[hash(name)]; *************** *** 276,290 **** return(pn); } hash(name) ! register char *name; { ! register int h, i; h = 0; /* name may be only UT_NAMESIZE long and not terminated */ for (i = UT_NAMESIZE; --i >= 0 && *name;) ! h = ((h << 2 | h >> HBITS - 2) ^ *name++) & HMASK; return(h); } --- 284,299 ---- return(pn); } + int hash(name) ! char *name; { ! int h, i; h = 0; /* name may be only UT_NAMESIZE long and not terminated */ for (i = UT_NAMESIZE; --i >= 0 && *name;) ! h = ((h << 2 | h >> (HBITS - 2)) ^ *name++) & HMASK; return(h); } *************** *** 302,310 **** WHERE * walloc(pn) ! register PERSON *pn; { ! register WHERE *w; if ((w = (WHERE *)malloc((u_int) sizeof(WHERE))) == NULL) { (void)fprintf(stderr, "finger: out of space.\n"); --- 311,319 ---- WHERE * walloc(pn) ! PERSON *pn; { ! WHERE *w; if ((w = (WHERE *)malloc((u_int) sizeof(WHERE))) == NULL) { (void)fprintf(stderr, "finger: out of space.\n"); *************** *** 324,330 **** prphone(num) char *num; { ! register char *p; int len; static char pbuf[15]; --- 333,339 ---- prphone(num) char *num; { ! char *p; int len; static char pbuf[15];