ladybird/Userland/Utilities/shuf.cpp
Ben Wiederhake 6b7ce19161 Everywhere: Remove unused includes of LibC/stdlib.h
These instances were detected by searching for files that include
stdlib.h, but don't match the regex:

\\b(_abort|abort|abs|aligned_alloc|arc4random|arc4random_buf|arc4random_
uniform|atexit|atof|atoi|atol|atoll|bsearch|calloc|clearenv|div|div_t|ex
it|_Exit|EXIT_FAILURE|EXIT_SUCCESS|free|getenv|getprogname|grantpt|labs|
ldiv|ldiv_t|llabs|lldiv|lldiv_t|malloc|malloc_good_size|malloc_size|mble
n|mbstowcs|mbtowc|mkdtemp|mkstemp|mkstemps|mktemp|posix_memalign|posix_o
penpt|ptsname|ptsname_r|putenv|qsort|qsort_r|rand|RAND_MAX|random|reallo
c|realpath|secure_getenv|serenity_dump_malloc_stats|serenity_setenv|sete
nv|setprogname|srand|srandom|strtod|strtof|strtol|strtold|strtoll|strtou
l|strtoull|system|unlockpt|unsetenv|wcstombs|wctomb)\\b

(Without the linebreaks.)

This regex is pessimistic, so there might be more files that don't
actually use anything from the stdlib.

In theory, one might use LibCPP to detect things like this
automatically, but let's do this one step after another.
2023-01-02 20:27:20 -05:00

65 lines
1.9 KiB
C++

/*
* Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
* Copyright (c) 2022, Eli Youngs <eli.m.youngs@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/DeprecatedString.h>
#include <AK/Random.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/Stream.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));
Core::ArgsParser args_parser;
StringView path;
Optional<size_t> head_count;
bool is_zero_terminated = false;
args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::No);
args_parser.add_option(head_count, "Output at most \"count\" lines", "head-count", 'n', "count");
args_parser.add_option(is_zero_terminated, "Split input on \\0, not newline", "zero-terminated", 'z');
args_parser.parse(arguments);
auto file = TRY(Core::Stream::File::open_file_or_standard_stream(path, Core::Stream::OpenMode::Read));
ByteBuffer buffer = TRY(file->read_until_eof());
u8 input_delimiter = is_zero_terminated ? '\0' : '\n';
Vector<Bytes> lines;
auto bytes = buffer.span();
size_t line_start = 0;
size_t line_length = 0;
for (size_t i = 0; i < bytes.size(); ++i) {
if (bytes[i] == input_delimiter) {
lines.append(bytes.slice(line_start, line_length));
line_start = i + 1;
line_length = 0;
} else {
++line_length;
}
}
if (line_length > 0) {
lines.append(bytes.slice(line_start));
}
if (lines.is_empty())
return 0;
AK::shuffle(lines);
Array<u8, 1> output_delimiter = { '\n' };
for (size_t i = 0; i < min(head_count.value_or(lines.size()), lines.size()); ++i) {
TRY(Core::System::write(STDOUT_FILENO, lines.at(i)));
TRY(Core::System::write(STDOUT_FILENO, output_delimiter));
}
return 0;
}