Skip to content

Commit ce001f3

Browse files
committed
add include string.h, wchar.h, link library, and format
1 parent 0e98aa7 commit ce001f3

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

flang/runtime/command.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,33 @@
1414
#include "flang/Runtime/descriptor.h"
1515
#include <cstdlib>
1616
#include <limits>
17+
#include <string.h>
1718

1819
#ifdef _WIN32
1920
#define WIN32_LEAN_AND_MEAN
2021
#define NOMINMAX
2122
#include <windows.h>
2223

23-
#include <Lmcons.h> // UNLEN=256
24+
#include <lmcons.h> // UNLEN=256
25+
#include <wchar.h> // wchar_t cast to LPWSTR
26+
#pragma comment(lib, "Advapi32.lib") // Link Advapi32.lib for GetUserName
2427

25-
inline char *getlogin() {
26-
char *username = NULL;
27-
DWORD size = UNLEN + 1; // Constant for the maximum username length
28-
username = (char *)malloc(size);
28+
static inline char *getlogin() {
29+
static char username[UNLEN + 1];
30+
wchar_t w_username[UNLEN + 1];
31+
DWORD namelen = sizeof(w_username) / sizeof(w_username[0]);
2932

30-
if (GetUserName(username, &size)) {
31-
// Username retrieved successfully
32-
return username;
33+
if (GetUserName(w_username, &namelen)) {
34+
// Convert the wchar_t string to a regular C string
35+
if (wcstombs(username, w_username, UNLEN + 1) == -1) {
36+
// Conversion failed
37+
return NULL;
38+
}
39+
return (username[0] == 0 ? NULL : username);
3340
} else {
34-
free(username);
3541
return NULL;
3642
}
43+
return nullptr;
3744
}
3845
#else
3946
#include <unistd.h>

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TEST_F(SeveralArguments, ArgValueTooShort) {
297297
ASSERT_NE(tooShort, nullptr);
298298
EXPECT_EQ(RTNAME(GetCommandArgument)(1, tooShort.get()), -1);
299299
CheckDescriptorEqStr(tooShort.get(), severalArgsArgv[1]);
300-
300+
301301
OwningPtr<Descriptor> length{EmptyIntDescriptor()};
302302
ASSERT_NE(length, nullptr);
303303
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor()};

0 commit comments

Comments
 (0)