[net.sources] friends.c: a selective who/finger

cs2534ai@unm-cvax.UUCP (02/06/85)

	Friends is a utility to check utmp against a database in your home
directory and give you a selective listing of users on the system.  I wrote
it out of disgust because all of our login names look the same around here.
It's depressing!  Look at this:
% users
cs2532af cs2532ah cs2532aj cs2532am cs2532at cs2532az cs2532bd cs2532bk cs2532bn cs2533ak cs2533as cs2533ba cs2534ai cs2534av cs2534av cs2534ay cs2551ak cs2551al cs2551am cs2551bl cs2551bs cwayne ee3261bz khcho ma3752at ma3752av ma3752bc
% 
Ah yessss, good ole cs2551am...I remember him from the war...
I would rather use names, buy HEY! now I can.

	Here is some sample output from the program:
-----
cs2534ay   tty61                                   eli
cs2534ai   tty43     David Thomas                  me
-----
This output indicates that of all the users on this system now (27 of us!),
the only people I know are myself and my friend Eli.  I have my name set in
my gecos field and he doesn't.  Also, both of our terminals are writeable.  An
unwritable terminal is prefixed by a star, as in...
cs2534ay  *tty61 ...
Actually I am not usually listed in my own .friends file (I'm so modest :-)),
but I put myself in for the demonstration.


	Before you can run friends, you need to have a .friends database. The
format is very simple: <username> <one space or tab> <your comment>

	Here is a sample .friends file:
-----
cs2532aa Ernie Longmire
quander kent quander
cs2534bk Kevin
cs2534as August
cs2534ay eli
-----

	Head Enchiladas may want to install friends in a local/bin for all
to use.  Everybody can run this object and it will always look in their home
directory for the database.

	Please notify me of any incompatibility/installation difficulties, or
IMPROVEMENTS.   In particular, can somebody make it check for the users'
processes like the "w" command?

---cut here---
/* tell which of my friends are logged on */
/* copywrong (c) 1984 -DT software */
 
#define FRIENDFILE ".friends"
#define MAXUSERS 60 /* change for bigger system */
#define MAXLINE 100 /* max length of line in friend file */
#define UTMP "/etc/utmp"
 
#include <stdio.h>
#include <utmp.h>
#include <pwd.h>
#include <ctype.h>
 
struct utmp     user[MAXUSERS];
struct passwd   *g,*getpwuid();
char    *index();
FILE    *fp,*fopen();
 
main()
{
        int     ut,nusers,n,i;
        char    friend[MAXLINE],*comment,friendfile[60],gecos[50];
        char    histty[50],mesgflag;

        if ( (g=getpwuid(getuid())) == NULL) {
                fprintf (stderr, "You do not exist!\n");
                exit(-1);
        }
        sprintf (friendfile,"%s/%s",g->pw_dir,FRIENDFILE);
        if ( (ut=open(UTMP,0)) == -1) {
                fprintf (stderr, "Can't open utmp\n");
                exit(-1);
        }
        nusers = 0;
        while ( (n = read(ut,&(user[nusers]),sizeof(struct utmp))) ==
            sizeof(struct utmp))
                if (user[nusers].ut_name[0] != '\0')
                        ++nusers;
        close (ut);
        if (n == -1) {
                fprintf (stderr, "error reading utmp\n");
                exit(-1);
        }
        if ( (fp=fopen(friendfile,"r")) == NULL) {
                fprintf (stderr,"%s not found\n",friendfile);
                exit(-1);
        }
        while (fgets(friend,MAXLINE,fp) != NULL) {
                friend[strlen(friend)-1] = '\0'; /* strip newline */
                for (comment = friend;!isspace(*comment);++comment)
                        if (*comment == '\0')
                                break;
                if (*comment != '\0') {
                        *comment = '\0';
                        ++comment;
                }
                for (i = 0;i < nusers;++i)
                        if (!strcmp(friend,user[i].ut_name)) {
                                if ( (g=getpwnam(friend)) == NULL)
                                        continue;
                                strcpy (gecos,g->pw_gecos);
                                if (index(gecos,','))
                                        *index(gecos,',') = '\0';
                                sprintf(histty,"/dev/%s",user[i].ut_line);
                                if (!access(histty,002))
                                        mesgflag = ' ';
                                else
                                        mesgflag = '*';
                                printf("%-10s%c%-10s%-30s%s\n",friend,
                                    mesgflag,user[i].ut_line,gecos,comment);
                        }
        }
        fclose(fp);
}
/* no flames please...if(isscrewy(MYCODE)) fflush(complaint);	*/
---cut here---

Thazzit.  Not much to it, really.  Hope someone can use it.

						-DT
					[The Deranged Terrestrial]

-----
___________HOME___________		_____________WORK______________________
David B. Thomas				Rocky Mountain Computers (Apple dealer)
1406 Calle Del Ranchero NE		2109 Wyoming Blvd. NE
Albuquerque, NM  87106			Albuquerque, NM  87112
(505) 266-1016				(505) 292-2775 that's 292-APPL

UUCP:   {{purdue,cmc12,ihnp4}!lanl,ucbvax}!unmvax!unm-cvax!cs2534ai
-----
"Remember, Anna, sometimes a banana is just a banana."