Files
Florian Fischer cad4804a95 cat: improve exist status
Exit with status 1 if one or more arguments could not be printed.
2024-02-13 13:15:36 +01:00

80 lines
2.3 KiB
C

/// @file cat.c
/// @brief `cat` program.
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "io/debug.h"
#include "stddef.h"
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <strerror.h>
#include <sys/stat.h>
static inline void print_content(const char *path, char *buffer, unsigned buflen)
{
pr_warning("Printing content of %s\n", path);
// Open the file.
int fd = open(path, O_RDONLY, 42);
if (fd >= 0) {
ssize_t bytes_read = 0;
// Put on the standard output the characters.
while ((bytes_read = read(fd, buffer, buflen)) > 0) {
write(STDOUT_FILENO, buffer, bytes_read);
}
// Close the file descriptor.
close(fd);
} else {
printf("cat: %s: %s\n\n", path, strerror(errno));
}
}
int main(int argc, char **argv)
{
if (argc < 2) {
printf("cat: missing operand.\n");
printf("Try 'cat --help' for more information.\n\n");
return 1;
}
// Check if `--help` is provided.
for (int i = 1; i < argc; ++i) {
if ((strcmp(argv[i], "--help") == 0) || (strcmp(argv[i], "-h") == 0)) {
printf("Prints the content of the given file.\n");
printf("Usage:\n");
printf(" cat <file>\n\n");
return 0;
}
}
int status = 0;
// Prepare the buffer for reading.
char buffer[BUFSIZ];
// Iterate the arguments.
for (int i = 1; i < argc; ++i) {
stat_t statbuf;
if (stat(argv[i], &statbuf) == -1) {
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
continue;
}
// If it is a regular file, just print the content.
if (S_ISREG(statbuf.st_mode)) {
print_content(argv[i], buffer, BUFSIZ);
} else if (S_ISDIR(statbuf.st_mode)) {
printf("cat: %s: Is a directory\n\n", argv[i]);
status = 1;
} else if (S_ISLNK(statbuf.st_mode)) {
if (readlink(argv[i], buffer, BUFSIZ)) {
print_content(buffer, buffer, BUFSIZ);
} else {
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
status = 1;
}
}
}
putchar('\n');
putchar('\n');
return status;
}