From: Ralph Corderoy Date: Fri, 22 Sep 2017 11:47:02 +0000 (+0100) Subject: runpty.c: Factor duplicate code into new open_master_pty(). X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/fad589fd1879f684ba437645e53811320942f8a4?ds=sidebyside;hp=ea1fadd88568e97c6891da8ebe09d02515282e66 runpty.c: Factor duplicate code into new open_master_pty(). Only called twice, but it shows the same actions are done both times. --- 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, ...) {