#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char** argv) {
    struct stat statbuf;
    int x,fiflags;

    for (x=0;x<NOFILE;x++) {
        if ((fstat(x,&statbuf)==-1))
             continue;
        printf("FD %3d: ", x);
        fiflags=fcntl(x,F_GETFL,0);
        switch(fiflags&(O_RDWR|O_WRONLY)) {
        case O_RDONLY:
             printf(" O_RDONLY\n");
             break;
        case O_WRONLY:
             printf(" O_WRONLY\n");
             break;
        case O_RDWR:
             printf(" O_RDWR\n");
             break;
        default:
             printf("\n");
             break;
        }
    }
    return(0); 
}

