Rev | Line | |
---|
[1] | 1 | #include <unistd.h> |
---|
| 2 | #include <errno.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <limits.h> |
---|
| 5 | #include <fcntl.h> |
---|
| 6 | |
---|
| 7 | #if CONFIG_LIBC_SYSCONF_DEBUG |
---|
| 8 | #include <pthread.h> |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #endif |
---|
| 11 | |
---|
| 12 | static long sysconf_get(char *token) { |
---|
| 13 | int fd=open("/sys/sysconf",O_RDONLY, 0); |
---|
| 14 | char buf[512]; |
---|
| 15 | size_t l; |
---|
| 16 | if (fd==-1) { errno=ENOSYS; return -1; } |
---|
| 17 | l=read(fd,buf,sizeof(buf)); |
---|
| 18 | |
---|
| 19 | #if CONFIG_LIBC_SYSCONF_DEBUG |
---|
| 20 | int cpu_id; |
---|
| 21 | pthread_attr_getcpuid_np(&cpu_id); |
---|
| 22 | |
---|
| 23 | if(cpu_id == 15) |
---|
| 24 | printf("%s: cpu %d: got %d [%s]\n", __FUNCTION__, cpu_id, l, &buf[0]); |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | if (l!=(size_t)-1) { |
---|
| 28 | char* c; |
---|
| 29 | buf[l]=0; |
---|
| 30 | c=strstr(buf,token); |
---|
| 31 | if (c) { |
---|
| 32 | c+=strlen(token); while (*c==' ' || *c=='\t') ++c; |
---|
| 33 | l=0; |
---|
| 34 | while (*c>='0' && *c<='9') { |
---|
| 35 | l=l*10+*c-'0'; |
---|
| 36 | ++c; |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | close(fd); |
---|
| 41 | return l; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | long sysconf(int name) |
---|
| 45 | { |
---|
| 46 | switch(name) |
---|
| 47 | { |
---|
| 48 | case _SC_OPEN_MAX: |
---|
| 49 | return OPEN_MAX; |
---|
| 50 | |
---|
| 51 | case _SC_CLK_TCK: |
---|
| 52 | #ifdef __alpha__ |
---|
| 53 | return 1024; |
---|
| 54 | #else |
---|
| 55 | return 100; |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | case _SC_PAGESIZE: |
---|
| 59 | return sysconf_get("PAGE_SIZE"); |
---|
| 60 | |
---|
| 61 | case _SC_ARG_MAX: |
---|
| 62 | return ARG_MAX; |
---|
| 63 | |
---|
| 64 | case _SC_NGROUPS_MAX: |
---|
| 65 | return NGROUPS_MAX; |
---|
| 66 | |
---|
| 67 | case _SC_NPROCESSORS_ONLN: |
---|
| 68 | return sysconf_get("CPU_ONLN_NR"); |
---|
| 69 | |
---|
| 70 | case _SC_PAGE_SIZE: |
---|
| 71 | return sysconf_get("PAGE_SIZE"); |
---|
| 72 | |
---|
| 73 | case _SC_CACHE_LINE_SIZE: |
---|
| 74 | return sysconf_get("CPU_CACHE_LINE_SIZE"); |
---|
| 75 | |
---|
| 76 | case _SC_HEAP_MAX_SIZE: |
---|
| 77 | return sysconf_get("HEAP_MAX_SIZE"); |
---|
| 78 | |
---|
| 79 | case _SC_NCLUSTERS_ONLN: |
---|
| 80 | return sysconf_get("CLUSTER_ONLN_NR"); |
---|
| 81 | } |
---|
| 82 | errno=ENOSYS; |
---|
| 83 | return -1; |
---|
| 84 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.