fts.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 70 ded389a5dc2a
parent 68 d549894aa6a9
child 77 49e0babccb23
permissions -rw-r--r--
Profiling support added * To use the benefits of profiling, set PROFILING=<target> * eg: mbroeker@localhost $ make -e PROFILING=linux * mbroeker@localhost $ export PROFILING=linux && make 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>
68
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    12
#include <errno.h>
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
int compare (const FTSENT ** a, const FTSENT ** b)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
{
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    return 0;
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
int main (int argc, char **argv)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
{
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    FTS *fts;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    FTSENT *current = NULL;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    char **paths;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    int i;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    if (argc < 2) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        printf ("Usage: %s [dir]...\n", argv[0]);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
        return EXIT_FAILURE;
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    if ((paths = calloc (argc + 1, sizeof (char *))) == NULL) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        perror ("CALLOC");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        return EXIT_FAILURE;
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    for (i = 1; i < argc; i++) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
        paths[i - 1] = argv[i];
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    paths[i] = NULL;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    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
    45
        perror ("fts_open");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        return EXIT_FAILURE;
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    while ((current = fts_read (fts)) != NULL) {
68
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    50
        if (!errno)
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    51
            printf ("%s/%s\n", current->fts_path, current->fts_name);
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    }
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    if (fts_close (fts) < 0)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        perror ("fts_close");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    if (paths != NULL)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        free (paths);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    return EXIT_SUCCESS;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
}