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.
This commit is contained in:
Matthew L. Curry 2020-10-15 00:31:33 -06:00 committed by Andreas Kling
parent 0c2327b5b8
commit 212aa85a55

View file

@ -27,6 +27,7 @@
#include <AK/QuickSort.h> #include <AK/QuickSort.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -44,9 +45,15 @@ int main(int argc, char** argv)
Vector<String> lines; Vector<String> lines;
for (;;) { for (;;) {
char buffer[BUFSIZ]; char* buffer = nullptr;
auto* str = fgets(buffer, sizeof(buffer), stdin); ssize_t buflen = 0;
if (!str) errno = 0;
buflen = getline(&buffer, nullptr, stdin);
if (buflen == -1 && errno != 0) {
perror("getline");
exit(1);
}
if (buflen == -1)
break; break;
lines.append(buffer); lines.append(buffer);
} }