Annotation of /tags/mkinitrd-6_3_1/busybox/miscutils/ttysize.c
Parent Directory | Revision Log
Revision 1143 -
(hide annotations)
(download)
Thu Aug 19 12:44:27 2010 UTC (14 years, 1 month ago) by niro
File MIME type: text/plain
File size: 987 byte(s)
Thu Aug 19 12:44:27 2010 UTC (14 years, 1 month ago) by niro
File MIME type: text/plain
File size: 987 byte(s)
tagged 'mkinitrd-6_3_1'
1 | niro | 816 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Replacement for "stty size", which is awkward for shell script use. | ||
4 | * - Allows to request width, height, or both, in any order. | ||
5 | * - Does not complain on error, but returns width 80, height 24. | ||
6 | * - Size: less than 200 bytes | ||
7 | * | ||
8 | * Copyright (C) 2007 by Denys Vlasenko <vda.linux@googlemail.com> | ||
9 | * | ||
10 | * Licensed under the GPL v2, see the file LICENSE in this tarball. | ||
11 | */ | ||
12 | #include "libbb.h" | ||
13 | |||
14 | int ttysize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
15 | niro | 984 | int ttysize_main(int argc UNUSED_PARAM, char **argv) |
16 | niro | 816 | { |
17 | unsigned w, h; | ||
18 | struct winsize wsz; | ||
19 | |||
20 | w = 80; | ||
21 | h = 24; | ||
22 | if (!ioctl(0, TIOCGWINSZ, &wsz)) { | ||
23 | w = wsz.ws_col; | ||
24 | h = wsz.ws_row; | ||
25 | } | ||
26 | |||
27 | niro | 984 | if (!argv[1]) { |
28 | niro | 816 | printf("%u %u", w, h); |
29 | } else { | ||
30 | const char *fmt, *arg; | ||
31 | |||
32 | fmt = "%u %u" + 3; /* "%u" */ | ||
33 | while ((arg = *++argv) != NULL) { | ||
34 | char c = arg[0]; | ||
35 | if (c == 'w') | ||
36 | printf(fmt, w); | ||
37 | if (c == 'h') | ||
38 | printf(fmt, h); | ||
39 | fmt = "%u %u" + 2; /* " %u" */ | ||
40 | } | ||
41 | } | ||
42 | bb_putchar('\n'); | ||
43 | return 0; | ||
44 | } |