// You should not need any additional header files, so please // don't add any. // For opendir(3) #include #include // For access(3) #include // For printf(3) #include // For exit(2) #include // For strncat(3), strncpy(3) #include // Please make sure that you return a -1 if you weren't // able to find any heap data in the filename that was // passed. int heap_space_used(char *filename) { /* Your code goes below here */ /* * * * * * * * * * * * * * */ // Please put your code in this area, not elsewhere. /* * * * * * * * * * * * * * */ /* Your code goes above here */ return(-1); } // Please don't modify anything below this int my_filter(const struct dirent *d) { const char *str = d->d_name; if(str != NULL) { if( (*str >= '0') && (*str <= '9') ) return(1); } return(0); } int main() { struct dirent **namelist; chdir("/proc"); int n = scandir(".", &namelist, my_filter, NULL); if(n < 0) { perror("Cannot scan directory /proc."); exit(1); } int index = 0; char pathname[256]; int count = 0; float sum = 0; while(index < n) { strncpy(pathname,"/proc/",256); strncat(pathname,namelist[index]->d_name,256); strncat(pathname,"/maps",256); int val = heap_space_used(pathname); if(val != -1) { printf("Usable result from %s.\n",pathname); count++; sum += val; } free(namelist[index]); index++; } free(namelist); printf("Processes read : %d, total heap used = %1.0f, average heap size = %10.2f\n",count,sum,sum/count); }