forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsmod_linux.go
49 lines (43 loc) · 981 Bytes
/
lsmod_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// List modules currently loaded in the Linux kernel
//
// Synopsis:
// lsmod
//
// Description:
// lsmod is a clone of lsmod(8)
//
// Author:
// Roland Kammerer <dev.rck@gmail.com>
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func main() {
file, err := os.Open("/proc/modules")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Println("Module Size Used by")
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := strings.Split(scanner.Text(), " ")
name, size, used, usedBy := s[0], s[1], s[2], s[3]
final := fmt.Sprintf("%-19s %8s %s", name, size, used)
if usedBy != "-" {
usedBy = usedBy[:len(usedBy)-1]
final += fmt.Sprintf(" %s", usedBy)
}
fmt.Println(final)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}