1
1
openmpi/opal/win32/opal_utsname.c
Jeff Squyres 28e51ad4d4 opal: convert from strncpy() -> opal_string_copy()
In many cases, this was a simple string replace.  In a few places, it
entailed:

1. Updating some comments and removing now-redundant foo[size-1]='\0'
   statements.
2. Updating passing (size-1) to (size) (because opal_string_copy()
   wants the entire destination buffer length).

This commit actually fixes a bunch of potential (yet quite unlikely)
bugs where we could have ended up with non-null-terminated strings.

Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
2018-09-27 11:56:18 -07:00

88 строки
3.1 KiB
C

/*
Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
University Research and Technology
Corporation. All rights reserved.
Copyright (c) 2004-2014 The University of Tennessee and The University
of Tennessee Research Foundation. All rights
reserved.
Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
University of Stuttgart. All rights reserved.
Copyright (c) 2004-2005 The Regents of the University of California.
All rights reserved.
$COPYRIGHT$
Additional copyrights may follow
$HEADER$
*/
#include "opal_config.h"
#include "opal/win32/opal_utsname.h"
#include "opal/util/string_copy.h"
/*
This has to fill in the following information
1. sysname: name of the operating system --
2. nodename: GetComputerName
3. release: GetVersionEx
4. version: GetVersionEx
5. machine: GetSystemInfo
*/
int uname( struct utsname *un )
{
TCHAR env_variable[] = "OS=%OS%";
DWORD info_buf_count;
OSVERSIONINFO version_info;
SYSTEM_INFO sys_info;
TCHAR info_buf[OPAL_UTSNAME_LEN];
info_buf_count = ExpandEnvironmentStrings( env_variable, info_buf, OPAL_UTSNAME_LEN);
if (0 == info_buf_count) {
snprintf( un->sysname, OPAL_UTSNAME_LEN, "Unknown" );
} else {
/* remove the "OS=" from the beginning of the string */
opal_string_copy( un->sysname, info_buf + 3, OPAL_UTSNAME_LEN );
}
info_buf_count = OPAL_UTSNAME_LEN;
if (!GetComputerName( un->nodename, &info_buf_count)) {
snprintf(un->nodename, OPAL_UTSNAME_LEN, "undefined");
}
version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&version_info)) {
snprintf(un->release, OPAL_UTSNAME_LEN, "undefined");
snprintf(un->version, OPAL_UTSNAME_LEN, "undefined");
} else {
/* fill in both release and version information */
snprintf( un->release, OPAL_UTSNAME_LEN, "%d.%d.%d",
version_info.dwMajorVersion,
version_info.dwMinorVersion,
version_info.dwBuildNumber);
snprintf( un->version, OPAL_UTSNAME_LEN, "%s", version_info.szCSDVersion );
}
/* get machine information */
GetSystemInfo(&sys_info);
switch( sys_info.wProcessorArchitecture ) {
case PROCESSOR_ARCHITECTURE_UNKNOWN:
snprintf( un->machine, OPAL_UTSNAME_LEN, "Unknown %d", sys_info.wProcessorLevel );
break;
case PROCESSOR_ARCHITECTURE_INTEL:
snprintf( un->machine, OPAL_UTSNAME_LEN, "Intel %d", sys_info.wProcessorLevel );
break;
case PROCESSOR_ARCHITECTURE_IA64:
snprintf( un->machine, OPAL_UTSNAME_LEN, "IA64 %d", sys_info.wProcessorLevel );
break;
case PROCESSOR_ARCHITECTURE_AMD64:
snprintf( un->machine, OPAL_UTSNAME_LEN, "AMD %d", sys_info.wProcessorLevel );
break;
default:
snprintf( un->machine, OPAL_UTSNAME_LEN, "UFO hardware %d", sys_info.wProcessorLevel );
break;
}
return 0;
}