mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +00:00
Userland: Improve cat(1)
* Port it to Core::ArgsParser - even though it doesn't support any options at the moment, it's nice to handle --help and reject other options; * Accept "-" to mean stdin; * Make sure to write out all data that we read, looping if needed; * Detect Useless Uses of Cat and print a cute warning :^)
This commit is contained in:
parent
6fcce077bb
commit
c39045b222
1 changed files with 41 additions and 9 deletions
|
@ -25,14 +25,31 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
bool detect_useless_use_of_cat(int argc)
|
||||||
|
{
|
||||||
|
// I can think of somewhat valid uses of zero-argument cat
|
||||||
|
// from a tty to a pipe, so let's allow those.
|
||||||
|
if (argc != 2)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
struct stat statbuf;
|
||||||
|
int rc = fstat(1, &statbuf);
|
||||||
|
if (rc < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return S_ISFIFO(statbuf.st_mode);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath", nullptr) < 0) {
|
if (pledge("stdio rpath", nullptr) < 0) {
|
||||||
|
@ -40,12 +57,23 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<const char*> paths;
|
||||||
|
|
||||||
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_positional_argument(paths, "File path", "path", Core::ArgsParser::Required::No);
|
||||||
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (detect_useless_use_of_cat(argc))
|
||||||
|
fprintf(stderr, "\033[34;1mMeow! Useless use of cat detected\033[0m\n");
|
||||||
|
|
||||||
Vector<int> fds;
|
Vector<int> fds;
|
||||||
if (argc > 1) {
|
if (!paths.is_empty()) {
|
||||||
for (int i = 1; i < argc; i++) {
|
for (const char* path : paths) {
|
||||||
int fd;
|
int fd;
|
||||||
if ((fd = open(argv[i], O_RDONLY)) == -1) {
|
if (StringView { path } == "-") {
|
||||||
fprintf(stderr, "Failed to open %s: %s\n", argv[i], strerror(errno));
|
fd = 0;
|
||||||
|
} else if ((fd = open(path, O_RDONLY)) == -1) {
|
||||||
|
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fds.append(fd);
|
fds.append(fd);
|
||||||
|
@ -69,14 +97,18 @@ int main(int argc, char** argv)
|
||||||
perror("read");
|
perror("read");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
ssize_t nwritten = write(1, buf, nread);
|
size_t already_written = 0;
|
||||||
|
while (already_written < (size_t)nread) {
|
||||||
|
ssize_t nwritten = write(1, buf + already_written, nread - already_written);
|
||||||
if (nwritten < 0) {
|
if (nwritten < 0) {
|
||||||
perror("write");
|
perror("write");
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
ASSERT(nwritten == nread);
|
already_written += nwritten;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue