Blame src/lftp_pty.c

Packit Service a2489d
/*
Packit Service a2489d
 * lftp - file transfer program
Packit Service a2489d
 *
Packit Service a2489d
 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
Packit Service a2489d
 *
Packit Service a2489d
 * This program is free software; you can redistribute it and/or modify
Packit Service a2489d
 * it under the terms of the GNU General Public License as published by
Packit Service a2489d
 * the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
 * (at your option) any later version.
Packit Service a2489d
 *
Packit Service a2489d
 * This program is distributed in the hope that it will be useful,
Packit Service a2489d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
 * GNU General Public License for more details.
Packit Service a2489d
 *
Packit Service a2489d
 * You should have received a copy of the GNU General Public License
Packit Service a2489d
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service a2489d
 */
Packit Service a2489d
Packit Service a2489d
/* Based on sshpty.c from openssh written by Tatu Ylonen <ylo@cs.hut.fi> */
Packit Service a2489d
Packit Service a2489d
#include <config.h>
Packit Service a2489d
Packit Service a2489d
#include <sys/types.h>
Packit Service a2489d
#include <unistd.h>
Packit Service a2489d
#include <string.h>
Packit Service a2489d
#include <signal.h>
Packit Service a2489d
#include <fcntl.h>
Packit Service a2489d
#include <stdio.h>
Packit Service a2489d
#include <stdlib.h>
Packit Service a2489d
#include <sys/stat.h>
Packit Service a2489d
Packit Service a2489d
#ifdef HAVE_TERMIOS_H
Packit Service a2489d
# include <termios.h>
Packit Service a2489d
#endif
Packit Service a2489d
#ifdef HAVE_UTIL_H
Packit Service a2489d
# include <util.h>
Packit Service a2489d
#endif
Packit Service a2489d
#ifdef HAVE_PTY_H
Packit Service a2489d
# include <pty.h>
Packit Service a2489d
#endif
Packit Service a2489d
#ifdef HAVE_SYS_STROPTS_H
Packit Service a2489d
# include <sys/stropts.h>
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
#include "lftp_pty.h"
Packit Service a2489d
Packit Service a2489d
const char *open_pty(int *ptyfd, int *ttyfd)
Packit Service a2489d
{
Packit Service a2489d
   const char *name=0;
Packit Service a2489d
   void *old_sigchld=signal(SIGCHLD,SIG_DFL);
Packit Service a2489d
   *ptyfd=*ttyfd=-1;
Packit Service a2489d
Packit Service a2489d
#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Packit Service a2489d
   /* openpty(3) exists in OSF/1 and some other os'es */
Packit Service a2489d
   openpty(ptyfd, ttyfd, NULL, NULL, NULL);
Packit Service a2489d
#else /* HAVE_OPENPTY */
Packit Service a2489d
#ifdef HAVE__GETPTY
Packit Service a2489d
   /*
Packit Service a2489d
    * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
Packit Service a2489d
    * pty's automagically when needed
Packit Service a2489d
    */
Packit Service a2489d
   name=_getpty(ptyfd,O_RDWR|O_NOCTTY,0600,0);
Packit Service a2489d
#else /* HAVE__GETPTY */
Packit Service a2489d
#if defined(HAVE_DEV_PTMX)
Packit Service a2489d
   /*
Packit Service a2489d
    * This code is used e.g. on Solaris 2.x.  (Note that Solaris 2.3
Packit Service a2489d
    * also has bsd-style ptys, but they simply do not work.)
Packit Service a2489d
    */
Packit Service a2489d
   *ptyfd=open("/dev/ptmx",O_RDWR|O_NOCTTY);
Packit Service a2489d
   if(*ptyfd<0)
Packit Service a2489d
      goto fail;
Packit Service a2489d
   if(grantpt(*ptyfd)<0)
Packit Service a2489d
      goto fail;
Packit Service a2489d
   if(unlockpt(*ptyfd)<0)
Packit Service a2489d
      goto fail;
Packit Service a2489d
   name=ptsname(*ptyfd);
Packit Service a2489d
#else /* HAVE_DEV_PTMX */
Packit Service a2489d
#ifdef HAVE_DEV_PTS_AND_PTC
Packit Service a2489d
{
Packit Service a2489d
   /* AIX-style pty code. */
Packit Service a2489d
   *ptyfd=open("/dev/ptc",O_RDWR|O_NOCTTY);
Packit Service a2489d
   if(*ptyfd<0)
Packit Service a2489d
      goto fail;
Packit Service a2489d
   name=ttyname(*ptyfd);
Packit Service a2489d
}
Packit Service a2489d
#else /* HAVE_DEV_PTS_AND_PTC */
Packit Service a2489d
{
Packit Service a2489d
   /* BSD-style pty code. */
Packit Service a2489d
   char master[64];
Packit Service a2489d
   static char slave[64];
Packit Service a2489d
   int i;
Packit Service a2489d
   const char *ptymajors="pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit Service a2489d
   const char *ptyminors="0123456789abcdef";
Packit Service a2489d
   int num_minors=strlen(ptyminors);
Packit Service a2489d
   int num_ptys=strlen(ptymajors)*num_minors;
Packit Service a2489d
Packit Service a2489d
   for(i=0; i
Packit Service a2489d
   {
Packit Service a2489d
      sprintf(master,"/dev/pty%c%c",ptymajors[i/num_minors],ptyminors[i%num_minors]);
Packit Service a2489d
      sprintf(slave, "/dev/tty%c%c",ptymajors[i/num_minors],ptyminors[i%num_minors]);
Packit Service a2489d
      *ptyfd=open(master,O_RDWR|O_NOCTTY);
Packit Service a2489d
      if(*ptyfd>=0)
Packit Service a2489d
	 break;
Packit Service a2489d
      /* Try SCO style naming */
Packit Service a2489d
      sprintf(master,"/dev/ptyp%d",i);
Packit Service a2489d
      sprintf(slave, "/dev/ttyp%d",i);
Packit Service a2489d
      *ptyfd=open(master,O_RDWR|O_NOCTTY);
Packit Service a2489d
      if(*ptyfd>=0)
Packit Service a2489d
	 break;
Packit Service a2489d
   }
Packit Service a2489d
   name=slave;
Packit Service a2489d
}
Packit Service a2489d
#endif /* HAVE_DEV_PTS_AND_PTC */
Packit Service a2489d
#endif /* HAVE_DEV_PTMX */
Packit Service a2489d
#endif /* HAVE__GETPTY */
Packit Service a2489d
#endif /* HAVE_OPENPTY */
Packit Service a2489d
Packit Service a2489d
   if(*ptyfd<0)
Packit Service a2489d
      goto fail;
Packit Service a2489d
   if(name && *ttyfd<0)
Packit Service a2489d
      *ttyfd=open(name,O_RDWR|O_NOCTTY);
Packit Service a2489d
   if(!name && *ttyfd>=0)
Packit Service a2489d
      name=ttyname(*ttyfd);
Packit Service a2489d
Packit Service a2489d
   if(!name || *ttyfd<0)
Packit Service a2489d
      goto fail;
Packit Service a2489d
Packit Service a2489d
#if defined(HAVE_DEV_PTMX) && defined(I_PUSH) \
Packit Service a2489d
 && !defined(HAVE__GETPTY) && !defined(HAVE_OPENPTY)
Packit Service a2489d
   ioctl(*ttyfd, I_PUSH, "ptem");
Packit Service a2489d
   ioctl(*ttyfd, I_PUSH, "ldterm");
Packit Service a2489d
   ioctl(*ttyfd, I_PUSH, "ttcompat");
Packit Service a2489d
#endif
Packit Service a2489d
#ifdef HAVE_FCHMOD
Packit Service a2489d
   fchmod(*ttyfd,0600);
Packit Service a2489d
#else
Packit Service a2489d
   chmod(name,0600);
Packit Service a2489d
#endif
Packit Service a2489d
   signal(SIGCHLD,old_sigchld);
Packit Service a2489d
   return name;
Packit Service a2489d
Packit Service a2489d
fail:
Packit Service a2489d
   if(*ttyfd>=0)
Packit Service a2489d
      close(*ttyfd);
Packit Service a2489d
   if(*ptyfd>=0)
Packit Service a2489d
      close(*ptyfd);
Packit Service a2489d
   signal(SIGCHLD,old_sigchld);
Packit Service a2489d
   return 0;
Packit Service a2489d
}