| 78 | } |
| 79 | |
| 80 | static void __attribute__((format(printf,2,3))) |
| 81 | cg_write(const char *attr, const char *fmt, ...) |
| 82 | { |
| 83 | int maybe = 0; |
| 84 | if (attr[0] == '?') |
| 85 | { |
| 86 | attr++; |
| 87 | maybe = 1; |
| 88 | } |
| 89 | |
| 90 | va_list args; |
| 91 | va_start(args, fmt); |
| 92 | |
| 93 | char buf[CG_BUFSIZE]; |
| 94 | int n = vsnprintf(buf, sizeof(buf), fmt, args); |
| 95 | if (n >= CG_BUFSIZE) |
| 96 | die("cg_write: Value for attribute %s is too long", attr); |
| 97 | |
| 98 | if (verbose > 1) |
| 99 | msg("CG: Write %s = %s", attr, buf); |
| 100 | |
| 101 | char path[PATH_MAX]; |
| 102 | cg_makepath(path, sizeof(path), attr); |
| 103 | |
| 104 | int fd = open(path, O_WRONLY | O_TRUNC); |
| 105 | if (fd < 0) |
| 106 | { |
| 107 | if (maybe) |
| 108 | goto fail; |
| 109 | else |
| 110 | die("Cannot write %s: %m", path); |
| 111 | } |
| 112 | |
| 113 | int written = write(fd, buf, n); |
| 114 | if (written < 0) |
| 115 | { |
| 116 | if (maybe) |
| 117 | goto fail_close; |
| 118 | else |
| 119 | die("Cannot set %s to %s: %m", path, buf); |
| 120 | } |
| 121 | if (written != n) |
| 122 | die("Short write to %s (%d out of %d bytes)", path, written, n); |
| 123 | |
| 124 | fail_close: |
| 125 | close(fd); |
| 126 | fail: |
| 127 | va_end(args); |
| 128 | } |
| 129 | |
| 130 | static FILE *cg_fopen(const char *attr) |
| 131 | { |
no test coverage detected
searching dependent graphs…