diff options
Diffstat (limited to 'src/cmd/ls/ls.c')
| -rwxr-xr-x | src/cmd/ls/ls.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/cmd/ls/ls.c b/src/cmd/ls/ls.c new file mode 100755 index 0000000..a937457 --- /dev/null +++ b/src/cmd/ls/ls.c @@ -0,0 +1,78 @@ +/* TODO: + * - add flags + * - add option to print more file information + */ +#include <stdio.h> +#include <sys/types.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/stat.h> + +void dirwalk(DIR *); +void error(char *); +void printdirent(struct dirent *); +void printfile(char *); + +/* print information about files and directories */ +int main(int argc, char **argv) +{ + DIR *dp; + if (argc == 1) { + if ((dp = opendir(".")) == NULL) { + perror("ls"); + return -1; + } + dirwalk(dp); + } else if (argc == 2) { + if ((dp = opendir(argv[1])) == NULL) { + perror("ls"); + return -1; + } + dirwalk(dp); + } else { + while (--argc > 0 && !ferror(stdout)) { + errno = 0; + if ((dp = opendir(*++argv)) == NULL) { + perror("ls"); + continue; + } + dirwalk(dp); + } + } +} + +/* dirwalk: read all the directory entries in a directory */ +void dirwalk(DIR *d) +{ + struct dirent *dir; + while ((dir = readdir(d)) != NULL) { + errno = 0; + if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) + continue; + printdirent(dir); + } + if (errno != 0) { + perror("ls"); + } +} + +/* printdirent: print a directory entry and any selected information */ +void printdirent(struct dirent *dir) +{ + printf("%s\n", dir->d_name); +} + +/* printfile: print a file and any other selected information */ +void printfile(char *name) +{ + struct stat stbuf; + int e = stat(name, &stbuf); + if (e != 0) { + perror("ls"); + return; + } + printf("%s\n", name); +} + |
