Rev 37 | Rev 43 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
//usr/bin/env go run $0 $@; exit $?
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
// =============================================================================
// Functions
// =============================================================================
func Usage(progname, mesg string) {
log.Print("Usage: " + progname + " directory")
log.Fatal(mesg)
}
func traverse(s string) {
curdir, err := os.Getwd()
if err := os.Chdir(s); err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
curdir2, err := os.Getwd()
files, err := ioutil.ReadDir(curdir2)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
fmt.Println (curdir2 + "/" + f.Name())
//fmt.Println(f.Name())
fi, err := os.Stat(f.Name())
if err != nil {
log.Fatal(err)
}
mode := fi.Mode();
if mode.IsDir() {
traverse(fi.Name())
}
}
os.Chdir(curdir)
}
func start_traverse(s string) {
fmt.Println(s)
traverse(s)
}
// =============================================================================
// MAIN
// =============================================================================
func main() {
progname := os.Args[0]
if len(os.Args) == 2 {
directory := os.Args[1]
fi, err := os.Stat(directory)
if err != nil {
_ = err // ugly hack to allow not using the err variable
Usage(progname, "ERROR: Not a directory : " + directory)
}
if fi.IsDir() {
start_traverse(directory)
} else {
Usage(progname, "ERROR: Not a directory : " + directory)
}
} else {
Usage(progname, "ERROR: 1 argument expected")
}
}