1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00
serenity/Userland/cat.cpp
Sergey Bugaev c39045b222 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 :^)
2020-06-17 15:02:03 +02:00

114 lines
3.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.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)
{
if (pledge("stdio rpath", nullptr) < 0) {
perror("pledge");
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;
if (!paths.is_empty()) {
for (const char* path : paths) {
int fd;
if (StringView { path } == "-") {
fd = 0;
} else if ((fd = open(path, O_RDONLY)) == -1) {
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
continue;
}
fds.append(fd);
}
} else {
fds.append(0);
}
if (pledge("stdio", nullptr) < 0) {
perror("pledge");
return 1;
}
for (auto& fd : fds) {
for (;;) {
char buf[32768];
ssize_t nread = read(fd, buf, sizeof(buf));
if (nread == 0)
break;
if (nread < 0) {
perror("read");
return 2;
}
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) {
perror("write");
return 3;
}
already_written += nwritten;
}
}
close(fd);
}
return 0;
}