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.

227 lines
4.6 KiB

  1. /*
  2. ** TOLWRUPR - translate [a-z] to [A-Z] or vice versa or both (!)
  3. **
  4. ** 1994-08-19 Fri - original version, based on DTOX, which translated
  5. ** If the output is the console, case line buffering is used.
  6. ** CR+LF newlines (MS-DOS) to LF-only newlines (XENIX).
  7. **
  8. ** This program (tolwrupr) is equivalent to:
  9. **
  10. ** tolwrupr -L: tr "[a-z]" "[A-Z]"
  11. ** tolwrupr -U: tr "[A-Z]" "[a-z]"
  12. ** tolwrupr -X: tr "[A-Z][a-z]" "[a-z][A-Z]"
  13. */
  14. #include <fcntl.h>
  15. #include <io.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #define CR '\r'
  19. #define LF '\n'
  20. #define REG register
  21. #ifndef BIGBUFSIZE
  22. #define BIGBUFSIZE 16384
  23. #endif
  24. char inbuf [ BIGBUFSIZE ] ;
  25. char outbuf [ BIGBUFSIZE ] ;
  26. int LineBuf;
  27. static char MsgInternalError [ ] = "tolwrupr: internal error: %s(%s)\n" ;
  28. static char MsgOpenError [ ] = "tolwrupr: cannot open `%s' for input\n" ;
  29. static char MsgStdin [ ] = "stdin" ;
  30. static char MsgStdout [ ] = "stdout" ;
  31. static char MsgSetmode [ ] = "setmode" ;
  32. static char MsgSetvbuf [ ] = "setvbuf" ;
  33. static char MsgFflush [ ] = "fflush" ;
  34. static unsigned char map [ 256 ] ;
  35. int main ( int argc , char * * argv ) ;
  36. void Usage ( void ) ;
  37. int main ( int argc , char * * argv )
  38. {
  39. REG int ch ;
  40. int countflag = 0 ;
  41. int toupperflag = -1 ;
  42. unsigned kilobytes ;
  43. unsigned bytecount ;
  44. char * MsgInput ;
  45. FILE * input ;
  46. char * cp ;
  47. -- argc ;
  48. ++ argv ;
  49. while ( argc > 0 && * ( cp = * argv ) == '-' )
  50. {
  51. while ( * ++ cp )
  52. {
  53. if ( * cp == 'k' )
  54. {
  55. if ( countflag != 0 )
  56. Usage ( ) ;
  57. countflag = 1 ;
  58. }
  59. else if ( * cp == 'L' )
  60. {
  61. if ( toupperflag != -1 )
  62. Usage ( ) ;
  63. toupperflag = 0 ;
  64. }
  65. else if ( * cp == 'U' )
  66. {
  67. if ( toupperflag != -1 )
  68. Usage ( ) ;
  69. toupperflag = 1 ;
  70. }
  71. else if ( * cp == 'X' )
  72. {
  73. if ( toupperflag != -1 )
  74. Usage ( ) ;
  75. toupperflag = 2 ;
  76. }
  77. else
  78. {
  79. Usage ( ) ;
  80. }
  81. }
  82. -- argc ;
  83. ++ argv ;
  84. }
  85. /*
  86. * Either -U or -L must be specified!
  87. */
  88. if ( toupperflag == -1 )
  89. Usage ( ) ;
  90. for ( ch = 0 ; ch < 256 ; ++ ch )
  91. map [ ch ] = ch ;
  92. /*
  93. * Set the case map
  94. */
  95. if ( toupperflag == 1 || toupperflag == 2 )
  96. {
  97. for ( ch = 'a' ; ch <= 'z' ; ++ ch )
  98. map [ ch ] -= 'a' - 'A' ;
  99. }
  100. if ( toupperflag == 0 || toupperflag == 2 )
  101. {
  102. for ( ch = 'A' ; ch <= 'Z' ; ++ ch )
  103. map [ ch ] += 'a' - 'A' ;
  104. }
  105. /*
  106. * Open the Input
  107. */
  108. if ( argc == 0 )
  109. {
  110. MsgInput = MsgStdin ;
  111. input = stdin ;
  112. if ( _setmode ( _fileno(stdin) , _O_BINARY ) == -1 )
  113. {
  114. fprintf ( stderr , MsgInternalError , MsgSetmode , MsgStdin ) ;
  115. exit ( -1 ) ;
  116. }
  117. }
  118. else if ( argc == 1 )
  119. {
  120. MsgInput = * argv ;
  121. if ( ! ( input = fopen ( MsgInput , "rb" ) ) )
  122. {
  123. fprintf ( stderr , MsgOpenError , MsgInput ) ;
  124. exit ( 1 ) ;
  125. }
  126. }
  127. else
  128. Usage ( ) ;
  129. if ( _setmode ( _fileno ( stdout ) , _O_BINARY ) == -1 )
  130. {
  131. fprintf ( stderr , MsgInternalError , MsgSetmode , MsgStdout ) ;
  132. exit ( -1 ) ;
  133. }
  134. if ( setvbuf ( input , inbuf , _IOFBF , BIGBUFSIZE ) )
  135. {
  136. fprintf ( stderr , MsgInternalError , MsgSetvbuf , MsgInput ) ;
  137. exit ( -1 ) ;
  138. }
  139. if ( setvbuf ( stdout , outbuf , _IOFBF , BIGBUFSIZE ) )
  140. {
  141. fprintf ( stderr , MsgInternalError , MsgSetvbuf , MsgStdout ) ;
  142. exit ( -1 ) ;
  143. }
  144. /* check for the need for line buffering */
  145. LineBuf = _isatty ( _fileno ( stdout ) ) ;
  146. /*
  147. * Process the Input
  148. */
  149. kilobytes = bytecount = 0 ;
  150. while ( ( ch = getc ( input ) ) != EOF )
  151. {
  152. ch = map [ ch ] ;
  153. putc ( ch , stdout ) ;
  154. if (ch == '\n' && LineBuf)
  155. fflush ( stdout ) ;
  156. if ( countflag )
  157. if ( ++ bytecount >= BIGBUFSIZE )
  158. {
  159. bytecount -= BIGBUFSIZE ;
  160. fprintf ( stderr , "%uK\r" , kilobytes += ( BIGBUFSIZE / 1024 ) ) ;
  161. }
  162. }
  163. if ( fflush ( stdout ) )
  164. {
  165. fprintf ( stderr , MsgInternalError , MsgFflush , MsgStdout ) ;
  166. return 1 ;
  167. }
  168. return 0 ;
  169. }
  170. void Usage ( void )
  171. {
  172. fprintf ( stderr ,
  173. "Usage: tolwrupr [-k] -(L|U|X) [ <InputFile> ]\n"
  174. "-k means echo progress in kilobytes to stderr\n"
  175. "-L means map lowercase characters to uppercase (tr \"[A-Z]\" \"[a-z]\")\n"
  176. "-U means map uppercase characters to lowercase (tr \"[a-z]\" \"[A-Z]\")\n"
  177. "-X means swap uppercase and lowercase (tr \"[a-z][A-Z]\" \"[A-Z][a-z]\")\n"
  178. "Exactly one of -L or -U or -X must be specified\n"
  179. ) ;
  180. exit ( 1 ) ;
  181. }