Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <string>
- #include <windows.h>
- int main(int argc, char **argv)
- {
- if(argc < 2) {
- printf("No arguments given.\n");
- return 0;
- }
- std::string command;
- for(int i=1; i<argc; ++i) {
- command += argv[i];
- if(i != argc-1) command += ' ';
- }
- std::string output;
- int gravisIndex = -1;
- for(size_t i=0; i<command.size(); ++i) {
- if(command[i] == '`') {
- if(gravisIndex >= 0) {
- TCHAR tempPath[MAX_PATH];
- DWORD tempPathRet = GetTempPath(MAX_PATH, tempPath);
- if(!tempPathRet || tempPathRet > MAX_PATH) {
- printf("Getting temp path failed.\n");
- return 0;
- }
- std::string fileName = tempPath;
- fileName += tmpnam(NULL)+1;
- fileName += "tmp";
- std::string sys = command.substr(gravisIndex+1, i-gravisIndex-1);
- sys = sys + " > " + fileName;
- system(sys.c_str());
- FILE *file = fopen(fileName.c_str(), "r");
- if(!file) {
- printf("Opening temp file failed.\n");
- return 0;
- }
- char buff[100];
- while(!feof(file)) {
- if(fgets(buff, 100, file)) {
- output += buff;
- }
- }
- fclose(file);
- while(output.size() && (output[output.size()-1] == '\n' ||
- output[output.size()-1] == '\r' ||
- output[output.size()-1] == ' ')) {
- output = output.substr(0, output.size()-1);
- }
- gravisIndex = -1;
- }
- else gravisIndex = i;
- }
- else if(gravisIndex < 0) output += command[i];
- }
- system(output.c_str());
- }
Advertisement
Add Comment
Please, Sign In to add comment