Thread: using popen

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    29

    using popen

    I'm using ubuntu 10.04

    I'm writing a small program to read the input of a gamepad (ps3) that needs a driver (sixad). I used popen to create a process and run the driver in background. Also I want to know when the gamepad is connected, so I need to catch the sixad messages, I made popen("sixad -s (start) 2>&1, "r")) to read from both stdout and stderr. When I got the message that the gamepad is connected I was thinking in breaking the while loop, and to proceed with the rest of the program.
    I tested the following code, but I don't understand the results (more below), why the loop isn't always running?.
    I'm a complete newbie, I don't know if this is the correct way of doing what I want, so any suggestions will be very appreciated.


    Code:
    FILE *fp1;
      printf("popen\n"); 
    
      if ((fp1 = popen("sixad -s 2>&1","r")) == NULL)
      {
        fprintf(stderr, "Fail opening popen\n");
        exit(EXIT_FAILURE);
      }
    
      char linha[400];
      printf("\n1");
      while(fgets(linha,400,fp1)!=NULL)
      {
        printf("\n2");
        char *n=strstr(linha,"Connected PLAYSTATION(R)3 Controller");
        printf("\n3");
        printf("\n[%s]%s",n,linha);
    
        printf("\n4");
        if(n=='\0')
        {
          printf("\n5");
        }
        printf("\n6");
       }
      printf("\n7");
    Terminal: when I don't press the PS button
    1
    2
    3
    [(null)]sixad[2114]: sixad started, press the PS button now

    4
    5
    Terminal: when I press the PS button @5 and the gamepad is sucessfully connected
    1
    2
    3
    [(null)]sixad[2432]: sixad started, press the PS button now

    4
    5
    6
    2
    3
    [Connected PLAYSTATION(R)3 Controller (00:23:06:A4:E6:AA)
    ]sixad[2446]: Connected PLAYSTATION(R)3 Controller (00:23:06:A4:E6:AA)

    4

  2. #2

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it will only print something when the thing at the other end of the pipe bothers to output some text. No output, no loop.

    If you need to break out of the loop because you've waited too long, then you need to use real pipes, which give you more control (but more code to write).

    > char *n=strstr(linha,"Connected PLAYSTATION(R)3 Controller");
    ALWAYS check the return result. If the string isn't found, then you get NULL.

    > [(null)]sixad[2432]: sixad started, press the PS button now
    Don't rely on everyone elses printf implementations to do something nice with a NULL pointer. Others may just crash.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    @Salem

    Thank you for the help.
    Sorry for the cross posting. I didn't know the correct place to post.

    Yes I want to break the loop because of waiting too long. What are real pipes btw? If you can get me some tutorials or indications about that I'll be very thankful.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    pipe(2): create pipe - Linux man page
    To create a pipe between two processes.

    select(2): synchronous I/O multiplexing - Linux man page
    To monitor file descriptors for a change in state, possibly with a timeout

    fopen(3): stream open functions - Linux man page
    Associate a FILE* with a descriptor, so you can use fgets() etc
    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.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    @Salem

    thank you once again.

    I only need to use these 3 functions? and forget popen? how I launch the driver then? I have no experience with processes.. can you make me the basic code if isn't asking too much?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seeing as there are plenty of examples on the board, maybe search and try yourself first.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. popen and fgets, underflow!
    By henrikstolpe in forum Linux Programming
    Replies: 0
    Last Post: 02-06-2009, 03:39 AM
  2. popen takes too long?
    By Largo in forum C Programming
    Replies: 2
    Last Post: 11-20-2006, 06:46 AM
  3. popen()
    By Cactus_Hugger in forum Windows Programming
    Replies: 2
    Last Post: 10-22-2005, 03:16 PM
  4. popen vs fopen
    By esme in forum Linux Programming
    Replies: 1
    Last Post: 11-25-2002, 10:37 AM