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
93
94
95
96
97
98
99
100
101
102
103
104
|
/* $Cambridge: exim/test/src/fd.c,v 1.1 2006/02/06 16:24:05 ph10 Exp $ */
/* A program to check on open file descriptors. There are some weird options
for running it in Exim testing. If -q is given, make output suitable for
queryprogram. If -f is given, copy the input as for a transport filter. If -s
is given, add extra output from stat(). */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
/* The way of finding out the maximum file descriptor various between OS.
Most have sysconf(), but a few don't. */
#ifdef _SC_OPEN_MAX
#define mac_maxfd (sysconf(_SC_OPEN_MAX) - 1)
#elif defined OPEN_MAX
#define mac_maxfd (OPEN_MAX - 1)
#elif defined NOFILE
#define mac_maxfd (NOFILE - 1)
#else
#define mac_maxfd 255; /* just in case */
#endif
int main(int argc, char **argv)
{
int fd;
int qpgm = 0;
int filter = 0;
int use_stat = 0;
struct stat statbuf;
char buffer[8192];
char *p = buffer;
while (argc > 1)
{
char *arg = argv[--argc];
if (strcmp(arg, "-q") == 0) qpgm = 1;
if (strcmp(arg, "-f") == 0) filter = 1;
if (strcmp(arg, "-s") == 0) use_stat = 1;
}
if (filter)
{
int len;
while ((len = read(0, buffer, sizeof(buffer))) > 0)
write(1, buffer, len);
}
p += sprintf(p, "max fd = %d\n", (int)mac_maxfd);
for (fd = 0; fd <= mac_maxfd; fd++)
{
int options = fcntl(fd, F_GETFD);
if (options >= 0)
{
int status = fcntl(fd, F_GETFL);
p += sprintf(p, "%3d opt=%d status=%X ", fd, options, status);
switch(status & 3)
{
case 0: p += sprintf(p, "RDONLY");
break;
case 1: p += sprintf(p, "WRONLY");
break;
case 2: p += sprintf(p, "RDWR");
break;
}
if (isatty(fd)) p += sprintf(p, " TTY");
if ((status & 8) != 0) p += sprintf(p, " APPEND");
if (use_stat && fstat(fd, &statbuf) >= 0)
{
p += sprintf(p, " mode=%o uid=%d size=%d", (int)statbuf.st_mode,
(int)statbuf.st_uid, (int)statbuf.st_size);
}
p += sprintf(p, "\n");
}
else if (errno != EBADF)
{
p += sprintf(p, "%3d errno=%d %s\n", fd, errno, strerror(errno));
}
}
if (qpgm)
{
for (p = buffer; *p != 0; p++)
if (*p == '\n') *p = ' ';
printf("ACCEPT DATA=\"%s\"\n", buffer);
}
else printf("%s", buffer);
exit(0);
}
/* End */
|