Browse Source

now there are some command flags

Sam Abradi 5 years ago
parent
commit
d45666450e
1 changed files with 61 additions and 7 deletions
  1. 61
    7
      ls.go

+ 61
- 7
ls.go View File

@@ -3,20 +3,74 @@ package main
3 3
 import (
4 4
   "io/ioutil"
5 5
   "fmt"
6
-//  "encoding/json"
7
-//  "os"
8 6
   "flag"
7
+  "log"
8
+  "syscall"
9
+  "strconv"
9 10
 )
10 11
 
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
+}
12 32
 
13 33
 func main() {
14
-  //dec := json.NewDecoder(os.Stdin)
15
-  //enc := json.NewEncoder(os.Stdout)
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
+
16 48
   flag.Parse()
17
-  root := flag.Arg(0)
49
+  root := "."//flag.Arg(0)
18 50
   dir,_ := ioutil.ReadDir(root)
19
-  for _,entry := range dir{
20
-      fmt.Printf("%s\n", entry.Name())
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
+    }
21 74
   }
75
+  fmt.Printf("]\n")
22 76
 }

Loading…
Cancel
Save