-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_reader.c
More file actions
82 lines (67 loc) · 1.55 KB
/
message_reader.c
File metadata and controls
82 lines (67 loc) · 1.55 KB
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
76
77
78
79
80
81
82
#include "message_slot.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define STDOUT 1
int main(int argc, char** argv)
{
int fd, ret_val, err=0;
char *filepath;
unsigned int channel;
char *message;
if (argc != 3) //verifying args number validity
{
perror(strerror(EINVAL));
exit(USER_FAILURE);
}
filepath = argv[1];
errno = 0;
channel = strtol(argv[2],NULL,10);
err = errno;
if (err != 0) //verifying channel argument validty
{
perror(strerror(err));
}
message = (char*)malloc(BUF_LEN*sizeof(char));
if (message == NULL)
{
err = errno;
perror(strerror(err));
exit(USER_FAILURE);
}
fd = open(filepath, O_RDWR ); //opening device file
if(fd < 0)
{
err = errno;
perror(strerror(err));
exit(USER_FAILURE);
}
ret_val = ioctl(fd, MSG_SLOT_CHANNEL, channel); //asigning channel to file descriptor
if (ret_val < 0)
{
err = errno;
perror(strerror(err));
exit(USER_FAILURE);
}
ret_val = read(fd,message,BUF_LEN); //reading from channel, by passing a buffer of length BUF_LEN
if (ret_val < 0)
{
err = errno;
perror(strerror(err));
exit(USER_FAILURE);
}
ret_val = write(STDOUT,message,ret_val); //writing message to stdout. Will only write ret_val cells from buffer, meaning will only write the length of recieved message!
if (ret_val < 0)
{
err = errno;
perror(strerror(err));
exit(USER_FAILURE);
}
free(message);
close(fd);
exit(SUCCESS);
}