Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 6_1.c:
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main(int argc, char *argv[], char *envs[])
- {
- int i,fd[2],npid,n,stat,cpid,childpid;
- FILE * file = fopen("/etc/passwd","r");
- FILE * file1 = fopen("./passwds","w");
- pipe(fd);
- if((childpid = fork()) == -1)
- {
- perror("fork");
- }
- if(childpid != 0) //родительский процесс
- {
- char buf[255];
- while ( !feof(file) )
- {
- fread(buf,255,1,file);
- close(fd[0]); /* Родитель закрывает вход */
- write(fd[1], buf, strlen(buf)); /* Посылаем "string" через выход канала */
- }//while
- }
- else //дочерний процесс
- {
- /* Потомок закрывает вход */
- close(fd[1]);
- /* Чтение строки из канала */
- char buf[255];
- while ( (n = read(fd[0], buf, sizeof(buf))) > 0){
- for (i=0; i<strlen(buf); i++){
- if (buf[i]=='b') { buf[i]='N';}
- if (buf[i]=='i') { buf[i]='a';}
- if (buf[i]=='n') { buf[i]='b';}
- }
- fwrite(buf,255,1,file1);
- }
- }
- return 0;
- }
- ------------------------------------
- serv.c:
- #include <sys/types.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #define FIFO "fifo.1"
- #define MAXBUFF 255
- int main (void){
- int fd, n;
- char buff[MAXBUFF]; /*буфер для чтения данных */
- /*Создадим специальный файл FIFO */
- if (mknod(FIFO, S_IFIFO | 0666, 0) < 0){
- printf("Невозможно создать FIFO\n");
- exit(1);
- }
- /*Получим доступ к FIFO*/
- if ((fd = open(FIFO, O_RDONLY)) < 0){
- printf("Невозможно открыть FIFO\n");
- exit(1);
- }
- while ( (n = read(fd, buff, MAXBUFF)) > 0)
- {
- char* commandStart = buff;
- FILE* fout = NULL;
- do
- {
- commandStart = strchr(commandStart, 'F');
- if ( commandStart != NULL && strncmp(commandStart, "FILENAME~", 9 ) == 0 )
- {
- commandStart+=9;
- char filename[128];
- strcpy(filename, commandStart);
- fout = fopen(filename, "w");
- commandStart += strlen(filename) + 1;
- if (fout != NULL)
- {
- printf("File opened\n");
- while (commandStart[0] != 0x26)
- {
- fprintf(fout, "%c", *commandStart);
- commandStart++;
- }
- fclose(fout);
- printf("File closed\n");
- }
- }
- } while ( (commandStart!= NULL) && (fout != NULL));
- }
- /* 3акроем FIFO, и удалим файл */
- close(fd);
- if (unlink(FIFO) < 0){
- printf("Невозможно удалить FIFO\n");
- exit(1);
- }
- exit(0);
- }
- ---------------------------------
- kl.c
- #include <sys/types.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/stat.h>
- /*Соглашение об имени FIFO*/
- #define FIFO "fifo.1"
- int main (void){
- int fd, n;
- /*Получим доступ к FIFO*/
- if ( (fd = open(FIFO, O_WRONLY)) < 0)
- {
- printf("Невозможно открыть FIFO\n");
- exit(1);
- }
- /*Передадим сообщение серверу FIFO*/
- if (write(fd, "qwFILENAME~abc.txt\0tut hranitsya chudo\x26", 39) != 39)
- {
- printf("Ошибка записи\n"); exit(1);
- }
- close(fd) ;
- exit (0);
- }
Add Comment
Please, Sign In to add comment