file traversal demo && minor changes
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 67 abe63a276a36
parent 66 2b4f786d9073
child 68 d549894aa6a9
file traversal demo && minor changes Traversal through a filesystem hierarchy made easy committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
fts.c
prog_limit.c
--- a/Makefile
+++ b/Makefile
@@ -55,6 +55,7 @@
 TARGET += fork
 TARGET += duff
 TARGET += unicode
+TARGET += fts
 
 .SUFFIXES: .c .cc .asm
 
@@ -288,6 +289,12 @@
 	@echo Linking $< ...
 	@$(CC) -std=c99 -o $@ $<
 
+fts: fts.c
+	@echo Compiling \(NON-ANSI\) $<...
+	@$(CC) -c -Wall -O2 $<
+	@echo Linking $< ...
+	@$(CC) -o $@ $<
+
 .PHONY: beauty clean uninstall
 
 clean:
new file mode 100644
--- /dev/null
+++ b/fts.c
@@ -0,0 +1,59 @@
+/**
+ * fts.c file hierarchy browser
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <fts.h>
+
+int compare (const FTSENT ** a, const FTSENT ** b)
+{
+    return 0;
+}
+
+int main (int argc, char **argv)
+{
+    FTS *fts;
+    FTSENT *current = NULL;
+
+    char **paths;
+
+    int i;
+
+    if (argc < 2) {
+        printf ("Usage: %s [dir]...\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    if ((paths = calloc (argc + 1, sizeof (char *))) == NULL) {
+        perror ("CALLOC");
+        return EXIT_FAILURE;
+    }
+
+    for (i = 1; i < argc; i++) {
+        paths[i - 1] = argv[i];
+    }
+
+    paths[i] = NULL;
+
+    if ((fts = fts_open (paths, FTS_NOSTAT, &compare)) == NULL) {
+        perror ("fts_open");
+        return EXIT_FAILURE;
+    }
+
+    while ((current = fts_read (fts)) != NULL) {
+        printf ("%s/%s\n", current->fts_path, current->fts_name);
+    }
+
+    if (fts_close (fts) < 0)
+        perror ("fts_close");
+
+    if (paths != NULL)
+        free (paths);
+
+    return EXIT_SUCCESS;
+}
--- a/prog_limit.c
+++ b/prog_limit.c
@@ -37,7 +37,7 @@
     switch (pid) {
     case 0:
         if (set_limit (atoi (argv[1])) == 0) {
-            i = execvp (argv[2], args);
+            i = execvp (args[0], args);
         } else {
             perror ("Limit Error");
             return EXIT_FAILURE;