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.

93 lines
2.0 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. tee.c
  5. Abstract:
  6. Utility program to read stdin and write it to stdout and a file.
  7. Author:
  8. Steve Wood (stevewo) 01-Feb-1992
  9. Revision History:
  10. --*/
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <errno.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. void
  18. Usage()
  19. {
  20. printf("Usage: tee [-a] OutputFileName(s)...\n" );
  21. exit(1);
  22. }
  23. #define MAX_OUTPUT_FILES 8
  24. __cdecl main( argc, argv )
  25. int argc;
  26. char *argv[];
  27. {
  28. int i, c;
  29. char *s, *OpenFlags;
  30. int NumberOfOutputFiles;
  31. FILE *OutputFiles[ MAX_OUTPUT_FILES ];
  32. if (argc < 2) {
  33. Usage();
  34. }
  35. NumberOfOutputFiles = 0;
  36. OpenFlags = "wb";
  37. for (i=1; i<argc; i++) {
  38. s = argv[ i ];
  39. if (*s == '-' || *s == '/') {
  40. s++;
  41. switch( tolower( *s ) ) {
  42. case 'a': OpenFlags = "ab"; break;
  43. default: Usage();
  44. }
  45. }
  46. else
  47. if (NumberOfOutputFiles >= MAX_OUTPUT_FILES) {
  48. fprintf( stderr, "TEE: too many output files specified - %s\n", s );
  49. }
  50. else
  51. if (!(OutputFiles[NumberOfOutputFiles] = fopen( s, OpenFlags ))) {
  52. fprintf( stderr, "TEE: unable to open file - %s\n", s );
  53. }
  54. else {
  55. NumberOfOutputFiles++;
  56. }
  57. }
  58. if (NumberOfOutputFiles == 0) {
  59. fprintf( stderr, "TEE: no output files specified.\n" );
  60. }
  61. while ((c = getchar()) != EOF) {
  62. putchar( c );
  63. for (i=0; i<NumberOfOutputFiles; i++) {
  64. if (c == '\n') {
  65. putc('\r', OutputFiles[ i ] ); //CRT reads cr/lf as lf
  66. putc('\n', OutputFiles[ i ] ); //must write as cr/lf
  67. fflush( OutputFiles[ i ] );
  68. }
  69. else {
  70. putc( c, OutputFiles[ i ] );
  71. }
  72. }
  73. }
  74. return( 0 );
  75. }