Листинг 5.17. Функция mq_open: первая часть
//my_pxmsg._mmap/mq_open. с
1 #include "unpipc.h"
2 #include "mqueue.h"
3 #include
4 #define MAX_TRIES 10
5 struct mymq_attr defattr =
6 { 0, 128, 1024, 0 };
7 mymqd_t
8 mymq_open(const char *pathname, int oflag, …)
9 {
10 int i, fd, nonblock, created, save_errno;
11 long msgsize, filesize, index;
12 va_list ap;
13 mode_t mode;
14 int8_t *mptr;
15 struct stat statbuff;
16 struct mymq_hdr *mqhdr;
17 struct mymsg_hdr *msghdr;
18 struct mymq_attr *attr;
19 struct mymq_info *mqinfo;
20 pthread_mutexattr_t mattr;
21 pthread_condattr_t cattr;
22 created = 0;
23 nonblock = oflag & O_NONBLOCK;
24 oflag &= ~O_NONBLOCK;
25 mptr = (int8_t *) MAP_FAILED;
26 mqinfo = NULL;
27 again:
28 if (oflag & O_CREAT) {
29 va_start(ap, oflag); /* ар инициализируется последним аргументом */
30 mode = va_arg(ap, va_mode_t) & ~S_IXUSR;
31 attr = va_arg(ap, struct mymq_attr *);
32 va_end(ap);
33 /* открытие с установкой бита user-execute */
34 fd = open (pathname, oflag | O_EXCL | O_RDWR, mode | S_IXUSR);
35 if (fd < 0) {
36 if (errno == EEXIST && (oflag & O_EXCL) == 0)
37 goto exists; /* уже существует, OK */
38 else
39 return((mymqd_t) –1);
40 }
41 created = 1;
42 /* при создании файла он инициализируется */
43 if (attr == NULL)
44 attr = &defattr;
45 else {
46 if (attr->mq_maxmsg <– 0 || attr->mq_msgsize <= 0) {
47 errno = EINVAL;
48 goto err;
49 }
50 }