#include <_proc.h> #include #include #include #include #include #include void usage(void) { fprintf(stderr, "usage: ps [path]\n"); exit(1); } int main(int argc, const char *argv[]) { const char *path = argc >= 2 ? argv[1] : "/proc/"; if (argc > 2) usage(); DIR *dir = opendir(path); if (!dir) { err(1, "couldn't open %s", path); } struct dirent *de; while ((de = readdir(dir))) { const char *name = de->d_name; char psdata[128]; char *s; if (isdigit(name[0]) && asprintf(&s, "%s%smem", path, name) >= 0) { FILE *g = fopen(s, "r"); free(s); if (!g) { warn("couldn't open \"%s\"", s); strcpy(psdata, "(can't peek)"); } else { fseek(g, (long)&_psdata_loc->desc, SEEK_SET); if (fread(psdata, 1, sizeof(psdata)-1, g) <= 0) { strcpy(psdata, "(no psdata)"); } psdata[sizeof(psdata)-1] = '\0'; fclose(g); } *strchr(name, '/') = '\0'; printf("%s\t%s\n", name, psdata); } } closedir(dir); return 0; }