Rev 37 | Rev 43 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 37 | Rev 38 | ||
---|---|---|---|
1 | //usr/bin/env go run $0 $@; exit $? |
1 | //usr/bin/env go run $0 $@; exit $? |
2 | package main |
2 | package main |
3 | 3 | ||
4 | import ( |
4 | import ( |
5 | "fmt" |
5 | "fmt" |
6 | "io/ioutil" |
6 | "io/ioutil" |
7 | "log" |
7 | "log" |
8 | "os" |
8 | "os" |
9 | ) |
9 | ) |
10 | 10 | ||
11 | // ============================================================================= |
11 | // ============================================================================= |
12 | // Functions |
12 | // Functions |
13 | // ============================================================================= |
13 | // ============================================================================= |
14 | 14 | ||
15 | func Usage(progname, mesg string) { |
15 | func Usage(progname, mesg string) { |
16 | log.Print ("Usage: " + progname + " directory") |
16 | log.Print("Usage: " + progname + " directory") |
17 | log.Fatal (mesg) |
17 | log.Fatal(mesg) |
18 | } |
18 | } |
19 | 19 | ||
20 | func traverse(s string) { |
20 | func traverse(s string) { |
21 | curdir, err := os.Getwd() |
21 | curdir, err := os.Getwd() |
22 | if err := os.Chdir(s); err != nil { |
22 | if err := os.Chdir(s); err != nil { |
23 | log.Fatal(err) |
23 | log.Fatal(err) |
24 | } |
24 | } |
25 | if err != nil { |
25 | if err != nil { |
26 | log.Fatal(err) |
26 | log.Fatal(err) |
27 | } |
27 | } |
28 | curdir2, err := os.Getwd() |
28 | curdir2, err := os.Getwd() |
29 | files, err := ioutil.ReadDir(curdir2) |
29 | files, err := ioutil.ReadDir(curdir2) |
30 | if err != nil { |
30 | if err != nil { |
31 | log.Fatal(err) |
31 | log.Fatal(err) |
32 | } |
32 | } |
33 | for _, f := range files { |
33 | for _, f := range files { |
34 | fmt.Println (curdir2 + "/" + f.Name()) |
34 | fmt.Println (curdir2 + "/" + f.Name()) |
35 | //fmt.Println(f.Name()) |
35 | //fmt.Println(f.Name()) |
36 | fi, err := os.Stat(f.Name()) |
36 | fi, err := os.Stat(f.Name()) |
37 | if err != nil { |
37 | if err != nil { |
38 | log.Fatal(err) |
38 | log.Fatal(err) |
39 | } |
39 | } |
40 | mode := fi.Mode(); |
40 | mode := fi.Mode(); |
41 | if mode.IsDir() { |
41 | if mode.IsDir() { |
42 | traverse(fi.Name()) |
42 | traverse(fi.Name()) |
43 | } |
43 | } |
44 | } |
44 | } |
45 | os.Chdir(curdir) |
45 | os.Chdir(curdir) |
46 | } |
46 | } |
47 | 47 | ||
48 | func start_traverse(s string) { |
48 | func start_traverse(s string) { |
49 | fmt.Println(s) |
49 | fmt.Println(s) |
50 | traverse(s) |
50 | traverse(s) |
51 | } |
51 | } |
52 | 52 | ||
53 | // ============================================================================= |
53 | // ============================================================================= |
54 | // MAIN |
54 | // MAIN |
55 | // ============================================================================= |
55 | // ============================================================================= |
56 | 56 | ||
57 | func main() { |
57 | func main() { |
58 | progname := os.Args[0] |
58 | progname := os.Args[0] |
59 | if len(os.Args) < 2 { |
59 | if len(os.Args) == 2 { |
60 | Usage(progname, "ERROR: 1 argument expected") |
- | |
61 | } |
- | |
62 | directory := os.Args[1] |
60 | directory := os.Args[1] |
63 | fi, err := os.Stat(directory) |
61 | fi, err := os.Stat(directory) |
64 | if err != nil { |
62 | if err != nil { |
65 | _ = err // ugly hack to allow not using the err variable |
63 | _ = err // ugly hack to allow not using the err variable |
66 | Usage(progname, "ERROR: Not a directory : " + directory) |
64 | Usage(progname, "ERROR: Not a directory : " + directory) |
67 | } |
65 | } |
68 | if fi.IsDir() { |
66 | if fi.IsDir() { |
69 | start_traverse(directory) |
67 | start_traverse(directory) |
70 | } else { |
68 | } else { |
71 | Usage(progname, "ERROR: Not a directory : " + directory) |
69 | Usage(progname, "ERROR: Not a directory : " + directory) |
72 | } |
70 | } |
- | 71 | } else { |
|
- | 72 | Usage(progname, "ERROR: 1 argument expected") |
|
73 | 73 | } |
|
74 | } |
74 | } |