Subversion Repositories Kolibri OS

Rev

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