Skip to content

Instantly share code, notes, and snippets.

@lloyd
Created November 13, 2013 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lloyd/7452319 to your computer and use it in GitHub Desktop.
Save lloyd/7452319 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <regex.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
char * stringarr[] = {
"817 (Calendar) S 246 112 0 0 -1 4194624 6534 0 83 0 139 8 0 0 38 18 11 0 22117 61526016 5437 4294967295 32768 34809 3204114848 3204111656 1074820320 0 0 69634 1073777896 4294967295 0 0 17 0 0 0 0 0 0",
"246 ((Nuwa)) S 112 112 0 0 -1 4194560 8245 0 33 0 108 18 0 0 21 1 13 0 1080 54116352 3415 4294967295 32768 34809 3204114848 3204111296 1074820320 0 0 69634 36072 4294967295 0 0 17 0 0 0 0 0 0",
"910 ((Preallocated a) S 246 112 0 0 -1 4194368 2715 0 0 0 5 5 0 0 38 18 10 0 27168 58302464 3049 4294967295 32768 34809 3204114848 3204111656 1074820320 0 0 69634 36072 4294967295 0 0 17 0 0 0 0 0 0",
"910 (I have a really long process name) S 246 112 0 0 -1 4194368 2715 0 0 0 5 5 0 0 38 18 10 0 27168 58302464 3049 4294967295 32768 34809 3204114848 3204111656 1074820320 0 0 69634 36072 4294967295 0 0 17 0 0 0 0 0 0",
"910 (I (ahem) am pareny) S 246 112 0 0 -1 4194368 2715 0 0 0 5 5 0 0 38 18 10 0 27168 58302464 3049 4294967295 32768 34809 3204114848 3204111656 1074820320 0 0 69634 36072 4294967295 0 0 17 0 0 0 0 0 0"
};
int main(void) {
static char * pattern =
"^" // beginning of string
"([0-9]+) " // pid
"(\\(.*\\)) "// comm
"[A-Z] " // state
"([0-9]+) " // ppid #include <sys/types.h>
"[0-9]+ " // pgrp
"[0-9]+ " // session
"[0-9]+ " // tty_nr
"-?[0-9]+ " // tpgid
"[0-9]+ " // flags
"[0-9]+ " // minflt (%lu)
"[0-9]+ " // cminflt (%lu)
"[0-9]+ " // majflt (%lu)
"[0-9]+ " // cmajflt (%lu)
"[0-9]+ " // utime (%lu)
"[0-9]+ " // stime (%ld)
"[0-9]+ " // cutime (%ld)
"[0-9]+ " // cstime (%ld)
"[0-9]+ " // priority (%ld)
"([0-9]+)"; // niceness
regex_t preg;
int i, rc;
if (0 != (rc = regcomp(&preg, pattern, REG_EXTENDED))) {
printf("regcomp() failed, returning nonzero (%d)\n", rc);
exit(EXIT_FAILURE);
}
for (i = 0 ; i < sizeof(stringarr) / sizeof(stringarr[0]) ; i++) {
char name[32];
long int nice;
int pid2, ppid;
size_t nmatch = 5;
regmatch_t pmatch[5];
int rc;
if (0 != (rc = regexec(&preg, stringarr[i], nmatch, pmatch, 0))) {
printf("Failed to match '%s' with '%s',returning %d.\n",
stringarr[i], pattern, rc);
} else {
pid2 = (int) strtol(stringarr[i] + pmatch[1].rm_so, (char **) NULL, 10);
strncpy(name, stringarr[i] + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
name[pmatch[2].rm_eo - pmatch[2].rm_so] = 0;
ppid = (int) strtol(stringarr[i] + pmatch[3].rm_so, (char **) NULL, 10);
nice = (int) strtol(stringarr[i] + pmatch[4].rm_so, (char **) NULL, 10);
printf("MATCHED %d/%d (%ld): %s\n", pid2, ppid, nice, name);
}
}
regfree(&preg);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment