[comp.sources.misc] v09i069: rwhop patch one

robert@elbert.LANCE.ColoState.Edu (Robert Thompson) (12/19/89)

Posting-number: Volume 9, Issue 69
Submitted-by: robert@elbert.LANCE.ColoState.Edu (Robert Thompson)
Archive-name: rwhoparse.p1

I ahve received several inquiries into the rwho parses posted recently.
This patch incorporates the changes and improvements suggested.   -Robert

+------------------------------------------+---------------------+
|                                          | Ask me a riddle and |
|              |                   |       | I'll reply:         |
|              |                   |       |                     |
|  /---\ /---\ |---\ /---\  /---\  |---    | "Cottleston,        |
|  |     |   | |   | |---/  |      |       |  Cottleston,        |
|  |     \---/ \---/ \----  |      \---/   |  Cottleston Pie."   |
|------------------------------------------|                     |
| ARPA: robert@longs.LANCE.ColoState.Edu   |  Winnie-the-Pooh    |
|------------------------------------------|---------------------|
| Center for Computer Assisted Engineering | Got something on    |
| Department of Electrical Engineering     | your mind ?         |
| Colorado State University                | >> send me mail  << |
+------------------------------------------+---------------------+


------- CUT HERE -------

*** Makefile.old	Sun Dec 17 13:07:42 1989
--- Makefile	Sun Dec 17 13:07:08 1989
***************
*** 5,14
  # No copyrights inplied or requested.  This code is supplied as is
  # have fun using it and report any bugs to:
  #
  #  	robert@longs.LANCE.ColoState.edu
  #
  #################################################################
  OBJS=	rwhoparse.o
  SRCS=	rwhoparse.c
  
  TARGET= rwhop

--- 5,16 -----
  # No copyrights inplied or requested.  This code is supplied as is
  # have fun using it and report any bugs to:
  #
  #  	robert@longs.LANCE.ColoState.edu
  #
+ # See rwhoparse.c for modification history
+ #
  #################################################################
  OBJS=	rwhoparse.o
  SRCS=	rwhoparse.c
  
  TARGET= rwhop
*** README.OLD	Sun Dec 17 13:06:13 1989
--- README	Sun Dec 17 13:05:49 1989
***************
*** 1,11
  rwhop is a program I wrote one bored Sunday night for something to do.  I
  was prompted to post this code for others to use.  The main reason for the
  program is to return the users real name instead of just the login name.
  If there are any problems, send me mail and I will try to rectify the bug.
  
! This coed is supplied without copyright, do with it what you please.  (Tell
  me of any improvements you have.)  I was thinking of making the program
  search for a host rather than just a user (maybe someday).  
  
  Robert
  CSU - Center forComputer Assisted Engineering

--- 1,11 -----
  rwhop is a program I wrote one bored Sunday night for something to do.  I
  was prompted to post this code for others to use.  The main reason for the
  program is to return the users real name instead of just the login name.
  If there are any problems, send me mail and I will try to rectify the bug.
  
! This code is supplied without copyright, do with it what you please.  (Tell
  me of any improvements you have.)  I was thinking of making the program
  search for a host rather than just a user (maybe someday).  
  
  I have received some suggestions for improvement.  They have been imployed
  for the most part.  See the source code for information.
***************
*** 4,13
  If there are any problems, send me mail and I will try to rectify the bug.
  
  This coed is supplied without copyright, do with it what you please.  (Tell
  me of any improvements you have.)  I was thinking of making the program
  search for a host rather than just a user (maybe someday).  
  
  Robert
  CSU - Center forComputer Assisted Engineering
  
  robert@longs.LANCE.ColoState.Edu

--- 4,16 -----
  If there are any problems, send me mail and I will try to rectify the bug.
  
  This code is supplied without copyright, do with it what you please.  (Tell
  me of any improvements you have.)  I was thinking of making the program
  search for a host rather than just a user (maybe someday).  
+ 
+ I have received some suggestions for improvement.  They have been imployed
+ for the most part.  See the source code for information.
  
  Robert
  CSU - Center forComputer Assisted Engineering
  
  robert@longs.LANCE.ColoState.Edu
*** rwhoparse.c.old	Sun Dec 17 12:52:40 1989
--- rwhoparse.c	Sun Dec 17 12:16:25 1989
***************
*** 1,5
  #include <stdio.h>
  #include <pwd.h>
  
  main(argc,argv)
  int	argc;

--- 1,29 -----
+ /*
+  * This software is provided without claims.  Bugs/suggestions can be sent
+  * to robert@longs.LANCE.ColoState.Edu.  Below is a new version employing
+  * changes as reported.
+  *
+  * mod date	description			who
+  * --------     -----------                     ----------------------------
+  * Dec 18, 89	Took care of missing argument	emb978@leah.Albany.Edu
+  *              problem.			(I dont know the name, sorry)
+  *
+  * Dec 18, 89   Put the null at the right 	same as above
+  *		place in the rean_name string.
+  *
+  * Dec 18, 89	Added some comments		Rober
+  *
+  * Dec 18, 89	If the user is unknown  	Robert
+  *		a message is printed.		
+  *
+  * Note:  It was suggested by Heiji Horde (horde@reed.BITNET) to account
+  *        for tty names that are longer than 6 characters.  Since this 
+  *	  dependent on the site.  I leave this up to you.  -robert
+  *        (I hope that Heiji Horde is your correct name, sorry if it is not)
+  */
+ 
  #include <stdio.h>
  #include <pwd.h>
  
  main(argc,argv)
  int	argc;
***************
*** 3,13
  
  main(argc,argv)
  int	argc;
  char	*argv[];
  {
! 	FILE	*pipe;
  
  	char	real_name[30], inline[80];
  	char	*user, *host, *time, *gecos, *tty, *strtok();
  
  	register int i;

--- 27,37 -----
  
  main(argc,argv)
  int	argc;
  char	*argv[];
  {
! 	FILE	*pipe;		/* The pipe opened to rwho		*/
  
  	char	real_name[20],	/* the users real name goes here	*/
  		inline[80],	/* the input from rwho			*/
  		*user,		/* the following 5 vars are the fields	*/
  		*host,		/* obtained from the output of rwho.	*/
***************
*** 5,16
  int	argc;
  char	*argv[];
  {
  	FILE	*pipe;
  
! 	char	real_name[30], inline[80];
! 	char	*user, *host, *time, *gecos, *tty, *strtok();
  
  	register int i;
  
  	struct	passwd	*pw;
  

--- 29,46 -----
  int	argc;
  char	*argv[];
  {
  	FILE	*pipe;		/* The pipe opened to rwho		*/
  
! 	char	real_name[20],	/* the users real name goes here	*/
! 		inline[80],	/* the input from rwho			*/
! 		*user,		/* the following 5 vars are the fields	*/
! 		*host,		/* obtained from the output of rwho.	*/
! 		*time,
! 		*gecos,
! 		*tty,
! 		*strtok();
  
  	register int i;
  
  	struct	passwd	*pw;	/* to get the gecos info.		*/
  
***************
*** 10,20
  	char	real_name[30], inline[80];
  	char	*user, *host, *time, *gecos, *tty, *strtok();
  
  	register int i;
  
! 	struct	passwd	*pw;
  
  	argv++;
  	strcpy(inline,"/usr/ucb/rwho ");
  	strcat(inline,*argv);
  

--- 40,50 -----
  		*tty,
  		*strtok();
  
  	register int i;
  
! 	struct	passwd	*pw;	/* to get the gecos info.		*/
  
  	argv++;
  	strcpy(inline,"/usr/ucb/rwho ");
  	if (*argv) strcat(inline,*argv);
  
***************
*** 14,24
  
  	struct	passwd	*pw;
  
  	argv++;
  	strcpy(inline,"/usr/ucb/rwho ");
! 	strcat(inline,*argv);
  
  	if ((pipe = popen(inline,"r")) == NULL)
  		{
  		fprintf (stderr,"Broken pipe\n");
  		exit(-1);

--- 44,54 -----
  
  	struct	passwd	*pw;	/* to get the gecos info.		*/
  
  	argv++;
  	strcpy(inline,"/usr/ucb/rwho ");
! 	if (*argv) strcat(inline,*argv);
  
  	/* open a pipe to rwho and read the output.  Parse as we go	*/
  	if ((pipe = popen(inline,"r")) == NULL)
  		{
  		fprintf (stderr,"Broken pipe\n");
***************
*** 16,25
  
  	argv++;
  	strcpy(inline,"/usr/ucb/rwho ");
  	strcat(inline,*argv);
  
  	if ((pipe = popen(inline,"r")) == NULL)
  		{
  		fprintf (stderr,"Broken pipe\n");
  		exit(-1);
  		}

--- 46,56 -----
  
  	argv++;
  	strcpy(inline,"/usr/ucb/rwho ");
  	if (*argv) strcat(inline,*argv);
  
+ 	/* open a pipe to rwho and read the output.  Parse as we go	*/
  	if ((pipe = popen(inline,"r")) == NULL)
  		{
  		fprintf (stderr,"Broken pipe\n");
  		exit(-1);
  		}
***************
*** 28,41
  	for (i=0;i<80;i++) printf ("-");
  	printf ("\n");
  
  	while (fgets(inline,80,pipe) != NULL)
  		{
! 		user = strtok(inline," ");
! 		host = strtok(NULL,":");
! 		tty = strtok(NULL," ");
! 		time = strtok(NULL,"\n");
  
  		if ((pw = getpwnam(user)) != NULL)
  			{
  			gecos = strtok(pw->pw_gecos,",");
  			

--- 59,72 -----
  	for (i=0;i<80;i++) printf ("-");
  	printf ("\n");
  
  	while (fgets(inline,80,pipe) != NULL)
  		{
! 		user 	= strtok(inline," ");
! 		host 	= strtok(NULL,":");
! 		tty 	= strtok(NULL," ");
! 		time 	= strtok(NULL,"\n");
  
  		if ((pw = getpwnam(user)) != NULL)
  			{
  			gecos = strtok(pw->pw_gecos,",");
  			
***************
*** 38,56
  		if ((pw = getpwnam(user)) != NULL)
  			{
  			gecos = strtok(pw->pw_gecos,",");
  			
  			strncpy(real_name,gecos,20);
! 			real_name[20] = NULL;
! 			
! 			while(time[0] == ' ') time++;
! 			while(host[0] == ' ') host++;
! 			
! 			printf ("%8s  %20s  %10s  %6s  %s\n",
! 				user,
! 				real_name,
! 				strtok(host,"."),
! 				tty,
! 				time);
  			}
  		}
  }

--- 69,79 -----
  		if ((pw = getpwnam(user)) != NULL)
  			{
  			gecos = strtok(pw->pw_gecos,",");
  			
  			strncpy(real_name,gecos,20);
! 			real_name[19] = NULL;
  			}
  		else
  			{
  			strcpy(real_name,"gecos unavailable");
  			}
***************
*** 50,56
  				real_name,
  				strtok(host,"."),
  				tty,
  				time);
  			}
  		}
  }

--- 71,92 -----
  			gecos = strtok(pw->pw_gecos,",");
  			
  			strncpy(real_name,gecos,20);
  			real_name[19] = NULL;
  			}
+ 		else
+ 			{
+ 			strcpy(real_name,"gecos unavailable");
+ 			}
+ 
+ 		/* strip leading spaces	*/
+ 		while(time[0] == ' ') time++;
+ 		while(host[0] == ' ') host++;
+ 		
+ 		printf ("%8s  %20s  %10s  %6s  %s\n",
+ 			user,
+ 			real_name,
+ 			strtok(host,"."),
+ 			tty,
+ 			time);
  		}
  }
*** rwhop.n.old	Sun Dec 17 12:59:44 1989
--- rwhop.n	Sun Dec 17 13:26:53 1989
***************
*** 13,23
  command takes the output from rwho(1) via a pipe stream connection.
  The returned information is the same as that of rwho with the following
  exceptions: 1) fully qualified host names are truncated to the first level.
  2) The users' gecos information is retreived via the getpwnam(3) call.
  The first field of the gecos information is printed un the heading of 'Real
! Name'
  .SH OPTIONS
  .IP \fB\-a\fR 20
  Lists all users.  Normally,
  .PN rwhop
  omits users who have not typed to the system for an hour or

--- 13,26 -----
  command takes the output from rwho(1) via a pipe stream connection.
  The returned information is the same as that of rwho with the following
  exceptions: 1) fully qualified host names are truncated to the first level.
  2) The users' gecos information is retreived via the getpwnam(3) call.
  The first field of the gecos information is printed un the heading of 'Real
! Name'  If a user's gecos information cannot be retreived, the message
! 
! 'gecos unavailable' is printed.
! 
  .SH OPTIONS
  .IP \fB\-a\fR 20
  Lists all users.  Normally,
  .PN rwhop
  omits users who have not typed to the system for an hour or
***************
*** 28,37
  Sorts users by host name.  Normally,
  .PN rwhop
  prints its output sorted by user name.  If the
  .B \-h
  flag is specified, the results are sorted by host name.
  .SH FILES
  /usr/spool/rwho/whod.*	Information about other machines
  .SH SEE ALSO
  ruptime(1c), rwhod(8c)
  .SH AUTHOR

--- 31,43 -----
  Sorts users by host name.  Normally,
  .PN rwhop
  prints its output sorted by user name.  If the
  .B \-h
  flag is specified, the results are sorted by host name.
+ .SH BUGS
+ If the tty name is longer than six characters, the time and idle columns
+ become misaligned.
  .SH FILES
  /usr/spool/rwho/whod.*	Information about other machines
  .SH SEE ALSO
  ruptime(1c), rwhod(8c), rwho(1), passwd(5)
  .SH AUTHOR
***************
*** 31,38
  .B \-h
  flag is specified, the results are sorted by host name.
  .SH FILES
  /usr/spool/rwho/whod.*	Information about other machines
  .SH SEE ALSO
! ruptime(1c), rwhod(8c)
  .SH AUTHOR
  Robert D. Thompson - Colorado State University  December 10, 1989

--- 37,44 -----
  If the tty name is longer than six characters, the time and idle columns
  become misaligned.
  .SH FILES
  /usr/spool/rwho/whod.*	Information about other machines
  .SH SEE ALSO
! ruptime(1c), rwhod(8c), rwho(1), passwd(5)
  .SH AUTHOR
  Robert D. Thompson - Colorado State University  December 10, 1989