]> diplodocus.org Git - nmh/commitdiff
runpty.c: Factor duplicate code into new open_master_pty().
authorRalph Corderoy <ralph@inputplus.co.uk>
Fri, 22 Sep 2017 11:47:02 +0000 (12:47 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Fri, 22 Sep 2017 11:47:02 +0000 (12:47 +0100)
Only called twice, but it shows the same actions are done both times.

test/runpty.c

index e8db35f2ebdf2451fa0f3e0841da4cde59e2a69a..ef5cf822f0da6932dd55401ee2e7dd542342fb6a 100644 (file)
@@ -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, ...)
 {