fopen hangs. I'm trying to simulate 'cat test.txt | wall"

This is a discussion on fopen hangs. I'm trying to simulate 'cat test.txt | wall" within the Linux Programming forums, part of the Platform Specific Boards category; The following programs 'hangs' at this point. Code: if ((data = fopen(temp_name,"w")) == NULL) { (void)fprintf(stderr, "cant open temp_name file\n"); ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    fopen hangs. I'm trying to simulate 'cat test.txt | wall"

    The following programs 'hangs' at this point.

    Code:
    if ((data = fopen(temp_name,"w")) == NULL) {
        (void)fprintf(stderr, "cant open temp_name file\n");
        exit(1);
      }
    Given the following
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    #include <signal.h>
    
    
    int main(void) {
      FILE *command;
      FILE *data;
      char *temp_name;
      int i = 0;
    
      if ((temp_name = tmpnam(NULL)) == 0) {
        (void)fprintf(stderr, "cant create temp file\n");
        exit(1);
       }
    
      printf ("Tempname #2: %s\n",temp_name);
    
      if(mkfifo(temp_name, S_IRUSR | S_IWUSR) != 0) {
        (void)fprintf(stderr, "cant create fifo\n");
        exit(1);
      }
    
      if ((command = popen("wall", "w")) == NULL) {
        (void)fprintf(stderr, "pipe error\n");
        exit(1);
      }
    
      (void)fprintf(command, "cat \"%s\" ", temp_name);
      fflush(command);
    
     /*fopen doesn't return*/
      if ((data = fopen(temp_name,"w")) == NULL) {
        (void)fprintf(stderr, "cant open temp_name file\n");
        exit(1);
      }
    
    
      for(i = 0; i < 10; i++) {
        (void)fprintf(data,"%s\n", "test");
      }
    
      fclose(data);
      fclose(command);
      unlink(temp_name);
      
      exit(0);
    }
    And here is what I get when I try to run it..

    $ gcc -g -Wall -Wextra broad3.c -o broad3
    /tmp/cczlHIZZ.o: In function `main':
    /home/cdalten/oakland/broad3.c:19: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
    [cdalten@localhost oakland]$ ./broad3
    Tempname #2: /tmp/filejEugU4
    Why is fopen() not returning?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well so far, all you've managed is
    echo "cat tempname" | wall

    You then write a bunch of stuff to your temp file, then delete it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    An attempt to open one end of a FIFO will hang until somebody else opens the other end. Read the manual.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HowTo (give your cat a pill)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-21-2004, 03:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21