generic/pty_dev.c
int pty_open(char *sl_name)
{
    int  mr_fd;
#if defined (HAVE_GETPT) && defined (HAVE_GRANTPT) && defined (HAVE_UNLOCKPT) && defined (HAVE_PTSNAME)
    char *ptyname;

    if((mr_fd=getpt()) < 0)
 	return -1;
    if(grantpt(mr_fd) != 0)
	return -1;
    if(unlockpt(mr_fd) != 0)
	return -1;
    if ((ptyname = (char*)ptsname(mr_fd)) == NULL)
	return -1;
    strcpy(sl_name, ptyname);
    return mr_fd;

#else

    char ptyname[] = "/dev/ptyXY";
    char ch[] = "pqrstuvwxyz";
    char digit[] = "0123456789abcdefghijklmnopqrstuv";
    int  l, m;

    /* This algorithm should work for almost all standard Unices */	
    for(l=0; ch[l]; l++ ) {
        for(m=0; digit[m]; m++ ) {
	 	ptyname[8] = ch[l];
		ptyname[9] = digit[m];
		/* Open the master */
		if( (mr_fd=open(ptyname, O_RDWR)) < 0 )
	 	   continue;
		/* Check the slave */
		ptyname[5] = 't';
		if( (access(ptyname, R_OK | W_OK)) < 0 ){
		   close(mr_fd);
		   ptyname[5] = 'p';
		   continue;
		}
		strcpy(sl_name,ptyname);
		return mr_fd;
	    }
	}
	return -1;
#endif
}
/* 
 * Allocate pseudo tty, returns master side fd. 
 * Stores slave name in the first arg(must be large enough).
 */
Allocate pseudo tty, returns master side fd.
Stores slave name in the first arg(must be large enough).
Allocate pseudo tty returns master side fd . Stores slave name in the first arg ( must be large enough ) .
int pty_open(char *sl_name)
{
    int  mr_fd;
#if defined (HAVE_GETPT) && defined (HAVE_GRANTPT) && defined (HAVE_UNLOCKPT) && defined (HAVE_PTSNAME)
    char *ptyname;
    if((mr_fd=getpt()) < 0)
 	return -1;
    if(grantpt(mr_fd) != 0)
	return -1;
    if(unlockpt(mr_fd) != 0)
	return -1;
    if ((ptyname = (char*)ptsname(mr_fd)) == NULL)
	return -1;
    strcpy(sl_name, ptyname);
    return mr_fd;
#else
    char ptyname[] = "/dev/ptyXY";
    char ch[] = "pqrstuvwxyz";
    char digit[] = "0123456789abcdefghijklmnopqrstuv";
    int  l, m;
    for(l=0; ch[l]; l++ ) {
        for(m=0; digit[m]; m++ ) {
	 	ptyname[8] = ch[l];
		ptyname[9] = digit[m];
		if( (mr_fd=open(ptyname, O_RDWR)) < 0 )
	 	   continue;
		ptyname[5] = 't';
		if( (access(ptyname, R_OK | W_OK)) < 0 ){
		   close(mr_fd);
		   ptyname[5] = 'p';
		   continue;
		}
		strcpy(sl_name,ptyname);
		return mr_fd;
	    }
	}
	return -1;
#endif
}
int pty_open ( char * sl_name ) { int mr_fd ; #if defined ( HAVE_GETPT ) && defined ( HAVE_GRANTPT ) && defined ( HAVE_UNLOCKPT ) && defined ( HAVE_PTSNAME ) char * ptyname ; if ( ( mr_fd = getpt ( ) ) < 0 ) return -1 ; if ( grantpt ( mr_fd ) != 0 ) return -1 ; if ( unlockpt ( mr_fd ) != 0 ) return -1 ; if ( ( ptyname = ( char * ) ptsname ( mr_fd ) ) == NULL ) return -1 ; strcpy ( sl_name , ptyname ) ; return mr_fd ; #else char ptyname [ ] = " " ; char ch [ ] = " " ; char digit [ ] = " " ; int l , m ; for ( l = 0 ; ch [ l ] ; l ++ ) { for ( m = 0 ; digit [ m ] ; m ++ ) { ptyname [ 8 ] = ch [ l ] ; ptyname [ 9 ] = digit [ m ] ; if ( ( mr_fd = open ( ptyname , O_RDWR ) ) < 0 ) continue ; ptyname [ 5 ] = ' ' ; if ( ( access ( ptyname , R_OK | W_OK ) ) < 0 ) { close ( mr_fd ) ; ptyname [ 5 ] = ' ' ; continue ; } strcpy ( sl_name , ptyname ) ; return mr_fd ; } } return -1 ; #endif }
Allocate pseudo tty, returns master side fd.
Allocate pseudo tty returns master side fd .
/* This algorithm should work for almost all standard Unices */ /* Open the master */ /* Check the slave */