Skip to content

Commit

Permalink
[#3] Use /etc/system-release and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Oct 31, 2022
1 parent f761bb1 commit 4ac3610
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ unsigned long
pgexporter_ext_total_space(char* path);

/**
* Trim whitespace from a string
* Clean a string
* @param s The string
* @return The result
*/
char*
pgexporter_ext_trim_whitespace(char* s);
pgexporter_ext_clean_string(char* s);

#ifdef __cplusplus
}
Expand Down
19 changes: 8 additions & 11 deletions src/pgexporter_ext/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,22 +380,17 @@ os_info(Tuplestorestate* tupstore, TupleDesc tupdesc)
nulls[OS_INFO_DOMAIN_NAME] = true;
}

os_info_file = fopen("/etc/os-release", "r");
os_info_file = fopen("/etc/system-release", "r");

if (!os_info_file)
{
nulls[OS_INFO_NAME] = true;
}
else
{
while (fgets(&buffer[0], max_length, os_info_file) != NULL)
if (fgets(&buffer[0], max_length, os_info_file) != NULL)
{
int length = strlen(buffer);
if (length > 0)
{
if (strstr(buffer, "PRETTY_NAME=") != NULL)
memcpy(os_name, (buffer + strlen("PRETTY_NAME=")), (length - strlen("PRETTY_NAME=")));
}
memcpy(os_name, buffer, strlen(buffer) - 1);
}

fclose(os_info_file);
Expand Down Expand Up @@ -427,6 +422,8 @@ os_info(Tuplestorestate* tupstore, TupleDesc tupdesc)

tuplestore_putvalues(tupstore, tupdesc, values, nulls);

return;

#else

Datum values[OS_INFO_NUMBER];
Expand Down Expand Up @@ -542,11 +539,11 @@ cpu_info(Tuplestorestate* tupstore, TupleDesc tupdesc)
{
if (strstr(&buffer[0], "vendor_id") != NULL)
{
memcpy(vendor_id, col + 1, strlen(col + 1));
memcpy(vendor_id, col + 2, strlen(col + 2) - 1);
}
else if (strstr(&buffer[0], "model name") != NULL)
{
memcpy(model_name, col + 1, strlen(col + 1));
memcpy(model_name, col + 2, strlen(col + 2) - 1);
}
else if (strstr(&buffer[0], "cpu cores") != NULL)
{
Expand Down Expand Up @@ -769,7 +766,7 @@ kb_to_bytes(char* s)
char* token;

col = strstr(s, ":");
trimmed = pgexporter_ext_trim_whitespace(col + 1);
trimmed = pgexporter_ext_clean_string(col + 1);

token = strtok(trimmed, " ");
value = atoll(token);
Expand Down
36 changes: 15 additions & 21 deletions src/pgexporter_ext/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <sys/types.h>

static char* pgexporter_ext_append(char* orig, char* s);
static char* pgexporter_ext_append_char(char* orig, char c);

unsigned long
pgexporter_ext_directory_size(char* directory)
Expand Down Expand Up @@ -149,10 +148,11 @@ pgexporter_ext_total_space(char* path)
}

char*
pgexporter_ext_trim_whitespace(char* orig)
pgexporter_ext_clean_string(char* orig)
{
size_t length;
size_t offset = 0;
size_t from = 0;
size_t to = 0;
char c = 0;
char* result = NULL;

Expand All @@ -166,29 +166,35 @@ pgexporter_ext_trim_whitespace(char* orig)
for (int i = 0; i < length; i++)
{
c = *(orig + i);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
if (c == ' ' || c == '\'' || c == '\"' || c == '\t' || c == '\r' || c == '\n')
{
offset++;
from++;
}
else
{
break;
}
}

for (int i = offset; i < length; i++)
to = length;
for (int i = length; i > 0; i--)
{
c = *(orig + i);
if (c == '\t' || c == '\r' || c == '\n')
if (c == ' ' || c == '\'' || c == '\"' || c == '\t' || c == '\r' || c == '\n')
{
break;
/* Skip */
}
else
{
result = pgexporter_ext_append_char(result, c);
to = i;
break;
}
}

result = (char*)malloc(to - from + 1);
memset(result, 0, to - from + 1);
memcpy(result, orig + from, to - from);

return result;
}

Expand Down Expand Up @@ -223,15 +229,3 @@ pgexporter_ext_append(char* orig, char* s)

return n;
}

static char*
pgexporter_ext_append_char(char* orig, char c)
{
char str[2];

memset(&str[0], 0, sizeof(str));
snprintf(&str[0], 2, "%c", c);
orig = pgexporter_ext_append(orig, str);

return orig;
}

0 comments on commit 4ac3610

Please sign in to comment.