* Extract the content of a the first occurrence of a given field in * the content of /proc/cpuinfo and return it as a heap-allocated * string that must be freed by the caller. * * Return NULL if not found */
| 130 | * Return NULL if not found |
| 131 | */ |
| 132 | static char* |
| 133 | extract_cpuinfo_field(const char* buffer, int buflen, const char* field) |
| 134 | { |
| 135 | int fieldlen = strlen(field); |
| 136 | const char* bufend = buffer + buflen; |
| 137 | char* result = NULL; |
| 138 | int len; |
| 139 | const char *p, *q; |
| 140 | |
| 141 | /* Look for first field occurrence, and ensures it starts the line. */ |
| 142 | p = buffer; |
| 143 | for (;;) { |
| 144 | p = memmem(p, bufend-p, field, fieldlen); |
| 145 | if (p == NULL) { |
| 146 | goto EXIT; |
| 147 | } |
| 148 | |
| 149 | if (p == buffer || p[-1] == '\n') { |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | p += fieldlen; |
| 154 | } |
| 155 | |
| 156 | /* Skip to the first column followed by a space */ |
| 157 | p += fieldlen; |
| 158 | p = memchr(p, ':', bufend-p); |
| 159 | if (p == NULL || p[1] != ' ') { |
| 160 | goto EXIT; |
| 161 | } |
| 162 | |
| 163 | /* Find the end of the line */ |
| 164 | p += 2; |
| 165 | q = memchr(p, '\n', bufend-p); |
| 166 | if (q == NULL) { |
| 167 | q = bufend; |
| 168 | } |
| 169 | |
| 170 | /* Copy the line into a heap-allocated buffer */ |
| 171 | len = q - p; |
| 172 | result = malloc(len + 1); |
| 173 | if (result == NULL) { |
| 174 | goto EXIT; |
| 175 | } |
| 176 | |
| 177 | memcpy(result, p, len); |
| 178 | result[len] = '\0'; |
| 179 | |
| 180 | EXIT: |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Checks that a space-separated list of items contains one given 'item'. |
no outgoing calls
no test coverage detected