Subversion Repositories svn.Prod repos

Rev

Rev 43 | Blame | Compare with Previous | Last modification | View Log | RSS feed

//usr/bin/env go run $0 $@; exit $?

// ============================================================================
// Create
// name  : Andre Rikkert de Koe          date:   Mar 2018
// descr : filefind
//         This is a demo of programming in the Go language.
//         Its part of a project to implement a program to create a directory
//         tree similar to the output of the Unix find program.
// EUID  : Any user who has read access to all the files being searched for.
// run   : interactive
//
// Changes
// name  :                               date:
// descr : short description
//
// ============================================================================

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")
        }
}