| 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include "common.h" |
| 10 | |
| 11 | long long int run_command_lld(const char *command) { |
| 12 | char buf[128]; |
| 13 | long long int res = 0; |
| 14 | |
| 15 | FILE *p = popen(command, "r"); |
| 16 | if (!p) { |
| 17 | fprintf(stderr, "コマンドを実効に失敗: %s", command); |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | while (fgets(buf, sizeof(buf), p) != NULL) { |
| 22 | buf[strcspn(buf, "\n")] = '\0'; |
| 23 | } |
| 24 | |
| 25 | res = atoll(buf); |
| 26 | pclose(p); |
| 27 | |
| 28 | return res; |
| 29 | } |
| 30 | |
| 31 | const char *run_command_s(const char *command) { |
| 32 | char buf[128]; |
| 33 | char *out = NULL; |
| 34 | size_t outsize = 0; |
| 35 | |
| 36 | FILE *p = popen(command, "r"); |
| 37 | if (!p) { |
| 38 | fprintf(stderr, "コマンドを実効に失敗: %s", command); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | while (fgets(buf, sizeof(buf), p) != NULL) { |
| 43 | buf[strcspn(buf, "\n")] = '\0'; |
| 44 | |
| 45 | size_t len = strlen(buf); |
| 46 | char *nout = realloc(out, outsize + len + 1); |
| 47 | if (nout == NULL) { |
| 48 | perror("メモリの役割に失敗"); |
| 49 | free(out); |
| 50 | pclose(p); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | out = nout; |
| 55 | |
| 56 | memccpy(out + outsize, buf, sizeof(buf), len); |
| 57 | outsize += len; |
| 58 | out[outsize] = '\0'; |
| 59 | } |
| 60 | |
| 61 | pclose(p); |
| 62 | |
| 63 | return out; |
| 64 | } |
| 65 | |
| 66 | #if !defined(__HAIKU__) |
| 67 | const char *from_cache(const char *file) { |
| 68 | struct stat st; |
| 69 | if (stat(file, &st) != 0) { |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | FILE *f = fopen(file, "r"); |
| 74 | if (!f) { |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | char *cmd = (char *)malloc(128 * sizeof(char)); |
| 79 | if (!cmd) { |
| 80 | fclose(f); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | if (fgets(cmd, 128, f) == NULL) { |
| 85 | free(cmd); |
| 86 | fclose(f); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | fclose(f); |
| 91 | |
| 92 | cmd[strcspn(cmd, "\n")] = '\0'; |
| 93 | return cmd; |
| 94 | } |
| 95 | |
| 96 | int to_cache(const char *file, const char *res) { |
| 97 | char dir[256]; |
| 98 | snprintf(dir, sizeof(dir), "%s", "/tmp/farfetch"); |
| 99 | |
| 100 | struct stat st; |
| 101 | |
| 102 | if (stat(dir, &st) != 0) { |
| 103 | if (mkdir(dir, 0755) != 0 && errno != EEXIST) return -1; |
| 104 | } |
| 105 | |
| 106 | FILE *f = fopen(file, "w"); |
| 107 | if (!f) return -1; |
| 108 | |
| 109 | fprintf(f, "%s", res); |
| 110 | fclose(f); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | void delete_cache() { |
| 116 | struct stat st; |
| 117 | |
| 118 | if (stat("/tmp/farfetch/os", &st) == 0) remove("/tmp/farfetch/os"); |
| 119 | if (stat("/tmp/farfetch/host", &st) == 0) remove("/tmp/farfetch/host"); |
| 120 | #if defined(__linux__) || defined(__sunos) |
| 121 | if (stat("/tmp/farfetch/distro", &st) == 0) remove("/tmp/farfetch/distro"); |
| 122 | #endif |
| 123 | if (stat("/tmp/farfetch/cpu", &st) == 0) remove("/tmp/farfetch/cpu"); |
| 124 | if (stat("/tmp/farfetch/gpu", &st) == 0) remove("/tmp/farfetch/gpu"); |
| 125 | } |
| 126 | #endif |