Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- int main(int argc, char *argv[])
- {
- FILE *output_file, *reading_file;
- if(*(argv[1] + 1) != 0)
- {
- //create verification of correct key
- //if pointer of next character is NULL then
- //x == 0 and everything is ok
- //else x != 0 and catch the exception for this error
- printf("%s is incorrect key\n", argv[1]);
- } else
- switch (*argv[1])
- {
- case 'r':
- //create archive file that includes itself a file from current directory
- output_file = fopen(argv[2], "w"); //create archive file for recording of files
- fprintf(output_file, "!<arch>\n");
- struct stat st;
- if (stat(argv[2], &st) == -1) // if not successful filling of structure
- {
- perror("stat");
- return 1;
- }
- FILE *input_file;
- for(int j = 3; j < argc; j++)
- {
- input_file = fopen(argv[j], "r");
- int c;
- long size_count = 0;
- char *file_array = malloc(sizeof(c) / 4);
- while ((c = getc(input_file)) != EOF)
- {
- size_count += sizeof(c);
- file_array = realloc(file_array, size_count / 4 * sizeof(c) / 4);
- file_array[size_count / 4 - 1] = (char) c;
- }
- char header[60]; //create header of archive file
- memset(header, 0x20, 60); //fill spaces all 60 bytes
- header[sprintf(header, "%s%c", argv[j], '/')] = 0x20;
- header[sprintf(header + 16, "%ld", st.st_mtime) + 16] = 0x20; //1552712807 change this?
- header[sprintf(header + 28, "%d", st.st_uid) + 28] = 0x20;
- header[sprintf(header + 34, "%d", st.st_gid) + 34] = 0x20;
- header[sprintf(header + 40, "%o", st.st_mode & 0xFFF) + 40] = 0x20;
- header[sprintf(header + 48, "%ld", size_count / 4) + 48] = 0x20;
- header[sprintf(header + 58, "%c%c", 0x60, 0x0A) + 58] = 0x20;
- for (int i = 0; i < 60; i++)
- {
- fprintf(output_file, "%c", header[i]);
- }
- for (long i = 0; i < size_count / 4; i++)
- {
- fprintf(output_file, "%c", file_array[i]);
- }
- free(file_array);
- fclose(input_file);
- }
- fclose(output_file);
- break;
- case 't':
- //show filenames of all files in archive file
- reading_file = fopen(argv[2], "r");
- int fc;
- char * filename = malloc(1000);
- int index = 0;
- bool ifPoint_ = false;
- bool ifSlash_ = false;
- int control_slash = 0;
- while ((fc = getc(reading_file)) != EOF)
- {
- filename[index] = (char)fc;
- index++;
- if(ifPoint_ && ifSlash_ && fc == '\n')
- {
- for(int i = 0; i < control_slash; i++)
- {
- printf("%c", filename[i]);
- }
- printf("\n");
- ifPoint_ = false;
- ifSlash_ = false;
- index = 0;
- }
- if(fc == '/')
- {
- ifSlash_ = true;
- control_slash = index - 1;
- }
- if(fc == 0x60 && index == 59)
- ifPoint_ = true;
- if(fc == '\n')
- {
- index = 0;
- }
- }
- free(filename);
- fclose(reading_file);
- break;
- case 'p':
- //show content of all files in archive file
- reading_file = fopen(argv[2], "r");
- char * stroke = malloc(1000);
- int i = 0;
- int ch;
- bool ifPoint = false;
- bool ifSlash = false;
- bool ifForbid = false;
- bool ifOutput = false;
- while((ch = getc(reading_file)) != EOF)
- {
- stroke[i] = (char)ch;
- if(ch == '/')
- ifSlash = true;
- if(ch == 0x60 && i == 58)
- ifPoint = true;
- if(ch == 0x0A && ifPoint && ifSlash)
- {
- ifOutput = false;
- ifForbid = true;
- }
- i++;
- if(ch == '\n' && ifOutput)
- {
- for(int j = 0; j < i; j++)
- printf("%c", stroke[j]);
- ifSlash = false;
- ifPoint = false;
- }
- if(ifForbid && ch == '\n')
- {
- ifForbid = false;
- ifOutput = true;
- ifSlash = false;
- ifPoint = false;
- }
- if(ch == '\n')
- i = 0;
- }
- free(stroke);
- fclose(reading_file);
- break;
- case 'd':
- //delete choice file in archive file
- reading_file = fopen(argv[2], "r");
- output_file = fopen("h6R6i6ttTweBD40jokeTy1288tyd.ar", "w");
- char * active_stroke = malloc(1000);
- int j = 0;
- bool ifNoWrite = false;
- int sizeof_file = 0;
- int cur_ch;
- int dex = 10;
- int size_index = 48;
- int len_filename = 0;
- while(*(argv[3] + j) != '\0')
- {
- len_filename++;
- j++;
- }
- j = 0;
- while((cur_ch = getc(reading_file)) != EOF)
- {
- active_stroke[j] = (char)cur_ch;
- j++;
- if(cur_ch == '\n')
- {
- int tmp_i = 0;
- while(active_stroke[tmp_i] == *(argv[3] + tmp_i))
- {
- if(tmp_i + 1 == len_filename && active_stroke[tmp_i + 1] == '/')
- {
- while (active_stroke[size_index] != ' ')
- {
- sizeof_file = sizeof_file * dex + ((int)active_stroke[size_index] - 48);
- size_index++;
- }
- size_index = 48;
- ifNoWrite = true;
- sizeof_file++;
- }
- tmp_i++;
- }
- if(!ifNoWrite)
- {
- for(int i = 0; i < j; i++)
- {
- fprintf(output_file, "%c", active_stroke[i]);
- }
- }
- j = 0;
- }
- if(ifNoWrite)
- {
- sizeof_file--;
- if(sizeof_file == 0)
- ifNoWrite = false;
- }
- }
- free(active_stroke);
- fclose(reading_file);
- fclose(output_file);
- output_file = fopen(argv[2], "w");
- reading_file = fopen("h6R6i6ttTweBD40jokeTy1288tyd.ar", "r");
- while((cur_ch = getc(reading_file)) != EOF)
- {
- fprintf(output_file, "%c", cur_ch);
- }
- fclose(reading_file);
- fclose(output_file);
- //delete time file in the current directory
- if (-1 == remove("h6R6i6ttTweBD40jokeTy1288tyd.ar"))
- printf("Error with deleted file\n");
- break;
- case 'x':
- //extract/export all files from archive file
- reading_file = fopen(argv[2], "r");
- char * reading_stroke = malloc(1000);
- int p = 0;
- bool ifCreate = false;
- bool ifCreate_ = false;
- int c_ch;
- int len = 0;
- char * tmp_str = malloc(16);
- char * _tmp_str = malloc(16); //fix
- while((c_ch = getc(reading_file)) != EOF)
- {
- reading_stroke[p] = (char)c_ch;
- p++;
- if(ifCreate)
- {
- ifCreate = false;
- ifCreate_ = true;
- }
- if(c_ch == '\n')
- {
- int tmp_i = 0;
- while(tmp_i != 16) //fix
- {
- if(reading_stroke[tmp_i + 1] == '/' && reading_stroke[58] == '`' && reading_stroke[59] == '\n')
- {
- if(ifCreate_)
- fclose(output_file);
- for(int i = 0; i < tmp_i + 1; i++)
- tmp_str[i] = reading_stroke[i];
- tmp_str[tmp_i + 1] = '\0';
- ifCreate = true;
- ifCreate_ = false;
- output_file = fopen(tmp_str, "w");
- break;
- }
- tmp_i++;
- }
- if(ifCreate_)
- {
- for(int i = 0; i < j; i++)
- {
- fprintf(output_file, "%c", reading_stroke[i]);
- }
- }
- p = 0;
- }
- }
- free(tmp_str);
- free(_tmp_str);
- free(reading_stroke);
- fclose(reading_file);
- fclose(output_file);
- break;
- default:
- // if user write down incorrect key
- printf("%s is incorrect key\n", argv[1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement