The simple parser is based on 'strstr', whereas the actual data does
include a white-space character to separate the 'id' (procs_running)
from the 'value' (a number). Yet parsing the number can off course only
take place 'after' the white-space. Subsequently, not finding a
'white-space' should be treated as an error condition.
Adding the 'white-space' in const string and defining the offset based
on 'strlen' fixes this properly.
static int procs_running(void) {
char buffer[4096] = {};
-
+ char id[] = "procs_running "; /* white space terminated */
char *running;
ssize_t status;
return -1;
}
- running = strstr(buffer, "procs_running");
+ /* the data contains :
+ * the literal string 'procs_running',
+ * a whitespace
+ * the number of running processes.
+ * The parser does include the white-space character.
+ */
+ running = strstr(buffer, id);
if (!running) {
WARNING("procs_running not found, assuming 1");
return 1;
}
-
- running += 14;
+ running += strlen(id);
return atoi(running);
}