-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ioctl instead of requiring curses (#1)
- Loading branch information
1 parent
ecb2bc7
commit 6069985
Showing
2 changed files
with
13 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
clang -Ofast -Wall -Wextra -Werror -pedantic -lcurses term-size.c -o term-size | ||
clang -Ofast -Wall -Wextra -Werror -pedantic -std=c99 term-size.c -o term-size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,27 @@ | ||
#include <stdio.h> | ||
#include <string.h> // strerror | ||
#include <errno.h> // errno | ||
#include <fcntl.h> // open(), O_RDONLY | ||
#include <unistd.h> // close() | ||
#include <curses.h> // setupterm(), tigetnum() | ||
#include <term.h> // ^ Same as above | ||
#include <string.h> // strerror | ||
#include <errno.h> // errno | ||
#include <fcntl.h> // open(), O_RDONLY | ||
#include <unistd.h> // close() | ||
#include <sys/ioctl.h> // ioctl() | ||
|
||
int main() { | ||
int tty_fd = open("/dev/tty", O_RDONLY); | ||
int tty_fd = open("/dev/tty", O_EVTONLY | O_NONBLOCK); | ||
if (tty_fd == -1) { | ||
fprintf(stderr, "Opening `/dev/tty` failed (%d): %s\n", errno, strerror(errno)); | ||
return 1; | ||
} | ||
|
||
// Intentionally not handling errors as none of | ||
// the documented errors can happen on macOS | ||
setupterm("xterm", tty_fd, NULL); | ||
struct winsize ws; | ||
int result = ioctl(tty_fd, TIOCGWINSZ, &ws); | ||
close(tty_fd); | ||
|
||
int cols = tigetnum("cols"); | ||
if (cols < 0) { | ||
fprintf(stderr, "tigetnum(\"cols\") returned error code %d\n", cols); | ||
close(tty_fd); | ||
if (result == -1) { | ||
fprintf(stderr, "Getting size failed (%d): %s\n", errno, strerror(errno)); | ||
return 1; | ||
} | ||
|
||
int rows = tigetnum("lines"); | ||
if (rows < 0) { | ||
fprintf(stderr, "tigetnum(\"lines\") returned error code %d\n", rows); | ||
close(tty_fd); | ||
return 1; | ||
} | ||
|
||
fprintf(stdout, "%d\n%d\n", cols, rows); | ||
fprintf(stdout, "%d\n%d\n", ws.ws_col, ws.ws_row); | ||
return 0; | ||
} | ||
|