Browse Source

Merge remote-tracking branch 'origin/ls' into feature/ps

Conflicts:
	README.md
Ian Adam Naval 5 years ago
parent
commit
b78852c82f
1 changed files with 76 additions and 0 deletions
  1. 76
    0
      ls.go

+ 76
- 0
ls.go View File

@@ -0,0 +1,76 @@
1
+package main
2
+
3
+import (
4
+  "io/ioutil"
5
+  "fmt"
6
+  "flag"
7
+  "log"
8
+  "syscall"
9
+  "strconv"
10
+)
11
+
12
+
13
+func get_fileinfo(f string, size bool, mode bool, inode bool) string{
14
+  var stat syscall.Stat_t
15
+  var ret string 
16
+  ret = "{\"name\":\"" + f + "\""
17
+  if err := syscall.Stat(f, &stat); err != nil {
18
+    log.Fatal(err)
19
+  }
20
+  if(size){
21
+    ret = ret + ", \"size\":" + strconv.FormatInt(stat.Size,10)
22
+  }
23
+  if(mode){
24
+    ret = ret + ", \"mode\":" + strconv.Itoa(int(stat.Mode))
25
+  }
26
+  if(inode){
27
+    ret = ret + ", \"inode\":" + strconv.FormatUint(stat.Ino,10)
28
+  }
29
+  ret = ret + "}"
30
+  return ret 
31
+}
32
+
33
+func main() {
34
+  // here be the ls flags
35
+
36
+  var a_flag bool                      // all files, even ones starting with .
37
+  var mode_flag bool                   // flags
38
+  var inode_flag bool		       // inode
39
+  var size_flag	bool		       // size
40
+  var first = true
41
+
42
+  flag.BoolVar(&a_flag, "a", false, "lists all files in directory, even hidden ones")
43
+  flag.BoolVar(&mode_flag, "f", false, "include flags for file")  
44
+  flag.BoolVar(&inode_flag, "i", false, "include flags for file")  
45
+  flag.BoolVar(&size_flag, "s", false, "include flags for file")  
46
+  // end ls flag
47
+
48
+  flag.Parse()
49
+  root := "."//flag.Arg(0)
50
+  dir,_ := ioutil.ReadDir(root)
51
+
52
+  fmt.Printf("[\n")
53
+  
54
+  if(!a_flag){
55
+    for _,entry := range dir{
56
+      if(entry.Name()[0]!='.'){
57
+        if(!first){
58
+          fmt.Printf(",")
59
+        }else{
60
+          first = false
61
+	}
62
+        fmt.Printf("%s\n", get_fileinfo(entry.Name(), size_flag, mode_flag, inode_flag))
63
+      }  
64
+    }
65
+  }else{
66
+    for _,entry := range dir{
67
+        if(!first){
68
+          fmt.Printf(",")
69
+        }else{
70
+          first = false
71
+	}
72
+        fmt.Printf("%s\n", get_fileinfo(entry.Name(), size_flag, mode_flag, inode_flag))
73
+    }
74
+  }
75
+  fmt.Printf("]\n")
76
+}

Loading…
Cancel
Save