Alters the behavior of the 'Create NAT on tunnel' to also add the appropriate NAT...
[tomato.git] / release / src / router / busybox / libbb / get_cpu_count.c
blobab468afdfca9e28814e7111ad3dc82e91367701d
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Factored out of mpstat/iostat.
5 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9 #include "libbb.h"
11 /* Does str start with "cpu"? */
12 int FAST_FUNC starts_with_cpu(const char *str)
14 return ((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')) == 0;
18 * Get number of processors. Uses /proc/stat.
19 * Return value 0 means one CPU and non SMP kernel.
20 * Otherwise N means N processor(s) and SMP kernel.
22 unsigned FAST_FUNC get_cpu_count(void)
24 FILE *fp;
25 char line[256];
26 int proc_nr = -1;
28 fp = xfopen_for_read("/proc/stat");
29 while (fgets(line, sizeof(line), fp)) {
30 if (!starts_with_cpu(line)) {
31 if (proc_nr >= 0)
32 break; /* we are past "cpuN..." lines */
33 continue;
35 if (line[3] != ' ') { /* "cpuN" */
36 int num_proc;
37 if (sscanf(line + 3, "%u", &num_proc) == 1
38 && num_proc > proc_nr
39 ) {
40 proc_nr = num_proc;
45 fclose(fp);
46 return proc_nr + 1;