Guest User

Untitled

a guest
Sep 3rd, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <cstdio>
  2. #include <string>
  3. #include <windows.h>
  4.  
  5. int main(int argc, char **argv)
  6. {
  7.     if(argc < 2) {
  8.         printf("No arguments given.\n");
  9.         return 0;
  10.     }
  11.     std::string command;
  12.     for(int i=1; i<argc; ++i) {
  13.         command += argv[i];
  14.         if(i != argc-1) command += ' ';
  15.     }
  16.     std::string output;
  17.     int gravisIndex = -1;
  18.     for(size_t i=0; i<command.size(); ++i) {
  19.         if(command[i] == '`') {
  20.             if(gravisIndex >= 0) {
  21.                 TCHAR tempPath[MAX_PATH];
  22.                 DWORD tempPathRet = GetTempPath(MAX_PATH, tempPath);
  23.                
  24.                 if(!tempPathRet || tempPathRet > MAX_PATH) {
  25.                     printf("Getting temp path failed.\n");
  26.                     return 0;
  27.                 }
  28.                
  29.                 std::string fileName = tempPath;
  30.                 fileName += tmpnam(NULL)+1;
  31.                 fileName += "tmp";
  32.                
  33.                 std::string sys = command.substr(gravisIndex+1, i-gravisIndex-1);
  34.                 sys = sys + " > " + fileName;
  35.                 system(sys.c_str());
  36.                
  37.                 FILE *file = fopen(fileName.c_str(), "r");
  38.                 if(!file) {
  39.                     printf("Opening temp file failed.\n");
  40.                     return 0;
  41.                 }
  42.                
  43.                 char buff[100];
  44.                 while(!feof(file)) {
  45.                     if(fgets(buff, 100, file)) {
  46.                         output += buff;
  47.                     }
  48.                 }
  49.                 fclose(file);
  50.                
  51.                 while(output.size() && (output[output.size()-1] == '\n' ||
  52.                                         output[output.size()-1] == '\r' ||
  53.                                         output[output.size()-1] == ' ')) {
  54.                     output = output.substr(0, output.size()-1);
  55.                 }
  56.                
  57.                 gravisIndex = -1;
  58.             }
  59.             else gravisIndex = i;
  60.         }
  61.         else if(gravisIndex < 0) output += command[i];
  62.     }
  63.     system(output.c_str());
  64. }
Advertisement
Add Comment
Please, Sign In to add comment