-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeWriter.c
55 lines (49 loc) · 822 Bytes
/
pipeWriter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* pipeWriter.c
*/
// C program to open and write to a FIFO
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// path to pipe
#define PIPE_PATH "./my_pipe"
#define MAX_BYTES 100
int main()
{
char buf[MAX_BYTES];
// Open FIFO as writer
int fd = open(PIPE_PATH, O_WRONLY | O_CREAT);
if (fd < 0)
{
perror("open");
exit(1);
}
while (1)
{
// get input from user
char *result = fgets(buf, MAX_BYTES, stdin);
if (result == NULL)
{
perror("fgets");
exit(1);
}
// write input into the pipe
int writeRes = write(fd, buf, strlen(buf)+1);
if (writeRes < 0)
{
perror("write");
exit(1);
}
}
fd = close(fd);
if (fd < 0)
{
perror("close");
exit(1);
}
return 0;
}