aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFerass El Hafidi <vitali64pmemail@protonmail.com>2023-03-03 19:46:19 +0100
committerFerass El Hafidi <vitali64pmemail@protonmail.com>2023-03-03 19:46:19 +0100
commit46f6f0e65f5ec7d3f49352c300fe97e5510eda1d (patch)
tree2307da44b2d7e3b9499d26fa68918650d3cb2ef2
parent4679af335b17fd8dafbef4dd4f801bd696c46cb9 (diff)
downloadfases-46f6f0e65f5ec7d3f49352c300fe97e5510eda1d.tar.gz
fases-46f6f0e65f5ec7d3f49352c300fe97e5510eda1d.zip
core/head: Fix a tiny typo (that introduced a bug) and a segmentation fault
Those bugs were basically introduced by typos... First bug --------- `head` was checking if argc was less than 2, which is incorrect because after getopt() is ran, argc would be equal to 1 if ran this way: $ head file As such, it would read standard input, then the file, which is a bug. Second bug ---------- The file got closed after the first line was read. When trying to read the (now closed) file it returns a segmentation fault. Signed-off-by: Ferass El Hafidi <vitali64pmemail@protonmail.com>
-rw-r--r--core/head.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/core/head.c b/core/head.c
index 8dd19e8..76bb9d9 100644
--- a/core/head.c
+++ b/core/head.c
@@ -29,7 +29,7 @@ int main(int argc, char *const argv[]) {
else
lines = 10;
} argc -= optind; argv += optind;
- if (argc < 2) {
+ if (argc < 1) {
while (read(STDIN_FILENO, s, 4096) > 0)
printf("%s", s);
}
@@ -44,8 +44,8 @@ int main(int argc, char *const argv[]) {
printf("%s", s);
else if (errno)
return errprint(argv0, "fgets()", errno);
- fclose(file);
}
+ fclose(file);
}
return 0;
}