2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2013-10-16 12:42:38 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
cat.cpp
|
2013-10-16 12:42:38 -04:00
|
|
|
Concatenate and print files to the standard output.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-10-16 12:42:38 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <errno.h>
|
2011-11-26 05:00:45 -05:00
|
|
|
#include <error.h>
|
2013-10-16 12:42:38 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static bool cat_fd(int fd, const char* path)
|
2012-03-02 12:15:55 -05:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
struct stat st;
|
|
|
|
if ( fstat(fd, &st) == 0 )
|
2012-03-02 12:15:55 -05:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
if ( S_ISDIR(st.st_mode) )
|
|
|
|
return error(0, EISDIR, "`%s'", path), false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t BUFFER_SIZE = 16 * 1024;
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
|
|
|
|
|
|
|
ssize_t buffer_used;
|
|
|
|
while ( 0 < (buffer_used = read(fd, buffer, BUFFER_SIZE)) )
|
|
|
|
{
|
|
|
|
size_t so_far = 0;
|
|
|
|
while ( so_far < (size_t) buffer_used )
|
2012-03-02 12:15:55 -05:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
ssize_t amount = write(1, buffer + so_far, buffer_used - so_far);
|
|
|
|
if ( amount <= 0 )
|
|
|
|
return error(0, errno, "`%s'", "<stdout>"), false;
|
|
|
|
so_far += amount;
|
2012-03-02 12:15:55 -05:00
|
|
|
}
|
2013-10-16 12:42:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( buffer_used < 0 )
|
|
|
|
return error(0, errno, "`%s'", path), false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool cat_path(const char* path)
|
|
|
|
{
|
|
|
|
if ( !strcmp("-", path) )
|
|
|
|
return cat_fd(0, "<stdin>");
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if ( fd < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "`%s'", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool result = cat_fd(fd, path);
|
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool cat_arguments(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if ( argc <= 1 )
|
|
|
|
return cat_path("-");
|
|
|
|
bool success = true;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
if ( !cat_path(argv[i]) )
|
|
|
|
success = false;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compact_arguments(int* argc, char*** argv)
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < *argc; i++ )
|
2015-07-29 19:08:49 -04:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
while ( i < *argc && !(*argv)[i] )
|
2012-03-02 12:15:55 -05:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
for ( int n = i; n < *argc; n++ )
|
|
|
|
(*argv)[n] = (*argv)[n+1];
|
|
|
|
(*argc)--;
|
2012-03-02 12:15:55 -05:00
|
|
|
}
|
2015-07-29 19:08:49 -04:00
|
|
|
}
|
2012-03-02 12:15:55 -05:00
|
|
|
}
|
|
|
|
|
2014-03-18 12:50:59 -04:00
|
|
|
static void help(FILE* fp, const char* argv0)
|
2011-11-18 13:29:52 -05:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
fprintf(fp, "Usage: %s [OPTION]... [FILE]...\n", argv0);
|
|
|
|
fprintf(fp, "Concatenate FILE(s), or standard input, to standard output.\n");
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fprintf(fp, " -u (ignored)\n");
|
|
|
|
fprintf(fp, " --help display this help and exit\n");
|
|
|
|
fprintf(fp, " --version output version information and exit\n");
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fprintf(fp, "With no FILE, or when FILE is -, read standard input.\n");
|
2012-03-16 10:56:09 -04:00
|
|
|
}
|
|
|
|
|
2013-10-16 12:42:38 -04:00
|
|
|
static void version(FILE* fp, const char* argv0)
|
2012-03-16 10:56:09 -04:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
|
|
|
fprintf(fp, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
|
|
|
|
fprintf(fp, "This is free software: you are free to change and redistribute it.\n");
|
|
|
|
fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n");
|
2012-03-16 10:56:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
const char* argv0 = argv[0];
|
2014-02-28 16:51:35 -05:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
2012-03-16 10:56:09 -04:00
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2013-10-16 12:42:38 -04:00
|
|
|
if ( arg[0] != '-' || !arg[1] )
|
|
|
|
continue;
|
2012-03-16 10:56:09 -04:00
|
|
|
argv[i] = NULL;
|
2013-10-16 12:42:38 -04:00
|
|
|
if ( !strcmp(arg, "--") )
|
|
|
|
break;
|
|
|
|
if ( arg[1] != '-' )
|
2011-11-22 11:26:47 -05:00
|
|
|
{
|
2013-10-16 12:42:38 -04:00
|
|
|
while ( char c = *++arg ) switch ( c )
|
|
|
|
{
|
|
|
|
case 'u': /* Ignored, POSIX compatibility. */ break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
2014-03-18 12:50:59 -04:00
|
|
|
help(stderr, argv0);
|
2013-10-16 12:42:38 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( !strcmp(arg, "--help") )
|
|
|
|
help(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--version") )
|
|
|
|
version(stdout, argv0), exit(0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
2014-03-18 12:50:59 -04:00
|
|
|
help(stderr, argv0);
|
2013-10-16 12:42:38 -04:00
|
|
|
exit(1);
|
2011-11-22 11:26:47 -05:00
|
|
|
}
|
2011-11-18 13:29:52 -05:00
|
|
|
}
|
|
|
|
|
2013-10-16 12:42:38 -04:00
|
|
|
compact_arguments(&argc, &argv);
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2013-10-16 12:42:38 -04:00
|
|
|
return cat_arguments(argc, argv) ? 0 : 1;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|