#include #include #include #include #include #include #include #include #include #include #include #include _syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count) const char *filename = "Makefile"; #define DE_BUF_SIZE 2000 void checkdir(const char *name) { int fd; char buffer[DE_BUF_SIZE]; struct stat file_info; long unsigned total = 0; char path[PATH_MAX]; snprintf(path, PATH_MAX, "%s/%s", name, filename); if (!lstat(path, &file_info)) { fprintf(stdout, "file info '%s':\n%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\n", path, file_info.st_dev, file_info.st_ino, file_info.st_mode, file_info.st_nlink, file_info.st_uid, file_info.st_gid, file_info.st_rdev, file_info.st_size, file_info.st_blksize, file_info.st_blocks, file_info.st_atime, file_info.st_mtime, file_info.st_ctime); } fd = open(name, O_RDONLY); if (fd == -1) { fprintf(stderr, "could not open directory '%s': %s\n", name, strerror(errno)); return; } while (1) { int offset = 0; struct dirent *entry; int count = getdents(fd, (struct dirent *) buffer, DE_BUF_SIZE); if (count == 0) { break; } else if (count == -1) { fprintf(stderr, "could not read directory entry: %s\n", strerror(errno)); break; } while (offset < count) { entry = (struct dirent *) (buffer + offset); if (strcmp(filename, entry->d_name)) { ++total; } else { fprintf(stdout, "%s\t%lu\n", name, total); goto out; } offset += entry->d_reclen; } } out: if (close(fd)) { fprintf(stderr, "could not close directory '%s': %s\n", name, strerror(errno)); } } int main(int argc, char *argv[]) { if (argc == 1) { fprintf(stderr, "Usage: %s [...]\n", argv[0]); exit(1); } for (++argv; *argv; ++argv) { checkdir(*argv); } return 0; }