Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.9 KiB

  1. use strict;
  2. use lib $ENV{RAZZLETOOLPATH};
  3. use Logmsg;
  4. # declare locals
  5. my( $ExitCode, $InputFile, $OutputFile );
  6. # set defaults
  7. $ExitCode = 0;
  8. # here we want to read in a file and generate a new one with only unique lines
  9. &ParseCommandLine();
  10. &ParseInputFile();
  11. exit( $ExitCode );
  12. sub ParseCommandLine
  13. {
  14. # declare locals
  15. my( $Argument );
  16. foreach $Argument ( @ARGV ) {
  17. if ( $Argument =~ /[\/\-]{0,1}\?/ ) { &UsageAndQuit(); }
  18. elsif ( $Argument =~ /[\/\-]i\:(.*)$/ ) {
  19. $InputFile = $1;
  20. } elsif ( $Argument =~ /[\/\-]o\:(.*)$/ ) {
  21. $OutputFile = $1;
  22. }
  23. else {
  24. print( "Unrecognized option '$Argument'.\n" );
  25. &UsageAndQuit();
  26. }
  27. }
  28. }
  29. sub ParseInputFile
  30. {
  31. # declare locals
  32. my( $Line, %FileHash );
  33. unless ( open( INFILE, $InputFile ) ) {
  34. print( "Failed to open $InputFile for reading, exiting.\n" );
  35. $ExitCode++;
  36. return;
  37. }
  38. if ( $OutputFile ) {
  39. unless ( open( OUTFILE, ">$OutputFile" ) ) {
  40. print( "Failed to open $OutputFile for writing, " .
  41. "will use stdout.\n" );
  42. undef( $OutputFile );
  43. $ExitCode++;
  44. }
  45. }
  46. while ( $Line = <INFILE> ) {
  47. chomp( $Line );
  48. unless ( $FileHash{ $Line } ) {
  49. if ( $OutputFile ) { print( OUTFILE "$Line\n" ); }
  50. else { print( "$Line\n" ); }
  51. $FileHash{ $Line } = "t";
  52. }
  53. }
  54. if ( $OutputFile ) { close( OUTFILE ); }
  55. close( INFILE );
  56. return;
  57. }
  58. sub UsageAndQuit
  59. {
  60. print( "$0 [-i:<input file>] [-o:<output file>]\n" );
  61. print( "\n-i:<input file> name of file to uniqify\n" );
  62. print( "-o:<output file> name of output file\n" );
  63. print( "\n$0 will parse the given input file and generate output\n" );
  64. print( "which contains only unique lines. if no output file is\n" );
  65. print( "given, the output is written to stdout. if no input file\n" );
  66. print( "is given, input is read from stdin.\n" );
  67. print( "\n" );
  68. exit( 1 );
  69. }