From 530564b920352c8b09e8570cf53afb0bc9b0cb84 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 16 Mar 2025 19:48:49 -0400 Subject: [PATCH] Print error if input file is a directory; print program name before error string --- cmd/main.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 9e1c9c0..ef360ee 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -73,11 +73,11 @@ func main() { // 3. Run the string against the NFA if len(flag.Args()) < 1 { // flag.Args() also strips out program name - fmt.Println("ERROR: Missing cmdline args") + fmt.Printf("%s: ERROR: Missing cmdline args\n", os.Args[0]) os.Exit(22) } if *recursiveFlag && len(flag.Args()) < 2 { // File/Directory must be provided with '-r' - fmt.Println("ERROR: Missing cmdline args") + fmt.Printf("%s: ERROR: Missing cmdline args\n", os.Args[0]) os.Exit(22) } var re string @@ -92,7 +92,16 @@ func main() { for _, inputFilename := range inputFilenames { inputFile, err := os.Open(inputFilename) if err != nil { - fmt.Printf("%s: No such file or directory\n", inputFilename) + fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename) + os.Exit(2) + } + fileStat, err := inputFile.Stat() + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(2) + } + if fileStat.Mode().IsDir() { + fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename) os.Exit(2) } inputFiles = append(inputFiles, inputFile)