Subversion Repositories Kolibri OS

Rev

Rev 6857 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. int cmd_cp(char param[])
  3. {
  4.     char* argv[100];
  5.     int argc;
  6.     char *filename_in = NULL;
  7.     char *filename_out = NULL;
  8.     char *buffer = NULL;
  9.  
  10.     kol_struct70 k70_in;
  11.     kol_struct70 k70_out;
  12.  
  13.     kol_struct_BDVK     bdvk;
  14.  
  15.     unsigned long long filesize;
  16.     unsigned result, buf_size;
  17.  
  18.     argc = parameters_prepare(param, argv);
  19.  
  20.     if (argc != 2)
  21.     {
  22.         #if LANG_ENG
  23.             printf("  cp <file_in> <file_out>\n\r");
  24.         #elif LANG_RUS
  25.             printf("  cp <¨áâ®ç­¨ª> <१ã«ìâ â>\n\r");
  26.         #endif
  27.         parameters_free(argc, argv);
  28.         return TRUE;
  29.     }
  30.  
  31.     filename_in  = (char*) malloc(FILENAME_MAX);
  32.     filename_out = (char*) malloc(FILENAME_MAX);
  33.  
  34.     if (argv[0][0] != '/')
  35.     {
  36.         strcpy(filename_in, cur_dir);
  37.         if (filename_in[strlen(filename_in)-1] != '/')
  38.         {
  39.             strcat(filename_in, "/"); // add slash
  40.         }
  41.         strcat(filename_in, argv[0]);
  42.     } else
  43.     {
  44.         strcpy(filename_in, argv[0]);
  45.     }
  46.     // -----
  47.     if (argv[1][0] != '/')
  48.     {
  49.         strcpy(filename_out, cur_dir);
  50.         if (filename_out[strlen(filename_out)-1] != '/')
  51.         {
  52.             strcat(filename_out, "/"); // add slash
  53.         }
  54.         strcat(filename_out, argv[1]);
  55.     } else
  56.     {
  57.         strcpy(filename_out, argv[1]);
  58.     }
  59.        
  60.     // add ability to use directory as destination
  61.     if ( dir_check(filename_out) )
  62.     {
  63.         char *fname = strrchr(filename_in, '/') + 1;  // always exist, as we add curdir
  64.         if (filename_out[strlen(filename_out)-1] != '/')
  65.         {
  66.             strcat(filename_out, "/"); // add slash
  67.         }
  68.         strcat(filename_out, fname);
  69.     }
  70.  
  71.  
  72.     k70_in.p00 = 5;
  73.     k70_in.p04 = 0LL;
  74.     k70_in.p12 = 0;
  75.     k70_in.p16 = (unsigned) &bdvk;
  76.     k70_in.p20 = 0;
  77.     k70_in.p21 = filename_in;
  78.  
  79.     result = kol_file_70(&k70_in); // get information about file
  80.     if ( 0 != result )
  81.         goto lbl_exit;
  82.  
  83.     // count buffer size up to 1Mb, but no more than 1/2 of free memory
  84.     buf_size = 1 << 20; // 1Mb
  85.     while( ((buf_size >> 10) > kol_system_memfree()) && (buf_size > 4096) )
  86.         buf_size /= 2;
  87.  
  88.     filesize = bdvk.p32; // getting file size (restriction - 4 GB only for FAT)
  89.     if (buf_size > filesize)
  90.         buf_size = (unsigned)filesize;  // may be zero!
  91.     if (buf_size == 0) buf_size = 4096; // ...
  92.  
  93.     buffer = (char*) malloc(buf_size);
  94.     if (!buffer)
  95.     {
  96.         result = E_NOMEM;
  97.         goto lbl_exit;
  98.     }
  99.  
  100.     k70_in.p00 = 0;
  101.     //k70_in.p08 = 0;
  102.     k70_in.p12 = buf_size;
  103.     k70_in.p16 = (unsigned) buffer;
  104.     k70_in.p20 = 0;
  105.     k70_in.p21 = filename_in;
  106.  
  107.     k70_out.p00 = 2;
  108.     //k70_out.p08 = 0;
  109.     k70_out.p12 = buf_size;
  110.     k70_out.p16 = (unsigned) buffer;
  111.     k70_out.p20 = 0;
  112.     k70_out.p21 = filename_out;
  113.  
  114.     unsigned long long offset = 0;
  115.     do
  116.     {
  117.         k70_in.p04 = offset;
  118.         if (offset + buf_size > filesize)  // last chunk
  119.         {
  120.             k70_in.p12 = k70_out.p12 = (unsigned)(filesize - offset); // filesize % buf_size;
  121.         }
  122.        
  123.         result = kol_file_70(&k70_in); // read
  124.         if (result != 0)
  125.         {
  126.             goto lbl_exit;
  127.         }
  128.  
  129.         k70_out.p04 = offset;
  130.         result = kol_file_70(&k70_out); // write
  131.         if (result != 0)
  132.         {
  133.            goto lbl_exit;
  134.         }
  135.  
  136.         if (k70_out.p00 == 2)
  137.         {
  138.             k70_out.p00 = 3; // changing function from create (2) to append (3)
  139.         }
  140.         offset += buf_size;
  141.     } while (offset < filesize);
  142.  
  143.     lbl_exit:
  144.  
  145.     parameters_free(argc, argv);
  146.     free(filename_in);
  147.     free(filename_out);
  148.     free(buffer);
  149.  
  150.     return (result == 0);
  151. }
  152.  
  153.