Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active August 3, 2024 05:29
Show Gist options
  • Save Low-power/a69269bb199e5122454c1d0a90eda8fd to your computer and use it in GitHub Desktop.
Save Low-power/a69269bb199e5122454c1d0a90eda8fd to your computer and use it in GitHub Desktop.
/* Copyright 2015-2024 Rivoreo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _ALL_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef __SVR4
#include <sys/stropts.h>
#endif
#include <limits.h>
#ifdef HAVE_OPENPTY
#if defined __GLIBC__ || defined __CYGWIN__ || defined __INTERIX
#include <pty.h>
#elif (defined __APPLE__ && defined __MACH__) || defined __NetBSD__ || defined __OpenBSD__
#include <util.h>
#else
#include <libutil.h>
#endif
#endif
#if defined __OpenBSD__ || defined __NetBSD__
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/tty.h>
#endif
#ifndef __INTERIX
#define HAVE_POSIX_OPENPT
#endif
#define BSD_PTY_NAME_1 "pqrstuvwxyzabcdePQRSTUVWXYZABCDE"
#define BSD_PTY_NAME_2 "0123456789abcdefghijklmnopqrstuv"
static int open_slave_from_master(int master_fd) {
if(grantpt(master_fd) == -1) {
perror("grantpt");
return -1;
}
if(unlockpt(master_fd) == -1) {
perror("unlockpt");
return -1;
}
char *name = ptsname(master_fd);
if(!name) {
perror("ptsname");
return -1;
}
printf("Slave device name \"%s\"\n", name);
int fd = open(name, O_RDWR | O_NOCTTY);
if(fd == -1) {
perror(name);
return -1;
}
#ifdef I_PUSH
ioctl(fd, I_PUSH, "ptem");
ioctl(fd, I_PUSH, "ldterm");
#endif
return fd;
}
int main(int argc, char **argv) {
int master_fd, slave_fd;
if(argc != 2) {
fprintf(stderr, "Usage: %s {"
#ifdef HAVE_POSIX_OPENPT
" posix | posix_openpt |"
#endif
" sysv | unix98 | ptmx[=<path>] | bsd | bsd_pty"
#ifdef HAVE_OPENPTY
" | openpty"
#endif
#ifdef __OpenBSD__
" | openbsd | ptm"
#endif
" }\n", argv[0]);
return -1;
}
#ifdef HAVE_POSIX_OPENPT
if(strcmp(argv[1], "posix") == 0 || strcmp(argv[1], "posix_openpt") == 0) {
master_fd = posix_openpt(O_RDWR | O_NOCTTY);
if(master_fd == -1) {
perror("posix_openpt");
return 1;
}
slave_fd = open_slave_from_master(master_fd);
if(slave_fd == -1) return 1;
} else
#endif
if(strcmp(argv[1], "sysv") == 0 || strcmp(argv[1], "unix98") == 0 || strcmp(argv[1], "ptmx") == 0 || strncmp(argv[1], "ptmx=", 5) == 0) {
if(argv[1][4] == '=') {
master_fd = open(argv[1] + 5, O_RDWR);
if(master_fd == -1) {
perror(argv[1] + 5);
return 1;
}
} else {
master_fd = open("/dev/ptmx", O_RDWR);
if(master_fd == -1) {
perror("open: /dev/ptmx");
return 1;
}
}
slave_fd = open_slave_from_master(master_fd);
if(slave_fd == -1) return 1;
} else if(strcmp(argv[1], "bsd") == 0 || strcmp(argv[1], "bsd_pty") == 0) {
unsigned int i, j;
char path[11] = "/dev/pty";
path[10] = 0;
for(i = 0; i < sizeof BSD_PTY_NAME_1 - 1; i++) {
for(j = 0; j < sizeof BSD_PTY_NAME_2 - 1; j++) {
path[8] = BSD_PTY_NAME_1[i];
path[9] = BSD_PTY_NAME_2[j];
master_fd = open(path, O_RDWR);
if(master_fd == -1) {
perror(path);
continue;
}
path[5] = 't';
printf("Slave device name \"%s\"\n", path);
slave_fd = open(path, O_RDWR);
if(slave_fd == -1) {
perror(path);
return 1;
}
goto allocated;
}
}
fputs("Cannot find a free master device\n", stderr);
return 1;
#ifdef HAVE_OPENPTY
} else if(strcmp(argv[1], "openpty") == 0) {
char path[PATH_MAX];
if(openpty(&master_fd, &slave_fd, path, NULL, NULL) < 0) {
perror("openpty");
return 1;
}
printf("Slave device name \"%s\"\n", path);
#endif
#if defined __OpenBSD__ || defined __NetBSD__
} else if(strcmp(argv[1], "openbsd") == 0 || strcmp(argv[1], "ptm") == 0) {
int ptm_fd = open("/dev/ptm", O_RDWR);
if(ptm_fd == -1) {
perror("open: /dev/ptm");
return 1;
}
printf("ptm_fd = %d\n", ptm_fd);
printf("isatty(%d) = %d\n", ptm_fd, isatty(ptm_fd));
struct ptmget data;
if(ioctl(ptm_fd, PTMGET, &data) < 0) {
perror("ioctl PTMGET");
return 1;
}
close(ptm_fd);
master_fd = data.cfd;
slave_fd = data.sfd;
printf("Slave device name \"%s\"\n", data.sn);
#endif
} else {
fprintf(stderr, "%s: API type %s not recognized\n", argv[0], argv[1]);
return -1;
}
allocated:
printf("master_fd = %d, slave_fd = %d\n", master_fd, slave_fd);
printf("isatty(%d) = %d, isatty(%d) = %d\n", master_fd, isatty(master_fd), slave_fd, isatty(slave_fd));
const char *name = ttyname(master_fd);
printf("ttyname(%d) = %p<%s>, ", master_fd, name, name);
name = ttyname(slave_fd);
printf("ttyname(%d) = %p<%s>\n", slave_fd, name, name);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment