It's now using setenv() under Haiku instead of its own stuff - and that will

now mimic setenv() instead of some strangely different thing :)
Rearranged and corrected the code that opens the PTY, and TTY.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13478 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-07-06 03:30:19 +00:00
parent 0b4ce825fb
commit f87938dc2e
+44 -42
View File
@@ -67,27 +67,33 @@ Check \"Shell\" in preferance panel.' "\
* Set environment varriable.
*/
void
Setenv (const char *var, const char *value)
#ifndef __HAIKU__
int
setenv(const char *var, const char *value, bool overwrite)
{
int envindex = 0;
const int len = strlen(var);
const int val_len = strlen (value);
while (environ[envindex] != NULL) {
if (strncmp (environ [envindex], var, len) == 0) {
if (!strncmp(environ[envindex], var, len)) {
/* found it */
environ[envindex] = (char *)malloc ((unsigned)len + val_len + 1);
sprintf (environ [envindex], "%s%s", var, value);
return;
if (overwrite) {
environ[envindex] = (char *)malloc((unsigned)len + val_len + 2);
sprintf(environ[envindex], "%s=%s", var, value);
}
return 0;
}
envindex++;
}
environ [envindex] = (char *) malloc ((unsigned)len + val_len + 1);
sprintf (environ [envindex], "%s%s", var, value);
environ[envindex] = (char *)malloc((unsigned)len + val_len + 2);
sprintf(environ[envindex], "%s=%s", var, value);
environ[++envindex] = NULL;
return 0;
}
#endif
/*
* reapchild. Child process is out there, let's catch its terminaltion.
@@ -122,13 +128,14 @@ typedef struct
pid_t sh_pid;
char tty_name[B_PATH_NAME_LENGTH];
int
spawn_shell(int row, int col, const char *command, const char *coding)
{
int done = 0;
pid_t pgrp;
int master = 0;
int master = -1;
int slave;
struct termios tio;
struct winsize ws;
@@ -154,32 +161,35 @@ spawn_shell (int row, int col, const char *command, const char *coding)
* which is already in use, so we simply go until the open succeeds.
*/
DIR *dir;
dir = opendir("/dev/pt/");
struct dirent *dir_entry;
char pty_name[B_PATH_NAME_LENGTH];
while(NULL != (dir_entry = readdir(dir)))
{
if (dir_entry->d_name[0] == '.') // skip . and ..
DIR *dir = opendir("/dev/pt/");
if (dir != NULL) {
struct dirent *dirEntry;
while ((dirEntry = readdir(dir)) != NULL) {
// skip '.' and '..'
if (dirEntry->d_name[0] == '.')
continue;
sprintf(pty_name, "/dev/pt/%s", dir_entry->d_name);
printf("%s\n", pty_name);
master = open(pty_name, O_RDWR);
if (master >= 0)
char ptyName[B_PATH_NAME_LENGTH];
sprintf(ptyName, "/dev/pt/%s", dirEntry->d_name);
master = open(ptyName, O_RDWR);
if (master >= 0) {
// Set the tty that corresponds to the pty we found
sprintf(tty_name, "/dev/tt/%s", dirEntry->d_name);
break;
} else {
fprintf(stderr, "could not open %s: %s\n", ptyName, strerror(errno));
}
}
closedir(dir);
}
// If master is still < 0 then we haven't found a tty we can use
if (master < 0)
{
if (master < 0) {
printf("didn't find any available pesudo ttys.");
return -1;
}
// Set the tty that corresponds to the pty we found
sprintf (tty_name, "/dev/tt/%s", dir_entry->d_name);
/*
* Get the modes of the current terminal. We will duplicates these
* on the pseudo terminal.
@@ -191,7 +201,6 @@ spawn_shell (int row, int col, const char *command, const char *coding)
return -1;
}
/* Fork a child process. */
if ((sh_pid = fork()) < 0) {
close(master);
@@ -199,15 +208,14 @@ spawn_shell (int row, int col, const char *command, const char *coding)
}
if (sh_pid == 0) {
/*
* Now in child process.
*/
// Now in child process.
/*
* Make our controlling tty the pseudo tty. This hapens because
* we cleared our original controlling terminal above.
*/
// ToDo: why two of them in the first place?
close(cp_pipe[0]);
close(pc_pipe[1]);
@@ -247,7 +255,6 @@ spawn_shell (int row, int col, const char *command, const char *coding)
*/
tio.c_line = 0;
tio.c_lflag |= ECHOE;
/* input: nl->nl, cr->nl */
@@ -260,7 +267,6 @@ spawn_shell (int row, int col, const char *command, const char *coding)
tio.c_oflag |= ONLCR;
tio.c_oflag |= OPOST;
/* baud rate is 19200 (equal beterm) */
tio.c_cflag &= ~(CBAUD);
tio.c_cflag |= B19200;
@@ -276,10 +282,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
* enable signals, canonical processing (erase, kill, etc), echo.
*/
tio.c_lflag |= ISIG|ICANON|ECHO|ECHOE|ECHONL;
tio.c_lflag &= ~ECHOK;
tio.c_lflag &= ~IEXTEN;
tio.c_lflag &= ~(ECHOK | IEXTEN);
/* set control charactors. */
tio.c_cc[VINTR] = 'C' & 0x1f; /* '^C' */
@@ -358,16 +361,15 @@ spawn_shell (int row, int col, const char *command, const char *coding)
/*
* setenv TERM and TTY.
*/
Setenv ("TERM=", "xterm");
Setenv ("TTY=",tty_name);
Setenv ("TTYPE=", coding);
setenv("TERM", "xterm", true);
setenv("TTY", tty_name, true);
setenv("TTYPE", coding, true);
/*
* If don't set command args, exec SHELL_COMMAND.
*/
if (command == NULL) {
if (command == NULL)
command = SHELL_COMMAND;
}
memcpy (com_line, command, 256);
ptr = com_line;
@@ -389,7 +391,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
args[i] = NULL;
Setenv ("SHELL=", *args);
setenv("SHELL", *args, true);
/*
* Print Welcome Message.