FreeBSD/usr.bin/write/write.c
/*
 * search_utmp - search utmp for the "best" terminal to write to
 *
 * Ignores terminals with messages disabled, and of the rest, returns
 * the one with the most recent access time.  Returns as value the number
 * of the user's terminals with messages enabled, or -1 if the user is
 * not logged in at all.
 *
 * Special case for writing to yourself - ignore the terminal you're
 * writing from, unless that's the only terminal with messages enabled.
 */
search utmp for the "best" terminal to write to
Ignores terminals with messages disabled, and of the rest, returns
the one with the most recent access time.  Returns as value the number
of the user's terminals with messages enabled, or -1 if the user is
not logged in at all.

Special case for writing to yourself - ignore the terminal you're
writing from, unless that's the only terminal with messages enabled.
search utmp for the " best " terminal to write to Ignores terminals with messages disabled and of the rest returns the one with the most recent access time . Returns as value the number of the user ' s terminals with messages enabled or - 1 if the user is not logged in at all . Special case for writing to yourself - ignore the terminal you ' re writing from unless that ' s the only terminal with messages enabled .
void
search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
{
	struct utmpx *u;
	time_t bestatime, atime;
	int nloggedttys, nttys, msgsok, user_is_me;
	nloggedttys = nttys = 0;
	bestatime = 0;
	user_is_me = 0;
	setutxent();
	while ((u = getutxent()) != NULL)
		if (u->ut_type == USER_PROCESS &&
		    strcmp(user, u->ut_user) == 0) {
			++nloggedttys;
			if (term_chk(u->ut_line, &msgsok, &atime, 0))
				continue;	
			if (myuid && !msgsok)
				continue;	
			if (strcmp(u->ut_line, mytty) == 0) {
				user_is_me = 1;
				continue;	
			}
			++nttys;
			if (atime > bestatime) {
				bestatime = atime;
				(void)strlcpy(tty, u->ut_line, MAXPATHLEN);
			}
		}
	endutxent();
	if (nloggedttys == 0)
		errx(1, "%s is not logged in", user);
	if (nttys == 0) {
		if (user_is_me) {		
			(void)strlcpy(tty, mytty, MAXPATHLEN);
			return;
		}
		errx(1, "%s has messages disabled", user);
	} else if (nttys > 1) {
		warnx("%s is logged in more than once; writing to %s", user, tty);
	}
}
void search_utmp ( char * user , char * tty , char * mytty , uid_t myuid ) { struct utmpx * u ; time_t bestatime , atime ; int nloggedttys , nttys , msgsok , user_is_me ; nloggedttys = nttys = 0 ; bestatime = 0 ; user_is_me = 0 ; setutxent ( ) ; while ( ( u = getutxent ( ) ) != NULL ) if ( u -> ut_type == USER_PROCESS && strcmp ( user , u -> ut_user ) == 0 ) { ++ nloggedttys ; if ( term_chk ( u -> ut_line , & msgsok , & atime , 0 ) ) continue ; if ( myuid && ! msgsok ) continue ; if ( strcmp ( u -> ut_line , mytty ) == 0 ) { user_is_me = 1 ; continue ; } ++ nttys ; if ( atime > bestatime ) { bestatime = atime ; ( void ) strlcpy ( tty , u -> ut_line , MAXPATHLEN ) ; } } endutxent ( ) ; if ( nloggedttys == 0 ) errx ( 1 , " " , user ) ; if ( nttys == 0 ) { if ( user_is_me ) { ( void ) strlcpy ( tty , mytty , MAXPATHLEN ) ; return ; } errx ( 1 , " " , user ) ; } else if ( nttys > 1 ) { warnx ( " " , user , tty ) ; } }
search_utmp - search utmp for the "best" terminal to write to
 Ignores terminals with messages disabled, and of the rest, returns
 the one with the most recent access time.
search_utmp - search utmp for the " best " terminal to write to Ignores terminals with messages disabled and of the rest returns the one with the most recent access time .
/* bad term? skip */ /* skip ttys with msgs off */ /* don't write to yourself */ /* ok, so write to yourself! */