1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 08:17:34 +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,12 +28,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (!paths.is_empty()) {
for (auto const& path : paths) {
FILE* stream = fopen(String(path).characters(), "r");
if (!stream) {
warnln("Failed to open {}: {}", path, strerror(errno));
continue;
if (path == "-") {
streams.append(stdin);
} else {
FILE* stream = fopen(String(path).characters(), "r");
if (!stream) {
warnln("Failed to open {}: {}", path, strerror(errno));
continue;
}
streams.append(stream);
}
streams.append(stream);
}
} else {
streams.append(stdin);
@ -43,8 +47,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
ScopeGuard guard = [&] {
free(buffer);
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");
}
}
};