/* xchar.c
 * This program was written by Vic Parkerson using the Aztec Manx
 * C compiler V3.40a on the Amega .  It is in the public domain.
 * It is an example of a program to reads one ASCII file and writes
 * another file.   Only characters with ASCII values above 126, and
 * less than 32, except for linefeed, are stored in the new file.
 * This program is intended to be run from the CLI.
 *                                     Vic Parkerson
 *                                     2607 Hillrise Drive
 *                                     Las Cruces, N.M. 88001
 * 12-24-87
 * Must compile using +L, link with -lc32 . */

#include <stdio.h>
#include <fcntl.h>

#define CR 13
#define LF 10

main( argc, argv)
int argc;
char **argv;
{
        int end;           /* fread returns zero when end of file */
        int err, err1;     /* fclose return zero if ok */
        int letter;        /* reply to prompt */
        FILE *fp1, *fp2;   /* file pointers, zero if fopen fails */
        char ch;           /* the input, checked and output character */
        char i;            /* extra variable to store charrage return */

        printf("Purpose:\n");
        printf("\n   An input file is read one character at a time,");
        printf("\n   all characters greater than ASCII value 31 and\n");
        printf("   less than 127, are stored in a new file.\n");
        printf("   Linefeeds are also passed to the new file.\n\n");
        printf("   One useage would be to filter out binary trash which\n");
        printf("   might be caused by noise on a phone line while using\n");
        printf("   a modem.\n\n");
        printf("   This has been used to fix a file, so that it can\n");
        printf("   then be loaded into Ed.\n\n");

/* Must have arguments to run */

        if (argc != 3) {
           printf("\nTo run program type: xchar infilename outfilename\n");
           exit();
           }

/* Open input file */

        fp1 = fopen(argv[1], "r");
        if (fp1 == 0) {
            printf("\nUnable to open file %s.",argv[1]);
            printf("  File or device not found.\n");
            exit();
            }
        printf("File %s open for read.\n",argv[1]);









/* open output file */

        fp2 = fopen(argv[2], "x");
        if (fp2 == 0) {
            printf("\nUnable to open file %s.",argv[2]);
            printf("  File may already exist.\n");
            printf("Write over existing file(Y/N)? ");
            letter = getchar();
            i = getchar();        /* clear the Charrage Return */
            if (letter == 89)
                letter = 'y';     /* convert to lower case y */
            if (letter == 'y') {  /* if not a "y" then quit */
                putc('Y');
                fp2 = fopen(argv[2], "w");
                if (fp2 == 0) {
                    printf("\nStill unable to open file!\n");
                    exit();
                    }
                }
            else
                exit();
            }
        printf("\nFile %s open for write.\n",argv[2]);

/* transfer characters with filtering */

        end = 1;
        printf("\n\nWorking...\n");
        while (end > 0)  {
            end = fread(&ch, 1, 1, fp1); /* (buffer,size,count,stream) */
            if (end > 0) {
                if ((ch < 127) && (ch > 31))
                    fwrite(&ch, 1, 1, fp2);
                if (ch == 10) /* Linefeed */
                    fwrite(&ch, 1, 1, fp2);
                }
            }

/* close files */

        err = fclose(fp2);
        if (err != 0) 
            printf("\nerror on write file close...");
        err1 = fclose(fp1);
        if (err1 != 0)
            printf("\nerror on read file close...");
        if (err + err1 == 0)
            printf("\nTermination normal.\n");
}

