hari_sasidharan |
12-28-2005 06:07 AM |
Unable to ssh in this shell
This is the code for a shell
But iam unable to ssh
Tried googling but did not find any useful tips
Code:
void
set_keypress (int fd)
{
struct termios new_settings;
if (tcgetattr (fd, &new_settings) < 0)
{
perror ("error getting the attributes for terminal ");
exit (-1);
}
new_settings.c_lflag &= ~ICANON;
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
if (tcsetattr (fd, TCSANOW, &new_settings) < 0)
{
perror ("error setting the attributes for terminal ");
exit (-1);
}
return;
}
int
open_pty_pair (int *amaster, int *aslave)
{
int master, slave;
char *name;
master = getpt ();
if (master < 0)
{
perror ("The master pty couldn't be opend");
return -1;
}
if (grantpt (master) < 0 || unlockpt (master) < 0)
{
perror ("Grant pt failed ");
close (master);
return -1;
}
name = ptsname (master);
if (name == NULL)
{
perror ("Error getting the master name");
close (master);
return -1;
}
slave = open (name, O_RDWR);
if (slave == -1)
{
perror ("Error opening the slave");
close (master);
return -1;
}
if (isastream (slave))
{
if (ioctl (slave, I_PUSH, "ptem") < 0
|| ioctl (slave, I_PUSH, "ldterm") < 0)
{
perror ("Streams file : ioctl failed");
close (master);
close (slave);
return -1;
}
}
*amaster = master;
*aslave = slave;
return 1;
}
int
main ()
{
int *amaster = malloc (4), *aslave = malloc (4);
if (open_pty_pair (amaster, aslave) < 0)
{
printf ("Allocation of pseudo terminal failed");
exit (0);
}
int pid = fork ();
if (!pid)
{
close (*amaster);
dup2 (*aslave, 0);
dup2 (*aslave, 1);
dup2 (*aslave, 2);
execl ("/bin/bash", "/bin/bash", (char *) 0);
}
else
{
close (*aslave);
set_keypress (*amaster);
struct termios new_settings;
if (tcgetattr (0, &new_settings) < 0)
{
perror ("Error getting the attributes for terminal 0");
exit (0);
}
new_settings.c_lflag &= (~ECHO);
if (tcsetattr (0, TCSANOW, &new_settings) < 0)
{
perror ("Error unsetting echo for terminal 0");
exit (0);
}
int flags;
if ((flags = fcntl (*amaster, F_GETFL, 0)) < 0)
{
perror ("fcntl:getfl failed\n");
exit (-1);
}
flags |= O_NONBLOCK;
if (fcntl (*amaster, F_SETFL, flags) < 0)
{
perror ("Fcntl:setfl failed\n");
exit (-1);
}
int ppid = fork ();
if (!ppid)
{
int ch;
set_keypress (0);
while (1)
{
ch = fgetc (stdin);
if (ch > 0)
{
if (write (*amaster, &ch, 1) < 0)
{
perror ("error writing to the master ");
}
}
}
}
else
{
int len;
char buffer;
dup2 (*amaster, 0);
while (1)
{
if ((buffer = fgetc (stdin)) != -1)
{
fprintf (stdout, "%c", buffer);
}
}
}
}
waitpid (pid, NULL, 0);
return 0;
}
|