gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/cmds/core/lsmod/lsmod_linux.go (about)

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