2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
cat.cpp
|
|
|
|
Concatenate files and print on the standard output.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2011-11-18 13:29:52 -05:00
|
|
|
#include <unistd.h>
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <string.h>
|
2012-03-16 10:56:09 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <errno.h>
|
2011-11-26 05:00:45 -05:00
|
|
|
#include <error.h>
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2012-03-02 12:15:55 -05:00
|
|
|
int docat(const char* inputname, int fd)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const size_t BUFFER_SIZE = 255;
|
|
|
|
char buffer[BUFFER_SIZE+1];
|
|
|
|
ssize_t bytesread = read(fd, buffer, BUFFER_SIZE);
|
|
|
|
if ( bytesread == 0 ) { break; }
|
|
|
|
if ( bytesread < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "read: %s", inputname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ( writeall(1, buffer, bytesread) )
|
|
|
|
{
|
|
|
|
error(0, errno, "write: %s", inputname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} while ( true );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-16 10:56:09 -04:00
|
|
|
void usage(FILE* fp, const char* argv0)
|
2011-11-18 13:29:52 -05:00
|
|
|
{
|
2012-03-16 10:56:09 -04:00
|
|
|
fprintf(fp, "usage: %s [FILE ...]\n", argv0);
|
|
|
|
fprintf(fp, "Concatenate files and print on the standard output.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void help(FILE* fp, const char* argv0)
|
|
|
|
{
|
|
|
|
return usage(fp, argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void version(FILE* fp, const char* argv0)
|
|
|
|
{
|
|
|
|
return usage(fp, argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
const char* argv0 = argv[0];
|
|
|
|
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
|
|
|
if ( arg[0] != '-' ) { continue; }
|
|
|
|
argv[i] = NULL;
|
|
|
|
if ( !strcmp(arg, "--") ) { break; }
|
|
|
|
if ( !strcmp(arg, "--help") ) { help(stdout, argv0); exit(0); }
|
|
|
|
if ( !strcmp(arg, "--usage") ) { usage(stdout, argv0); exit(0); }
|
|
|
|
if ( !strcmp(arg, "--version") ) { version(stdout, argv0); exit(0); }
|
|
|
|
error(0, 0, "unrecognized option: %s", arg);
|
|
|
|
usage(stderr, argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-11-18 13:29:52 -05:00
|
|
|
int result = 0;
|
|
|
|
|
2012-03-16 10:56:09 -04:00
|
|
|
bool any = false;
|
2011-11-18 13:29:52 -05:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
2012-03-16 10:56:09 -04:00
|
|
|
if ( !argv[i] ) { continue; }
|
|
|
|
any = true;
|
2011-11-18 13:29:52 -05:00
|
|
|
int fd = open(argv[i], O_RDONLY);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( fd < 0 )
|
|
|
|
{
|
2011-11-26 05:00:45 -05:00
|
|
|
error(0, errno, "%s", argv[i]);
|
2011-11-22 11:26:47 -05:00
|
|
|
result = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-02 12:15:55 -05:00
|
|
|
|
|
|
|
result |= docat(argv[i], fd);
|
2011-11-18 13:29:52 -05:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2012-03-16 10:56:09 -04:00
|
|
|
if ( !any ) { result = docat("<stdin>", 0); }
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2012-03-16 10:56:09 -04:00
|
|
|
return result;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|
|
|
|
|