From beb8d507d2f2a024839efa91aaedad4100759039 Mon Sep 17 00:00:00 2001 From: WlodekM Date: Sat, 15 Feb 2025 12:59:03 +0200 Subject: ls --- progs/ls/ls.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 progs/ls/ls.c (limited to 'progs') 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 +#include +#include +#include +#include +#include +#include + +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 -- cgit 1.4.1-2-gfad0