static int path_sibling( const char *path, ... ) { int c; long n; int result = 0; char s[2]; FILE *fp; va_list ap; va_start( ap, path ); fp = path_vfopen( "r", path, ap ); va_end( ap ); while ( ( c = fgetc( fp ) ) != EOF ) { if ( isxdigit( c ) ) { s[0] = ( char ) c; s[1] = '\0'; for ( n = strtol( s, NULL, 16 ); n > 0; n /= 2 ) { if ( n % 2 ) result++; } } } fclose( fp ); return result; }
static cpu_set_t * path_cpuparse(int maxcpus, int islist, const char *path, va_list ap) { FILE *fd; cpu_set_t *set; size_t setsize, len = maxcpus * 7; char buf[len]; fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap); if (!fgets(buf, len, fd)) err(EXIT_FAILURE, _("cannot read %s"), pathbuf); fclose(fd); len = strlen(buf); if (buf[len - 1] == '\n') buf[len - 1] = '\0'; set = cpuset_alloc(maxcpus, &setsize, NULL); if (!set) err(EXIT_FAILURE, _("failed to callocate cpu set")); if (islist) { if (cpulist_parse(buf, set, setsize, 0)) errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf); } else { if (cpumask_parse(buf, set, setsize)) errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf); } return set; }
FILE * path_fopen(const char *mode, int exit_on_error, const char *path, ...) { FILE *fd; va_list ap; va_start(ap, path); fd = path_vfopen(mode, exit_on_error, path, ap); va_end(ap); return fd; }
void path_read_str(char *result, size_t len, const char *path, ...) { FILE *fd; va_list ap; va_start(ap, path); fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap); va_end(ap); if (!fgets(result, len, fd)) err(EXIT_FAILURE, _("cannot read %s"), pathbuf); fclose(fd); len = strlen(result); if (result[len - 1] == '\n') result[len - 1] = '\0'; }
uint64_t path_read_u64(const char *path, ...) { FILE *fd; va_list ap; uint64_t result; va_start(ap, path); fd = path_vfopen("r", 1, path, ap); va_end(ap); if (fscanf(fd, "%"SCNu64, &result) != 1) { if (ferror(fd)) err(EXIT_FAILURE, _("cannot read %s"), pathbuf); else errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); } fclose(fd); return result; }
int path_read_s32(const char *path, ...) { FILE *fd; va_list ap; int result; va_start(ap, path); fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap); va_end(ap); if (fscanf(fd, "%d", &result) != 1) { if (ferror(fd)) err(EXIT_FAILURE, _("cannot read %s"), pathbuf); else errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); } fclose(fd); return result; }
int path_getnum(const char *path, ...) { FILE *fd; va_list ap; int result; va_start(ap, path); fd = path_vfopen("r", 1, path, ap); va_end(ap); if (fscanf(fd, "%d", &result) != 1) { if (ferror(fd)) err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); else errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); } fclose(fd); return result; }