Compare commits
2 Commits
d81c72590a
...
1d32865e76
Author | SHA1 | Date | |
---|---|---|---|
1d32865e76 | |||
66f4ca31d1 |
@@ -32,13 +32,21 @@ func genRange[T character](start, end T) []T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns whether or not the given file contains a NULL character
|
// Returns whether or not the given file contains a NULL character
|
||||||
func fileContainsNullChar(file *os.File) bool {
|
func fileContainsNullChar(filename string) (bool, error) {
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
if strings.Contains(line, "\000") {
|
if strings.Contains(line, "\000") {
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
if err := scanner.Err(); err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
}
|
}
|
||||||
|
11
kg/main.go
11
kg/main.go
@@ -91,6 +91,7 @@ func main() {
|
|||||||
inputFilenames := flag.Args()[1:]
|
inputFilenames := flag.Args()[1:]
|
||||||
for _, inputFilename := range inputFilenames {
|
for _, inputFilename := range inputFilenames {
|
||||||
inputFile, err := os.Open(inputFilename)
|
inputFile, err := os.Open(inputFilename)
|
||||||
|
defer inputFile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename)
|
fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename)
|
||||||
} else {
|
} else {
|
||||||
@@ -102,8 +103,14 @@ func main() {
|
|||||||
if fileStat.Mode().IsDir() {
|
if fileStat.Mode().IsDir() {
|
||||||
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
|
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
|
||||||
} else {
|
} else {
|
||||||
if fileContainsNullChar(inputFile) {
|
var nullCharPresent bool
|
||||||
fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename)
|
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 {
|
} else {
|
||||||
inputFiles = append(inputFiles, inputFile)
|
inputFiles = append(inputFiles, inputFile)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user