1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

rev: Read from stdin if the filename '-' is given

The implementation of `rev` found on Linux systems does not have this
behavior, however other utilities do offer this behavior and so there
really isn't too much of an argument to be made for *not* having this as
a feature.
This commit is contained in:
Thomas Voss 2022-09-27 20:46:03 +02:00 committed by Sam Atkins
parent 58d50d2657
commit b556bfe8eb

View file

@ -28,6 +28,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (!paths.is_empty()) { if (!paths.is_empty()) {
for (auto const& path : paths) { for (auto const& path : paths) {
if (path == "-") {
streams.append(stdin);
} else {
FILE* stream = fopen(String(path).characters(), "r"); FILE* stream = fopen(String(path).characters(), "r");
if (!stream) { if (!stream) {
warnln("Failed to open {}: {}", path, strerror(errno)); warnln("Failed to open {}: {}", path, strerror(errno));
@ -35,6 +38,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
} }
streams.append(stream); streams.append(stream);
} }
}
} else { } else {
streams.append(stdin); streams.append(stdin);
} }
@ -43,9 +47,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
ScopeGuard guard = [&] { ScopeGuard guard = [&] {
free(buffer); free(buffer);
for (auto* stream : streams) { for (auto* stream : streams) {
if (fclose(stream)) // If the user passes '-' as an argument multiple times, then we will end up trying to
// close stdin multiple times. This will cause `fclose()` to fail but with no error, so
// we need to manually check errno.
if (fclose(stream) && errno != 0) {
perror("fclose"); perror("fclose");
} }
}
}; };
TRY(Core::System::pledge("stdio"sv)); TRY(Core::System::pledge("stdio"sv));