This source file includes following definitions.
- hwloc_snprintf
- hwloc_namecoloncmp
- hwloc_add_uname_info
- hwloc_progname
1
2
3
4
5
6
7
8
9 #include <private/autogen/config.h>
10 #include <private/private.h>
11 #include <private/misc.h>
12
13 #include <stdarg.h>
14 #ifdef HAVE_SYS_UTSNAME_H
15 #include <sys/utsname.h>
16 #endif
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <ctype.h>
22
23 #ifdef HAVE_PROGRAM_INVOCATION_NAME
24 #include <errno.h>
25 extern char *program_invocation_name;
26 #endif
27 #ifdef HAVE___PROGNAME
28 extern char *__progname;
29 #endif
30
31 int hwloc_snprintf(char *str, size_t size, const char *format, ...)
32 {
33 int ret;
34 va_list ap;
35 static char bin;
36 size_t fakesize;
37 char *fakestr;
38
39
40 if (!size) {
41 str = &bin;
42 size = 1;
43 }
44
45 va_start(ap, format);
46 ret = vsnprintf(str, size, format, ap);
47 va_end(ap);
48
49 if (ret >= 0 && (size_t) ret != size-1)
50 return ret;
51
52
53
54
55
56 fakesize = size;
57 fakestr = NULL;
58 do {
59 fakesize *= 2;
60 free(fakestr);
61 fakestr = malloc(fakesize);
62 if (NULL == fakestr)
63 return -1;
64 va_start(ap, format);
65 errno = 0;
66 ret = vsnprintf(fakestr, fakesize, format, ap);
67 va_end(ap);
68 } while ((size_t) ret == fakesize-1 || (ret < 0 && (!errno || errno == ERANGE)));
69
70 if (ret >= 0 && size) {
71 if (size > (size_t) ret+1)
72 size = ret+1;
73 memcpy(str, fakestr, size-1);
74 str[size-1] = 0;
75 }
76 free(fakestr);
77
78 return ret;
79 }
80
81 int hwloc_namecoloncmp(const char *haystack, const char *needle, size_t n)
82 {
83 size_t i = 0;
84 while (*haystack && *haystack != ':') {
85 int ha = *haystack++;
86 int low_h = tolower(ha);
87 int ne = *needle++;
88 int low_n = tolower(ne);
89 if (low_h != low_n)
90 return 1;
91 i++;
92 }
93 return i < n;
94 }
95
96 void hwloc_add_uname_info(struct hwloc_topology *topology __hwloc_attribute_unused,
97 void *cached_uname __hwloc_attribute_unused)
98 {
99 #ifdef HAVE_UNAME
100 struct utsname _utsname, *utsname;
101
102 if (hwloc_obj_get_info_by_name(topology->levels[0][0], "OSName"))
103
104 return;
105
106 if (cached_uname)
107 utsname = (struct utsname *) cached_uname;
108 else {
109 utsname = &_utsname;
110 if (uname(utsname) < 0)
111 return;
112 }
113
114 if (*utsname->sysname)
115 hwloc_obj_add_info(topology->levels[0][0], "OSName", utsname->sysname);
116 if (*utsname->release)
117 hwloc_obj_add_info(topology->levels[0][0], "OSRelease", utsname->release);
118 if (*utsname->version)
119 hwloc_obj_add_info(topology->levels[0][0], "OSVersion", utsname->version);
120 if (*utsname->nodename)
121 hwloc_obj_add_info(topology->levels[0][0], "HostName", utsname->nodename);
122 if (*utsname->machine)
123 hwloc_obj_add_info(topology->levels[0][0], "Architecture", utsname->machine);
124 #endif
125 }
126
127 char *
128 hwloc_progname(struct hwloc_topology *topology __hwloc_attribute_unused)
129 {
130 #if HAVE_DECL_GETMODULEFILENAME
131 char name[256], *basename;
132 unsigned res = GetModuleFileName(NULL, name, sizeof(name));
133 if (res == sizeof(name) || !res)
134 return NULL;
135 basename = strrchr(name, '\\');
136 if (!basename)
137 basename = name;
138 else
139 basename++;
140 return strdup(basename);
141 #else
142 const char *name, *basename;
143 #if HAVE_DECL_GETPROGNAME
144 name = getprogname();
145 #elif HAVE_DECL_GETEXECNAME
146 name = getexecname();
147 #elif defined HAVE_PROGRAM_INVOCATION_NAME
148 name = program_invocation_name;
149
150 #elif defined HAVE___PROGNAME
151 name = __progname;
152 #else
153
154
155 name = NULL;
156 #endif
157 if (!name)
158 return NULL;
159 basename = strrchr(name, '/');
160 if (!basename)
161 basename = name;
162 else
163 basename++;
164 return strdup(basename);
165 #endif
166 }