summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libshouldbeinlibc/idvec.c81
-rw-r--r--libshouldbeinlibc/idvec.h46
-rw-r--r--libshouldbeinlibc/termsize.c54
3 files changed, 181 insertions, 0 deletions
diff --git a/libshouldbeinlibc/idvec.c b/libshouldbeinlibc/idvec.c
new file mode 100644
index 00000000..412e7c32
--- /dev/null
+++ b/libshouldbeinlibc/idvec.c
@@ -0,0 +1,81 @@
+/* Routines for vectors of integers
+
+ Copyright (C) 1995 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <malloc.h>
+#include <string.h>
+#include <ivec.h>
+
+/* Return a new ivec, or NULL if there wasn't enough memory. */
+struct ivec *
+make_ivec ()
+{
+ struct ivec *ivec = malloc (sizeof (struct ivec));
+ if (ivec)
+ {
+ ivec->ints = 0;
+ ivec->num = ivec->alloced = 0;
+ }
+ return ivec;
+}
+
+/* Insert I into IVEC at position POS, returning ENOMEM if there wasn't
+ enough memory, or 0. */
+error_t
+ivec_insert (struct ivec *ivec, unsigned pos, int i)
+{
+ if (ivec->alloced == ivec->num)
+ {
+ unsigned new_size = ivec->alloced * 2 + 1;
+ int *new_ints = realloc (ivec->ints, new_size * sizeof (int));
+
+ if (! new_ints)
+ return ENOMEM;
+
+ ivec->alloced = new_size;
+ ivec->ints = new_ints;
+ }
+
+ if (pos < ivec->num)
+ bcopy (ivec->ints + pos, ivec->ints + pos + 1, ivec->num - pos);
+
+ ivec->ints[pos] = i;
+ ivec->num++;
+
+ return 0;
+}
+
+/* Add I onto the end of IVEC, returning ENOMEM if there's not enough memory,
+ or 0. */
+error_t
+ivec_add (struct ivec *ivec, int i)
+{
+ return ivec_insert (ivec, ivec->num, i);
+}
+
+/* Returns true if IVEC contains I. */
+int
+ivec_contains (struct ivec *ivec, int i)
+{
+ unsigned j;
+ for (j = 0; j < ivec->num; j++)
+ if (ivec->ints[j] == i)
+ return 1;
+ return 0;
+}
diff --git a/libshouldbeinlibc/idvec.h b/libshouldbeinlibc/idvec.h
new file mode 100644
index 00000000..7ac8c50d
--- /dev/null
+++ b/libshouldbeinlibc/idvec.h
@@ -0,0 +1,46 @@
+/* Routines for vectors of integers
+
+ Copyright (C) 1995 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef __IVEC_H__
+#define __IVEC_H__
+
+#include <errno.h>
+
+struct ivec
+{
+ int *ints;
+ unsigned num, alloced;
+};
+
+/* Return a new ivec, or NULL if there wasn't enough memory. */
+struct ivec *make_ivec ();
+
+/* Insert I into IVEC at position POS, returning ENOMEM if there wasn't
+ enough memory, or 0. */
+error_t ivec_insert (struct ivec *ivec, unsigned pos, int i);
+
+/* Add I onto the end of IVEC, returning ENOMEM if there's not enough memory,
+ or 0. */
+error_t ivec_add (struct ivec *ivec, int i);
+
+/* Returns true if IVEC contains I. */
+int ivec_contains (struct ivec *ivec, int i);
+
+#endif /* __IVEC_H__ */
diff --git a/libshouldbeinlibc/termsize.c b/libshouldbeinlibc/termsize.c
new file mode 100644
index 00000000..46666975
--- /dev/null
+++ b/libshouldbeinlibc/termsize.c
@@ -0,0 +1,54 @@
+/* Function to try and deduce what size the terminal is
+
+ Copyright (C) 1995 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <sys/ioctl.h>
+
+/* Returns what we think is the size of the terminal attached to
+ file descriptor FD, of type TYPE, in WIDTH and/or HEIGHT. If FD is
+ negative, the terminal isn't queried, and if TYPE is NULL, it isn't used.
+ Both WIDTH and HEIGHT may be NULL if only partial information is needed.
+ True is returned upon success. Even if false is returned, both output
+ values are still written, with 0 for unknown, in case partial information
+ is useful. */
+int
+deduce_term_size (int fd, char *type, int *width, int *height)
+{
+ int w = 0, h = 0;
+ struct winsize ws;
+
+ if (fd >= 0 && ioctl (fd, TIOCGWINSZ, &ws) == 0)
+ /* Look at the actual terminal. */
+ {
+ w = ws.ws_col;
+ h = ws.ws_row;
+ }
+ if (((width && !w) || (height && !h)) && type)
+ /* Try the terminal type. */
+ {
+ /* XXX */
+ }
+
+ if (width)
+ *width = w;
+ if (height)
+ *height = h;
+
+ return (!width || w) && (!height && h);
+}