Re: Compiling problem on AIX 4.2.1 with gcc

From: Henrik Nordstrom <hno@dont-contact.us>
Date: Tue, 20 Apr 1999 23:03:05 +0200

Duane Wessels wrote:

> while((j=open("/dev/null", 0)) > 0) i=j;

I hope this only is for testing purposes.

How about something like this?

The difference between SETMAXFILES and SETMAXFILES2 is that SETMAXFILES2
only sets the limit if no limit is known prior to the call, while
SETMAXFILES sets the limit to at most the argument.

int
main(int argc, char *argv[]) {
    FILE *fp;
    int i=-1,j;
#define SETMAXFILES(n) (j=(n),i=((i<0||j<i&&j>0)?j:i))
#define SETMAXFILES2(n) (j=(n),i=((i<0)?j:i))
#if HAVE_SETRLIMIT
    struct rlimit rl;
#endif
#if __CYGWIN32__
    /* getrlimit and sysconf returns bogous values on cygwin32.
     * Number of fds is virtually unlimited in cygwin (sys/param.h)
     */
    SETMAXFILES(NOFILE)
#endif
#if HAVE_SYSCONF && defined(_SC_OPEN_MAX)
    /* sysconf should be trustful if it exists */
    SETMAXFILES(sysconf(_SC_OPEN_MAX));
#endif
#if HAVE_GETRLIMIT && defined(RLIMIT_NOFILE)
    /* getrlimit returns the currently configured limit,
     * which is not neccesary the upper limit of the system...
     */
    if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
        perror("getrlimit: RLIMIT_NOFILE");
    else if (i<0)
        SETMAXFILES2(rl.rlim_cur);
#endif
#if HAVE_GETDTABLESIZE
    /* getdtablesize works on some systems, always the default
     * value on some other.. */
    SETMAXFILES2(getdtablesize());
#endif
#if defined(OPEN_MAX)
    SETMAXFILES2(OPEN_MAX);
#endif
#if defined(NOFILE)
    SETMAXFILES2(NOFILE);
#endif
#if defined(FD_SETSIZE) && USE_POLL
    SETMAXFILES2(FD_SETSIZE)
#elif defined(FD_SETSIZE)
    SETMAXFILES(FD_SETSIZE)
#endif
    if (j<64) {
        fprintf(stderr,"Unable to find max number of filedescriptors.
probing....\n")
#if HAVE_DUP2
        for(j=128;j<2000000;j++)
             if(dup2(0,j)==0) {
                i=j+1; close(j);
             }
#else
        while((j=open("/dev/null", 0)) > 0) i=j+1;
        for(j=i;j>10; j--) close(j);
#endif
        SETMAXFILES(j)
    }
    fp = fopen("conftestval", "w");
    fprintf (fp, "%d\n", i);
    close(fp);
}

/Henrik
Received on Tue Jul 29 2003 - 13:15:57 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:12:05 MST