summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com>2022-09-27 20:46:03 +0200
committerSam Atkins <atkinssj@gmail.com>2022-10-06 16:06:50 +0100
commitb556bfe8eb4df813e0758b3d39c0ac77dc1ac86e (patch)
treefdc1b17327c612575cb9795fba042075d9eb3351 /Userland/Utilities
parent58d50d2657fb3ecaad55a150bcdceb43f9166676 (diff)
downloadserenity-b556bfe8eb4df813e0758b3d39c0ac77dc1ac86e.zip
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.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/rev.cpp20
1 files changed, 14 insertions, 6 deletions
diff --git a/Userland/Utilities/rev.cpp b/Userland/Utilities/rev.cpp
index a653177e87..258c2926b0 100644
--- a/Userland/Utilities/rev.cpp
+++ b/Userland/Utilities/rev.cpp
@@ -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");
+ }
}
};