Chinaunix首页 | 论坛 | 博客
  • 博客访问: 783541
  • 博文数量: 143
  • 博客积分: 2077
  • 博客等级: 大尉
  • 技术积分: 2393
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-28 12:57
文章存档

2016年(2)

2015年(3)

2014年(3)

2013年(41)

2012年(94)

分类: LINUX

2013-04-26 11:31:05

#include	
#include	
#include	
#include        
#include        
/*
 * write1.c
 *
 *	purpose: send messages to another terminal
 *	 method: open the other terminal for output then
 *	 	 copy from stdin to that terminal
 *        usage: write1 username
 */

main( int ac, char *av[] )
{
	int	fd;
	char	buf[BUFSIZ];
        char    buf_enter[10];
	char	*get_tty(), *tty_for_user;

	
	if ( ac != 2 ){                                    /* check args */
		fprintf(stderr,"usage: write0 logname\n");
		exit(1);
	}

	tty_for_user = get_tty( av[1] );                  /* find user  */
	if ( tty_for_user == NULL ) {
                fprintf(stderr,"\n %s not exist or not login",av[1]);
		return 1;
        }

	sprintf(buf, "/dev/%s", tty_for_user);	          /* open device */
	fd = open( buf, O_WRONLY );
	if ( fd == -1 ){
		perror(buf); exit(1);
	}
   
        sprintf(buf_enter,"\n");
        write(fd,buf_enter,strlen(buf_enter));           /* input Enterkey */

	while( fgets(buf, BUFSIZ, stdin) != NULL )       /* write to user */
		if ( write(fd, buf, strlen(buf)) == -1 )
			break;
	close( fd );
}

char *
get_tty( char *logname )
/*
 * purpose: find the tty at which 'logname' is logged in
 * returns: a string or NULL if not logged in
 *  errors: does not handle multiple logins 
 */
{
	static struct utmp utrec;
	int	      utfd;
	int	      namelen = sizeof( utrec.ut_name );
	char	      *retval = NULL ;

	if ( (utfd = open( UTMP_FILE, O_RDONLY )) == -1 )   /* open utmp */
		return NULL;

	/* look for a line where the user is logged in */
	while( read( utfd, &utrec, sizeof(utrec)) == sizeof(utrec) )
		if ( strncmp(logname, utrec.ut_name, namelen ) == 0 )
		{
			retval = utrec.ut_line ;
			break;
		}

	close(utfd);                                       /* close & go */
	return retval;
}
阅读(1025) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~