/* ls4.c James S. Plank CS360 -- Spring, 1994 */ /* This is the same as ls3.c except the input is formatted so that all the filenames are padded to the size of the largest one. */ #include #include #include #include #include #include #include "dllist.h" int main(int argc, char **argv) { struct stat buf; int exists; DIR *d; struct dirent *de; Dllist files, tmp; int maxlen; d = opendir("."); if (d == NULL) { fprintf(stderr, "Couldn't open \".\"\n"); exit(1); } maxlen = 0; files = new_dllist(); for (de = readdir(d); de != NULL; de = readdir(d)) { /* List all fo the files and store in a linked list. */ dll_append(files, new_jval_s(strdup(de->d_name))); if (strlen(de->d_name) > maxlen) maxlen = strlen(de->d_name); /* Maintain the size of the longers filename. */ } closedir(d); dll_traverse(tmp, files) { /* Now traverse the list and call stat() on each file to determine its size. */ exists = stat(tmp->val.s, &buf); if (exists < 0) { fprintf(stderr, "%s not found\n", tmp->val.s); } else { printf("%*s %10lld\n", -maxlen, tmp->val.s, buf.st_size); } } return 0; }