Thread: Fully buffered popen?!?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    72

    Fully buffered popen?!?

    Hi,

    I'm having trouble with `popen`, it seems like it's fully buffered instead of line buffered. I have the following code:
    Code:
    // Declare includes.
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // Declare defines.
    #define RESPONSE_SIZE   100
    
    // Declare function prototypes.
    void run_process (void);
    
    int main (int argc, char *argv[] __attribute__((unused)))
    {
    // Check number of arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", argc ? program_invocation_short_name : "");
            exit(EXIT_FAILURE);
        }
    
    // Run process.
        run_process();
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }
    
    void run_process (void)
    {
    // Declare variables.
        FILE *pp = NULL;
        char command[] = "sudo ir-keytable -p nec -t";
        char response[RESPONSE_SIZE + 1] = {0};
    
    // Open the pipe for reading.
        if((pp = popen(command, "r")) == NULL)
        {
            fprintf(stderr, "%s: %s error: popen failed (%s) (%s)\n", program_invocation_short_name, __func__, command, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Process all input.
        while(fgets(response, RESPONSE_SIZE, pp) != NULL)
        {
    // Display input.
            puts(response);
        }
    }
    I don't remember having problems with `popen` in the past, and I cant understand why it seems fully buffered. I don't get any response until about 27 key presses, then it's all dumped at once.

    Can someone point me to a solution I can enable in C. I have seen references online to a program called `unbuffered`, but that's enabled outside the scope of the program which requires it.

    I know I have used a `getch` function in linux by changing from line mode to character mode, but I'm lost thinking about how it might be changed to help this situation.

    FYI, this is not one of those posts that will be answered by the poster in an attempt to increase post count. I really am looking for help!

    Ty.

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    72
    I've tried the following without success:
    Code:
    if(setvbuf(pp, NULL, _IONBF, 0) != 0)
    I'm not understanding this, because when I run the command on a terminal, it's line buffered.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,704
    A better question might be why the program you're trying to run needs so much user input to make it work.

    Pipes (however you disguise them) are block devices by their nature.
    stdout - How to make output of any shell command unbuffered? - Stack Overflow
    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
    Apr 2019
    Posts
    72
    Quote Originally Posted by Salem View Post
    A better question might be why the program you're trying to run needs so much user input to make it work.
    The short story is I'm setting up a remote control for a credit card size computer. While setting it up, I found out that I don't need the hassle of setting up LIRC anymore, thanks to ir-keytables working at the kernel level (less overhead and setup frustration).

    Bottom line is that the program needs so much user input, because it does what the user wants, when they want it.

    Ty for your suggestion, I have setup a psuedo-terminal before, I'll try that route.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buffered input
    By BlaX in forum C Programming
    Replies: 10
    Last Post: 02-07-2010, 07:36 AM
  2. Buffered/UnBuffered I/O
    By valaris in forum C Programming
    Replies: 1
    Last Post: 08-06-2008, 11:31 PM
  3. Buffered I/O subsystem
    By bto19 in forum C Programming
    Replies: 2
    Last Post: 03-21-2004, 05:49 PM
  4. Buffered vs Unbuffered
    By PutoAmo in forum C Programming
    Replies: 4
    Last Post: 10-24-2002, 08:54 PM
  5. why buffered?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-21-2002, 11:35 AM

Tags for this Thread