Skip to content

Instantly share code, notes, and snippets.

@tiagovignatti
Created September 3, 2015 14:11
Show Gist options
  • Save tiagovignatti/7a6c5d31a03dd34536f2 to your computer and use it in GitHub Desktop.
Save tiagovignatti/7a6c5d31a03dd34536f2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
struct smaps_sizes {
int Pss;
int Rss;
};
int main(const int argc, const char **argv)
{
const char *filename = argv[1];
FILE *file = fopen(filename, "r");
struct smaps_sizes sizes;
memset(&sizes, 0, sizeof sizes);
if (!file) {
perror(filename);
return 1;
}
char line [BUFSIZ];
while (fgets(line, sizeof line, file))
{
char substr[32];
int n;
if (sscanf(line, "%31[^:]: %d", substr, &n) == 2)
{
if (strcmp(substr, "Pss") == 0) { sizes.Pss += n; }
else if (strcmp(substr, "Rss") == 0) { sizes.Rss += n; }
}
}
fclose(file);
printf("Pss %d\n", sizes.Pss);
printf("Rss %d\n", sizes.Rss);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment