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.