Simple "table" command for pipe testing

This commit is contained in:
Mark 2020-07-02 13:03:27 +03:00
parent 4c2741e015
commit 4153749f79
2 changed files with 62 additions and 0 deletions

View File

@ -30,6 +30,7 @@ STAGE_BIN=$(STAGE)/init \
$(STAGE)/sbin/insmod \
$(STAGE)/sbin/reboot \
$(STAGE)/sbin/lspci \
$(STAGE)/bin/table \
$(STAGE)/bin/vsh \
$(STAGE)/bin/grep \
$(STAGE)/test.ko

61
core/bin/table.c Normal file
View File

@ -0,0 +1,61 @@
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
int main(int argc, char **argv) {
// 25 x 3
size_t width;
size_t cols = 4;
struct winsize winsz;
int index = 0;
char linebuf[1024];
if (!isatty(STDOUT_FILENO)) {
fprintf(stderr, "stdout is not a TTY\n");
return -1;
}
// Get terminal width
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsz) != 0) {
perror("stdout");
return -1;
}
width = (winsz.ws_col - 1) / cols;
while (fgets(linebuf, sizeof(linebuf), stdin)) {
// Remove trailing \n
char *p = strchr(linebuf, '\n');
if (p && *linebuf) {
*p = 0;
}
// Skip leading whitespace
p = linebuf;
while (isspace(*p)) {
++p;
}
size_t len = strlen(p);
if (len > width) {
len = width;
}
fwrite(p, 1, len, stdout);
for (size_t i = len; i < width; ++i) {
fputc(' ', stdout);
}
if ((++index) == cols) {
fputc('\n', stdout);
index = 0;
}
}
if (index != 0) {
fputc('\n', stdout);
}
fflush(stdout);
return 0;
}