#include #include #include #include #include #ifndef REOPEN #define REOPEN 0 #endif int main(int argc, char **argv) { int bufsize = 1024; char *buf = NULL; int bufused; int got; int fd = -1; char path[100]; if (argc <= 1) strcpy(path, "/proc/self/maps"); else sprintf(path, "/proc/%s/maps", argv[1]); again: if (fd == -1) fd = open(path, O_RDONLY); if (fd == -1) { perror("open"); exit(1); } if (lseek(fd, 0, SEEK_SET) == -1) { perror("lseek"); exit(1); } bufused = 0; if (buf == NULL) buf = malloc(bufsize); do { int want = bufsize - bufused; got = read(fd, &buf[bufused], want > 4000 ? 4000 : want); /* work around other bug */ if (got < 0) { perror("read"); exit(1); } bufused += got; } while(bufused < bufsize && got != 0); if (bufused == bufsize) { free(buf); buf = NULL; bufsize *= 2; if (REOPEN) { /* reopen */ close(fd); fd = -1; } goto again; } close(fd); write(1, buf, bufused); return 0; }