Subversion Repositories Kolibri OS

Rev

Rev 3247 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2.  
  3. int newline_to_file(char *fn, unsigned long long pos)
  4. {
  5.         char *newline = "\n\r";
  6.  
  7.         kol_struct70 k70_out;
  8.  
  9.         k70_out.p00 = 3;
  10.         k70_out.p04 = pos; // offset
  11.     //k70_out.p08 = 0;
  12.     k70_out.p12 = 2;
  13.     k70_out.p16 = (unsigned)newline;
  14.     k70_out.p20 = 0;
  15.     k70_out.p21 = fn;
  16.  
  17.     return kol_file_70(&k70_out); // write
  18. }
  19.  
  20.  
  21. int cmd_echo(char text[])
  22. { // added output redirection by rgimad 2020.
  23.  
  24.     int text_len = strlen(text);
  25.     int out_len = text_len;
  26.     int i, redirect = 0, redirect_mode = 0; // if redirect = 0 echo to screen, if 1 to file. If redirect_mode = 0 rewrite the file, if 1 append to the file
  27.     char *filename; // pointer to name of the file redirect to
  28.     int ignore_redir_char = 0;
  29.  
  30.     for (i = 0; i < text_len; i++)
  31.     {
  32.         if (text[i] == '"')
  33.         {
  34.                 ignore_redir_char = !ignore_redir_char;
  35.                 continue;
  36.         }
  37.         if (!ignore_redir_char && text[i] == '>')
  38.         {
  39.                 if (i + 1 < text_len && text[i + 1] == '>')
  40.                 {
  41.                         redirect = 1;
  42.                         redirect_mode = 1;
  43.                         filename = text + i + 2;
  44.                         out_len = i;
  45.                         break;
  46.                 } else
  47.                 {
  48.                         redirect = 1;
  49.                         redirect_mode = 0;
  50.                         filename = text + i + 1;
  51.                         out_len = i;
  52.                         break;
  53.                 }
  54.         }
  55.     }
  56.  
  57.     // remove leading spaces in filename
  58.     while (*filename == ' ') { filename++; }
  59.  
  60.     // remove spaces at the end of out_len
  61.     while (out_len > 0 && text[out_len - 1] == ' ') { out_len--; }
  62.  
  63.     // delete quotes if has
  64.     if (text[out_len - 1] == '"') { out_len--; }
  65.     if (text[0] == '"') { text++; out_len--; }
  66.  
  67.  
  68.     if (redirect == 0) // echo to screen
  69.     {
  70.         text[out_len] = '\0';
  71.         printf("%s\n\r", text);
  72.     } else
  73.     {
  74.             char *filename_out = (char*) malloc(FILENAME_MAX); // abs path
  75.  
  76.             if (filename[0] != '/')
  77.             {
  78.                 strcpy(filename_out, cur_dir);
  79.                 if (filename_out[strlen(filename_out)-1] != '/')
  80.                 {
  81.                     strcat(filename_out, "/"); // add slash
  82.                 }
  83.                 strcat(filename_out, filename);
  84.             } else
  85.             {
  86.                 strcpy(filename_out, filename);
  87.             }
  88.  
  89.             kol_struct70 k70_out, k70_in;
  90.             int result;
  91.  
  92.         if (redirect_mode == 0) // rewrite the output file
  93.         {
  94.  
  95.                 k70_out.p00 = 2;
  96.                 k70_out.p04 = 0; // offset
  97.                     //k70_out.p08 = 0;
  98.                     k70_out.p12 = out_len;
  99.                     k70_out.p16 = (unsigned)text;
  100.                     k70_out.p20 = 0;
  101.                     k70_out.p21 = filename_out;
  102.  
  103.                     result = kol_file_70(&k70_out); // write
  104.  
  105.                 if (result != 0) // unable to write
  106.                 {
  107.                         free(filename_out);
  108.                         return FALSE;
  109.                 }
  110.                 newline_to_file(filename_out, out_len);
  111.  
  112.                 free(filename_out);
  113.                 return TRUE;
  114.         } else // append to the output file
  115.         {
  116.                 kol_struct_BDVK bdvk;
  117.  
  118.                 k70_in.p00 = 5;
  119.                     k70_in.p04 = 0LL;
  120.                     k70_in.p12 = 0;
  121.                     k70_in.p16 = (unsigned) &bdvk;
  122.                     k70_in.p20 = 0;
  123.                     k70_in.p21 = filename_out;
  124.  
  125.                     result = kol_file_70(&k70_in); // get information about file
  126.                     if ( 0 != result ) // file doesnt exist, then rewrite
  127.                     {
  128.                         k70_out.p00 = 2;
  129.                         k70_out.p04 = 0; // offset
  130.                             //k70_out.p08 = 0;
  131.                             k70_out.p12 = out_len;
  132.                             k70_out.p16 = (unsigned)text;
  133.                             k70_out.p20 = 0;
  134.                             k70_out.p21 = filename_out;
  135.  
  136.                             result = kol_file_70(&k70_out); // write
  137.                         if (result != 0) // unable to write
  138.                         {
  139.                                 free(filename_out);
  140.                                 return FALSE;
  141.                         }
  142.                         newline_to_file(filename_out, out_len);
  143.  
  144.                         free(filename_out);
  145.                         return TRUE;
  146.                     }
  147.                     // if exists, append
  148.                     unsigned long long filesize = bdvk.p32;
  149.  
  150.                     k70_out.p00 = 3;
  151.                 k70_out.p04 = filesize; // offset
  152.                     //k70_out.p08 = 0;
  153.                     k70_out.p12 = out_len;
  154.                     k70_out.p16 = (unsigned)text;
  155.                     k70_out.p20 = 0;
  156.                     k70_out.p21 = filename_out;
  157.  
  158.                     result = kol_file_70(&k70_out); // write
  159.                 if (result != 0) // unable to write
  160.                 {
  161.                         free(filename_out);
  162.                         return FALSE;
  163.                 }
  164.  
  165.                 newline_to_file(filename_out, filesize + out_len);
  166.                 free(filename_out);
  167.                 return TRUE;
  168.         }
  169.     }
  170.     //free(filename_out);
  171.     return TRUE;
  172. }
  173.  
  174.