Subversion Repositories svn.Prod repos

Rev

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

Rev 2 Rev 23
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>
-
 
18
 
17
#include <stdio.h>
19
#include <stdio.h>
18
#include <libgen.h>
20
#include <libgen.h>
19
#include "checkfiles.h"
21
#include "checkfiles.h"
20
#include <unistd.h> // getcwd
22
#include <unistd.h> // getcwd
21
 
23
 
22
 
24
 
23
#include <sys/types.h>
25
#include <sys/types.h>
24
#include <sys/stat.h>
26
#include <sys/stat.h>
25
#include <dirent.h>
27
#include <dirent.h>
26
#include <unistd.h>
28
#include <unistd.h>
27
 
29
 
28
#include <errno.h>
30
#include <errno.h>
29
 
31
 
30
#define PATH_MAX 255
32
//define PATH_MAX 255
31
 
33
 
32
// ============================================================================
34
// ============================================================================
33
// Functions
35
// Functions
34
// ============================================================================
36
// ============================================================================
35
 
37
 
36
void Usage(char *progname, char *mesg)
38
void Usage(char *progname, char *mesg)
37
{
39
{
38
        printf("Usage: %s directory\n" ,progname);
40
        printf("Usage: %s directory\n" ,progname);
39
        printf("%s\n", mesg);
41
        printf("%s\n", mesg);
40
}
42
}
41
 
43
 
42
// http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html
44
// http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html
43
 
45
 
44
void traverse(char *directory)
46
void traverse(char *directory)
45
{
47
{
46
        char curdir[PATH_MAX];
48
        char curdir[PATH_MAX];
47
        char curdir2[PATH_MAX];
49
        char curdir2[PATH_MAX];
48
 
50
 
49
        getcwd(curdir, PATH_MAX);
51
        getcwd(curdir, PATH_MAX);
50
        chdir(directory);
52
        chdir(directory);
51
        getcwd(curdir2, PATH_MAX);
53
        getcwd(curdir2, PATH_MAX);
52
 
54
 
53
        DIR *dir = NULL;
55
        DIR *dir = NULL;
54
        struct dirent entry;
56
        struct dirent entry;
55
        struct dirent *entryPtr = NULL;
57
        struct dirent *entryPtr = NULL;
56
        int retval = 0;
58
        int retval = 0;
57
        char pathName[PATH_MAX + 1];
59
        char pathName[PATH_MAX + 1];
58
        char pfile[PATH_MAX + 1];
60
        char pfile[PATH_MAX + 1];
59
 
61
 
60
        /* Open the given directory, if you can. */  
62
        /* Open the given directory, if you can. */  
61
        dir = opendir(curdir2);
63
        dir = opendir(curdir2);
62
        if (dir == NULL)
64
        if (dir == NULL)
63
        {
65
        {
64
                printf( "Error opening %s: %s", directory, strerror( errno ) );
66
                printf( "Error opening %s: %s", directory, strerror( errno ) );
65
        }
67
        }
66
        retval = readdir_r( dir, &entry, &entryPtr );
68
        retval = readdir_r( dir, &entry, &entryPtr );
67
        while( entryPtr != NULL )
69
        while( entryPtr != NULL )
68
        {
70
        {
69
                struct stat entryInfo;
71
                struct stat entryInfo;
70
       
72
       
71
                if( ( ! strncmp( entry.d_name, ".",  PATH_MAX ) == 0 ) &&
73
                if( ( ! strncmp( entry.d_name, ".",  PATH_MAX ) == 0 ) &&
72
                    ( ! strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) )
74
                    ( ! strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) )
73
                {
75
                {
74
                        sprintf(pfile,"%s/%s", curdir2, entry.d_name);
76
                        sprintf(pfile,"%s/%s", curdir2, entry.d_name);
75
                        if (isdir(entry.d_name) && ! issymlink(entry.d_name))
77
                        if (isdir(entry.d_name) && ! issymlink(entry.d_name))
76
                        {
78
                        {
77
                                printf("%s\n", pfile);
79
                                printf("%s\n", pfile);
78
                                traverse(entry.d_name);
80
                                traverse(entry.d_name);
79
                        }
81
                        }
80
                        else
82
                        else
81
                        {
83
                        {
82
                                if ((isregular(entry.d_name)) || issymlink(entry.d_name))
84
                                if ((isregular(entry.d_name)) || issymlink(entry.d_name))
83
                                        printf("%s\n", pfile);
85
                                        printf("%s\n", pfile);
84
                                else
86
                                else
85
                                        printf("WARRNING: Could not process file %s\n", entry.d_name);
87
                                        printf("WARRNING: Could not process file %s\n", entry.d_name);
86
                        }      
88
                        }      
87
                }
89
                }
88
                retval = readdir_r( dir, &entry, &entryPtr );
90
                retval = readdir_r( dir, &entry, &entryPtr );
89
        }
91
        }
90
        chdir(curdir);
92
        chdir(curdir);
91
}
93
}
92
 
94
 
93
void start_traverse(char *directory)
95
void start_traverse(char *directory)
94
{
96
{
95
        printf("%s\n", directory);
97
        printf("%s\n", directory);
96
        traverse(directory);
98
        traverse(directory);
97
}
99
}
98
 
100
 
99
// ============================================================================
101
// ============================================================================
100
// MAIN
102
// MAIN
101
// ============================================================================
103
// ============================================================================
102
 
104
 
103
main(int argc, char *argv[])
105
int main(int argc, char *argv[])
104
{
106
{
105
        char *progname = argv[0];
107
        char *progname = argv[0];
106
        char *directory;
108
        char *directory;
107
        char mesg[80];
109
        char mesg[80];
108
 
110
 
109
        progname = basename(progname);
111
        progname = basename(progname);
110
        if (argc == 2)
112
        if (argc == 2)
111
        {
113
        {
112
                directory = argv[1];
114
                directory = argv[1];
113
                if (isdir(directory))
115
                if (isdir(directory))
114
                {
116
                {
115
                        start_traverse(directory);
117
                        start_traverse(directory);
116
                }
118
                }
117
                else
119
                else
118
                {
120
                {
119
                        sprintf(mesg, "ERROR: No directory %s found", directory);
121
                        sprintf(mesg, "ERROR: No directory %s found", directory);
120
                        Usage(progname, mesg);
122
                        Usage(progname, mesg);
121
                }
123
                }
122
        }
124
        }
123
        else
125
        else
124
        {
126
        {
125
                Usage(progname, "ERROR : 1 argument expected");
127
                Usage(progname, "ERROR : 1 argument expected");
126
        }
128
        }
127
}
129
}
128
 
130