| 33 | } |
| 34 | |
| 35 | static int |
| 36 | cg_read(const char *attr, char *buf) |
| 37 | { |
| 38 | int result = 0; |
| 39 | int maybe = 0; |
| 40 | if (attr[0] == '?') |
| 41 | { |
| 42 | attr++; |
| 43 | maybe = 1; |
| 44 | } |
| 45 | |
| 46 | char path[PATH_MAX]; |
| 47 | cg_makepath(path, sizeof(path), attr); |
| 48 | |
| 49 | int fd = open(path, O_RDONLY); |
| 50 | if (fd < 0) |
| 51 | { |
| 52 | if (maybe) |
| 53 | goto fail; |
| 54 | die("Cannot read %s: %m", path); |
| 55 | } |
| 56 | |
| 57 | int n = read(fd, buf, CG_BUFSIZE); |
| 58 | if (n < 0) |
| 59 | { |
| 60 | if (maybe) |
| 61 | goto fail_close; |
| 62 | die("Cannot read %s: %m", path); |
| 63 | } |
| 64 | if (n >= CG_BUFSIZE - 1) |
| 65 | die("Attribute %s too long", path); |
| 66 | if (n > 0 && buf[n-1] == '\n') |
| 67 | n--; |
| 68 | buf[n] = 0; |
| 69 | |
| 70 | if (verbose > 1) |
| 71 | msg("CG: Read %s = <%s>\n", attr, buf); |
| 72 | |
| 73 | result = 1; |
| 74 | fail_close: |
| 75 | close(fd); |
| 76 | fail: |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | static void __attribute__((format(printf,2,3))) |
| 81 | cg_write(const char *attr, const char *fmt, ...) |
no test coverage detected
searching dependent graphs…