Skip to content

Commit

Permalink
Use gethostname instead of popen/nirtcfg
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Sep 1, 2023
1 parent 07babb7 commit 91b229e
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions hal/src/main/native/athena/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,37 +356,25 @@ size_t HAL_GetComments(char* buffer, size_t size) {
}

void InitializeTeamNumber(void) {
auto handle = popen(
"/usr/local/natinst/bin/nirtcfg --file=/etc/natinst/share/ni-rt.ini "
"--get section=systemsettings,token=host_name",
"r");

if (!handle) {
char hostnameBuf[25];
auto status = gethostname(hostnameBuf, sizeof(hostnameBuf));
if (status != 0) {
teamNumber = 0;
return;
}

std::string result;
std::array<char, std::string::traits_type::length("roboRIO-00000-FRC")>
buffer;

do {
size_t count =
std::fread(buffer.data(), sizeof(buffer[0]), buffer.size(), handle);
result.append(buffer.data(), count);
} while (!(std::feof(handle) || std::ferror(handle)));
std::string hostname{hostnameBuf, sizeof(hostnameBuf)};

pclose(handle);
auto start = result.find('-') + 1;
auto end = result.find('-', start);
auto start = hostname.find('-') + 1;
auto end = hostname.find('-', start);

if (start == std::string::npos || end == std::string::npos) {
teamNumber = 0;
return;
}

try {
teamNumber = std::stoi(result.substr(start, end - start));
teamNumber = std::stoi(hostname.substr(start, end - start));
} catch (...) {
teamNumber = 0;
}
Expand Down

0 comments on commit 91b229e

Please sign in to comment.