示例#1
0
int open_master_pty(void)
{
  int fd;

  fd = open("/dev/ptmx", O_RDWR);
  if (fd < 0) {
    perror("Failed to open /dev/ptmx");
    return fd;
  }

  setup_tty_attributes(fd);

  return fd;
}
示例#2
0
int open_master_pty(void)
{
  int fd;

  fd = open("/dev/ptmx", O_RDWR);
  if (fd < 0) {
    perror("Failed to open /dev/ptmx");
    return fd;
  }

  /* set control tty */
  if (ioctl(fd, TIOCSCTTY, 1) != 0 )
    perror("Failed to set control tty");

  setup_tty_attributes(fd);

  return fd;
}
示例#3
0
int
main (int argc, char* argv[])
{
  struct termios oldtm;
  fd_set sel, sel0;
  int status;
  char* newarg0;

  /* SIGINT and SIGBREAK are indistinctive under cygwin environment. */
  /* Using Win32API to handle SIGINT.                              */
  SetConsoleCtrlHandler (ctrl_handler, TRUE);

  if (argc < 1)
    {
      fprintf (stderr, "Unable to get arg[0].");
      exit (1);
    }

  newarg0 = real_command_name (argv[0]);
  if (newarg0)
    {
      argv[0] = newarg0;
      exec_target (argv);     /* This sets globals masterfd, child_pid */
    }
  else if (argc > 1)
    {
      exec_target (argv + 1); /* This sets globals masterfd, child_pid */
    }
  else
    {
      fprintf (stderr, "Unable to get arg[1].");
      exit (1);
    }

  setup_tty_attributes ();

  FD_ZERO (&sel0);
  FD_SET (masterfd, &sel0);
  FD_SET (0, &sel0);

  /* communication loop */
  while (1)
    {
      char buf[BUFSIZE];
      int ret;

      sel = sel0;
      if (select (FD_SETSIZE, &sel, NULL, NULL, NULL) <= 0)
	break;

      if (FD_ISSET (masterfd, &sel))
	{
	  ret = read (masterfd, buf, BUFSIZE);
	  if (ret > 0)
	      write (1, buf, ret);
	  else
	    break;
	}
      else if (FD_ISSET (0, &sel))
	{
	  ret = read (0, buf, BUFSIZE);
	  if (ret > 0)
	      write (masterfd, buf, ret);
	  else
	    {
	      FD_CLR (0, &sel0);
	      close (masterfd);
	    }
	}
    }

  restore_tty_attributes ();

  kill (child_pid, SIGKILL);
  waitpid (child_pid, &status, 0);
  return WEXITSTATUS (status);
}