setrlimit always fails

Patrick Chkoreff pc@fexl.com
Mon Feb 15 19:54:26 GMT 2021


Corinna Vinschen wrote on 2/15/21 4:14 AM:

> That looks wrong.  The __USE_<standard> flags are internal flags from
> GLibc and not supposed to be used by application code.  Check the Linux
> man page for strptime, the usage of _XOPEN_SOURCE or another flag
> including _XOPEN_SOURCE (e. g. _GNU_SOURCE) is required.  So this:
> 
>   #define _XOPEN_SOURCE
>   #include <time.h>

One would think so, but I tried it on two different Linux machines and
it failed with:

error: ‘strptime’ undeclared

To fix that, I must define _USE_XOPEN.

I have stripped down my requirements as far as possible.  I created a
file "test_stuff.c" as follows, with the most minimal preambles that
actually work on each platform (Cygwin or Linux):

~~
#ifdef __CYGWIN__

#define _XOPEN_SOURCE
#include <stdint.h>
#include <sys/time.h>
#include <time.h>

#else

#include <stdint.h>
#include <sys/time.h>
#define __USE_XOPEN
#include <time.h>

#endif

void stuff(void)
    {
    uint64_t n;
    time_t t;
    struct timeval tv;
    (void)n;
    (void)t;
    (void)tv;
    (void)time;
    (void)gettimeofday;
    (void)timegm;
    (void)timelocal;
    (void)gmtime;
    (void)localtime;
    (void)strftime;
    (void)strptime;
    }

~~


That compiles successfully on both Cygwin and Linux, using the simple
command "gcc -c test_stuff.c".

Note that in the case of Linux, the define __USE_XOPEN is absolutely
essential, and define _XOPEN_SOURCE makes no difference.  This is an
experimental fact on the two Linux machines I tested.

Note also that on Cygwin, I must define _XOPEN_SOURCE before including
stdint.h.  There are all kinds of weird interactions and different
ordering requirements between the Linux and Cygwin code.  So let's look
at some options, and show how they work on both Linux and Cygwin.

OPTION 1:

#define _XOPEN_SOURCE
#include <stdint.h>
#include <sys/time.h>
#include <time.h>

Result on Cygwin: SUCCESS

Result on Linux:
error: ‘timegm’ undeclared
error: ‘timelocal’ undeclared


OPTION 2:

#include <stdint.h>
#include <sys/time.h>
#define _XOPEN_SOURCE
#include <time.h>

Result on Cygwin:
error: 'strptime' undeclared

Result on Linux:
error: 'strptime' undeclared


OPTION 3:

#include <stdint.h>
#include <sys/time.h>
#define __USE_XOPEN
#define _XOPEN_SOURCE
#include <time.h>

Result on Cygwin:
error: 'strptime' undeclared

Result on Linux:  SUCCESS


Consequently I see nothing simpler than the test_stuff.c code at the top
of this email.  By all means correct me if I am wrong.  Propose a
preamble on this list, and I'll jam it into my test code and see if it
compiles on both Cygwin and Linux.  I can literally compile the exact
same code file on both Cygwin and Linux, due to how I've got sharing set up.


-- Patrick


More information about the Cygwin mailing list