00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <fcntl.h>
00023 #include <termios.h>
00024 #ifndef TIOCGWINSZ
00025 #include <sys/ioctl.h>
00026 #endif
00027 #include <unistd.h>
00028 #include <errno.h>
00029 #include <string.h>
00030
00031 #include <archon/util/term.H>
00032
00033 namespace Archon
00034 {
00035 namespace Utilities
00036 {
00040 namespace Term
00041 {
00042 int getWidth()
00043 {
00044 const int tty = open("/dev/tty", O_RDONLY);
00045 if(tty < 0)
00046 ARCHON_THROW1(ResourceException,
00047 "System::Term::getWidth: Could not open "
00048 "/dev/tty: " + string(strerror(errno)));
00049 winsize s;
00050 if(ioctl(tty, TIOCGWINSZ, reinterpret_cast<char *>(&s)) < 0)
00051 ARCHON_THROW1(ResourceException,
00052 "System::Term::getWidth: Could not do "
00053 "TIOCGWINSZ on /dev/tty: " + string(strerror(errno)));
00054 if(close(tty) < 0)
00055 ARCHON_THROW1(ResourceException,
00056 "System::Term::getWidth: Could close "
00057 "/dev/tty: " + string(strerror(errno)));
00058 return s.ws_col;
00059 }
00060 }
00061 }
00062 }