2 * Copyright © 2009 CNRS
3 * Copyright © 2009-2014 Inria. All rights reserved.
4 * Copyright © 2009-2010 Université Bordeaux
5 * Copyright © 2009-2018 Cisco Systems, Inc. All rights reserved.
6 * See COPYING in top-level directory.
9 #include <private/autogen/config.h>
10 #include <private/private.h>
11 #include <private/misc.h>
14 #ifdef HAVE_SYS_UTSNAME_H
15 #include <sys/utsname.h>
23 #ifdef HAVE_PROGRAM_INVOCATION_NAME
25 extern char *program_invocation_name
;
27 #ifdef HAVE___PROGNAME
28 extern char *__progname
;
31 int hwloc_snprintf(char *str
, size_t size
, const char *format
, ...)
39 /* Some systems crash on str == NULL */
46 ret
= vsnprintf(str
, size
, format
, ap
);
49 if (ret
>= 0 && (size_t) ret
!= size
-1)
52 /* vsnprintf returned size-1 or -1. That could be a system which reports the
53 * written data and not the actually required room. Try increasing buffer
54 * size to get the latter. */
61 fakestr
= malloc(fakesize
);
66 ret
= vsnprintf(fakestr
, fakesize
, format
, ap
);
68 } while ((size_t) ret
== fakesize
-1 || (ret
< 0 && (!errno
|| errno
== ERANGE
)));
70 if (ret
>= 0 && size
) {
71 if (size
> (size_t) ret
+1)
73 memcpy(str
, fakestr
, size
-1);
81 int hwloc_namecoloncmp(const char *haystack
, const char *needle
, size_t n
)
84 while (*haystack
&& *haystack
!= ':') {
86 int low_h
= tolower(ha
);
88 int low_n
= tolower(ne
);
96 void hwloc_add_uname_info(struct hwloc_topology
*topology __hwloc_attribute_unused
,
97 void *cached_uname __hwloc_attribute_unused
)
100 struct utsname _utsname
, *utsname
;
102 if (hwloc_obj_get_info_by_name(topology
->levels
[0][0], "OSName"))
103 /* don't annotate twice */
107 utsname
= (struct utsname
*) cached_uname
;
110 if (uname(utsname
) < 0)
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 /* HAVE_UNAME */
128 hwloc_progname(struct hwloc_topology
*topology __hwloc_attribute_unused
)
130 #if HAVE_DECL_GETMODULEFILENAME
131 char name
[256], *local_basename
;
132 unsigned res
= GetModuleFileName(NULL
, name
, sizeof(name
));
133 if (res
== sizeof(name
) || !res
)
135 local_basename
= strrchr(name
, '\\');
137 local_basename
= name
;
140 return strdup(local_basename
);
141 #else /* !HAVE_GETMODULEFILENAME */
142 const char *name
, *local_basename
;
143 #if HAVE_DECL_GETPROGNAME
144 name
= getprogname(); /* FreeBSD, NetBSD, some Solaris */
145 #elif HAVE_DECL_GETEXECNAME
146 name
= getexecname(); /* Solaris */
147 #elif defined HAVE_PROGRAM_INVOCATION_NAME
148 name
= program_invocation_name
; /* Glibc. BGQ CNK. */
149 /* could use program_invocation_short_name directly, but we have the code to remove the path below anyway */
150 #elif defined HAVE___PROGNAME
151 name
= __progname
; /* fallback for most unix, used for OpenBSD */
153 /* TODO: _NSGetExecutablePath(path, &size) on Darwin */
154 /* TODO: AIX, HPUX, OSF */
159 local_basename
= strrchr(name
, '/');
161 local_basename
= name
;
164 return strdup(local_basename
);
165 #endif /* !HAVE_GETMODULEFILENAME */