Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <sys/stat.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9.  
  10. #include <signal.h>
  11.  
  12. #define UnusedArg(x) ((void)x)
  13. #define osErrorFatal(userMsg) osErrorFatalImpl(userMsg, __FILE__, __func__, __LINE__)
  14. #define osAssert(expr, userMsg) \
  15.     do { \
  16.         if(!(expr))\
  17.             osErrorFatal(userMsg);\
  18.     } while(0)
  19.    
  20. #define PIPE_RD 0
  21. #define PIPE_WR 1
  22.  
  23. void osErrorFatalImpl(const char* userMsg, const char* fileName, const char* functionName, const int lineNumber) {
  24.     perror(userMsg);
  25.     fprintf(stderr, "File: %s\nFunction: %s\nLine: %d\n", fileName, functionName, lineNumber);
  26.     exit(EXIT_FAILURE);
  27. }
  28.  
  29. int main(int argc, char** argv) {
  30.     osAssert(2 == argc, "./2 path/to/file.");
  31.    
  32.     int pipeFd1[2], pipeFd2[2];
  33.     osAssert(-1 != pipe(pipeFd1), "Failed to make pipe.");
  34.     osAssert(-1 != pipe(pipeFd2), "Failed to make pipe.");
  35.     int fd = open(argv[1], O_RDONLY);
  36.     osAssert(-1 != fd, "Failed to open file.");
  37.    
  38.     pid_t childPid = fork();
  39.     osAssert(-1 != childPid, "Fork failed.");
  40.    
  41.     if(childPid > 0) { //parent
  42.         close(pipeFd1[PIPE_RD]);
  43.         close(pipeFd2[PIPE_WR]);
  44.         unsigned bufLen = 1 << 13;
  45.         char* buf = malloc(bufLen);
  46.         osAssert(NULL != buf, "Failed to allocate memory for bufer.");
  47.        
  48.         int bytesRead = 0;
  49.         osAssert(-1 != (bytesRead = read(fd, buf, bufLen)), "Failed to read from file.");
  50.         buf[bytesRead] = 0;
  51. //      printf("%s\n", buf);
  52.         osAssert(-1 != write(pipeFd1[PIPE_WR], buf, bytesRead), "Failed to write from parent.");
  53.         osAssert(-1 != wait(NULL), "Failed to wait for child.");
  54.         osAssert(-1 != (bytesRead = read(pipeFd2[PIPE_RD], buf, bufLen)), "Failed to read from child.");
  55.        
  56.         buf[bytesRead - 1] = 0;
  57.         printf("After tr:\n%s\n", buf);
  58.        
  59.         close(pipeFd1[PIPE_WR]);
  60.         close(pipeFd2[PIPE_RD]);
  61.         free(buf);
  62.     }
  63.    
  64.     else { //child
  65.         close(pipeFd2[PIPE_RD]);
  66.         close(pipeFd1[PIPE_WR]);
  67.         unsigned bufLen = 1 << 13;
  68.         char* buf = malloc(bufLen);
  69.         osAssert(NULL != buf, "Failed to allocate memory for bufer.");
  70.        
  71.         int bytesRead = 0;
  72.         osAssert(-1 != (bytesRead = read(pipeFd1[PIPE_RD], buf, bufLen)), "Failed to read from file.");
  73.         buf[bytesRead] = 0;
  74.        
  75.         int fdSave = dup(STDOUT_FILENO);
  76.         osAssert(-1 != dup2(pipeFd2[PIPE_WR], STDOUT_FILENO), "Failed to replace STDOUT_FILENO.");
  77.        
  78.         char* cmd = malloc(bufLen+15);
  79.         osAssert(NULL != cmd, "Failed to allocate memory for system command.");
  80.        
  81.         sprintf(cmd, "echo \"%s\" | tr a A", buf);
  82.         int returnSys =  system(cmd);
  83.         osAssert(returnSys != -1, "System failed.");
  84.         osAssert(EXIT_SUCCESS == WEXITSTATUS(returnSys), "System did not exit with EXIT_SUCCESS.");
  85.        
  86.         osAssert(-1 != dup2(fdSave, STDOUT_FILENO), "Failed to return STDOUT_FILENO.");
  87.         close(pipeFd1[PIPE_RD]);
  88.         close(pipeFd2[PIPE_WR]);
  89.         free(cmd);
  90.         free(buf);
  91.     }
  92.    
  93.    
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement