Skip to content

Instantly share code, notes, and snippets.

@rprichard
Last active April 22, 2017 23:32
Show Gist options
  • Save rprichard/d1512c1fea735b8929371d82dfcb08cc to your computer and use it in GitHub Desktop.
Save rprichard/d1512c1fea735b8929371d82dfcb08cc to your computer and use it in GitHub Desktop.
SIGWINCH testing for Cygwin and WSL
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
void handler(int signo, siginfo_t *info, void *ctx) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
char buf[256];
sprintf(buf, "%d: (%d,%d)\n", signo, w.ws_col, w.ws_row);
write(STDOUT_FILENO, buf, strlen(buf));
}
int main() {
struct sigaction act = { 0 };
act.sa_sigaction = handler;
act.sa_flags = SA_RESTART;
sigaction(SIGWINCH, &act, NULL);
printf("Commands (press 's' or 'w' then Enter)\n");
handler(0, NULL, NULL);
while (true) {
write(STDOUT_FILENO, "reading: ", strlen("reading: "));
char buf[256] = { 0 };
read(STDIN_FILENO, buf, sizeof(buf));
if (buf[0] == 's') {
handler(0, NULL, NULL);
} else if (buf[0] == 'w') {
write(STDOUT_FILENO, "writing: ", strlen("writing: "));
for (int i = 0; i < 20; ++i) {
usleep(200 * 1000);
write(STDOUT_FILENO, ".", 1);
}
write(STDOUT_FILENO, "\n", 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment