Subversion Repositories Kolibri OS

Rev

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

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