Subversion Repositories Kolibri OS

Rev

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