flip: Newline conversion between Unix, Macintosh and MS-DOS ASCII files



ASCII text files can contain different forms of newlines, depending on which operating system is being used. Converting between these formats is often necessary if you use several operating systems. The flip program will convert the newlines to any format given in the table below:

      Operating System   Newline Character(s)

      Microsoft Windows / MS-DOS   0Dh 0Ah
      Apple Macintosh OS 9 and earlier   0Dh
      Unix (e.g., Linux), also Apple OS X and higher   0Ah

Unix and Macintosh text files use one character to indicate a new line of text. The character which is used in Unix can be represented as the hexadecimal number 0x0a (decimal number: 10) which you can see on a Unix computer if you run a file through the od command, e.g.: od -h flip.cpp , which displays the bytes in the file as hexadecimal numbers. Older Macs use 0x0d instead of 0x0a. "0x" is a marker in C programming to indicate a hexadecimal number.

On the other hand for MS-DOS based computers, a new line of text is specified by two characters: 0x0d followed by 0x0a which function similar to a typewriter: one character advances the paper one line (linefeed) and the other character moves the current positon to the beginning of the line (carriage return).

If you edit a MS-DOS ASCII file in Unix you will often see the characters ^M at the end of the line. This is the extra character 0x0d that is used to indicate a new line of text in MS-DOS. More recent versions of vi and emacs automatically detect the newline style of the file and will use that style when editing the file, keeping the newlines consistent.

Many Windows 95 programs can recognize Unix text file newlines, so it is usually not too much problem going the other way by using Unix text files in MS-DOS.

If you edit a Mac-style text file in Unix (depending on the editor), you will see the entire file on a single line with ^M characters displayed instead of newlines.

The flip command will convert between each of these types of newline formats. Here is the usage statement for the program:

Usage: flip [-t|-u|-d|-m] filename[s]
   Converts ASCII files between Unix, MS-DOS/Windows, or Macintosh newline formats

   Options:
      -u  =  convert file(s) to Unix newline format (newline)
      -d  =  convert file(s) to MS-DOS/Windows newline format (linefeed + newline)
      -m  =  convert file(s) to Macintosh newline format (linefeed)
      -t  =  display current file type, no file modifications
If you want to see what format a file is in, type flip -t filename and the program will return its analysis of the file. If the file has a mixture of methods for new lines, then the flip program will assign it to be a MS-DOS ASCII file. Analyzing or changing binary files (such as executable programs or sound or picture files) with the flip program does not make sense, since there may be 0x0a and 0x0d bytes in the file which are not intended to be new lines. However, the flip program might be able to reverse the damage if you ftp a binary file accidentally as an ascii file.

The flip program will overwrite the old version of a file with the specified format although the -t will not write anything since that option is used only to determine the type of the file.







craig@ccrma.stanford.edu