2011-05-23 01:36:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
2015-05-19 15:44:15 +00:00
|
|
|
#include <string.h>
|
2011-05-24 10:05:36 +00:00
|
|
|
#include <unistd.h>
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2011-05-26 17:18:42 +00:00
|
|
|
#include "text.h"
|
2011-05-23 01:36:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2016-03-01 10:04:11 +00:00
|
|
|
static void
|
|
|
|
uconcat(FILE *fp1, const char *s1, FILE *fp2, const char *s2)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
setbuf(fp2, NULL);
|
|
|
|
while ((c = getc(fp1)) != EOF)
|
|
|
|
putc(c, fp2);
|
|
|
|
}
|
|
|
|
|
2013-10-07 15:07:19 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-01 23:36:51 +00:00
|
|
|
eprintf("usage: %s [-u] [file ...]\n", argv0);
|
2013-10-07 15:07:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2014-11-13 14:15:20 +00:00
|
|
|
int ret = 0;
|
2016-03-01 10:04:11 +00:00
|
|
|
void (*cat)(FILE *, const char *, FILE *, const char *) = &concat;
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2012-05-31 18:38:18 +00:00
|
|
|
ARGBEGIN {
|
2014-11-13 14:15:20 +00:00
|
|
|
case 'u':
|
2016-03-01 10:04:11 +00:00
|
|
|
cat = &uconcat;
|
2014-11-13 14:15:20 +00:00
|
|
|
break;
|
2012-05-31 18:38:18 +00:00
|
|
|
default:
|
2013-10-07 15:07:19 +00:00
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2012-05-31 18:38:18 +00:00
|
|
|
|
2015-03-16 09:36:36 +00:00
|
|
|
if (!argc) {
|
2016-03-01 10:04:11 +00:00
|
|
|
cat(stdin, "<stdin>", stdout, "<stdout>");
|
2014-04-22 10:43:01 +00:00
|
|
|
} else {
|
2015-03-02 13:19:26 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
2015-05-19 15:44:15 +00:00
|
|
|
if (!strcmp(*argv, "-")) {
|
2015-05-15 11:28:39 +00:00
|
|
|
*argv = "<stdin>";
|
|
|
|
fp = stdin;
|
2015-03-01 23:36:51 +00:00
|
|
|
} else if (!(fp = fopen(*argv, "r"))) {
|
|
|
|
weprintf("fopen %s:", *argv);
|
2014-11-13 14:15:20 +00:00
|
|
|
ret = 1;
|
2015-05-15 11:28:39 +00:00
|
|
|
continue;
|
2014-04-22 10:43:01 +00:00
|
|
|
}
|
2016-03-01 10:04:11 +00:00
|
|
|
cat(fp, *argv, stdout, "<stdout>");
|
2015-05-15 11:28:39 +00:00
|
|
|
if (fp != stdin && fshut(fp, *argv))
|
|
|
|
ret = 1;
|
2013-11-13 11:39:24 +00:00
|
|
|
}
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
2015-03-01 23:36:51 +00:00
|
|
|
|
2015-05-24 23:33:19 +00:00
|
|
|
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
|
|
|
|
|
|
|
|
return ret;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|