./c0de/c/type.txt

0.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
#include <sgtty.h>
#include <sys/file.h>
#include <stdio.h>
#include <sys/param.h>
#include <strings.h>
#include <fcntl.h>
 
/*
 * type - make the system think that a command was entered on
 * another user's terminal.
 *
 * type [-n] <terminal> <command>
 *
 *	<terminal> is the line that the command should be entered on.
 *		examples: "/dev/ttyj4" ; "ttyj4" ; "j4"
 *
 *	<command> is the command that should be entered on that terminal.
 *		examples: "logout" ; "jobs > /dev/console"
 *
 * a newline will be appended to <command> unless type is given a "-n"
 * option.
 *
 * type can only be used on the terminal you are on, unless you are the
 * superuser, in which case you can send commands to any terminal.
 */
 
static int fd;		/* file descriptor for terminal line */
extern int wakeup();	/* routine to wakeup if open of terminal line hangs */
 
main(argc, argv, environ)
int argc;
char *argv[], *environ[];
{
	int flag_n = 0;		/* do not append newline to <command> */
 
	if ( argc <= 1 ) {
		usage();
	}
 
	if ( argv[1][0] == '-' ) {
		switch ( argv[1][1] ) {
			case 'n' :
				flag_n++;
				break;
			case '-' :
				break;
			default:
				usage();
				break;
		}
		argc--, argv++;
	}
 
	if ( argc < 3 ) {
		usage();
	}
 
	if ( index( argv[1], '/' ) == NULL ) {
		char terminal[MAXPATHLEN+1];
 
		/*
		 * since a full pathname was not given, assume the
		 * file is in the "/dev" directory, and if only 2
		 * characters were given, assume that means a "tty".
		 */
		strcpy( terminal, "/dev/" );
		if (strlen(argv[1]) == 2)
			strcat( terminal, "tty" );
		argv[1] = strcat( terminal, argv[1] );
	}
 
	/*
	 * O_NDELAY does not work (?), so set a timer to
	 * wake up if the open hangs.
	 */
 
	signal( SIGALRM, wakeup );
	alarm( 30 );
 
	if ( (fd = open(argv[1], O_WRONLY|O_NDELAY, 0)) < 0 ) {
		perror( "open" );
		exit( 2 );
	} else {
		char *cp = argv[2];
 
		alarm( 0 );
 
		while ( *cp != '\0' ) {
			send( *cp );
			cp++;
		}
 
		if ( ! flag_n ) send( '\n' );
	}
}
 
usage()
{
	fprintf( stderr, "usage: type [-n] <terminal> <command>\n" );
	exit( 1 );
}
 
send( c )
char c;
{
	register int status;
 
	status = ioctl( fd, TIOCSTI, &c );
 
	if ( status != 0 ) {
		perror( "ioctl" );
		exit( 3 );
	}
 
	return status;
}
 
wakeup()
{
	fprintf( stderr, "open hung\n" );
	exit( 4 );
}
 
 
 

Back