From 1d32865e767482eadaf8eb22e918b0ae41dbd9d3 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan <aadhavan@twomorecents.org> Date: Fri, 18 Apr 2025 22:35:48 -0400 Subject: [PATCH] Added extra check if fileContainsNullChar returns err; pass filename to it instead of file --- kg/main.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kg/main.go b/kg/main.go index 22ad26e..c5b8397 100644 --- a/kg/main.go +++ b/kg/main.go @@ -91,6 +91,7 @@ func main() { inputFilenames := flag.Args()[1:] for _, inputFilename := range inputFilenames { inputFile, err := os.Open(inputFilename) + defer inputFile.Close() if err != nil { fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename) } else { @@ -102,8 +103,14 @@ func main() { if fileStat.Mode().IsDir() { fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename) } else { - if fileContainsNullChar(inputFile) { - fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename) + var nullCharPresent bool + if nullCharPresent, err = fileContainsNullChar(inputFilename); nullCharPresent { + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } else { + fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename) + } } else { inputFiles = append(inputFiles, inputFile) }