-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.c
76 lines (61 loc) · 1.35 KB
/
fifo.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "fifo.h"
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
static int _fifo = -1;
static const char* _fifo_path = "/tmp/xcbnotif.fifo";
int init_fifo()
{
struct stat buffer;
if(_fifo >= 0)
return 1;
if(has_entry("global.fifo"))
_fifo_path = get_string("global.fifo");
if(stat(_fifo_path, &buffer) >= 0)
remove(_fifo_path);
if(mkfifo(_fifo_path, 0777) != 0)
return 0;
_fifo = open(_fifo_path, O_RDWR | O_NONBLOCK);
if(_fifo < 0)
return 0;
return 1;
}
void close_fifo()
{
close(_fifo);
remove(_fifo_path);
}
int get_fifo_id()
{
return _fifo;
}
srv_order_t get_order_fifo(int size, char* buffer)
{
ssize_t rd;
char buf[256];
char* str;
rd = read(_fifo, buf, 256);
buf[rd] = '\0';
str = strtok(buf, " \n");
if(!str)
return NONE;
if(strcmp(str, "close") == 0)
return CLOSE;
else if(strcmp(str, "close_all") == 0)
return CLOSE_ALL;
else if(strcmp(str, "end") == 0 || strcmp(str, "kill") == 0)
return END;
else if(strcmp(str, "notif") != 0)
return NONE;
str = strtok(NULL, "\n");
if(!str)
return NONE;
if(buffer)
memcpy(buffer, str, size);
return NOTIF;
}