示例#1
0
int
allocatePty(int *pty_return, char **line_return)
{
    char name[12], *line = NULL;
    int pty = -1;
    const char *name1 = "pqrstuvwxyzPQRST";
    char buffer[80];
    char *name2 = strcpy(buffer, "0123456789abcdefghijklmnopqrstuv");
    const char *p1;
    char *p2;

#if defined(HAVE_GRANTPT)
    int rc;

#ifdef HAVE_POSIX_OPENPT
    pty = posix_openpt(O_RDWR);
#else
    pty = open("/dev/ptmx", O_RDWR);
#endif
    if (pty < 0)
	goto bsd;

    rc = grantpt(pty);
    if (rc < 0) {
	close(pty);
	goto bsd;
    }

    rc = unlockpt(pty);
    if (rc < 0) {
	close(pty);
	goto bsd;
    }

    line = strmalloc(ptsname(pty));
    if (!line) {
	close(pty);
	goto bsd;
    }

    *pty_return = pty;
    *line_return = line;
    return 0;

  bsd:
#elif defined(HAVE_OPENPTY)
    int rc;
    char ttydev[80];		/* OpenBSD says at least 16 bytes */

    rc = openpty(&pty, &opened_tty, ttydev, NULL, NULL);
    if (rc < 0) {
	close(pty);
	goto bsd;
    }
    line = strmalloc(ttydev);
    if (!line) {
	close(pty);
	goto bsd;
    }

    *pty_return = pty;
    *line_return = line;
    return 0;

  bsd:
#endif /* HAVE_GRANTPT, etc */

    strcpy(name, "/dev/pty??");
    for (p1 = name1; *p1; p1++) {
	name[8] = *p1;
	for (p2 = name2; *p2; p2++) {
	    name[9] = *p2;
	    pty = open(name, O_RDWR);
	    if (pty >= 0)
		goto found;
	    /* Systems derived from 4.4BSD differ in their pty names,
	       so ENOENT doesn't necessarily imply we're done. */
	    continue;
	}
    }

    goto bail;

  found:
    if ((line = strmalloc(name)) != 0) {
	line[5] = 't';
	fix_pty_perms(line);
	*pty_return = pty;
	*line_return = line;
	return 0;
    }

  bail:
    if (pty >= 0)
	close(pty);
    if (line)
	free(line);
    return -1;
}
示例#2
0
int
allocatePty(int *pty_return, char **line_return)
{
    char name[12], *line = NULL;
    int pty = -1;
    char *name1 = "pqrstuvwxyzPQRST", 
        *name2 = "0123456789abcdefghijklmnopqrstuv";
    char *p1, *p2;

#ifdef HAVE_GRANTPT
    char *temp_line;
    int rc;

    pty = open("/dev/ptmx", O_RDWR);
    if(pty < 0)
        goto bsd;

    rc = grantpt(pty);
    if(rc < 0) {
        close(pty);
        goto bsd;
    }

    rc = unlockpt(pty);
    if(rc < 0) {
        close(pty);
        goto bsd;
    }

    temp_line = ptsname(pty);
    if(!temp_line) {
        close(pty);
        goto bsd;
    }
    line = strdup(temp_line);
    if(!line) {
        close(pty);
        return -1;
    }

    fix_pty_perms(line);

    *pty_return = pty;
    *line_return = line;
    return 0;
#endif /* HAVE_GRANTPT */

#ifdef HAVE_OPENPTY
    if(openpty(pty_return, &opened_tty, NULL, NULL, NULL) < 0)
        return -1;
    *line_return = NULL;	/* unused, but free()ed */
    return 0;
#endif /* HAVE_OPENPTY */

  bsd:
    strcpy(name, "/dev/pty??");
    for(p1 = name1; *p1; p1++) {
        name[8] = *p1;
        for(p2 = name2; *p2; p2++) {
            name[9] = *p2;
            pty = open(name, O_RDWR);
            if(pty >= 0)
                goto found;
            /* Systems derived from 4.4BSD differ in their pty names,
               so ENOENT doesn't necessarily imply we're done. */
            continue;
        }
    }

    goto bail;

  found:
    line = strdup(name);
    if(!line)
	goto bail;
    line[5] = 't';
    fix_pty_perms(line);
    *pty_return = pty;
    *line_return = line;
    return 0;

  bail:
    if(pty >= 0)
        close(pty);
    if(line)
        free(line);
    return -1;
}