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

void un(FILE *, FILE *, int);

int main(int argc, char *argv[]) 
{
        char file1[256];
        char file2[256];
        char temps[256];
        char tempchar;
        int temp;
        int strip=1;     /* 1 to strip ctrl-m chars, 0 to add them */
        int hlp=0;
        int numfiles=0, argcount=0;
        FILE *ifp, *ofp;


        while ((argcount + 1) < argc) {
          ++argcount;
          if (argv[argcount][0] == '-') {
                temp=0;
                tempchar = ' ';
                if (strlen(argv[argcount]) > 1)
                        tempchar = argv[argcount][1];
                if (tempchar == 's' || tempchar == 'S') {temp=1; strip=1;}
                if (tempchar == 'a' || tempchar == 'A') {temp=1; strip=0;}
                if (tempchar == '?') {temp=1; hlp=1;}

                if (temp==0) printf("Could not understand that "
                                    "option: -%c\n", tempchar);
                }
          else {
                switch (numfiles) {
                    case 0: 
                    strcpy(file1, argv[argcount]);
                    ++numfiles;
                    break;

                    case 1:
                    strcpy(file2, argv[argcount]);
                    ++numfiles;
                    break;

                    default:
                    fprintf(stderr, "Error: Too many arguments!\n");
                    exit(1);
                }
               }
        }
                    

        if (numfiles == 0 || hlp) {
            printf("%s","Un13 by Jools Henn, 1998.\n\n"
                        "This is a simple program to remove all the\n"
                        "ctrl-m characters from an MS-DOS text file.\n\n"
                        "Usage: un13 [options] <inputfile> [<outputfile>]\n\n"
                        "Options:\n"
                        "-s  Strip off ctrl-m characters from file (default).\n"
                        "-a  Add ctrl-m characters to file.\n");
            exit(0);
        }

        if (numfiles == 1) {
            strcpy(temps, file1);  /* If only one arg, add .un13 onto   */
            strcpy(file2, strcat(temps, ".un13"));  /* output file.     */
        }


        ifp = fopen(file1, "rb");
        if (ifp == NULL) {
                fprintf(stderr, "Error: Cannot open file %s for input.\n",
                        file1);
                exit(1);
                }

        ofp = fopen(file2, "wb");
        if (ofp == NULL) {
                fprintf(stderr, "Error: Cannot open file %s for output.\n",
                        file2);
                exit(1);
                }

        un(ifp, ofp, strip);

        fclose(ifp);
        fclose(ofp);
        return 0;
}


void un(FILE* ifp, FILE* ofp, int strip)
{       
        int temp;

        while ((temp = getc(ifp)) != EOF) {
                if (strip == 0 && temp == 10) putc(13, ofp);
                if (temp != 13) putc(temp, ofp);
                }
}