Rev 37 | 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 | #!/usr/bin/env perl |
1 | #!/usr/bin/env perl |
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 perl language. |
6 | # This is a demo of programming in the perl language. |
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 | # Because of the example, we don't use the standard File::Find module. |
9 | # Because of the example, we don't use the standard File::Find module. |
10 | # EUID : Any user who has read access to all the files being searched for. |
10 | # EUID : Any user who has read access to all the files being searched for. |
11 | # run : interactive |
11 | # run : interactive |
12 | # |
12 | # |
13 | # Changes |
13 | # Changes |
14 | # name : date: |
14 | # name : date: |
15 | # descr : short description |
15 | # descr : short description |
16 | # |
16 | # |
17 | # ============================================================================= |
17 | # ============================================================================= |
18 | 18 | ||
19 | use File::Basename; |
19 | use File::Basename; |
20 | use Cwd; |
20 | use Cwd; |
21 | 21 | ||
22 | # ============================================================================= |
22 | # ============================================================================= |
23 | # Functions |
23 | # Functions |
24 | # ============================================================================= |
24 | # ============================================================================= |
25 | 25 | ||
26 | sub Usage |
26 | sub Usage |
27 | { |
27 | { |
28 | 28 | ||
29 | my ($progname, $mesg) = @_; |
29 | my ($progname, $mesg) = @_; |
30 | 30 | ||
31 | print "Usage: $progname directory\n"; |
31 | print "Usage: $progname directory\n"; |
32 | print "$mesg\n" |
32 | print "$mesg\n" |
33 | } |
33 | } |
34 | 34 | ||
35 | # |
35 | # |
36 | # dir is directory to traverse |
36 | # dir is directory to traverse |
37 | # |
37 | # |
38 | sub traverse |
38 | sub traverse |
39 | { |
39 | { |
40 | my ($dir) = @_; |
40 | my ($dir) = @_; |
41 | 41 | ||
42 | my $curdir = getcwd(); |
42 | my $curdir = getcwd(); |
43 | chdir($dir); |
43 | chdir($dir); |
44 | my $curdir2 = getcwd(); |
44 | my $curdir2 = getcwd(); |
45 | opendir(FH, $curdir2); |
45 | opendir(FH, $curdir2); |
46 | my @files = readdir(FH); |
46 | my @files = readdir(FH); |
47 | foreach $file (@files) |
47 | foreach $file (@files) |
48 | { |
48 | { |
49 | my $pfile = $curdir2 . "/" . $file; |
49 | my $pfile = $curdir2 . "/" . $file; |
50 | if (-d $file && ! -l $file) |
50 | if (-d $file && ! -l $file) |
51 | { |
51 | { |
52 | if ($file ne ".." && $file ne ".") |
52 | if ($file ne ".." && $file ne ".") |
53 | { |
53 | { |
54 | print "$pfile\n"; |
54 | print "$pfile\n"; |
55 | traverse($file) |
55 | traverse($file) |
56 | } |
56 | } |
57 | } |
57 | } |
58 | else |
58 | else |
59 | { |
59 | { |
60 | if (-f $file || -l $file) |
60 | if (-f $file || -l $file) |
61 | { |
61 | { |
62 | print "$pfile\n" |
62 | print "$pfile\n" |
63 | } |
63 | } |
64 | else |
64 | else |
65 | { |
65 | { |
66 | print("WARNING: Could not process file $pfile\n") |
66 | print("WARNING: Could not process file $pfile\n") |
67 | } |
67 | } |
68 | } |
68 | } |
69 | } |
69 | } |
70 | close(FH); |
70 | close(FH); |
71 | chdir($curdir) |
71 | chdir($curdir) |
72 | } |
72 | } |
73 | 73 | ||
74 | sub start_traverse |
74 | sub start_traverse |
75 | { |
75 | { |
76 | my ($directory) = @_; |
76 | my ($directory) = @_; |
77 | 77 | ||
78 | print "$directory\n"; |
78 | print "$directory\n"; |
79 | traverse($directory) |
79 | traverse($directory) |
80 | } |
80 | } |
81 | 81 | ||
82 | # ============================================================================= |
82 | # ============================================================================= |
83 | # MAIN |
83 | # MAIN |
84 | # ============================================================================= |
84 | # ============================================================================= |
85 | 85 | ||
86 | $progname = basename($0); |
86 | $progname = basename($0); |
87 | if ($#ARGV == 0) |
87 | if ($#ARGV == 0) |
88 | { |
88 | { |
89 | $directory = $ARGV[0]; |
89 | $directory = $ARGV[0]; |
90 | if (-d $directory) |
90 | if (-d $directory) |
91 | { |
91 | { |
92 | start_traverse($directory); |
92 | start_traverse($directory); |
93 | } |
93 | } |
94 | else |
94 | else |
95 | { |
95 | { |
96 | Usage ($progname, "ERROR: Not a directory : $directory"); |
96 | Usage ($progname, "ERROR: Not a directory : $directory"); |
97 | } |
97 | } |
98 | } |
98 | } |
99 | else |
99 | else |
100 | { |
100 | { |
101 | Usage ($progname, "ERROR: 1 argument expected"); |
101 | Usage ($progname, "ERROR: 1 argument expected"); |
102 | } |
102 | } |
103 | 103 |