From 97a3a01053a70a09e1d62148efd50b4968803218 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 10 Oct 2022 00:10:34 +0400 Subject: [PATCH] util.go -> proc.go. ioutil.ReadDir -> os.ReadDir. Error handling for dir name convert to int. Added build flags to restrict it to UNIX-like OSs only. --- util.go => proc.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) rename util.go => proc.go (71%) diff --git a/util.go b/proc.go similarity index 71% rename from util.go rename to proc.go index 1831dd9..f1e8ca7 100644 --- a/util.go +++ b/proc.go @@ -1,7 +1,8 @@ +//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd + package main import ( - "io/ioutil" "os" "strconv" "strings" @@ -10,7 +11,7 @@ import ( func GetProcessPIDs(name string) ([]int, error) { var pids []int - dir, err := ioutil.ReadDir("/proc/") + dir, err := os.ReadDir("/proc/") if err != nil { return nil, err } @@ -23,7 +24,10 @@ func GetProcessPIDs(name string) ([]int, error) { } if strings.Contains(string(cmdline), name) { - pid, _ := strconv.Atoi(entry.Name()) + pid, err := strconv.Atoi(entry.Name()) + if err != nil { + continue + } pids = append(pids, pid) } }