CVSing the test suite.
[exim.git] / test / src / fd.c
1 /* $Cambridge: exim/test/src/fd.c,v 1.1 2006/02/06 16:24:05 ph10 Exp $ */
2
3 /* A program to check on open file descriptors. There are some weird options
4 for running it in Exim testing. If -q is given, make output suitable for
5 queryprogram. If -f is given, copy the input as for a transport filter. If -s
6 is given, add extra output from stat(). */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17
18
19 /* The way of finding out the maximum file descriptor various between OS.
20 Most have sysconf(), but a few don't. */
21
22 #ifdef _SC_OPEN_MAX
23 #define mac_maxfd (sysconf(_SC_OPEN_MAX) - 1)
24 #elif defined OPEN_MAX
25 #define mac_maxfd (OPEN_MAX - 1)
26 #elif defined NOFILE
27 #define mac_maxfd (NOFILE - 1)
28 #else
29 #define mac_maxfd 255; /* just in case */
30 #endif
31
32
33 int main(int argc, char **argv)
34 {
35 int fd;
36 int qpgm = 0;
37 int filter = 0;
38 int use_stat = 0;
39 struct stat statbuf;
40 char buffer[8192];
41 char *p = buffer;
42
43 while (argc > 1)
44 {
45 char *arg = argv[--argc];
46 if (strcmp(arg, "-q") == 0) qpgm = 1;
47 if (strcmp(arg, "-f") == 0) filter = 1;
48 if (strcmp(arg, "-s") == 0) use_stat = 1;
49 }
50
51 if (filter)
52 {
53 int len;
54 while ((len = read(0, buffer, sizeof(buffer))) > 0)
55 write(1, buffer, len);
56 }
57
58 p += sprintf(p, "max fd = %d\n", (int)mac_maxfd);
59
60 for (fd = 0; fd <= mac_maxfd; fd++)
61 {
62 int options = fcntl(fd, F_GETFD);
63 if (options >= 0)
64 {
65 int status = fcntl(fd, F_GETFL);
66 p += sprintf(p, "%3d opt=%d status=%X ", fd, options, status);
67 switch(status & 3)
68 {
69 case 0: p += sprintf(p, "RDONLY");
70 break;
71 case 1: p += sprintf(p, "WRONLY");
72 break;
73 case 2: p += sprintf(p, "RDWR");
74 break;
75 }
76 if (isatty(fd)) p += sprintf(p, " TTY");
77 if ((status & 8) != 0) p += sprintf(p, " APPEND");
78
79 if (use_stat && fstat(fd, &statbuf) >= 0)
80 {
81 p += sprintf(p, " mode=%o uid=%d size=%d", (int)statbuf.st_mode,
82 (int)statbuf.st_uid, (int)statbuf.st_size);
83 }
84
85 p += sprintf(p, "\n");
86 }
87 else if (errno != EBADF)
88 {
89 p += sprintf(p, "%3d errno=%d %s\n", fd, errno, strerror(errno));
90 }
91 }
92
93 if (qpgm)
94 {
95 for (p = buffer; *p != 0; p++)
96 if (*p == '\n') *p = ' ';
97 printf("ACCEPT DATA=\"%s\"\n", buffer);
98 }
99 else printf("%s", buffer);
100
101 exit(0);
102 }
103
104 /* End */