
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
    size_t i;
    int open_max;

    open_max = sysconf(_SC_OPEN_MAX);
    if (open_max < 0)
        return EXIT_FAILURE;
    for (i = 0; i < open_max; i++) {
        int f;
        int closed;
        closed = fcntl(i, F_GETFD, &f) == -1 &&
            errno == EBADF;
        if (!closed)
            printf("FD:%zu\n", i);
    }

    return EXIT_SUCCESS;
}
