Andrew Jeffery | 41f211b | 2017-04-12 14:08:03 +0930 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <stdbool.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include "common.h" |
| 7 | |
| 8 | static bool is_pnor_part(const char *str) |
| 9 | { |
| 10 | return strcasestr(str, "pnor") != NULL; |
| 11 | } |
| 12 | |
| 13 | char *get_dev_mtd(void) |
| 14 | { |
| 15 | FILE *f; |
| 16 | char *ret = NULL, *pos = NULL; |
| 17 | char line[255]; |
| 18 | |
| 19 | f = fopen("/proc/mtd", "r"); |
| 20 | if (!f) |
| 21 | return NULL; |
| 22 | |
| 23 | while (!pos && fgets(line, sizeof(line), f) != NULL) { |
| 24 | /* Going to have issues if we didn't get the full line */ |
| 25 | if (line[strlen(line) - 1] != '\n') |
| 26 | break; |
| 27 | |
| 28 | if (is_pnor_part(line)) { |
| 29 | pos = strchr(line, ':'); |
| 30 | if (!pos) |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 | fclose(f); |
| 35 | |
| 36 | if (pos) { |
| 37 | *pos = '\0'; |
| 38 | if (asprintf(&ret, "/dev/%s", line) == -1) |
| 39 | ret = NULL; |
| 40 | } |
| 41 | |
| 42 | return ret; |
| 43 | } |