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