Guest User

Untitled

a guest
Dec 13th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. dir > List.txt
  2.  
  3. dir > list.txt 2>&1
  4.  
  5. /*
  6. Tee utility by VladD
  7. Use it in whichever way you like
  8. Hopefully it doesn't do anything bad, but no warranty
  9. Compiles with MSVC 2012
  10. */
  11.  
  12. #include "stdafx.h" // comment this line out if compiling with non-Microsoft compiler
  13.  
  14. #include <iostream>
  15. #include <fstream>
  16. #include <vector>
  17. #include <string>
  18. #include <cstdlib>
  19.  
  20. using namespace std;
  21.  
  22. bool append = false;
  23. char** curr_arg;
  24. vector<ostream*> target_streams;
  25. vector<string> stream_names;
  26.  
  27. bool have_open_error = false;
  28. bool have_io_error = false;
  29.  
  30. void evaluate_args()
  31. {
  32. for (; *curr_arg; curr_arg++)
  33. {
  34. string arg(*curr_arg);
  35. if (arg == "--")
  36. {
  37. curr_arg++;
  38. break;
  39. }
  40. else if (arg == "-a")
  41. {
  42. append = true;
  43. }
  44. // add more arguments here
  45. else if (arg.size() > 0 && arg[0] == '-')
  46. {
  47. cerr << "Unknown argument: " << arg << endl;
  48. exit(1);
  49. }
  50. else
  51. {
  52. break;
  53. }
  54. }
  55. }
  56.  
  57. void create_streams()
  58. {
  59. auto openmode = ios::out | (append ? ios::app : ios::trunc);
  60.  
  61. target_streams.push_back(&cout);
  62. stream_names.push_back("-");
  63.  
  64. for (; *curr_arg; curr_arg++)
  65. {
  66. string arg(*curr_arg);
  67. ostream* p;
  68. if (arg == "-")
  69. {
  70. p = &cout;
  71. }
  72. else
  73. {
  74. p = new ofstream(arg, openmode);
  75. if (!*p)
  76. {
  77. delete p;
  78. cerr << "Cannot open file `" << arg << "'" << endl;
  79. have_open_error = true;
  80. return;
  81. }
  82. }
  83. target_streams.push_back(p);
  84. stream_names.push_back(arg);
  85. }
  86. }
  87.  
  88. void destroy_streams()
  89. {
  90. for (auto p : target_streams)
  91. {
  92. if (p != &cout)
  93. delete p;
  94. }
  95. }
  96.  
  97. void process()
  98. {
  99. string line;
  100. while (getline(cin, line))
  101. {
  102. for (size_t i = 0; i < target_streams.size(); i++)
  103. {
  104. auto p = target_streams[i];
  105. if (!p)
  106. continue;
  107. (*p) << line << endl;
  108. if (!*p)
  109. {
  110. cerr << "Error occured writing to stream `" <<
  111. stream_names[i] << "'" << endl;
  112. delete p;
  113. target_streams[i] = nullptr;
  114. have_io_error = true;
  115. }
  116. }
  117. }
  118. }
  119.  
  120. int main(int argc, char* argv[])
  121. {
  122. curr_arg = argv + 1;
  123. evaluate_args();
  124.  
  125. create_streams();
  126. process();
  127.  
  128. destroy_streams();
  129. return (have_open_error ? 0x2 : 0) | (have_io_error ? 0x4 : 0);
  130. }
  131.  
  132. tee [ -ai ] [ File ... ]
  133.  
  134. -a Append the output to the files rather than overwriting them.
  135.  
  136. -i Ignore the SIGINT signal.
  137.  
  138. dir > C:log.txt && dir
  139.  
  140. dir >list.txt >&2
  141.  
  142. dir >list.txt 1>&2
Add Comment
Please, Sign In to add comment