aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-02-02 07:27:35 -0600
committerRob Landley <rob@landley.net>2012-02-02 07:27:35 -0600
commit26e7b5ece4b8b0440048759bc49f3b357a4c4c45 (patch)
tree882313b0de2bb81573806f691d0049591934ad76 /lib/lib.c
parentce8a2671896dcdc7baa9fa9d77cea966e44b9b6b (diff)
downloadtoybox-26e7b5ece4b8b0440048759bc49f3b357a4c4c45.tar.gz
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 82e32aee..a71e20a6 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -174,7 +174,8 @@ void xexec(char **argv)
{
toy_exec(argv);
execvp(argv[0], argv);
- error_exit("No %s", argv[0]);
+
+ perror_exit("exec %s", argv[0]);
}
void xaccess(char *path, int flags)
@@ -756,3 +757,51 @@ void crc_init(unsigned int *crc_table, int little_endian)
crc_table[i] = c;
}
}
+
+// Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
+// set *x=0 and *y=0 before calling to detect failure to set either, or
+// x=80 y=25 to provide defaults
+
+void terminal_size(unsigned *x, unsigned *y)
+{
+ struct winsize ws;
+ int i;
+
+ //memset(&ws, 0, sizeof(ws));
+ for (i=0; i<3; i++) {
+ if (ioctl(i, TIOCGWINSZ, &ws)) continue;
+ if (x) *x = ws.ws_col;
+ if (y) *y = ws.ws_row;
+ }
+ if (x) {
+ char *s = getenv("COLUMNS");
+
+ i = s ? atoi(s) : 0;
+ if (i>0) *x = i;
+ }
+ if (y) {
+ char *s = getenv("ROWS");
+
+ i = s ? atoi(s) : 0;
+ if (i>0) *y = i;
+ }
+}
+
+// This should use a raw tty, fixit later.
+int yesno(int def)
+{
+ char buf[16];
+ int i;
+
+ for (i=0; i<3 && !isatty(i); i++);
+ if (i == 3) return 1;
+
+ sprintf(buf, "(%c/%c):", def ? 'Y' : 'y', def ? 'n' : 'N');
+ write(i, buf, 6);
+ while (read(i, buf, 1)) {
+ if (isspace(*buf)) break;
+ if (tolower(*buf) == 'y') return 1;
+ if (tolower(*buf) == 'n') return 0;
+ }
+ return def;
+}