fts.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Tue, 09 Feb 2010 08:38:32 +0100
changeset 110 e1bc09732fa3
parent 77 49e0babccb23
child 112 9bfb339a8156
permissions -rw-r--r--
Proper location of glassfish 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
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 68
diff changeset
     2
 * fts.c
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 68
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 68
diff changeset
     4
 *
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 68
diff changeset
     5
 * file hierarchy browser
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
 */
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 <stdio.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <stdlib.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 <sys/types.h>
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <sys/stat.h>
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
#include <fts.h>
68
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    15
#include <errno.h>
67
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
int compare (const FTSENT ** a, const FTSENT ** b)
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
    return 0;
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
int main (int argc, char **argv)
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
    FTS *fts;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    FTSENT *current = NULL;
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
    char **paths;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    int i;
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
    if (argc < 2) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        printf ("Usage: %s [dir]...\n", argv[0]);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        return EXIT_FAILURE;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    }
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
    if ((paths = calloc (argc + 1, sizeof (char *))) == NULL) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        perror ("CALLOC");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
        return EXIT_FAILURE;
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
    for (i = 1; i < argc; i++) {
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        paths[i - 1] = argv[i];
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    paths[i] = NULL;
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
    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
    48
        perror ("fts_open");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
        return EXIT_FAILURE;
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
    while ((current = fts_read (fts)) != NULL) {
68
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    53
        if (!errno)
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    54
            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
    55
    }
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 (fts_close (fts) < 0)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        perror ("fts_close");
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
    if (paths != NULL)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        free (paths);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    return EXIT_SUCCESS;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
}