+/*
+ * We store our pty backends in a tree sorted by master fd, so that
+ * when we get an uxsel notification we know which backend instance
+ * is the owner of the pty that caused it.
+ */
+static int pty_compare_by_fd(void *av, void *bv)
+{
+ Pty a = (Pty)av;
+ Pty b = (Pty)bv;
+
+ if (a->master_fd < b->master_fd)
+ return -1;
+ else if (a->master_fd > b->master_fd)
+ return +1;
+ return 0;
+}
+
+static int pty_find_by_fd(void *av, void *bv)
+{
+ int a = *(int *)av;
+ Pty b = (Pty)bv;
+
+ if (a < b->master_fd)
+ return -1;
+ else if (a > b->master_fd)
+ return +1;
+ return 0;
+}
+
+static tree234 *ptys_by_fd = NULL;
+
+/*
+ * We also have a tree sorted by child pid, so that when we wait()
+ * in response to the signal we know which backend instance is the
+ * owner of the process that caused the signal.
+ */
+static int pty_compare_by_pid(void *av, void *bv)
+{
+ Pty a = (Pty)av;
+ Pty b = (Pty)bv;
+
+ if (a->child_pid < b->child_pid)
+ return -1;
+ else if (a->child_pid > b->child_pid)
+ return +1;
+ return 0;
+}
+
+static int pty_find_by_pid(void *av, void *bv)
+{
+ pid_t a = *(pid_t *)av;
+ Pty b = (Pty)bv;
+
+ if (a < b->child_pid)
+ return -1;
+ else if (a > b->child_pid)
+ return +1;
+ return 0;
+}
+
+static tree234 *ptys_by_pid = NULL;
+
+/*
+ * If we are using pty_pre_init(), it will need to have already
+ * allocated a pty structure, which we must then return from
+ * pty_init() rather than allocating a new one. Here we store that
+ * structure between allocation and use.
+ *
+ * Note that although most of this module is entirely capable of
+ * handling multiple ptys in a single process, pty_pre_init() is
+ * fundamentally _dependent_ on there being at most one pty per
+ * process, so the normal static-data constraints don't apply.
+ *
+ * Likewise, since utmp is only used via pty_pre_init, it too must
+ * be single-instance, so we can declare utmp-related variables
+ * here.
+ */
+static Pty single_pty = NULL;