From 212aa85a555777c682b31d51af4dfb89404a09e2 Mon Sep 17 00:00:00 2001 From: "Matthew L. Curry" Date: Thu, 15 Oct 2020 00:31:33 -0600 Subject: [PATCH] Userland/sort: Convert sort to use getline Sort uses a statically sized buffer. This commit changes that to use getline, so lines of any size will be handled properly. --- Userland/sort.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Userland/sort.cpp b/Userland/sort.cpp index a8fa559fbe6..5eb520b1eb7 100644 --- a/Userland/sort.cpp +++ b/Userland/sort.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -44,9 +45,15 @@ int main(int argc, char** argv) Vector lines; for (;;) { - char buffer[BUFSIZ]; - auto* str = fgets(buffer, sizeof(buffer), stdin); - if (!str) + char* buffer = nullptr; + ssize_t buflen = 0; + errno = 0; + buflen = getline(&buffer, nullptr, stdin); + if (buflen == -1 && errno != 0) { + perror("getline"); + exit(1); + } + if (buflen == -1) break; lines.append(buffer); }