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..
Quote:
$ 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?