Cover V03, I03
Article
Figure 1
Figure 2
Figure 3
Listing 1
Listing 2
Listing 3

may94.tar


Listing 3: wruser

#include <stdio.h>
#include <fcntl.h>
#include <utmp.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

main( argc, argv )
int argc;
char *argv[];

{

int i,term,u,length;
char message[255];
struct utmp ut;

char terminal[12];
char user_name[8];
int  pid;

if (argc <  3 )
{
fprintf(stderr,"error, the correct syntax is: \n");
fprintf(stderr," %s term  message\n\n",argv[0]);
fprintf(stderr,"where: term    is the terminal where the message is to be placed\n");
fprintf(stderr,"       message is the message to be displayed\n");
return(-1);
}

strcpy(message,"\007\007\r\n");

for (i=2; i < argc ; i++ )
{
strcat(message,argv[i]); /* put all of the other args together */
strcat(message," ");     /* into one long string, ie, the msg */
}
strcat(message,"\r\n");
length=strlen(message);

u=open("/etc/utmp",O_RDONLY,0);

if (u < 0) {
fprintf(stderr,"unable to open utmp..\n");
exit(-1);
}

while(read(u,&ut,sizeof(struct utmp)) == sizeof(struct utmp))
{
if (ut.ut_line[0] != '\0' && ut.ut_name[0] != '\0')
{
if ( !strcmp(argv[1], ut.ut_line) )
{
strcpy(user_name,ut.ut_name);
strcpy(terminal,"/dev/");
strcat(terminal,ut.ut_line);
pid = ut.ut_pid;
}
}

}
close(u);
exit(0);
term=open(terminal,O_RDWR,0600);
if (term < 0)
{
fprintf(stderr,"Error opening term - %s (%s)\n",terminal,user_name);
exit(-1);
}

write(term,message,length) ;
close(term);
printf("%d",pid);
return(pid);
}