diff options
author | WlodekM <[email protected]> | 2025-02-15 12:59:03 +0200 |
---|---|---|
committer | WlodekM <[email protected]> | 2025-02-15 12:59:03 +0200 |
commit | beb8d507d2f2a024839efa91aaedad4100759039 (patch) | |
tree | 3c5041347fc11d99ffd700718cfca0052285491d /progs | |
parent | 82ee985d93484f8ac1cabd688e33a35cb2ebe926 (diff) |
ls
Diffstat (limited to 'progs')
-rw-r--r-- | progs/ls/ls.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/progs/ls/ls.c b/progs/ls/ls.c new file mode 100644 index 0000000..976e8c9 --- /dev/null +++ b/progs/ls/ls.c @@ -0,0 +1,50 @@ +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <string.h> +#include <dirent.h> + +void joinPath(char *result, const char *base, const char *rel) { + strcpy(result, base); + strcat(result, "/"); + strcat(result, rel); +} + +int main(int argc, char *argv[]) { + char dirp[128] = "."; + if (argc > 1) { + strcpy(dirp, argv[1]); + } + char newpath[128] = {0}; + char newresolvedpath[128] = {0}; + if (dirp[0] == '/') { + strcpy(newresolvedpath, dirp); + } else { + joinPath(newpath, ".", dirp); + realpath(newpath, newresolvedpath); + } + if (!access(newresolvedpath, F_OK)) { + struct stat statResult; + stat(newresolvedpath, &statResult); + if (!S_ISDIR(statResult.st_mode)) { + write(1, "uuuuh das not a dir\n", 21); + exit(1); + } + } else { + write(1, "uuuuh das not a dir\n", 21); + exit(1); + } + DIR *d; + struct dirent *dir; + d = opendir(dirp); + //TODO - sort + if (d) { + while ((dir = readdir(d)) != NULL) { + printf("%s\n", dir->d_name); + } + closedir(d); + } + exit(0); +} \ No newline at end of file |