forked from aamine/stdlinux2-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdupread.c
43 lines (39 loc) · 754 Bytes
/
dupread.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
static void
readshow(int fd)
{
char buf[8];
int n;
n = read(fd, buf, 1);
if (n < 0) {
perror("read");
exit(1);
}
if (n == 0)
exit(0);
switch (buf[0]) {
case ' ': strcpy(buf, "' '"); break;
case '\n': strcpy(buf, "\\n"); break;
case '\r': strcpy(buf, "\\r"); break;
case '\t': strcpy(buf, "\\t"); break;
case '\v': strcpy(buf, "\\v"); break;
default:
buf[1] = '\0';
break;
}
printf("fd%d: %s\n", fd, buf);
}
int
main(int argc, char *argv[])
{
int newfd;
newfd = dup(0);
for (;;) {
readshow(0);
readshow(newfd);
}
exit(0);
}