hush: move msh/lash config into hush.c, no code changes
[oweals/busybox.git] / libbb / getpty.c
index 4b65188fbb76f64f6a8a569f51bccdc541aa7488..4bffd9ae635ff56fcba524d8230420eecd0b846a 100644 (file)
@@ -8,21 +8,31 @@
 
 #include "libbb.h"
 
-int getpty(char *line, int size)
+#define DEBUG 0
+
+int FAST_FUNC xgetpty(char *line)
 {
        int p;
+
 #if ENABLE_FEATURE_DEVPTS
        p = open("/dev/ptmx", O_RDWR);
        if (p > 0) {
+               grantpt(p); /* chmod+chown corresponding slave pty */
+               unlockpt(p); /* (what does this do?) */
+#if 0 /* if ptsname_r is not available... */
                const char *name;
-               grantpt(p);
-               unlockpt(p);
-               name = ptsname(p);
+               name = ptsname(p); /* find out the name of slave pty */
                if (!name) {
-                       bb_perror_msg("ptsname error (is /dev/pts mounted?)");
-                       return -1;
+                       bb_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
+               }
+               safe_strncpy(line, name, GETPTY_BUFSIZE);
+#else
+               /* find out the name of slave pty */
+               if (ptsname_r(p, line, GETPTY_BUFSIZE-1) != 0) {
+                       bb_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
                }
-               safe_strncpy(line, name, size);
+               line[GETPTY_BUFSIZE-1] = '\0';
+#endif
                return p;
        }
 #else
@@ -50,7 +60,5 @@ int getpty(char *line, int size)
                }
        }
 #endif /* FEATURE_DEVPTS */
-       return -1;
+       bb_error_msg_and_die("can't find free pty");
 }
-
-