/* unix2dos.cpp */



/*

This program reads input by line, and exports it again,
replacing simple LF chars with CR/LF combinations.

Copyright Flowtrack Pty Ltd 2004

This program is placed in the public domain.

*/

#include <stdio.h>


int main ( int argc, char * argv [] ) {

    FILE * infile;
    FILE * outfile;
    char   cVar;
    char   pszOutFileName [80] = "out.txt";


    switch ( argc ) {
    case 3:
        if ( strlen ( argv [2] ) > 63 ) {
            printf ( "\nUnix2Dos : outfilename cannot exceed 63 characters ( DOS limitation )\n" );
            return 2;
        } /* endif */
        strcpy ( pszOutFileName, argv [2] );
        // intentional fall-through

    case 2:
        if ( strlen ( argv [1] ) > 63 ) {
            printf ( "\nUnix2Dos : infilename cannot exceed 63 characters ( DOS limitation )\n" );
            return 3;
        } /* endif */
        if ( argv [1][0] != '/' ) break;
        // intentional fall-through

    default:
        printf ( "\nUnix2Dos was written by Flowtrack to convert UNIX files to DOS" );
        printf ( "\nfiles by translating any LF characters to a CR/LF pair.\n" );
        printf ( "\nIf no 'outfilename' is given, the file is saved as 'out.txt'.\n" );
        printf ( "\nUsage : unix2dos <infilename> [outfilename]\n" );
        return 1;
    } /* endswitch */

    printf ( "\nUnix2Dos : Converting unix file '%s' to dos file '%s'\n",
             argv [1], pszOutFileName );

    infile = fopen ( argv [1], "rb" );
    if ( infile == NULL ) {
        printf ( "\nUnix2Dos : Unable to open input file '%s'\n",
                 argv [1] );
        return 4;
    } /* endif */

    outfile = fopen ( pszOutFileName, "wb" );
    if ( outfile == NULL ) {
        printf ( "\nUnix2Dos : Unable to open output file '%s'\n",
                 pszOutFileName );
        return 5;
    } /* endif */

    while ( !feof ( infile ) ) {
        cVar = fgetc ( infile );
        if ( cVar == 10 ) fputc ( 13, outfile );
        fputc ( cVar, outfile );
    } /* endwhile */
    
    fclose ( infile );
    fclose ( outfile );

    printf ( "\nUnix2Dos : Conversion complete\n" );

    return 0;
}
