Rev 13 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
12 | ls | 1 | #!/usr/bin/env perl |
2 | # ============================================================================= |
||
3 | # Create |
||
4 | # name : Andre Rikkert de Koe date: apr 2011 |
||
5 | # descr : filefind |
||
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 |
||
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. |
||
10 | # EUID : Any user who has read access to all the files being searched for. |
||
11 | # run : interactive |
||
12 | # |
||
13 | # Changes |
||
14 | # name : date: |
||
15 | # descr : short description |
||
16 | # |
||
17 | # ============================================================================= |
||
18 | |||
19 | use File::Basename; |
||
20 | use Cwd; |
||
21 | |||
22 | # ============================================================================= |
||
23 | # Functions |
||
24 | # ============================================================================= |
||
25 | |||
26 | sub Usage |
||
27 | { |
||
28 | |||
29 | my ($progname, $mesg) = @_; |
||
30 | |||
31 | print "Usage : $progname directory\n"; |
||
32 | print "$mesg\n" |
||
33 | } |
||
34 | |||
35 | # |
||
36 | # dir is directory to traverse |
||
37 | # |
||
38 | sub traverse |
||
39 | { |
||
40 | my ($dir) = @_; |
||
41 | |||
42 | my $curdir = getcwd(); |
||
43 | chdir($dir); |
||
44 | my $curdir2 = getcwd(); |
||
45 | opendir(FH, $curdir2); |
||
46 | my @files = readdir(FH); |
||
47 | foreach $file (@files) |
||
48 | { |
||
49 | my $pfile = $curdir2 . "/" . $file; |
||
50 | if (-d $file && ! -l $file) |
||
51 | { |
||
52 | if ($file ne ".." && $file ne ".") |
||
53 | { |
||
54 | print "$pfile\n"; |
||
55 | traverse($file) |
||
56 | } |
||
57 | } |
||
58 | else |
||
59 | { |
||
60 | if (-f $file || -l $file) |
||
61 | { |
||
62 | print "$pfile\n" |
||
63 | } |
||
64 | else |
||
65 | { |
||
66 | print("WARNING: Could not process file $pfile\n") |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | close(FW); |
||
71 | chdir($curdir) |
||
72 | } |
||
73 | |||
74 | sub start_traverse |
||
75 | { |
||
76 | my ($directory) = @_; |
||
77 | |||
78 | print "$directory\n"; |
||
79 | traverse($directory) |
||
80 | } |
||
81 | |||
82 | # ============================================================================= |
||
83 | # MAIN |
||
84 | # ============================================================================= |
||
85 | |||
86 | $progname = basename($0); |
||
87 | if ($#ARGV == 0) |
||
88 | { |
||
89 | $directory = $ARGV[0]; |
||
90 | if (-d $directory) |
||
91 | { |
||
92 | start_traverse($directory); |
||
93 | } |
||
94 | else |
||
95 | { |
||
96 | Usage ($progname, "ERROR: No directory $directory found"); |
||
97 | } |
||
98 | } |
||
99 | else |
||
100 | { |
||
101 | Usage ($progname, "ERROR: 1 argument expected"); |
||
102 | } |