[v0.96] Consider the case where present file does not contain a hyphen

This commit is contained in:
Dr-Noob
2021-04-07 20:22:29 +02:00
parent 586283a1be
commit d239906f22

View File

@@ -18,34 +18,42 @@
int get_ncores_from_cpuinfo() { int get_ncores_from_cpuinfo() {
// Examples: // Examples:
// 0-271 // 0-271
// 0-5
// 0-7 // 0-7
/* // 0
int filelen; int filelen;
char* buf; char* buf;
if((buf = read_file(_PATH_CPUS_PRESENT, &filelen)) == NULL) { if((buf = read_file(_PATH_CPUS_PRESENT, &filelen)) == NULL) {
perror("open"); perror("open");
return UNKNOWN; return UNKNOWN;
}
int ncores;
char* tmp1;
if((tmp1 = strstr(buf, "-")) == NULL) {
// file contains no - character, we assume that it contains 0,
// which means that the CPU contains only one core
return 1;
}
else {
tmp1++;
} }
int ncores = 0;
char* tmp1 = strstr(buf, "-") + 1;
char* tmp2 = strstr(buf, "\n"); char* tmp2 = strstr(buf, "\n");
char ncores_str[filelen]; char ncores_str[filelen];
memset(ncores_str, 0, sizeof(char) * filelen); memset(ncores_str, 0, sizeof(char) * filelen);
memcpy(ncores_str, tmp1, tmp2-tmp1); memcpy(ncores_str, tmp1, tmp2-tmp1);
char* end; char* end;
errno = 0; errno = 0;
ncores = strtol(ncores_str, &end, 10) + 1; ncores = strtol(ncores_str, &end, 10) + 1;
if(errno != 0) { if(errno != 0) {
perror("strtol"); perror("strtol");
return UNKNOWN; return UNKNOWN;
} }
free(buf);*/ free(buf);
return 1; return ncores;
} }
long parse_cpuinfo_field(char* buf, char* field_str, int field_base) { long parse_cpuinfo_field(char* buf, char* field_str, int field_base) {