Загрузка данных


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> 
#include <sys/types.h>
#include <sys/wait.h>

typedef struct {
    int shift;
    char text[1000];
} Message;


void ceasar_encrypt(char *text, int shift){
    for (int i = 0; text[i] != '\0'; i++){
        if (text[i] >= 'a' && text[i] <= 'z'){
            text[i] = 'a' + (text[i] - 'a' + shift) % 26;
        } else if (text[i] >= 'A' && text[i] <= 'Z') {
            text[i] = 'A' + (text[i] -'A' + shift) % 26;
        }
    }
}


int main(){
    int parent_to_child[2];
    int child_to_parent[2];

    // Создаем два канала
    if (pipe(parent_to_child) == -1 || pipe(child_to_parent) == -1){
        perror("pipe");
        return 1;
    }

    pid_t pid = fork();

    if (pid == -1){
        perror("fork");
        return 1;
    }

    // Дочерний процесс
    if (pid == 0){
        // Дочернему процессу не нужны эти концы каналов
        close(parent_to_child[1]);
        close(child_to_parent[0]);

        Message msg;
        read(parent_to_child[0], &msg, sizeof(msg));
        
        // Шифруем
        ceasar_encrypt(msg.text, msg.shift);
        
        // Отправляем результат
        write(child_to_parent[1], msg.text, strlen(msg.text) + 1);

        close(parent_to_child[0]);
        close(child_to_parent[1]);

        return 0;
    }

    // Родительский процесс
    else {
        Message msg;
        printf("Введите текст на английский: ");
        fflush(stdout);
        if (!fgets(msg.text, 1000, stdin)) return 1;
        msg.text[strcspn(msg.text, "\n")] = '\0';

        printf("Введите сдвиг: ");
        fflush(stdout);
        if (scanf("%d", &msg.shift) != 1) return 1;
        msg.shift = ((msg.shift % 26) + 26) % 26;

        // recieve text and shift to child process
        write(parent_to_child[1], &msg, sizeof(msg));
        close(parent_to_child[1]);

        // get encrypted text
        char buf[1000];
        read(child_to_parent[0], buf, sizeof(buf));

        printf("Зашифрованный текст: %s\n", buf);

        close(child_to_parent[0]);

        // wait the end of child process
        wait(NULL);
    }
    return 0;
}