Skip to content

errno definitions not an integer constant #103

Closed
@alisonatwork

Description

@alisonatwork

I was experimenting with compiling some non-cosmo code and came across this error:

$ cat etest.c
#ifdef NO_COSMO
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#endif
int main() {
  struct stat s;
  int i = stat("nope", &s);
  printf("stat=-1? %d\n", i);
  switch (errno) {
    case ENOENT:
      printf("hooray!\n");
  }
  printf("errno=2? %d\n", errno);
}
$ cc -DNO_COSMO etest.c
$ ./a.out
stat=-1? -1
hooray!
errno=2? 2
$ cc -g -Og -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone -fno-omit-frame-pointer -pg -mnop-mcount \
> -o etest.com.dbg etest.c -fuse-ld=bfd -Wl,-T,ape.lds \
> -include o/cosmopolitan.h o/libc/crt/crt.o o/ape/ape.o o/cosmopolitan.a
etest.c: In function 'main':
etest.c:11:5: error: case label does not reduce to an integer constant
   11 |     case ENOENT:
      |     ^~~~
$ grep ENOENT o/cosmopolitan.h
#define ENOENT          ENOENT           /* no such file or directory */
hidden extern const long ENOENT;
#define kNtErrorFileNotFound 2     /* ENOENT */

I tried moving things around in cosmopolitan.h a bit, but no joy. It seems that the actual definition is in libc/sysv/consts/ENOENT.S, and I guess it changes (for some of them at least) at runtime. Is there any way to get around this, or would it be better to patch the code to use if/else instead of switch?

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions