53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#define _XOPEN_SOURCE
|
|
#define _XOPEN_SOURCE_EXTENDED
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
|
|
int main(int argc, char **argv) {
|
|
int ptm;
|
|
char ptsname[64];
|
|
|
|
if ((ptm = open("/dev/ptmx", O_RDWR)) < 0) {
|
|
fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "open()", "/dev/ptmx", strerror(errno));
|
|
|
|
goto error_open_ptmx;
|
|
}
|
|
|
|
if (grantpt(ptm) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n", argv[0], "grantpt()", strerror(errno));
|
|
|
|
goto error_grantpt_ptm;
|
|
}
|
|
|
|
if (unlockpt(ptm) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n", argv[0], "unlockpt()", strerror(errno));
|
|
|
|
goto error_unlockpt_ptm;
|
|
}
|
|
|
|
if (ptsname_r(ptm, ptsname, sizeof(ptsname)) < 0) {
|
|
fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "ptsname_r()", "/dev/ptmx", strerror(errno));
|
|
|
|
goto error_ptsname_r_ptm;
|
|
}
|
|
|
|
printf("pts %s\n", ptsname);
|
|
|
|
close(ptm);
|
|
|
|
return 0;
|
|
|
|
error_unlockpt_ptm:
|
|
error_grantpt_ptm:
|
|
error_ptsname_r_ptm:
|
|
close(ptm);
|
|
|
|
error_open_ptmx:
|
|
return 127;
|
|
}
|