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.

194 lines
4.0 KiB

  1. /*
  2. * Trailing - strip trailing tabs and blanks from input stream
  3. * Assumes no sequence of tabs or spaces is more than MAXWHITE chars long.
  4. * This filter also enforces that '\n' is preceeded by '\r'
  5. *
  6. * Last Modified Mon 12 Oct 1987 by Steve Salisbury
  7. * 1988 Mar 09 Wed (SKS) Reworked buffering, output counting
  8. * check for buffer overflow
  9. *
  10. * cl -Oaltr -G2s trailing.c -o trailing.exr -link slibcr libh /nod:slibce;
  11. * cl trailing -link slibcp libh /nod:slibce, trailing;
  12. */
  13. #define MAXWHITE 4096
  14. #ifndef BIGBUFSIZE
  15. #define BIGBUFSIZE 8192
  16. #endif
  17. #include <stdio.h>
  18. #include <fcntl.h>
  19. #define REG register
  20. char InBuf [ BIGBUFSIZE ] ;
  21. char OutBuf [ BIGBUFSIZE ] ;
  22. char Line [ MAXWHITE ] ;
  23. int main ( int argc , char * * argv )
  24. {
  25. FILE * input ;
  26. FILE * output ;
  27. char * inputname ;
  28. char * outputname ;
  29. REG int ch ;
  30. REG char * whiteptr ;
  31. int ch_save ;
  32. int kbytes = 0 ;
  33. int numbytes = 0 ;
  34. int countflag = 0 ;
  35. char * arg ;
  36. if ( -1 == _setmode ( _fileno(stdin) , O_BINARY ) )
  37. {
  38. fprintf ( stderr , "trailing: internal error (setmode stdin)\n" ) ;
  39. exit ( 1 ) ;
  40. }
  41. if ( -1 == _setmode ( _fileno(stdout) , O_BINARY ) )
  42. {
  43. fprintf ( stderr , "trailing: internal error (setmode stdout)\n" ) ;
  44. exit ( 1 ) ;
  45. }
  46. -- argc ;
  47. ++ argv ;
  48. while ( argc > 0 && * * argv == '-' )
  49. {
  50. arg = * argv ++ ;
  51. -- argc ;
  52. while ( * ++ arg )
  53. switch ( * arg )
  54. {
  55. case 'k' :
  56. countflag = 1 ;
  57. break ;
  58. default :
  59. goto Usage;
  60. }
  61. }
  62. if ( argc > 2 )
  63. {
  64. Usage:
  65. fprintf ( stderr , "Usage: trailing [-k] [input [output]]\n" ) ;
  66. fprintf ( stderr , "`-' for input means use standard input\n" ) ;
  67. exit ( 1 ) ;
  68. }
  69. if ( argc >= 1 && strcmp ( argv [ 0 ] , "-" ) )
  70. {
  71. input = fopen ( inputname = argv [ 0 ] , "rb" ) ;
  72. if ( ! input )
  73. {
  74. fprintf ( stderr , "trailing: cannot open `%s'\n" , argv [ 0 ] ) ;
  75. exit ( 2 ) ;
  76. }
  77. }
  78. else
  79. {
  80. input = stdin ;
  81. inputname = "<standard input>" ;
  82. }
  83. if ( argc == 2 && strcmp ( argv [ 1 ] , "-" ) )
  84. {
  85. output = fopen ( outputname = argv [ 1 ] , "wb" ) ;
  86. if ( ! output )
  87. {
  88. fprintf ( stderr , "trailing: cannot open `%s'\n" , argv [ 1 ] ) ;
  89. exit ( 3 ) ;
  90. }
  91. }
  92. else
  93. {
  94. output = stdout ;
  95. outputname = "<standard output>" ;
  96. }
  97. if ( setvbuf ( input , InBuf , _IOFBF , BIGBUFSIZE ) )
  98. {
  99. fprintf ( stderr , "trailing: internal error (setvbuf input)\n" ) ;
  100. exit ( 1 ) ;
  101. }
  102. if ( setvbuf ( output , OutBuf , _IOFBF , BIGBUFSIZE ) )
  103. {
  104. fprintf ( stderr , "trailing: internal error (setvbuf output)\n" ) ;
  105. exit ( 1 ) ;
  106. }
  107. whiteptr = Line ;
  108. while ( ( ch = getc ( input ) ) != EOF )
  109. {
  110. if ( ch == '\r' )
  111. {
  112. /*
  113. ** '\r' followed by '\n' gets swallowed
  114. */
  115. if ( ( ch = getc ( input ) ) != '\n' )
  116. {
  117. ungetc ( ch , input ) ; /* pushback */
  118. ch = '\r' ;
  119. }
  120. else
  121. ++ numbytes ;
  122. }
  123. if ( ch == ' ' || ch == '\t' )
  124. {
  125. * whiteptr ++ = ch ;
  126. if ( whiteptr > Line + sizeof ( Line ) )
  127. {
  128. fprintf ( stderr , "trailing: too many spaces/tabs (%d)\n" ,
  129. whiteptr - Line ) ;
  130. exit ( 4 ) ;
  131. }
  132. }
  133. else if ( ch == '\n' )
  134. {
  135. putc ( '\r' , output ) ;
  136. putc ( '\n' , output ) ;
  137. whiteptr = Line ;
  138. }
  139. else
  140. {
  141. if ( whiteptr != Line )
  142. {
  143. /*
  144. * Flush the white space buffer
  145. */
  146. ch_save = ch ;
  147. ch = whiteptr - Line ;
  148. whiteptr = Line ;
  149. do
  150. putc ( * whiteptr ++ , output ) ;
  151. while ( -- ch ) ;
  152. whiteptr = Line ;
  153. ch = ch_save ;
  154. }
  155. putc ( ch , output ) ;
  156. }
  157. if ( ++ numbytes >= 4096 )
  158. {
  159. numbytes -= 4096 ;
  160. if ( countflag )
  161. fprintf ( stderr , "%uK\r" , 4 * ++ kbytes ) ;
  162. }
  163. }
  164. if ( fflush ( output ) )
  165. {
  166. fprintf ( stderr , "trailing: cannot flush %s\n" , argv [ 1 ] ) ;
  167. exit ( 4 ) ;
  168. }
  169. fclose ( input ) ;
  170. fclose ( output ) ;
  171. }