2008-02-27 20:03:28 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini getpty implementation for busybox
|
|
|
|
* Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-03-06 03:31:45 +05:30
|
|
|
#define DEBUG 0
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC xgetpty(char *line)
|
2008-02-27 20:03:28 +05:30
|
|
|
{
|
|
|
|
int p;
|
|
|
|
#if ENABLE_FEATURE_DEVPTS
|
|
|
|
p = open("/dev/ptmx", O_RDWR);
|
|
|
|
if (p > 0) {
|
|
|
|
const char *name;
|
|
|
|
grantpt(p);
|
|
|
|
unlockpt(p);
|
|
|
|
name = ptsname(p);
|
|
|
|
if (!name) {
|
|
|
|
bb_perror_msg("ptsname error (is /dev/pts mounted?)");
|
2008-05-19 13:48:50 +05:30
|
|
|
goto fail;
|
2008-02-27 20:03:28 +05:30
|
|
|
}
|
2008-03-17 14:34:04 +05:30
|
|
|
safe_strncpy(line, name, GETPTY_BUFSIZE);
|
2008-02-27 20:03:28 +05:30
|
|
|
return p;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct stat stb;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
strcpy(line, "/dev/ptyXX");
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
line[8] = "pqrstuvwxyzabcde"[i];
|
|
|
|
line[9] = '0';
|
|
|
|
if (stat(line, &stb) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
line[9] = j < 10 ? j + '0' : j - 10 + 'a';
|
|
|
|
if (DEBUG)
|
|
|
|
fprintf(stderr, "Trying to open device: %s\n", line);
|
|
|
|
p = open(line, O_RDWR | O_NOCTTY);
|
|
|
|
if (p >= 0) {
|
|
|
|
line[5] = 't';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* FEATURE_DEVPTS */
|
2008-05-19 13:48:50 +05:30
|
|
|
USE_FEATURE_DEVPTS( fail:)
|
|
|
|
bb_error_msg_and_die("open pty");
|
|
|
|
return -1; /* never get here */
|
2008-02-27 20:03:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|