Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     FILE *output_file, *reading_file;
  12.     if(*(argv[1] + 1) != 0)
  13.     {
  14.         //create verification of correct key
  15.         //if pointer of next character is NULL then
  16.         //x == 0 and everything is ok
  17.         //else x != 0 and catch the exception for this error
  18.         printf("%s is incorrect key\n", argv[1]);
  19.     } else
  20.     switch (*argv[1])
  21.     {
  22.         case 'r':
  23.             //create archive file that includes itself a file from current directory
  24.             output_file = fopen(argv[2], "w"); //create archive file for recording of files
  25.             fprintf(output_file, "!<arch>\n");
  26.  
  27.             struct stat st;
  28.             if (stat(argv[2], &st) == -1) // if not successful filling of structure
  29.             {
  30.                 perror("stat");
  31.                 return 1;
  32.             }
  33.  
  34.             FILE *input_file;
  35.  
  36.             for(int j = 3; j < argc; j++)
  37.             {
  38.                 input_file = fopen(argv[j], "r");
  39.  
  40.                 int c;
  41.                 long size_count = 0;
  42.                 char *file_array = malloc(sizeof(c) / 4);
  43.                 while ((c = getc(input_file)) != EOF)
  44.                 {
  45.                     size_count += sizeof(c);
  46.                     file_array = realloc(file_array, size_count / 4 * sizeof(c) / 4);
  47.                     file_array[size_count / 4 - 1] = (char) c;
  48.                 }
  49.  
  50.                 char header[60]; //create header of archive file
  51.                 memset(header, 0x20, 60); //fill spaces all 60 bytes
  52.                 header[sprintf(header, "%s%c", argv[j], '/')] = 0x20;
  53.                 header[sprintf(header + 16, "%ld", st.st_mtime) + 16] = 0x20; //1552712807 change this?
  54.                 header[sprintf(header + 28, "%d", st.st_uid) + 28] = 0x20;
  55.                 header[sprintf(header + 34, "%d", st.st_gid) + 34] = 0x20;
  56.                 header[sprintf(header + 40, "%o", st.st_mode & 0xFFF) + 40] = 0x20;
  57.                 header[sprintf(header + 48, "%ld", size_count / 4) + 48] = 0x20;
  58.                 header[sprintf(header + 58, "%c%c", 0x60, 0x0A) + 58] = 0x20;
  59.  
  60.                 for (int i = 0; i < 60; i++)
  61.                 {
  62.                     fprintf(output_file, "%c", header[i]);
  63.                 }
  64.  
  65.                 for (long i = 0; i < size_count / 4; i++)
  66.                 {
  67.                     fprintf(output_file, "%c", file_array[i]);
  68.                 }
  69.                 free(file_array);
  70.  
  71.                 fclose(input_file);
  72.             }
  73.             fclose(output_file);
  74.             break;
  75.         case 't':
  76.             //show filenames of all files in archive file
  77.             reading_file = fopen(argv[2], "r");
  78.             int fc;
  79.             char * filename = malloc(1000);
  80.             int index = 0;
  81.             bool ifPoint_ = false;
  82.             bool ifSlash_ = false;
  83.             int control_slash = 0;
  84.             while ((fc = getc(reading_file)) != EOF)
  85.             {
  86.                 filename[index] = (char)fc;
  87.                 index++;
  88.                 if(ifPoint_ && ifSlash_ && fc == '\n')
  89.                 {
  90.                     for(int i = 0; i < control_slash; i++)
  91.                     {
  92.                         printf("%c", filename[i]);
  93.                     }
  94.                     printf("\n");
  95.                     ifPoint_ = false;
  96.                     ifSlash_ = false;
  97.                     index = 0;
  98.                 }
  99.                 if(fc == '/')
  100.                 {
  101.                     ifSlash_ = true;
  102.                     control_slash = index - 1;
  103.                 }
  104.                 if(fc == 0x60 && index == 59)
  105.                     ifPoint_ = true;
  106.                 if(fc == '\n')
  107.                 {
  108.                     index = 0;
  109.                 }
  110.             }
  111.             free(filename);
  112.  
  113.             fclose(reading_file);
  114.             break;
  115.         case 'p':
  116.             //show content of all files in archive file
  117.             reading_file = fopen(argv[2], "r");
  118.             char * stroke = malloc(1000);
  119.             int i = 0;
  120.             int ch;
  121.             bool ifPoint = false;
  122.             bool ifSlash = false;
  123.             bool ifForbid = false;
  124.             bool ifOutput = false;
  125.             while((ch = getc(reading_file)) != EOF)
  126.             {
  127.                 stroke[i] = (char)ch;
  128.                 if(ch == '/')
  129.                     ifSlash = true;
  130.                 if(ch == 0x60 && i == 58)
  131.                     ifPoint = true;
  132.                 if(ch == 0x0A && ifPoint && ifSlash)
  133.                 {
  134.                     ifOutput = false;
  135.                     ifForbid = true;
  136.                 }
  137.                 i++;
  138.                 if(ch == '\n' && ifOutput)
  139.                 {
  140.                     for(int j = 0; j < i; j++)
  141.                         printf("%c", stroke[j]);
  142.                     ifSlash = false;
  143.                     ifPoint = false;
  144.                 }
  145.                 if(ifForbid && ch == '\n')
  146.                 {
  147.                     ifForbid = false;
  148.                     ifOutput = true;
  149.                     ifSlash = false;
  150.                     ifPoint = false;
  151.                 }
  152.                 if(ch == '\n')
  153.                     i = 0;
  154.             }
  155.             free(stroke);
  156.  
  157.             fclose(reading_file);
  158.             break;
  159.         case 'd':
  160.             //delete choice file in archive file
  161.             reading_file = fopen(argv[2], "r");
  162.             output_file = fopen("h6R6i6ttTweBD40jokeTy1288tyd.ar", "w");
  163.             char * active_stroke = malloc(1000);
  164.             int j = 0;
  165.             bool ifNoWrite = false;
  166.             int sizeof_file = 0;
  167.             int cur_ch;
  168.             int dex = 10;
  169.             int size_index = 48;
  170.             int len_filename = 0;
  171.             while(*(argv[3] + j) != '\0')
  172.             {
  173.                 len_filename++;
  174.                 j++;
  175.             }
  176.             j = 0;
  177.  
  178.             while((cur_ch = getc(reading_file)) != EOF)
  179.             {
  180.                 active_stroke[j] = (char)cur_ch;
  181.                 j++;
  182.                 if(cur_ch == '\n')
  183.                 {
  184.                     int tmp_i = 0;
  185.                     while(active_stroke[tmp_i] == *(argv[3] + tmp_i))
  186.                     {
  187.                         if(tmp_i + 1 == len_filename && active_stroke[tmp_i + 1] == '/')
  188.                         {
  189.                             while (active_stroke[size_index] != ' ')
  190.                             {
  191.                                 sizeof_file = sizeof_file * dex + ((int)active_stroke[size_index] - 48);
  192.                                 size_index++;
  193.                             }
  194.                             size_index = 48;
  195.                             ifNoWrite = true;
  196.                             sizeof_file++;
  197.                         }
  198.                         tmp_i++;
  199.                     }
  200.                     if(!ifNoWrite)
  201.                     {
  202.                         for(int i = 0; i < j; i++)
  203.                         {
  204.                             fprintf(output_file, "%c", active_stroke[i]);
  205.                         }
  206.                     }
  207.                     j = 0;
  208.                 }
  209.  
  210.                 if(ifNoWrite)
  211.                 {
  212.                     sizeof_file--;
  213.                     if(sizeof_file == 0)
  214.                         ifNoWrite = false;
  215.                 }
  216.             }
  217.             free(active_stroke);
  218.             fclose(reading_file);
  219.             fclose(output_file);
  220.  
  221.             output_file = fopen(argv[2], "w");
  222.             reading_file = fopen("h6R6i6ttTweBD40jokeTy1288tyd.ar", "r");
  223.             while((cur_ch = getc(reading_file)) != EOF)
  224.             {
  225.                 fprintf(output_file, "%c", cur_ch);
  226.             }
  227.             fclose(reading_file);
  228.             fclose(output_file);
  229.  
  230.             //delete time file in the current directory
  231.             if (-1 == remove("h6R6i6ttTweBD40jokeTy1288tyd.ar"))
  232.                 printf("Error with deleted file\n");
  233.  
  234.             break;
  235.         case 'x':
  236.             //extract/export all files from archive file
  237.             reading_file = fopen(argv[2], "r");
  238.             char * reading_stroke = malloc(1000);
  239.             int p = 0;
  240.             bool ifCreate = false;
  241.             bool ifCreate_ = false;
  242.             int c_ch;
  243.             int len = 0;
  244.             char * tmp_str = malloc(16);
  245.             char * _tmp_str = malloc(16); //fix
  246.  
  247.             while((c_ch = getc(reading_file)) != EOF)
  248.             {
  249.                 reading_stroke[p] = (char)c_ch;
  250.                 p++;
  251.                 if(ifCreate)
  252.                 {
  253.                     ifCreate = false;
  254.                     ifCreate_ = true;
  255.                 }
  256.                 if(c_ch == '\n')
  257.                 {
  258.                     int tmp_i = 0;
  259.                     while(tmp_i != 16) //fix
  260.                     {
  261.                         if(reading_stroke[tmp_i + 1] == '/' && reading_stroke[58] == '`' && reading_stroke[59] == '\n')
  262.                         {
  263.                             if(ifCreate_)
  264.                                 fclose(output_file);
  265.                             for(int i = 0; i < tmp_i + 1; i++)
  266.                                 tmp_str[i] = reading_stroke[i];
  267.                             tmp_str[tmp_i + 1] = '\0';
  268.                             ifCreate = true;
  269.                             ifCreate_ = false;
  270.                             output_file = fopen(tmp_str, "w");
  271.                             break;
  272.                         }
  273.                         tmp_i++;
  274.                     }
  275.                     if(ifCreate_)
  276.                     {
  277.                         for(int i = 0; i < j; i++)
  278.                         {
  279.                             fprintf(output_file, "%c", reading_stroke[i]);
  280.                         }
  281.                     }
  282.                     p = 0;
  283.                 }
  284.  
  285.             }
  286.             free(tmp_str);
  287.             free(_tmp_str);
  288.             free(reading_stroke);
  289.             fclose(reading_file);
  290.             fclose(output_file);
  291.  
  292.             break;
  293.         default:
  294.             // if user write down incorrect key
  295.             printf("%s is incorrect key\n", argv[1]);
  296.     }
  297.  
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement