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