From fad589fd1879f684ba437645e53811320942f8a4 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Fri, 22 Sep 2017 12:47:02 +0100 Subject: [PATCH] runpty.c: Factor duplicate code into new open_master_pty(). Only called twice, but it shows the same actions are done both times. --- test/runpty.c | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/test/runpty.c b/test/runpty.c index e8db35f2..ef5cf822 100644 --- a/test/runpty.c +++ b/test/runpty.c @@ -21,6 +21,7 @@ #define COMMAND_TIMEOUT 30 +static int open_master_pty(const char *desc); static void die(const char *fmt, ...); int @@ -39,31 +40,8 @@ main(int argc, char *argv[]) *argv, *argv); } - if ((master_in = posix_openpt(O_RDWR | O_NOCTTY)) < 0) { - die("Unable to open master pseudo-tty: %s\n", strerror(errno)); - } - - if ((master_out = posix_openpt(O_RDWR | O_NOCTTY)) < 0) { - die("Unable to open master pseudo-tty: %s\n", strerror(errno)); - } - - if (grantpt(master_in) < 0) { - die("Unable to grant permissions to master pty: %s\n", - strerror(errno)); - } - - if (grantpt(master_out) < 0) { - die("Unable to grant permissions to master pty: %s\n", - strerror(errno)); - } - - if (unlockpt(master_in) < 0) { - die("Unable to unlock master pty: %s\n", strerror(errno)); - } - - if (unlockpt(master_out) < 0) { - die("Unable to unlock master pty: %s\n", strerror(errno)); - } + master_in = open_master_pty("input"); + master_out = open_master_pty("output"); child = fork(); @@ -174,6 +152,25 @@ main(int argc, char *argv[]) exit(0); } +static int +open_master_pty(const char *desc) +{ + int fd; + + if ((fd = posix_openpt(O_RDWR | O_NOCTTY)) == -1) { + die("Unable to open master %s pseudo-tty: %s\n", desc, strerror(errno)); + } + if (grantpt(fd) == -1) { + die("Unable to grant permissions to master %s pty: %s\n", desc, + strerror(errno)); + } + if (unlockpt(fd) == -1) { + die("Unable to unlock master %s pty: %s\n", desc, strerror(errno)); + } + + return fd; +} + static void die(const char *fmt, ...) { -- 2.56.0