1 /* A program to check on open file descriptors. There are some weird options
2 for running it in Exim testing. If -q is given, make output suitable for
3 queryprogram. If -f is given, copy the input as for a transport filter. If -s
4 is given, add extra output from stat(). */
17 /* The way of finding out the maximum file descriptor various between OS.
18 Most have sysconf(), but a few don't. */
21 #define mac_maxfd (sysconf(_SC_OPEN_MAX) - 1)
22 #elif defined OPEN_MAX
23 #define mac_maxfd (OPEN_MAX - 1)
25 #define mac_maxfd (NOFILE - 1)
27 #define mac_maxfd 255; /* just in case */
31 int main(int argc, char **argv)
43 char *arg = argv[--argc];
44 if (strcmp(arg, "-q") == 0) qpgm = 1;
45 if (strcmp(arg, "-f") == 0) filter = 1;
46 if (strcmp(arg, "-s") == 0) use_stat = 1;
52 while ((len = read(0, buffer, sizeof(buffer))) > 0)
53 if (write(1, buffer, len) < 0)
57 p += sprintf(p, "max fd = %d\n", (int)mac_maxfd);
59 for (fd = 0; fd <= mac_maxfd; fd++)
61 int options = fcntl(fd, F_GETFD);
64 int status = fcntl(fd, F_GETFL);
65 p += sprintf(p, "%3d opt=%d status=%X ", fd, options, status);
68 case O_RDONLY: p += sprintf(p, "RDONLY"); break;
69 case O_WRONLY: p += sprintf(p, "WRONLY"); break;
70 case O_RDWR: p += sprintf(p, "RDWR"); break;
72 if (isatty(fd)) p += sprintf(p, " TTY");
73 if ((status & 8) != 0) p += sprintf(p, " APPEND");
75 if (use_stat && fstat(fd, &statbuf) >= 0)
76 p += sprintf(p, " mode=%o uid=%d size=%d", (int)statbuf.st_mode,
77 (int)statbuf.st_uid, (int)statbuf.st_size);
79 p += sprintf(p, "\n");
81 else if (errno != EBADF)
82 p += sprintf(p, "%3d errno=%d %s\n", fd, errno, strerror(errno));
87 for (p = buffer; *p != 0; p++)
88 if (*p == '\n') *p = ' ';
89 printf("ACCEPT DATA=\"%s\"\n", buffer);
91 else printf("%s", buffer);