cleaner and safer getpty() I wrote long ago.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23390 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
François Revol 2008-01-11 12:12:58 +00:00
parent 6e11b3f991
commit 586cf6aeba
2 changed files with 36 additions and 18 deletions

View File

@ -40,29 +40,20 @@ SpawningUploadClient::Connect(const string& server, const string& login, const s
bool
SpawningUploadClient::SpawnCommand()
{
char ptypath[20];
char ttypath[20];
//XXX: should use a system-provided TerminalCommand class
BString shellcmd = "exec";
const char *args[] = { "/bin/sh", "-c", NULL, NULL };
char path[20];
int pty;
for (pty = 0; pty < 0x50; pty++) {
int fd;
sprintf(path, "/dev/pt/%c%1x", 'p' + pty / 16, pty & 0x0f);
printf("trying %s\n", path);
fd = open(path, O_RDWR);
if (fd < 0)
continue;
fPty = fd;
break;
}
if (fPty < 0)
return false;
sprintf(path, "/dev/tt/%c%1x", 'p' + pty / 16, pty & 0x0f);
shellcmd << " 0<" << path;
shellcmd << " 1>" << path;
int pty = getpty(ptypath, ttypath);
if (pty < 0)
return B_ERROR;
shellcmd << " 0<" << ttypath;
shellcmd << " 1>" << ttypath;
shellcmd += " 2>&1";
shellcmd << " ; ";
shellcmd << "export TTY=" << path << "; "; // BeOS hack
shellcmd << "export TTY=" << ttypath << "; "; // BeOS hack
shellcmd << "export LC_ALL=C; export LANG=C; ";
shellcmd << "exec ";
shellcmd << fCommand.c_str();
@ -112,6 +103,32 @@ SpawningUploadClient::ParseReply()
}
int
SpawningUploadClient::getpty(char *pty, char *tty)
{
static const char major[] = "pqrs";
static const char minor[] = "0123456789abcdef";
uint32 i, j;
int32 fd = -1;
for (i = 0; i < sizeof(major); i++)
{
for (j = 0; j < sizeof(minor); j++)
{
sprintf(pty, "/dev/pt/%c%c", major[i], minor[j]);
sprintf(tty, "/dev/tt/%c%c", major[i], minor[j]);
fd = open(pty, O_RDONLY|O_NOCTTY);
if (fd >= 0)
{
return fd;
}
}
}
return fd;
}
/* the rest is empty */

View File

@ -35,6 +35,7 @@ status_t SetCommandLine(const char *command);
ssize_t SendCommand(const char *cmd);
ssize_t ReadReply(BString *to);
virtual status_t ParseReply();
int getpty(char *pty, char *tty);
int InputPipe() const { return fPty; };
int OutputPipe() const { return fPty; };