1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include "libbb.h"
- int FAST_FUNC starts_with_cpu(const char *str)
- {
- return ((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')) == 0;
- }
- unsigned FAST_FUNC get_cpu_count(void)
- {
- FILE *fp;
- char line[256];
- int proc_nr = -1;
- fp = xfopen_for_read("/proc/stat");
- while (fgets(line, sizeof(line), fp)) {
- if (!starts_with_cpu(line)) {
- if (proc_nr >= 0)
- break;
- continue;
- }
- if (line[3] != ' ') {
- int num_proc;
- if (sscanf(line + 3, "%u", &num_proc) == 1
- && num_proc > proc_nr
- ) {
- proc_nr = num_proc;
- }
- }
- }
- fclose(fp);
- return proc_nr + 1;
- }
|