-- Program Copyright Shawn Pringle -- Permission to use, and to redistribute is granted. -- This program changes the line breaks of a file. -- It converts UNIX like or DOS like line breaks to line breaks -- as they should be for your OS. -- If you pass a directory to the command line of this -- program it will recurse down that directory to all -- of its children. include std/filesys.e include std/io.e function change_line_breaks( sequence path, sequence file ) integer rfd, wfd sequence file_name object line integer wpos, rpos, thisch, lastch file_name = file[D_NAME] if find( 'd', file[D_ATTRIBUTES] ) then return 0 end if if not move_file( path & SLASH & file_name, path & SLASH & file_name & "~", 1 ) then printf(2, "Couldn\'t move %s to %s~\n", repeat( path & SLASH & file_name, 2 ) ) return -2 end if rfd = open( path & SLASH & file_name & "~", "rb" ) wfd = open( path & SLASH & file_name, "w" ) if rfd = -1 or wfd = -1 then printf(2, "Couldn\'t open %s\'s back up for reading or itself for writing.\n", { path & SLASH & file_name } ) goto "undo" end if line = gets(rfd) if sequence(line) and match( "MZ", line ) = 1 then printf(2,"EXE file format detected (%s)\n", {path & SLASH & file_name}) goto "undo" end if while sequence(line) do if find( 1, line > 127 ) then printf(2,"Non ASCII detected in %s at the line(%s)! I am cowardly quiting!\n", {path & SLASH & file_name, line}) goto "undo" end if if length(line) > 1 and line[$-1] = '\r' then line = line[1..$-2] & '\n' end if puts( wfd, line ) line = gets( rfd ) end while close( rfd ) close( wfd ) if not delete_file( path & SLASH & file_name & "~" ) then end if goto "exit" label "undo" if rfd != -1 then close( rfd ) elsif wfd != -1 then close( wfd ) end if if move_file( path & SLASH & file_name & "~", path & SLASH & file_name, 1 ) = 0 then printf( 2, "Fatal problem: Cannot rename %s~ to %s\n", repeat( path & SLASH & file_name, 2 ) ) return 1 end if label "exit" return 0 end function procedure main(sequence args) sequence fse, dirpath integer er for i = 3 to length(args) do fse = dir( args[i] ) if not find('d',fse[1][D_ATTRIBUTES]) then if find( SLASH, args[i] ) then dirpath = dirname( args[i] ) else dirpath = "." end if if change_line_breaks( dirpath, fse[1] ) then puts(2, "An error occured.\n") exit end if else er = walk_dir( args[i], routine_id("change_line_breaks"), 1 ) if er then printf(2,"An error occurred changing line breaks (%d)\n", { er } ) end if end if end for end procedure main( command_line() )