コミットを比較

...

2 コミット

作成者 SHA1 メッセージ 日付
守矢諏訪子 89b7ad439c 安定化 2023-08-27 14:44:13 +09:00
守矢諏訪子 e0ee70cba9 もっと最適化 2023-08-27 14:31:19 +09:00
1個のファイルの変更54行の追加59行の削除

ファイルの表示

@ -48,65 +48,47 @@ struct utsname uname_info;
struct sysinfo my_sysinfo;
int title_length, status;
void remove_newline(char *s) {
while (*s != '\0' && *s != '\n') {
s++;
}
*s = '\0';
}
int remove_newline_get_length(char *s) {
int remove_newline(char *s) {
int i;
for (i = 0; *s != '\0' && *s != '\n'; s++, i++);
*s = '\0';
for (i = 0; s[i] != '\0' && s[i] != '\n'; ++i);
s[i] = '\0';
return i;
}
void truncate_spaces(char *str) {
int src = 0, dst = 0;
while (*(str + dst) == ' ') dst++;
char *src = str, *dst = str;
while (*(str + dst) != '\0') {
*(str + src) = *(str + dst);
if(*(str + (dst++)) == ' ') {
while (*(str + dst) == ' ') {
dst++;
}
while (*dst == ' ') ++dst;
while ((*src = *dst) != '\0') {
if (*(dst++) == ' ') {
while (*dst == ' ') ++dst;
}
src++;
++src;
}
*(str + src) = '\0';
}
void remove_substring(char *str, const char* substring, size_t len) {
char *sub = strstr(str, substring);
if (sub == NULL) {
return;
if (sub) {
memmove(sub, sub + len, strlen(sub + len) + 1);
}
int i = 0;
do {
*(sub+i) = *(sub+i+len);
} while (*(sub+(++i)) != '\0');
}
void replace_substring(char *str, const char *sub_str, const char *repl_str, size_t sub_len, size_t repl_len) {
char buffer[BUF_SIZE / 2];
char *start = strstr(str, sub_str);
if (start == NULL) {
return;
if (start) {
size_t tail_len = strlen(start + sub_len);
if (tail_len + repl_len < BUF_SIZE / 2) {
strcpy(buffer, start + sub_len);
strncpy(start, repl_str, repl_len);
strcpy(start + repl_len, buffer);
} else {
status = -1;
halt_and_catch_fire("new substring too long to replace");
}
}
if (strlen(str) - sub_len + repl_len >= BUF_SIZE / 2) {
status = -1;
halt_and_catch_fire("new substring too long to replace");
}
strcpy(buffer, start + sub_len);
strncpy(start, repl_str, repl_len);
strcpy(start + repl_len, buffer);
}
static char *get_title() {
@ -179,34 +161,47 @@ static char *get_kernel() {
}
static char *get_host() {
char *host = malloc(BUF_SIZE), buffer[BUF_SIZE/2];
FILE *product_name, *product_version, *model;
char buffer[BUF_SIZE / 2];
char *host = NULL;
if ((product_name = fopen("/sys/devices/virtual/dmi/id/product_name", "r")) != NULL) {
if ((product_version = fopen("/sys/devices/virtual/dmi/id/product_version", "r")) != NULL) {
fread(host, 1, BUF_SIZE/2, product_name);
product_name = fopen("/sys/devices/virtual/dmi/id/product_name", "r");
if (product_name != NULL) {
host = malloc(BUF_SIZE);
size_t read_len = fread(host, 1, BUF_SIZE / 2, product_name);
if (read_len > 0) {
remove_newline(host);
strcat(host, " ");
fread(buffer, 1, BUF_SIZE/2, product_version);
remove_newline(buffer);
if (strcmp(buffer, "To Be Filled By O.E.M.") != 0) {
strcat(host, " ");
strcat(host, buffer);
product_version = fopen("/sys/devices/virtual/dmi/id/product_version", "r");
if (product_version != NULL) {
read_len = fread(buffer, 1, BUF_SIZE / 2, product_version);
if (read_len > 0) {
remove_newline(buffer);
if (strcmp(buffer, "To Be Filled By O.E.M.") != 0) {
size_t host_len = strlen(host);
size_t remain_size = (BUF_SIZE - host_len) - 1;
if (remain_size > 0) {
snprintf(host + host_len, remain_size, " %s", buffer);
}
}
}
fclose(product_version);
}
fclose(product_version);
} else {
fclose(product_name);
goto model_fallback;
return host;
}
fclose(product_name);
return host;
}
model_fallback:
if ((model = fopen("/sys/firmware/devicetree/base/model", "r")) != NULL) {
fread(host, 1, BUF_SIZE, model);
remove_newline(host);
return host;
model = fopen("/sys/firmware/devicetree/base/model", "r");
if (model != NULL) {
host = malloc(BUF_SIZE);
size_t read_len = fread(host, 1, BUF_SIZE, model);
if (read_len > 0) {
remove_newline(host);
return host;
}
fclose(model);
}
status = -1;