fts.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 05 Mar 2010 22:04:25 +0100
changeset 116 ae784a3a45b1
parent 112 9bfb339a8156
permissions -rw-r--r--
The find utility needs a path parameter 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;
112
9bfb339a8156 respect the prototype
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    46
    errno = 0;
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
112
9bfb339a8156 respect the prototype
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    48
    if ((fts = fts_open (paths, FTS_NOSTAT, (void *)&compare)) == NULL) {
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
        perror ("fts_open");
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
        return EXIT_FAILURE;
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
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    while ((current = fts_read (fts)) != NULL) {
68
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    54
        if (!errno)
d549894aa6a9 Trivial Changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
    55
            printf ("%s/%s\n", current->fts_path, current->fts_name);
112
9bfb339a8156 respect the prototype
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    56
        else
9bfb339a8156 respect the prototype
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    57
            perror ("fts_read");
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    }
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 (fts_close (fts) < 0)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        perror ("fts_close");
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
    if (paths != NULL)
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
        free (paths);
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    return EXIT_SUCCESS;
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
}