Guest User

Untitled

a guest
Aug 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. 6_1.c:
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9.  
  10. int main(int argc, char *argv[], char *envs[])
  11. {
  12.  
  13.  int i,fd[2],npid,n,stat,cpid,childpid;
  14.  FILE * file = fopen("/etc/passwd","r");
  15.  FILE * file1 = fopen("./passwds","w");
  16.  
  17.  pipe(fd);
  18.  
  19. if((childpid = fork()) == -1)
  20.            {
  21.                    perror("fork");
  22.            }
  23.            if(childpid != 0) //родительский процесс
  24.            {
  25.          char buf[255];
  26.          while ( !feof(file) )
  27.          {
  28.              fread(buf,255,1,file);
  29.          close(fd[0]); /* Родитель закрывает вход */
  30.              write(fd[1], buf, strlen(buf));    /* Посылаем "string" через выход канала */
  31.         }//while
  32.            }
  33.            else //дочерний процесс
  34.            {
  35.                    /* Потомок закрывает вход */
  36.            close(fd[1]);
  37.            /* Чтение строки из канала */
  38.            char buf[255];
  39.         while ( (n = read(fd[0], buf, sizeof(buf))) > 0){
  40.             for (i=0; i<strlen(buf); i++){
  41.             if (buf[i]=='b') { buf[i]='N';}
  42.             if (buf[i]=='i') { buf[i]='a';}
  43.             if (buf[i]=='n') { buf[i]='b';}
  44.             }
  45.         fwrite(buf,255,1,file1);
  46.             }
  47.           }
  48.  
  49.  return 0;  
  50. }
  51.  
  52.  
  53.  
  54. ------------------------------------
  55. serv.c:
  56. #include <sys/types.h>
  57. #include <fcntl.h>
  58. #include <stdio.h>
  59. #include <sys/stat.h>
  60. #define FIFO "fifo.1"
  61. #define MAXBUFF 255
  62.  
  63. int main (void){
  64.  
  65.  int fd, n;
  66.  char buff[MAXBUFF]; /*буфер для чтения данных */
  67.  /*Создадим специальный файл FIFO */
  68.  if (mknod(FIFO, S_IFIFO | 0666, 0) < 0){
  69.  printf("Невозможно создать FIFO\n");
  70.  exit(1);
  71.  }
  72.  /*Получим доступ к FIFO*/
  73.  if ((fd = open(FIFO, O_RDONLY)) < 0){
  74.  printf("Невозможно открыть FIFO\n");
  75.  exit(1);
  76.  }
  77.  
  78.  while ( (n = read(fd, buff, MAXBUFF)) > 0)
  79.  {
  80.    char* commandStart = buff;
  81.    FILE* fout = NULL;
  82.  
  83.    do
  84.    {
  85.        commandStart = strchr(commandStart, 'F');
  86.        if ( commandStart != NULL && strncmp(commandStart, "FILENAME~", 9 ) == 0 )
  87.        {
  88.          commandStart+=9;
  89.          char filename[128];
  90.          strcpy(filename, commandStart);
  91.          fout = fopen(filename, "w");
  92.              commandStart += strlen(filename) + 1;
  93.  
  94.            if (fout != NULL)
  95.            {
  96.                printf("File opened\n");
  97.                while (commandStart[0] != 0x26)
  98.                {
  99.                    fprintf(fout, "%c", *commandStart);
  100.                    commandStart++;
  101.                }
  102.                fclose(fout);
  103.                printf("File closed\n");
  104.            }
  105.  
  106.        }
  107.  
  108.  
  109.    } while ( (commandStart!= NULL) && (fout != NULL));
  110.    
  111.  }
  112.  /* 3акроем FIFO, и удалим файл */
  113.  close(fd);
  114.  if (unlink(FIFO) < 0){
  115.  printf("Невозможно удалить FIFO\n");
  116.  exit(1);
  117.  }
  118.  
  119. exit(0);
  120.  
  121. }
  122.  
  123.  
  124. ---------------------------------
  125. kl.c
  126.  
  127. #include <sys/types.h>
  128. #include <fcntl.h>
  129. #include <stdio.h>
  130. #include <sys/stat.h>
  131. /*Соглашение об имени FIFO*/
  132. #define FIFO "fifo.1"
  133.  
  134. int main (void){
  135.  int fd, n;
  136.  /*Получим доступ к FIFO*/
  137.  if ( (fd = open(FIFO, O_WRONLY)) < 0)
  138.  {
  139.   printf("Невозможно открыть FIFO\n");
  140.   exit(1);
  141.  }
  142.  /*Передадим сообщение серверу FIFO*/
  143.  if (write(fd, "qwFILENAME~abc.txt\0tut hranitsya chudo\x26", 39) != 39)
  144.   {
  145.   printf("Ошибка записи\n"); exit(1);
  146.  }
  147.  
  148.  close(fd) ;
  149.  exit (0);
  150. }
Add Comment
Please, Sign In to add comment