Rev 37 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 37 | Rev 43 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | # ============================================================================= |
2 | # ============================================================================= |
3 | # Create |
3 | # Create |
4 | # name : Andre Rikkert de Koe date: apr 2011 |
4 | # name : Andre Rikkert de Koe date: Apr 2011 |
5 | # descr : filefind |
5 | # descr : filefind |
6 | # This is a demo of programming in the PHP language (CLI). |
6 | # This is a demo of programming in the PHP language (CLI). |
7 | # Its part of a project to implement a program to create a directory |
7 | # Its part of a project to implement a program to create a directory |
8 | # tree similar to the output of the Unix find program. |
8 | # tree similar to the output of the Unix find program. |
9 | # EUID : Any user who has read access to all the files being searched for. |
9 | # EUID : Any user who has read access to all the files being searched for. |
10 | # run : interactive |
10 | # run : interactive |
11 | # |
11 | # |
12 | # Changes |
12 | # Changes |
13 | # name : date: |
13 | # name : date: |
14 | # descr : short description |
14 | # descr : short description |
15 | # |
15 | # |
16 | # ============================================================================= |
16 | # ============================================================================= |
17 | 17 | ||
18 | # ============================================================================= |
18 | # ============================================================================= |
19 | # Functions |
19 | # Functions |
20 | # ============================================================================= |
20 | # ============================================================================= |
21 | 21 | ||
22 | function Usage($progname ,$mesg) |
22 | function Usage($progname ,$mesg) |
23 | { |
23 | { |
24 | echo "Usage: $progname directory\n"; |
24 | echo "Usage: $progname directory\n"; |
25 | echo "$mesg\n"; |
25 | echo "$mesg\n"; |
26 | } |
26 | } |
27 | 27 | ||
28 | function traverse($dir) |
28 | function traverse($dir) |
29 | { |
29 | { |
30 | $curdir = getcwd(); |
30 | $curdir = getcwd(); |
31 | chdir($dir); |
31 | chdir($dir); |
32 | $curdir2 = getcwd(); |
32 | $curdir2 = getcwd(); |
33 | $myDirectory = opendir("."); |
33 | $myDirectory = opendir("."); |
34 | # http://php.net/manual/fr/function.readdir.php |
34 | # http://php.net/manual/fr/function.readdir.php |
35 | while (false !== ($file = readdir($myDirectory))) |
35 | while (false !== ($file = readdir($myDirectory))) |
36 | { |
36 | { |
37 | $pfile = $curdir2 . "/" . $file; |
37 | $pfile = $curdir2 . "/" . $file; |
38 | if (is_dir($file) && !is_link($file)) |
38 | if (is_dir($file) && !is_link($file)) |
39 | { |
39 | { |
40 | if ($file != ".." && $file != ".") |
40 | if ($file != ".." && $file != ".") |
41 | { |
41 | { |
42 | echo "$pfile\n"; |
42 | echo "$pfile\n"; |
43 | traverse($file); |
43 | traverse($file); |
44 | } |
44 | } |
45 | } |
45 | } |
46 | else |
46 | else |
47 | { |
47 | { |
48 | if (is_file($file) || is_link($file)) |
48 | if (is_file($file) || is_link($file)) |
49 | echo "$pfile\n"; |
49 | echo "$pfile\n"; |
50 | else |
50 | else |
51 | echo "WARNING: Could not process file $pfile\n"; |
51 | echo "WARNING: Could not process file $pfile\n"; |
52 | } |
52 | } |
53 | } |
53 | } |
54 | chdir($curdir); |
54 | chdir($curdir); |
55 | 55 | ||
56 | } |
56 | } |
57 | 57 | ||
58 | function start_traverse($directory) |
58 | function start_traverse($directory) |
59 | { |
59 | { |
60 | echo "$directory\n"; |
60 | echo "$directory\n"; |
61 | traverse($directory); |
61 | traverse($directory); |
62 | } |
62 | } |
63 | 63 | ||
64 | # ============================================================================= |
64 | # ============================================================================= |
65 | # MAIN |
65 | # MAIN |
66 | # ============================================================================= |
66 | # ============================================================================= |
67 | 67 | ||
68 | $progname = $argv[0]; |
68 | $progname = $argv[0]; |
69 | 69 | ||
70 | if ( $argc == 2 ) |
70 | if ( $argc == 2 ) |
71 | { |
71 | { |
72 | $directory = $argv[1]; |
72 | $directory = $argv[1]; |
73 | if (is_dir($directory)) |
73 | if (is_dir($directory)) |
74 | start_traverse($directory); |
74 | start_traverse($directory); |
75 | else |
75 | else |
76 | Usage($progname, "ERROR: Not a directory : $directory"); |
76 | Usage($progname, "ERROR: Not a directory : $directory"); |
77 | } |
77 | } |
78 | else |
78 | else |
79 | Usage($progname, "ERROR: 1 argument expected"); |
79 | Usage($progname, "ERROR: 1 argument expected"); |
80 | 80 | ||
81 | ?> |
81 | ?> |
82 | 82 |