001
002
003
004 #include <utmp.h>
005 #include <sys/types.h>
006 #include <sys/stat.h>
007 #include <fcntl.h>
008 #include <unistd.h>
009 #include <stdio.h>
010 #include <stdlib.h>
011 #include <termios.h>
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033 struct termios saved_attributes;
034
035 static void
036 reset_input_mode (void)
037 {
038 tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
039 }
040
041 static void
042 set_input_mode (void)
043 {
044 struct termios tattr;
045
046
047 if (!isatty (STDIN_FILENO))
048 {
049 fprintf (stderr, "Not a terminal.\n");
050 exit (EXIT_FAILURE);
051 }
052
053
054 tcgetattr (STDIN_FILENO, &saved_attributes);
055 atexit (reset_input_mode);
056
057
058 tcgetattr (STDIN_FILENO, &tattr);
059 tattr.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
060 |INLCR|IGNCR|ICRNL|IXON);
061 tattr.c_lflag &= ~(ICANON|ECHO);
062 tattr.c_cflag |= CLOCAL;
063 tattr.c_cc[VMIN] = 1;
064 tattr.c_cc[VTIME] = 0;
065 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
066 }
067
068 int main (int argc, char *argv[])
069 {
070 int term;
071
072 if (argc < 2)
073 {
074 fprintf(stderr, "Usage: %s /dev/pts/number\n", argv[0]);
075 return -1;
076 }
077
078 term = open(argv[1], O_RDWR);
079 if (term < 0)
080 {
081 perror("open");
082 return -1;
083 }
084 if (! isatty(term))
085 {
086 fprintf(stderr, "%s is not a valid terminal\n", argv[1]);
087 return -1;
088 }
089
090 set_input_mode();
091
092 while (1)
093 {
094 fd_set cur_set;
095 FD_ZERO(& cur_set);
096 FD_SET(STDIN_FILENO, & cur_set);
097 FD_SET(term, & cur_set);
098 if (select(FD_SETSIZE, & cur_set, NULL, NULL, NULL) < 1)
099 continue;
100
101 if (FD_ISSET(term, & cur_set))
102 {
103 char buf[1024];
104 int len = read(term, buf, sizeof(buf));
105
106 if (len >= 1)
107 write(STDOUT_FILENO, buf, len);
108 else
109 {
110 fprintf(stderr, "Master exitted\n");
111 break;
112 }
113 }
114
115 if (FD_ISSET(STDIN_FILENO, & cur_set))
116 {
117 char c;
118 if (read(STDIN_FILENO, &c, 1) == 1)
119 {
120 if (c == 0x4)
121 break;
122 write(term, &c, 1);
123 }
124 else
125 break;
126 }
127 }
128
129 return 0;
130 }