Converting DOS text files

Eric Johnson erc at pai.UUCP
Mon Oct 15 23:05:15 AEST 1990


This is for those of you who have SCO's OpenDesktop with a DOS
under UNIX, or any other DOS under UNIX that has this problem.

The problem is this: when you use a DOS-based copy command to copy a text
file onto your system (from a PC floppy, say), that DOS text file
is full of CR/LFs (instead of the UNIX line feed) and has a trailing
Ctrl-Z. On SCO, there is a program to take care of this, called
dtox. Unfortunately, dtox is a filter. That is, you call it
with something like:

     dtox dosfile > unixfile

This is nice, but I have a big problem. I have 30 to 40 files I
want to un-DOS at a time. I want to be able to type something
like 

    undos *.txt

And have a program go to work stripping all the extra DOS characters
out of the files. In addition, dtox didn't seem to deal with the
trailing Ctrl-Z properly. So, here is my (hacked) solution, undos.c

undos takes all the files on its command line and converts the
format to a UNIX text format (from a DOS text format). It's simple,
dumb, and I'm sure you can come up with a better, more efficient
method. Oh well, it works.

Please note that this is NOT in the public domain. It is copyrighted 
in my name, but I used essentially the very liberal terms of the X Window
copyrights. (More liberal than the GNU public license.) 

You can freely distribute this program so long as you keep my
copyright message intact. There is absolutely no warranty of
any kind with this software--you are on your own.

You should be able to compile undos with a UNIX command like:

   cc -o undos undos.c

I'm posting this in hope it helps save time for others out there.
If it doesn't save you any time, it's not worth your bother.
-Eric

----------------cut here for undos.c---------------------------------------
/*
 * undos.c
 *
 * Copyright 1990 Eric F. Johnson
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of E F Johnson not be used in advertising 
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. I (Eric Johnson) make no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Eric F. Johnson
 *
 * undos.c
 *
 * Program to strip carriage returns and control-Z's
 * from a DOS-based text file. This program acts
 * like the SCO program dtox, but in acts on the
 * file in place, as well as strips the trailing
 * control-Z from the DOS file. By saying this program
 * acts on a text file in place, I mean that it will
 * overwrite the source file.
 *
 * Usage is:
 *      undos file1 file2 file3 ...
 *
 *      Where each file is an ASCII text file in DOS format.
 *
 * 12 October 90
 *
 */


#include  <stdio.h>


main( argc, argv )

int     argc;
char    *argv[];

{       /* main */
        int     i;
        char    *temp_file, *mktemp();

        /*
         * Get a temporary file name
         * to use for storing the       
         * un-DOS-ed file until 
         * we're done.
         */
        temp_file = mktemp( "dosXXXXX" );



        if ( argc < 2 )
                {
                fprintf( stderr, 
                        "Error: Usage is undos dosfile1 dosfile2...\n" );
                }

        for( i = 1; i < argc; i++ )
                {
                printf( "Converting %s to a UNIX text file.\n",
                        argv[i] );

                /*
                 * Remove CR/LFs and Ctrl-Zs
                 */
                undos_file( argv[i], temp_file );


        	/*
        	 * Delete temp_file when done
        	 */
        	unlink( temp_file );
                }


        exit( 0 );
        
}       /* main */


undos_file( source_file, temp_file )

char    source_file[];
char    temp_file[];

{       /* undos_file */
        FILE    *in_file, *outfile;
        int     c;

        in_file = fopen( source_file, "r" );
        outfile = fopen( temp_file, "w" );

        if ( ( in_file == (FILE *) NULL ) || ( outfile == (FILE *) NULL ) )
                {
                fprintf( stderr, "Error in opening files %s or %s\n",
                        source_file, temp_file );
                return( -1 );
                }


        while( !feof( in_file ) )
                {
                c = fgetc( in_file );

                if ( !feof( in_file ) )
                        {
                        if ( ( c == 26 ) ||     /* Ctrl-Z */
                                ( c > '~' ) ) 
                                {
                                c = '\n';
                                }

                        if ( c != '\r' ) 
                                {
                                fputc( c, outfile );
                                }
                        }
                }

        fclose( in_file );
        fclose( outfile );

        /*
         * Now, copy the file back
         */
        in_file = fopen( temp_file, "r" );
        outfile = fopen( source_file, "w" );

        if ( ( in_file == (FILE *) NULL ) || ( outfile == (FILE *) NULL ) )
                {
                fprintf( stderr, "Error in opening files %s or %s\n",
                        source_file, temp_file );
                unlink( temp_file );
                return( -1 );
                }


        while( !feof( in_file ) )
                {
                c = fgetc( in_file );

                if ( !feof( in_file ) )
                        {
                        fputc( c, outfile );
                        }
                }

        fclose( in_file );
        fclose( outfile );
                
        return( 0 );

}       /* undos_file */

/*
 *      end of file
 */

----------------cut here ---------------------------------------

-- 
Eric F. Johnson               phone: +1 612 894 0313    BTI: Industrial
Boulware Technologies, Inc.   fax:   +1 612 894 0316    automation systems
415 W. Travelers Trail        email: erc at pai.mn.org     and services
Burnsville, MN 55337 USA



More information about the Comp.unix.sysv386 mailing list