/* crlf - Check an ASCII file to see what type newline sequence     */
/* it has.                                                          */
/*                                                                  */
/* This program scans through the input file for carraige return    */
/* (CR, \r, 13d, 0Dh) and linefeed or new line (LF, \n, 10h, 0Ah)   */
/* characters.  When it finds the first set, it output a line       */
/* indicating the where the file is UNIX, DOS or other type of      */
/* ASCII file format.                                               */
/*                                                                  */
/*   Usage:  crlf file_name                                         */
/*                                                                  */
/*      where                                                       */
/*              file_name is the name of the input file to be       */
/*              converted.                                          */
/*                                                                  */
/*  Output: One line description of file format <stdout> as         */
/*          follows:                                                */
/*                                                                  */
/*                      CRLF (\r\n) formatted DOS  file.            */
/*                      LF   (\r)   formatted UNIX file.            */
/*                      LFCR (\n\r) incorrectly formatted file.     */
/*                      CR   (\n)   incorrectly formatted file.     */
/*                      No CRLF characters found.                   */

// David Edwards <dee.engineering@usa.net> Wed Jul 18 14:41:25  2001

// Make with: gcc [-ggdb] -o crlf crlf.c
// Or with any ANSI C compiler...

/********************** REVISION HISTORY ****************************
07-18-01:   Original release
*********************************************************************/

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

int main(int argc,char **argv)
{
    FILE *fin;
    int c, prev, indx;
    char *msg[5] = {
            "DOS  Formatted file (CR-LF):        ",
            "UNIX Formatted file (LF):           ",
            "Incorrectly formatted file (LF-CR): ",
            "Incorrectly formatted file (CR):    ",
            "No CR or LF characters found:       "
         };

/*********************************
 Initialization Portion of Program
**********************************/

// Error handling
    if (argc != 2) {
      fprintf(stderr,"\nCheck ASCII file format."
"      (DEE 07-18-01 Rev 1.0)\n\n"
"   Usage:  crlf filename\n\n"
"   Output: A one line description of filename format as\n"
"           follows:\n\n"
"                     DOS  Formatted file (CR-LF).\n"
"                     UNIX Formatted file (LF).\n"
"                     Incorrectly formatted file (LF-CR).\n"
"                     Incorrectly formatted file (CR).\n"
"                     No CR or LF characters found.\n\n");
      return 1;
    }

    // Open the input file
    if ((fin = fopen(argv[1], "rb")) == NULL) {
        fprintf(stderr, "\nCannot open input file: %s\n",argv[1]);
        return 1;
    }


/*********************************************
 Check the file and write the result to stdout
**********************************************/

    prev = 500;     // Initialize to a non character integer
    indx = 4;       // Default file type is "No CRLF char's

    while ((c=getc(fin)) != EOF) {

        // This handles the case when the last char in the file
        // is the only end of line char in it. It also covers the
        // case of only one end of line character formats.
        if ( c == '\n' ) {
            indx = 1;
        } else if ( c == '\r' ) {
            indx = 3;
        }


        if ( prev == '\r' || prev == '\n') {

            // Handle case of pair of end of line chars found
            if ( c == '\r' || c == '\n') {
                switch (prev*10 + c) {
                    case 140: indx = 0; break;
                    case 110: indx = 1; break;
                    case 113: indx = 2; break;
                    case 143: indx = 3; break;
                }
            }
            break;
        }
        prev = c;
    }

    fclose(fin);

    printf("%s%s\n",msg[indx], argv[1]);

    return 0;
}