Changeset 8076
- Timestamp:
- 11/01/04 16:37:19 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/j/src/jpty/jpty.c
r2 r8076 2 2 * jpty.c 3 3 * 4 * Copyright (C) 2000-200 1Peter Graves4 * Copyright (C) 2000-2004 Peter Graves 5 5 * 6 6 * This program is free software; you can redistribute it and/or … … 25 25 #include <sys/stat.h> 26 26 #include <termios.h> 27 #define __USE_XOPEN 27 28 #endif 28 29 … … 40 41 { 41 42 #ifndef __CYGWIN__ 42 char slave_name[2 0];43 char slave_name[256]; 43 44 pid_t pid; 44 45 int fdm; … … 46 47 int i = 1; 47 48 48 if (argc < 2) exit(1); 49 if (argc < 2) 50 exit(1); 49 51 50 52 /* Check for -cd option. */ 51 if (!strcmp("-cd", argv[i])) 52 { 53 if (!strcmp("-cd", argv[i])) { 53 54 /* Next arg is directory to change to. */ 54 if (++i < argc) 55 { 55 if (++i < argc) { 56 56 if (chdir(argv[i]) < 0) 57 57 exit(1); … … 61 61 62 62 /* We should not be out of args here! */ 63 if (i >= argc) exit(1); 63 if (i >= argc) 64 exit(1); 64 65 65 66 #ifdef __CYGWIN__ … … 68 69 exit(1); /* Not reached. */ 69 70 #else 70 71 71 fdm = open_master_pty(slave_name, sizeof(slave_name)); 72 72 73 if (fdm < 0) exit(1); 73 if (fdm < 0) 74 exit(1); 74 75 75 76 pid = fork(); 76 77 77 if (pid < 0) exit(1); 78 if (pid < 0) 79 exit(1); 78 80 79 if (pid == 0) 80 { 81 if (pid == 0) { 81 82 /* Child process. */ 82 83 int slave; … … 84 85 close(fdm); 85 86 86 if (setsid() < 0) exit(1); 87 if (setsid() < 0) 88 exit(1); 87 89 88 90 slave = open(slave_name, O_RDWR); 89 91 90 if (slave < 0) exit(1); 92 if (slave < 0) 93 exit(1); 91 94 92 95 #ifdef __linux__ 93 if (ioctl(slave, TIOCSCTTY, NULL) < 0) exit(1); 96 if (ioctl(slave, TIOCSCTTY, NULL) < 0) 97 exit(1); 94 98 #endif 99 if (dup2(slave, STDIN_FILENO) != STDIN_FILENO) 100 exit(1); 101 if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO) 102 exit(1); 103 if (dup2(slave, STDERR_FILENO) != STDERR_FILENO) 104 exit(1); 95 105 96 if (dup2(slave, STDIN_FILENO) != STDIN_FILENO) exit(1); 97 98 if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO) exit(1); 99 100 if (dup2(slave, STDERR_FILENO) != STDERR_FILENO) exit(1); 101 102 if (slave > STDERR_FILENO) close(slave); 106 if (slave > STDERR_FILENO) 107 close(slave); 103 108 104 109 set_noecho(STDIN_FILENO); … … 113 118 114 119 /* Parent process. */ 115 116 120 loop(fdm); 117 118 121 exit(0); 119 120 122 #endif 121 123 } … … 125 127 { 126 128 struct termios t; 127 if (tcgetattr(fd, &t) < 0) exit(1); 129 if (tcgetattr(fd, &t) < 0) 130 exit(1); 128 131 t.c_lflag &= ~(ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); 129 132 t.c_oflag &= ~(ONLCR); 130 if (tcsetattr(fd, TCSANOW, &t) < 0) exit(1); 133 if (tcsetattr(fd, TCSANOW, &t) < 0) 134 exit(1); 131 135 } 132 136 … … 138 142 int done = 0; 139 143 140 while (!done) 141 { 144 while (!done) { 142 145 FD_ZERO(&fdset); 143 146 FD_SET(STDIN_FILENO, &fdset); … … 146 149 select(fdm + 1, &fdset, NULL, NULL, NULL); 147 150 148 if (FD_ISSET(fdm, &fdset)) 149 { 151 if (FD_ISSET(fdm, &fdset)) { 150 152 int i = read(fdm, buf, sizeof(buf)); 151 153 … … 156 158 } 157 159 158 if (FD_ISSET(STDIN_FILENO, &fdset)) 159 { 160 if (FD_ISSET(STDIN_FILENO, &fdset)) { 160 161 int i = read(STDIN_FILENO, buf, sizeof(buf)); 161 162 … … 170 171 static int open_master_pty(char *namebuf, size_t bufsize) 171 172 { 172 char pattern[] = "/dev/ptyXX"; 173 int fdm; 174 char *sname; 173 175 174 if (bufsize >= sizeof(pattern))175 {176 int i, j;176 fdm = open("/dev/ptmx", O_RDWR); 177 if (fdm < 0) 178 return -1; 177 179 178 strcpy(namebuf, pattern); 180 unlockpt(fdm); 181 grantpt(fdm); 179 182 180 for (i = 0; i < 16; i++) 181 { 182 struct stat statbuf; 183 sname = ptsname(fdm); 184 if (strlen(sname) >= bufsize) 185 return -1; 186 strcpy(namebuf, sname); 183 187 184 namebuf[8] = "pqrstuvwxyzabcde"[i]; 185 namebuf[9] = '0'; 186 187 if (stat(namebuf, &statbuf) < 0) 188 continue; 189 190 for (j = 0; j < 16; j++) 191 { 192 int fdm; 193 194 namebuf[9] = "0123456789abcdef"[j]; 195 196 fdm = open(namebuf, O_RDWR); 197 198 if (fdm > 0) 199 { 200 namebuf[5] = 't'; 201 return fdm; 202 } 203 } 204 } 205 } 206 207 return -1; 188 return fdm; 208 189 } 209 190 #endif 210
Note: See TracChangeset
for help on using the changeset viewer.