daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Dec 13th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pipe_local.h"
  2. int main()
  3. {
  4. int publicfifo, privatefifo, n;
  5. static char buffer[PIPE_BUF];
  6. struct message msg;
  7.  
  8. /*Using sprintf to create a unique fifo name
  9. and save into message structure*/
  10. sprintf(msg.fifo_name, "/tmp/fifo%d", getpid());
  11.  
  12. /*Creating the PRIVATE fifo*/
  13. if(mknod(msg.fifo_name, S_IFIFO | 0666, 0) < 0) {
  14.   perror(msg.fifo_name);
  15.   exit(1);
  16. }
  17.  
  18. /*Opening PUBLIC fifo in WRITE ONLY mode*/
  19. if((publicfifo = open(PUBLIC,O_WRONLY)) < 0) {
  20.   unlink(msg.fifo_name);
  21.   perror(PUBLIC);
  22. exit(1);
  23. }
  24.  
  25. while(1) {
  26.  
  27.   write(fileno(stdout), "\n cmd>", 6);
  28.   memset(msg.cmd_line, 0x0, B_SIZE);
  29.   n = read(fileno(stdin), msg.cmd_line, B_SIZE);
  30.  
  31. if(strncmp("quit", msg.cmd_line, n-1) == 0) {
  32.   break;
  33. }
  34.  
  35. write(publicfifo, &msg, sizeof(msg));
  36.  
  37. if((privatefifo = open(msg.fifo_name, O_RDONLY)) < 0) {
  38.   printf("1\n");
  39.   perror(msg.fifo_name);
  40.   goto CLEANUP;
  41. }
  42.  
  43. while((n = read(privatefifo, buffer, PIPE_BUF)) > 0) {
  44.   write(fileno(stderr), buffer, n);
  45. }
  46.  
  47. close(privatefifo);
  48. }
  49.  
  50. CLEANUP:
  51. close(publicfifo);
  52. unlink(msg.fifo_name);
  53.  
  54. return 0;
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top