summaryrefslogtreecommitdiff
path: root/test/src/fd.c
diff options
context:
space:
mode:
authorPhilip Hazel <ph10@hermes.cam.ac.uk>2006-02-06 16:22:56 +0000
committerPhilip Hazel <ph10@hermes.cam.ac.uk>2006-02-06 16:22:56 +0000
commitc55a77db55ebf46a399f136eeb3a928b1e862772 (patch)
treec732f478642b0066b751b1034e195a200b43f5c2 /test/src/fd.c
parent151b83f867487080e8f0e5cd6179e857dc6b3ccb (diff)
CVSing the test suite.
Diffstat (limited to 'test/src/fd.c')
-rw-r--r--test/src/fd.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/test/src/fd.c b/test/src/fd.c
new file mode 100644
index 000000000..4616f6fd1
--- /dev/null
+++ b/test/src/fd.c
@@ -0,0 +1,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 */