diff --git a/kg/helpers.go b/kg/helpers.go index 0031ea8..043c01c 100644 --- a/kg/helpers.go +++ b/kg/helpers.go @@ -1,6 +1,11 @@ package main -import "slices" +import ( + "bufio" + "os" + "slices" + "strings" +) type character interface { int | rune @@ -25,3 +30,15 @@ func genRange[T character](start, end T) []T { } return toRet } + +// Returns whether or not the given file contains a NULL character +func fileContainsNullChar(file *os.File) bool { + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, "\000") { + return true + } + } + return false +} diff --git a/kg/main.go b/kg/main.go index 3eceeae..22ad26e 100644 --- a/kg/main.go +++ b/kg/main.go @@ -102,12 +102,19 @@ func main() { if fileStat.Mode().IsDir() { fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename) } else { - inputFiles = append(inputFiles, inputFile) + if fileContainsNullChar(inputFile) { + fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename) + } else { + inputFiles = append(inputFiles, inputFile) + } } } } } } + if len(inputFiles) == 0 { // No valid files given + os.Exit(2) + } var test_str string var err error