-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.c
92 lines (83 loc) · 1.62 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
static void
terminfo(int fd)
{
struct winsize w;
char *tty;
printf("fd: %d\n", fd);
if (!isatty(fd)) {
printf("\tNot a tty!\n");
return;
}
if (-1 == ioctl(fd, TIOCGWINSZ, &w)) {
printf("\tioctl(TIOCGWINSZ) fail: %s\n", strerror(errno));
return;
}
tty = ttyname(fd);
if (tty) {
printf("\tttyname(): %s\n", tty);
}
printf("\tSize: %dx%d\n", w.ws_row, w.ws_col);
}
int main()
{
if (0) {
setvbuf(stdout, (char *)NULL, _IOLBF, 0);
setvbuf(stderr, (char *)NULL, _IOLBF, 0);
}
if (1) {
printf("ctermid(): %s\n", ctermid(0));
}
if (1) {
terminfo(0);
terminfo(1);
terminfo(2);
}
if (1) {
fprintf(stdout, "0.1 stdout\n");
sleep(1);
fprintf(stderr, "0.2 stderr\n");
sleep(1);
fprintf(stdout, "0.3 stdout\n");
fflush(stdout);
fflush(stderr);
sleep(3);
fprintf(stdout, "1.1 stdout\n");
fprintf(stderr, "1.2 stderr\n");
fprintf(stdout, "1.3 stdout\n");
sleep(1);
}
/* \r test */
if (1) {
fprintf(stdout, "Progress: wait...");
fflush(stdout);
sleep(1);
fprintf(stdout, "\rProgress: done \n");
}
/* stdin test */
if (1) {
int nana;
printf("Give me an integer: ");
fflush(stdout);
scanf("%d", &nana);
printf("Value: %d\n", nana);
}
if (0) {
char buf[1024];
int n;
printf("reading from stdin...\n"); fflush(stdout);
n = read(0, buf, sizeof(buf));
printf("done (%d)...\n", n); fflush(stdout);
}
if (0) {
printf("Closing stdin...\n"); fflush(stdout);
close(0);
printf("done...\n"); fflush(stdout);
}
return 0;
}