#include "common.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <error.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
static size_t output_io_size;
static void __attribute__ ((noreturn)) die_bad_usage()
{
fprintf(stderr, "Try `%s --help' for more information.\n", program_invocation_name);
exit(EXIT_FAILURE);
}
static void print_help()
{
printf("Usage: %s [OPTION] [FILE]...\n", program_invocation_name);
puts("Concatenate FILE(s), or standard input, to standard output.");
putchar('\n');
puts(" -h, --help display this help and exit");
putchar('\n');
puts("With no FILE, or when FILE is -, read standard input.");
}
static int cat(int fd, const char *filename) {
int err;
struct stat statbuf;
size_t input_io_size;
size_t buf_size;
char *buf;
ssize_t n_read;
ssize_t n_write;
err = 0;
if (fstat(fd, &statbuf) == -1) {
error(0, errno, filename);
err = 1;
goto end;
}
if (S_ISDIR(statbuf.st_mode)) {
error(0, EISDIR, filename);
err = 1;
goto end;
}
input_io_size = statbuf.st_blksize > 0 ? statbuf.st_blksize : 4096;
buf_size = input_io_size > output_io_size ? input_io_size : output_io_size;
if (!(buf = malloc(buf_size)))
error(EXIT_FAILURE, errno, "malloc");
while (1) {
if ((n_read = read(fd, buf, buf_size)) == -1) {
error(0, errno, filename);
err = 1;
goto free_buf;
}
if (n_read == 0)
break;
if ((n_write = write(STDOUT_FILENO, buf, n_read)) == -1)
error(EXIT_FAILURE, errno, "write error");
if (n_write != n_read)
error(EXIT_FAILURE, ENOSPC, "write error");
}
free_buf:
free(buf);
end:
return err ? -1 : 0;
}
int main(int argc, char **argv)
{
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
int c;
struct stat statbuf;
int err;
int fd;
atexit(close_stdout);
while ((c = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (c) {
case 'h':
print_help();
return EXIT_SUCCESS;
case '?':
die_bad_usage();
}
if (fstat(STDOUT_FILENO, &statbuf) == -1)
error(EXIT_FAILURE, errno, "standard output");
output_io_size = statbuf.st_blksize > 0 ? statbuf.st_blksize : 4096;
err = 0;
if (optind == argc) {
if (cat(STDIN_FILENO, "standard input") == -1)
err = 1;
}
else
do
if (!strcmp(argv[optind], "-")) {
if (cat(STDIN_FILENO, "standard input") == -1)
err = 1;
}
else {
if ((fd = open(argv[optind], O_RDONLY)) == -1) {
error(0, errno, argv[optind]);
err = 1;
continue;
}
if (cat(fd, argv[optind]) == -1)
err = 1;
if (close(fd) == -1) {
error(0, errno, argv[optind]);
err = 1;
continue;
}
}
while (++optind < argc);
return err ? EXIT_FAILURE : EXIT_SUCCESS;
}