Subversion Repositories Kolibri OS

Rev

Rev 8467 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Programme name: Backy
  3.  * Description: The programme for backing up a file.
  4.  *
  5.  * Backy.c
  6.  * Author: JohnXenox aka Aleksandr Igorevich.
  7.  * Port to GCC: maxcodehack
  8.  *
  9.  * Works from command line, only!
  10.  */
  11.  
  12. #define CREATION_DATE "2020.05.27"
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/kos_LoadConsole.h>
  18.  
  19. #include "Backy_lib.h"
  20.  
  21. int date = 0;
  22. int time = 0;
  23.  
  24. char years = 0;
  25. char months = 0;
  26. char days = 0;
  27.  
  28. char hours = 0;
  29. char minutes = 0;
  30. char seconds = 0;
  31.  
  32. char *data = 0;
  33. int length = 0;
  34.  
  35. char path_in[4096] = {0};
  36. char path_out[4096] = {0};
  37.  
  38. char num[3] = {0};
  39.  
  40. char full_date[25] = {0};
  41. char ext[] = ".bak";
  42.  
  43. char flag = 0;
  44.  
  45. char state;
  46.  
  47.  
  48. int main(int argc, char** argv)
  49. {
  50.     load_console();
  51.  
  52.     con_set_title("Backy");
  53.  
  54. // ============================================================ //
  55. // preprocessing arguments from the command line. ============= //
  56. //
  57. // 0 argument - name of the programme.
  58. // 1 argument - path to the file with name that need to be backup.
  59. // 2 argument - the key (-o).
  60. // 3 argument - path to the output directory without the name of the file.
  61.  
  62. // printf("Number of args: %d\n", argc);
  63. // printf("Argv 0: %s\n", argv[0]);
  64. // sprintf("Argv 1: %s\n\n", argv[1]);
  65.  
  66.  
  67. // ============================================================ //
  68. // process the command line arguments. ======================== //
  69.  
  70.     if (argc > 1)
  71.     {
  72.         for (int i = 1; i < argc; i++)
  73.         {
  74.             // if found the key "-o", then copy output path into the array "path_out".
  75.             if (*argv[i] == '-') // && (*(argv[i] + 1) == 'o'))
  76.             {
  77.                 // printf("Key -o is found!\n");
  78.  
  79.                 i++;
  80.  
  81.                 flag = 1;
  82.  
  83.                 if (i <= argc)
  84.                 {
  85.                     // copying of a current path into the array "path_out".
  86.                     strcpy(path_out, argv[i]);
  87.  
  88.                     // printf("Path output is copyed!\n");
  89.  
  90.                     i++;
  91.  
  92.                     break;
  93.                 }
  94.             }
  95.  
  96.             // if input path is found, then copy it into the array "path_in".
  97.             if (*argv[i] == '/')
  98.             {
  99.                 flag = 2;
  100.  
  101.                 // copying of a current path into the buffer.
  102.                 strcpy(path_in, argv[i]);
  103.  
  104.                 if (flag != 1)
  105.                 {
  106.                     int idx = strlen(path_in);
  107.                     while (path_in[idx]  !=  '/')
  108.                     {
  109.                         idx--;
  110.                     }
  111.  
  112.                     strncpy(path_out, path_in, idx);
  113.                 }
  114.  
  115.                 // printf("Path input is copyed!\n");
  116.             }
  117.  
  118.             // if found characters.
  119.             if ( (*argv[i] > '0') && (*argv[i] < '9') || \
  120.                  (*argv[i] > 'A') && (*argv[i] < 'Z') || \
  121.                  (*argv[i] > 'a') && (*argv[i] < 'z') )
  122.             {
  123.                  flag = 3;
  124.  
  125.                  strcpy(path_in, argv[0]);
  126.                  // printf("Arg 0 is copyed!\n");
  127.  
  128.                  int idx = strlen(path_in);
  129.  
  130.                  while (path_in[idx]  !=  '/')
  131.                  {
  132.                      path_in[idx]  =  0;
  133.                      idx--;
  134.                  }
  135.  
  136.                  idx++;
  137.                  strcpy(path_out, path_in);
  138.                  strcpy(&path_in[idx], argv[1]);
  139.                  // printf("Arg 1 is added!\n");
  140.  
  141.             }
  142.         }
  143.  
  144.         // if not found the flag, then copy path from "path_in" into "path_out".
  145.         if ((flag == 0) && (flag != 2)  && (flag != 3))
  146.         {
  147.             // copying the input path into the output path,
  148.             strcpy(path_out, path_in);
  149.             //printf("Path input is copyed into the path output!\n");
  150.         }
  151.     }
  152.     else
  153.     {
  154.         con_set_title("Useful info!");
  155.  
  156.         #if defined (lang_en)
  157.  
  158.             con_printf("\n Name: Backy");
  159.             con_printf("\n Date: %s", CREATION_DATE);
  160.             con_printf("\n Description: The programme for backing up a file.\n");
  161.  
  162.             con_printf("\n Author: JohnXenox\n");
  163.  
  164.             con_printf("\n Usage: backy <path1> <-o path2>\n");
  165.             con_printf("  path1 - path to a file to be backuped.\n");
  166.             con_printf("  -o path2 - path to the output directory without the name of a file.\n\n");
  167.  
  168.             con_printf(" Examples:\n");
  169.             con_printf("  backy test.c\n");
  170.             con_printf("  backy test.c -o /tmp0/1/\n");
  171.             con_printf("  backy /hd0/1/test.c\n");
  172.             con_printf("  backy /hd0/1/test.c -o /tmp0/1/\n");
  173.  
  174.         #elif defined (lang_ru)
  175.  
  176.             con_printf("\n ˆ¬ï: Backy");
  177.             con_printf("\n „ â : %s", CREATION_DATE);
  178.             con_printf("\n Ž¯¨á ­¨¥: à®£à ¬¬  ¤«ï ᮧ¤ ­¨ï १¥à¢­®© ª®¯¨¨ ä ©« .\n");
  179.  
  180.             con_printf("\n €¢â®à: JohnXenox\n");
  181.  
  182.             con_printf("\n ˆá¯®«ì§®¢ ­¨¥: backy <path1> <-o path2>\n");
  183.             con_printf("  path1 - ¯ãâì ª ä ©«ã, ª®â®àë© ­ ¤® ᪮¯¨à®¢ âì.\n");
  184.             con_printf("  -o path2 - ¯ãâì ª ¤¨à¥ªâ®à¨¨, ¢ ª®â®àãî ¡ã¤¥â ᪮¯¨à®¢ ­  १¥à¢­ ï ª®¯¨ï ä ©« .\n\n");
  185.  
  186.             con_printf(" à¨¬¥àë:\n");
  187.             con_printf("  backy test.c\n");
  188.             con_printf("  backy test.c -o /tmp0/1/\n");
  189.             con_printf("  backy /hd0/1/test.c\n");
  190.             con_printf("  backy /hd0/1/test.c -o /tmp0/1/\n");
  191.  
  192.         #endif
  193.  
  194.         return 0;
  195.     }
  196.  
  197.     //printf("Path_in: %s\n", path_in);
  198.     //printf("Path_out: %s\n", path_out);
  199.  
  200.  
  201. // ============================================================ //
  202. // getting the time in BCD. =================================== //
  203.  
  204.     time = getTime();  // time = 0x00SSMMHH.
  205.  
  206.     hours = (char)time;
  207.     minutes = (char)(time >> 8);
  208.     seconds = (char)(time >> 16);
  209.  
  210. // ============================================================ //
  211. // getting the date in BCD. =================================== //
  212.  
  213.     date = getDate();  // date = 0x00DDMMYY.
  214.  
  215.     years = (char)date;
  216.     months = (char)(date >> 8);
  217.     days = (char)(date >> 16);
  218.  
  219. // ============================================================ //
  220. // fills the array with the date in ASCII. ==================== //
  221.  
  222.     char ofs = 0;
  223.     char *dta = 0;
  224.  
  225.     for (char i = 0; i < 3; i++)
  226.     {
  227.         if (i == 0)
  228.         {
  229.             dta = &years;
  230.             full_date[ofs] = '2';
  231.             ofs++;
  232.             full_date[ofs] = '0';
  233.             ofs++;
  234.         }
  235.         if (i == 1)
  236.         {
  237.             dta = &months;
  238.         }
  239.         if (i == 2)
  240.         {
  241.             dta = &days;
  242.         }
  243.  
  244.  
  245.         itoab(*dta, num, 16);
  246.  
  247.         if (num[1] == 0)
  248.         {
  249.             full_date[ofs] = '0';
  250.             ofs++;
  251.             full_date[ofs] = num[0];
  252.         }
  253.         else
  254.         {
  255.             full_date[ofs] = num[0];
  256.             ofs++;
  257.             full_date[ofs] = num[1];
  258.         }
  259.  
  260.         ofs++;
  261.  
  262.         if (i != 2)
  263.         {
  264.             full_date[ofs] = '.';
  265.             ofs++;
  266.         }
  267.     }
  268.  
  269.     full_date[ofs] = '_';
  270.     ofs++;
  271.  
  272. // ============================================================ //
  273. // fills the array with the time in ASCII. ==================== //
  274.  
  275.     ofs = 11;
  276.     dta = 0;
  277.  
  278.     for (char i = 0; i < 3; i++)
  279.     {
  280.         if (i == 0)
  281.             dta = &hours;
  282.         if (i == 1)
  283.             dta = &minutes;
  284.         if (i == 2)
  285.             dta = &seconds;
  286.  
  287.         itoab(*dta, num, 16);
  288.  
  289.         if (num[1] == 0)
  290.         {
  291.             full_date[ofs] = '0';
  292.             ofs++;
  293.             full_date[ofs] = num[0];
  294.         }
  295.         else
  296.         {
  297.             full_date[ofs] = num[0];
  298.             ofs++;
  299.             full_date[ofs] = num[1];
  300.         }
  301.  
  302.         ofs++;
  303.  
  304.         if (i < 2)
  305.         {
  306.             full_date[ofs] = '.';
  307.         }
  308.         //else
  309.         //{
  310.         //    full_date[ofs] = '_';
  311.         //}
  312.  
  313.         ofs++;
  314.     }
  315.  
  316. // ============================================================ //
  317. // adding the name of the input file to the output path. ====== //
  318.  
  319.     int i = 0;
  320.     int y = 0;
  321.  
  322.     // searching for a zero terminator in the input path.
  323.     while (path_in[i] != 0)
  324.     {
  325.         i++;
  326.     }
  327.  
  328.     // searching for a slash in the input path.
  329.     while (path_in[i] != '/')
  330.     {
  331.         i--;
  332.     }
  333.  
  334.     // searching for a zero terminator in the output path.
  335.     while (path_out[y] != 0)
  336.     {
  337.         y++;
  338.     }
  339.  
  340.     // searching for a slash in the output path.
  341.     if (path_out[y - 1] == '/')
  342.     {
  343.         y--;
  344.     }
  345.  
  346.     // copying the input name of the file into the output path,
  347.     strcpy(&path_out[y], &path_in[i]);
  348.  
  349. // ============================================================ //
  350. // adding the extension and full date to the path. ============ //
  351.  
  352.     i = 0;
  353.  
  354.     // searching for a zero terminator in the output path.
  355.     while (path_out[i] != 0)
  356.     {
  357.         i++;
  358.     }
  359.  
  360.     path_out[i] = '_';
  361.     i++;
  362.  
  363.     // adding full date.
  364.     strcpy(&path_out[i], full_date);
  365.  
  366.     i += strlen(full_date);
  367.  
  368.     // adding the extension to a path.
  369.     strncpy(&path_out[i], ext, 4);
  370.  
  371.     //printf("Path_in: %s\n", path_in);
  372.     //printf("Path_out: %s\n", path_out);
  373.  
  374.     data = openFile(&length, path_in);
  375.  
  376.     if(data == 0)
  377.     {
  378.         #if defined (lang_en)
  379.  
  380.             con_printf("\nThe file isn't found!\n");
  381.  
  382.         #elif defined (lang_ru)
  383.  
  384.             con_printf("\n” ©« ­¥ ­ ©¤¥­!\n");
  385.  
  386.         #endif
  387.  
  388.         return 13;
  389.     }
  390.  
  391.    
  392.     saveFile(length, data, 0, path_out);//return checkStateOnSave(saveFile(length, data, 0, path_out));
  393. }
  394.  
  395.