fts.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 67 abe63a276a36
child 68 d549894aa6a9
permissions -rw-r--r--
file traversal demo && minor changes Traversal through a filesystem hierarchy made easy committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * fts.c file hierarchy browser
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 */
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
#include <stdio.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdlib.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <sys/types.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <sys/stat.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <fts.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
int compare (const FTSENT ** a, const FTSENT ** b)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
{
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    return 0;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
}
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
int main (int argc, char **argv)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
{
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    FTS *fts;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    FTSENT *current = NULL;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    char **paths;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    int i;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    if (argc < 2) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        printf ("Usage: %s [dir]...\n", argv[0]);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        return EXIT_FAILURE;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    }
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    if ((paths = calloc (argc + 1, sizeof (char *))) == NULL) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        perror ("CALLOC");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        return EXIT_FAILURE;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    }
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    for (i = 1; i < argc; i++) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
        paths[i - 1] = argv[i];
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    }
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    paths[i] = NULL;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    if ((fts = fts_open (paths, FTS_NOSTAT, &compare)) == NULL) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
        perror ("fts_open");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        return EXIT_FAILURE;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    }
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    while ((current = fts_read (fts)) != NULL) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
        printf ("%s/%s\n", current->fts_path, current->fts_name);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    }
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    if (fts_close (fts) < 0)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        perror ("fts_close");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    if (paths != NULL)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        free (paths);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    return EXIT_SUCCESS;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
}