Subversion Repositories svn.Prod repos

Rev

Rev 37 | Rev 53 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37 Rev 43
1
/* ============================================================================
1
/* ============================================================================
2
# Create
2
# Create
3
# name  : Andre Rikkert de Koe          date:   apr 2011
3
# name  : Andre Rikkert de Koe          date:   Apr 2011
4
# descr : filefind
4
# descr : filefind
5
#         This is a demo of programming in the C language.
5
#         This is a demo of programming in the C language.
6
#         Its part of a project to implement a program to create a directory
6
#         Its part of a project to implement a program to create a directory
7
#         tree similar to the output of the Unix find program.
7
#         tree similar to the output of the Unix find program.
8
# EUID  : Any user who has read access to all the files being searched for.
8
# EUID  : Any user who has read access to all the files being searched for.
9
# run   : interactive
9
# run   : interactive
10
#
10
#
11
# Changes
11
# Changes
12
# name  :                               date:
12
# name  :                               date:
13
# descr : short description
13
# descr : short description
14
#
14
#
15
============================================================================ */
15
============================================================================ */
16
 
16
 
17
#include <string.h>
17
#include <string.h>
18
 
18
 
19
#include <stdio.h>
19
#include <stdio.h>
20
#include <libgen.h>
20
#include <libgen.h>
21
#include "checkfiles.h"
21
#include "checkfiles.h"
22
#include <unistd.h> // getcwd
22
#include <unistd.h> // getcwd
23
 
23
 
24
 
24
 
25
#include <sys/types.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>
26
#include <sys/stat.h>
27
#include <dirent.h>
27
#include <dirent.h>
28
#include <unistd.h>
28
#include <unistd.h>
29
 
29
 
30
#include <errno.h>
30
#include <errno.h>
31
 
31
 
32
#define PATH_MAX 255
32
#define PATH_MAX 255
33
 
33
 
34
// ============================================================================
34
// ============================================================================
35
// Functions
35
// Functions
36
// ============================================================================
36
// ============================================================================
37
 
37
 
38
void Usage(char *progname, char *mesg)
38
void Usage(char *progname, char *mesg)
39
{
39
{
40
        printf("Usage: %s directory\n" ,progname);
40
        printf("Usage: %s directory\n" ,progname);
41
        printf("%s\n", mesg);
41
        printf("%s\n", mesg);
42
}
42
}
43
 
43
 
44
// http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html
44
// http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html
45
 
45
 
46
void traverse(char *directory)
46
void traverse(char *directory)
47
{
47
{
48
        char curdir[PATH_MAX];
48
        char curdir[PATH_MAX];
49
        char curdir2[PATH_MAX];
49
        char curdir2[PATH_MAX];
50
 
50
 
51
        getcwd(curdir, PATH_MAX);
51
        getcwd(curdir, PATH_MAX);
52
        chdir(directory);
52
        chdir(directory);
53
        getcwd(curdir2, PATH_MAX);
53
        getcwd(curdir2, PATH_MAX);
54
 
54
 
55
        DIR *dir = NULL;
55
        DIR *dir = NULL;
56
        struct dirent entry;
56
        struct dirent entry;
57
        struct dirent *entryPtr = NULL;
57
        struct dirent *entryPtr = NULL;
58
        int retval = 0;
58
        int retval = 0;
59
        char pathName[PATH_MAX + 1];
59
        char pathName[PATH_MAX + 1];
60
        char pfile[PATH_MAX + 1];
60
        char pfile[PATH_MAX + 1];
61
 
61
 
62
        /* Open the given directory, if you can. */  
62
        /* Open the given directory, if you can. */  
63
        dir = opendir(curdir2);
63
        dir = opendir(curdir2);
64
        if (dir == NULL)
64
        if (dir == NULL)
65
        {
65
        {
66
                printf( "Error opening %s: %s", directory, strerror( errno ) );
66
                printf( "Error opening %s: %s", directory, strerror( errno ) );
67
        }
67
        }
68
        retval = readdir_r( dir, &entry, &entryPtr );
68
        retval = readdir_r( dir, &entry, &entryPtr );
69
        while( entryPtr != NULL )
69
        while( entryPtr != NULL )
70
        {
70
        {
71
                struct stat entryInfo;
71
                struct stat entryInfo;
72
       
72
       
73
                if( ( ! strncmp( entry.d_name, ".",  PATH_MAX ) == 0 ) &&
73
                if( ( ! strncmp( entry.d_name, ".",  PATH_MAX ) == 0 ) &&
74
                    ( ! strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) )
74
                    ( ! strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) )
75
                {
75
                {
76
                        sprintf(pfile,"%s/%s", curdir2, entry.d_name);
76
                        sprintf(pfile,"%s/%s", curdir2, entry.d_name);
77
                        if (isdir(entry.d_name) && ! issymlink(entry.d_name))
77
                        if (isdir(entry.d_name) && ! issymlink(entry.d_name))
78
                        {
78
                        {
79
                                printf("%s\n", pfile);
79
                                printf("%s\n", pfile);
80
                                traverse(entry.d_name);
80
                                traverse(entry.d_name);
81
                        }
81
                        }
82
                        else
82
                        else
83
                        {
83
                        {
84
                                if ((isregular(entry.d_name)) || issymlink(entry.d_name))
84
                                if ((isregular(entry.d_name)) || issymlink(entry.d_name))
85
                                        printf("%s\n", pfile);
85
                                        printf("%s\n", pfile);
86
                                else
86
                                else
87
                                        printf("WARRNING: Could not process file %s\n", entry.d_name);
87
                                        printf("WARRNING: Could not process file %s\n", entry.d_name);
88
                        }      
88
                        }      
89
                }
89
                }
90
                retval = readdir_r( dir, &entry, &entryPtr );
90
                retval = readdir_r( dir, &entry, &entryPtr );
91
        }
91
        }
92
        chdir(curdir);
92
        chdir(curdir);
93
}
93
}
94
 
94
 
95
void start_traverse(char *directory)
95
void start_traverse(char *directory)
96
{
96
{
97
        printf("%s\n", directory);
97
        printf("%s\n", directory);
98
        traverse(directory);
98
        traverse(directory);
99
}
99
}
100
 
100
 
101
// ============================================================================
101
// ============================================================================
102
// MAIN
102
// MAIN
103
// ============================================================================
103
// ============================================================================
104
 
104
 
105
int main(int argc, char *argv[])
105
int main(int argc, char *argv[])
106
{
106
{
107
        char *progname = argv[0];
107
        char *progname = argv[0];
108
        char *directory;
108
        char *directory;
109
        char mesg[80];
109
        char mesg[80];
110
 
110
 
111
        progname = basename(progname);
111
        progname = basename(progname);
112
        if (argc == 2)
112
        if (argc == 2)
113
        {
113
        {
114
                directory = argv[1];
114
                directory = argv[1];
115
                if (isdir(directory))
115
                if (isdir(directory))
116
                {
116
                {
117
                        start_traverse(directory);
117
                        start_traverse(directory);
118
                }
118
                }
119
                else
119
                else
120
                {
120
                {
121
                        sprintf(mesg, "ERROR: Not a directory : %s", directory);
121
                        sprintf(mesg, "ERROR: Not a directory : %s", directory);
122
                        Usage(progname, mesg);
122
                        Usage(progname, mesg);
123
                }
123
                }
124
        }
124
        }
125
        else
125
        else
126
        {
126
        {
127
                Usage(progname, "ERROR: 1 argument expected");
127
                Usage(progname, "ERROR: 1 argument expected");
128
        }
128
        }
129
}
129
}
130
 
130