/*
 * append.c
 *
 * Copyright (C) 1993 by Olaf 'Rhialto' Seibert. All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char **argv)
{
    char	   *outfile = "ascii.g3";
    FILE	   *ofp;
    FILE	   *ifp;
    extern char    *optarg;
    extern int	    optind;
    extern int	    getopt(int, char **, char *);
    int 	    errflg = 0;
    int 	    c;

    while ((c = getopt(argc, argv, "ao:rv")) != -1) {
	switch (c) {
	    /* Append ignored */
	case 'o':
	    outfile = optarg;
	    break;
	    /* Raw ignored */
	    /* Verbose ignored */
	case '?':
	    errflg++;
	    break;
	}
    }

    if (errflg || optind >= argc) {
	printf(
"Usage: append [-o fax-file] files\n");
	exit(EXIT_FAILURE);
    }

    ofp = fopen(outfile, "ab");
    if (ofp == NULL) {
	printf("Can't open output file %s.\n", outfile);
	exit(EXIT_FAILURE);
    }

    while (optind < argc) {
	char		buf[4096];
	int		size;

	if (ifp = fopen(argv[optind], "rb")) {
	    while ((size = fread(buf, 1, sizeof(buf), ifp)) > 0)
		fwrite(buf, 1, size, ofp);

	    fclose(ifp);
	} else {
	    printf("Can't open input file %s.\n", argv[optind]);
	}
	optind++;
    }

    return 0;
}
