commit b5904455de3e151eae8979415a3abb2975f91b54 Author: inex Date: Thu Mar 5 11:45:24 2020 +0300 Partial migration to a public repository diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3934f62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +owo +test diff --git a/task1/task1.c b/task1/task1.c new file mode 100644 index 0000000..88ba161 --- /dev/null +++ b/task1/task1.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include + +typedef struct { + FILE *file; + long int size_of_file; + char *contents; +} TEXTFILE; + +long int filesize(FILE *fp) { + long int size_of_file; + fseek(fp, 0, SEEK_END); + size_of_file = ftell(fp); + fseek(fp, 0, SEEK_SET); + return(size_of_file); +} + +TEXTFILE* init_file(char* path) { + TEXTFILE *buf; + buf = malloc(sizeof(TEXTFILE)); + if (buf == NULL) { + fprintf(stderr, "Error allocating structure"); + return NULL; + } + buf -> file = fopen(path, "r"); + if (buf -> file == NULL) { + fprintf(stderr, "Error opening file"); + free(buf); + return NULL; + } + buf -> size_of_file = filesize(buf -> file); + buf -> contents = (char *)malloc(buf -> size_of_file); + if (buf -> contents == NULL) { + fprintf(stderr, "Error allocating contents array"); + fclose(buf -> file); + free(buf); + return NULL; + } + fread(buf -> contents, sizeof(char), buf -> size_of_file, buf -> file); + return buf; +} + +void close_file(TEXTFILE* file) { + free(file -> contents); + fclose(file -> file); + free(file); +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + printf("Некорректные аргументы. Требуется файл для чтения и файл для записи.\n"); + return 1; + } else { + TEXTFILE* input_file = init_file(argv[1]); + if (input_file == NULL) { return 1;} + char* pos; + pos = strstr(input_file -> contents, "777"); + while (pos != NULL) { + *pos = 'M'; + *(pos+1) = 'A'; + *(pos+2) = 'I'; + pos = strstr(pos, "777"); + } + FILE* output_file = fopen(argv[2], "w"); + fwrite(input_file -> contents, sizeof(char), input_file -> size_of_file, output_file); + fclose(output_file); + close_file(input_file); + return 0; + } +} diff --git a/task2/task2.c b/task2/task2.c new file mode 100644 index 0000000..dccc25e --- /dev/null +++ b/task2/task2.c @@ -0,0 +1,73 @@ +#include +#include + +typedef struct { + int* st; + int cnt; + int size; +} STACK; + +STACK* init_stack(int size) { + if (size <= 0) { + return NULL; + } + STACK *buf; + buf = malloc(sizeof(STACK)); + if (buf == NULL) { + fprintf(stderr, "Error allocating structure\n"); + return NULL; + } + buf -> st = malloc(sizeof(int)*size); + if (buf -> st == NULL) { + fprintf(stderr, "Error allocating array\n"); + free(buf); + return NULL; + } + buf -> cnt = 0; + buf -> size = size; + return buf; +} + +void destroy_stack(STACK* s) { + free(s -> st); + free(s); +} + +void push(STACK* st, int val) { + if ((st -> cnt) < (st -> size)) { + st -> st[st -> cnt] = val; + (st -> cnt)++; + } else { + fprintf(stderr, "Stack is full! Stack size is %d, value to add is %d\n", st -> size, val); + } +} + +int pop(STACK* st) { + if ((st -> cnt) > 0) { + (st -> cnt)--; + return st -> st[st -> cnt]; + + } else { + fprintf(stderr, "Attempted to pop an empty stack\n"); + return NULL; + } +} + +int main() { + printf("What's the meaning of my life?\n"); + STACK* huh = init_stack(5); + push(huh, 3); + push(huh, 2); + push(huh, 4); + printf("%d", pop(huh)); + printf("%d\n", pop(huh)); + printf("Nice. Let's try to overflow this stack now,\n"); + push(huh, 3); + push(huh, 3); + push(huh, 3); + push(huh, 3); + push(huh, 3); + printf("thx bye\n"); + return 0; +} +