Guest User

Untitled

a guest
Nov 9th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7.  
  8. int main(int argc, char** argv) {
  9. pid_t pid;
  10. int rv;
  11. int fd[2];
  12.  
  13. if (pipe(fd) == -1) {
  14. perror("pipe error");
  15. exit(1);
  16. }
  17.  
  18. if ((pid = fork()) < 0) {
  19. perror("fork error");
  20. exit(1);
  21.  
  22. } else if (pid == 0) { // потомок
  23. close(fd[0]); // потомок не читает
  24. dup2(fd[1], STDOUT_FILENO); // перенаправление stdout
  25. rv = execvp(argv[1], argv + 1);
  26. if (rv) {
  27. perror("exec error");
  28. }
  29. _exit(rv);
  30.  
  31. } else { // родитель
  32. waitpid(pid, &rv, 0);
  33.  
  34. close(fd[1]); // родитель не записывает
  35. char buf[1];
  36.  
  37. while(read(fd[0], buf, 1) > 0) {
  38. if (islower(buf[0])) {
  39. buf[0] = toupper(buf[0]); //замена строчных букв на заглавные
  40. }
  41. write(1, buf, 1); // в stdout
  42. }
  43. exit(WEXITSTATUS(rv));
  44. }
  45. }
Add Comment
Please, Sign In to add comment