Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/hwmonitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <curl/curl.h>

#include "display.h"
#include "theme.h"
#include "util.h"
#include "api/groq.h"

Expand All @@ -28,6 +29,9 @@ main(int argc, char **argv)
/* Parsed from util.c */
parse_arguments(argc, argv, &config);

/* Initialize theme colors based on TTY check */
theme_init(false);

if (config.watch_mode) {
signal(SIGINT, handle_sigint);
}
Expand Down
32 changes: 22 additions & 10 deletions src/linux/hardware/cpu.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "base.h"
#include "file.h"
#include "io.h"
#include <string.h>
#include "cpu.h"

static int16_t
Expand All @@ -14,10 +15,19 @@ cpu_get_total_cores(void)
int start, end;
int16_t count = 0;

if (sscanf(online, "%d-%d", &start, &end) == 2) {
count = (end - start) + 1;
} else if (sscanf(online, "%d", &start) == 1) {
count = 1;
char *saveptr = NULL;
char *token = strtok_r(online, ",", &saveptr);

while (token != NULL) {
if (sscanf(token, "%d-%d", &start, &end) == 2) {
if (end >= start) {
count += (end - start) + 1;
}
} else if (sscanf(token, "%d", &start) == 1) {
count += 1;
}

token = strtok_r(NULL, ",", &saveptr);
}

free(online);
Expand All @@ -36,29 +46,31 @@ cpu_get_total_freq_mhz(uint16_t core_id, const char *filename)
}

int32_t khz = atoi(cpu_freq);

free(cpu_freq);

if (khz <= 0) {
return -1;
}

return khz / 1000;
return khz / 1000.0f;
}

static char *
cpu_get_arch(const char *flags)
{
if (flags == NULL) {
return "x86";
return strdup("x86");
}

char *flag = str_find_value(flags, "lm", NULL);
if (flag == NULL) {
return "x86";
} else {
free(flag);
return "x86_64";
return strdup("x86");
}

free(flag);

return strdup("x86_64");
}

CPU *
Expand Down
70 changes: 37 additions & 33 deletions src/linux/io.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "base.h"
#include "io.h"

char *
Expand All @@ -8,46 +7,51 @@ str_find_value(const char *source, const char *search, const char *delim)
return NULL;
}

/* Locate the key in the source string */
const char *start = strstr(source, search);
if (start == NULL) {
return NULL;
}
size_t search_len = strlen(search);
const char *current = source;

/* Skip past the length of the key itself */
start += strlen(search);
while ((current = strstr(current, search)) != NULL) {
bool valid_start = (current == source || *(current - 1) == '\n');

/* Skip leading separators and whitespaces to reach the start of the value */
while (*start != '\0' && (isspace((unsigned char)*start) || *start == ':')) {
++start;
}
if (valid_start) {
const char *start = current + search_len;

/* Find the end of the value based on the provided delimiters */
const char *end = start;
while (*end != '\0' && strchr(delim, *end) == NULL) {
++end;
}
/* Skip leading separators and whitespaces to reach the start of the value */
while (*start != '\0' && (isspace((unsigned char)*start) || *start == ':' || *start == '=')) {
++start;
}

/* Backtrack to trim any trailing whitespace */
while (end > start && isspace((unsigned char)*(end - 1))) {
--end;
}
/* Find the end of the value based on the provided delimiters */
const char *end = start;
while (*end != '\0' && strchr(delim, *end) == NULL) {
++end;
}

uint64_t len = end - start;
if (len == 0) {
return NULL;
}
/* Backtrack to trim any trailing whitespace */
while (end > start && isspace((unsigned char)*(end - 1))) {
--end;
}

/* Allocate memory and copy the resulting value */
char *value = malloc(len + 1);
if (value == NULL) {
return NULL;
}
uint64_t len = end - start;
if (len == 0) {
return NULL;
}

memcpy(value, start, len);
value[len] = '\0';
/* Allocate memory and copy the resulting value */
char *value = malloc(len + 1);
if (value == NULL) {
return NULL;
}

memcpy(value, start, len);
value[len] = '\0';

return value;
}
current += 1;
}

return value;
return NULL;
}

double
Expand Down
Loading