aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-02-27 14:33:28 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-02-27 14:33:28 +0000
commitc8f2f74ddd518ab3d5dedbd6fa75ee2d89b09e85 (patch)
tree05e759953e75b25e136d6a58d69be32067de163a /libbb
parent5014dada3fa0bb6f6873e28fe6491f0789239cdc (diff)
downloadbusybox-c8f2f74ddd518ab3d5dedbd6fa75ee2d89b09e85.tar.gz
libbb: add forgotten part of "script" applet change
Diffstat (limited to 'libbb')
-rw-r--r--libbb/getpty.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/libbb/getpty.c b/libbb/getpty.c
new file mode 100644
index 000000000..4b65188fb
--- /dev/null
+++ b/libbb/getpty.c
@@ -0,0 +1,56 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini getpty implementation for busybox
+ * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+
+int getpty(char *line, int size)
+{
+ int p;
+#if ENABLE_FEATURE_DEVPTS
+ p = open("/dev/ptmx", O_RDWR);
+ if (p > 0) {
+ const char *name;
+ grantpt(p);
+ unlockpt(p);
+ name = ptsname(p);
+ if (!name) {
+ bb_perror_msg("ptsname error (is /dev/pts mounted?)");
+ return -1;
+ }
+ safe_strncpy(line, name, size);
+ return p;
+ }
+#else
+ struct stat stb;
+ int i;
+ int j;
+
+ strcpy(line, "/dev/ptyXX");
+
+ for (i = 0; i < 16; i++) {
+ line[8] = "pqrstuvwxyzabcde"[i];
+ line[9] = '0';
+ if (stat(line, &stb) < 0) {
+ continue;
+ }
+ for (j = 0; j < 16; j++) {
+ line[9] = j < 10 ? j + '0' : j - 10 + 'a';
+ if (DEBUG)
+ fprintf(stderr, "Trying to open device: %s\n", line);
+ p = open(line, O_RDWR | O_NOCTTY);
+ if (p >= 0) {
+ line[5] = 't';
+ return p;
+ }
+ }
+ }
+#endif /* FEATURE_DEVPTS */
+ return -1;
+}
+
+