summaryrefslogtreecommitdiff
path: root/readdir_test.c
diff options
context:
space:
mode:
authorKjetil Ørbekk <orbekk@pvv.ntnu.no>2007-11-21 20:09:39 +0100
committerKjetil Ørbekk <orbekk@pvv.ntnu.no>2007-11-21 20:09:39 +0100
commite660aacdb3fe0f79d1531d9aa0c8ce2f158b7ae3 (patch)
treeb733c00074911793d6299c03ac897179c43cab8f /readdir_test.c
parent7de7ada912e81e4eedab3a1e94992b4ab778a669 (diff)
- Added Makefile
- Moved `test programs` out of source tree (So the Makefile actually works)
Diffstat (limited to 'readdir_test.c')
-rw-r--r--readdir_test.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/readdir_test.c b/readdir_test.c
new file mode 100644
index 0000000..0cca325
--- /dev/null
+++ b/readdir_test.c
@@ -0,0 +1,52 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <string.h>
+#include <errno.h>
+
+extern int errno;
+
+void print_directory (char *path, int level)
+{
+ DIR *dir = opendir (path);
+ int i;
+
+ if (dir == NULL) {
+ for (i=0; i<level; i++) printf (" ");
+ if (errno == EACCES)
+ printf ("* No access\n");
+ return;
+ }
+
+ struct dirent *de;
+
+ while ((de = readdir (dir)) != NULL) {
+ if (strcmp (de->d_name, "." ) == 0 ||
+ strcmp (de->d_name, "..") == 0)
+ continue;
+
+ for (i=0; i<level; i++) printf (" ");
+ printf ("- %s\n", de->d_name);
+
+ if (de->d_type == 4) { /* ToDo: have to use lstat or something */
+ char subdir[strlen(path) + strlen(de->d_name) + 1];
+ strcpy (subdir, path);
+ strcpy (subdir+strlen(path), "/");
+ strcpy (subdir+strlen(path)+1, de->d_name);
+
+ print_directory (subdir, level+1);
+ }
+ }
+}
+
+int main (int argc, char **argv)
+{
+ if (argc != 2)
+ return -1;
+
+ print_directory (argv[1], 0);
+ return 0;
+}
+