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
|
int
do_shmio(optype, arglast)
int optype;
int *arglast;
{
#ifdef HAS_SHM
register STR **st = stack->ary_array;
register int sp = arglast[0];
STR *mstr;
char *mbuf, *shm;
int id, mpos, msize;
struct shmid_ds shmds;
#ifndef VOIDSHMAT
extern char *shmat();
#endif
id = (int)str_gnum(st[++sp]);
mstr = st[++sp];
mpos = (int)str_gnum(st[++sp]);
msize = (int)str_gnum(st[++sp]);
errno = 0;
if (shmctl(id, IPC_STAT, &shmds) == -1)
return -1;
if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
errno = EFAULT; /* can't do as caller requested */
return -1;
}
shm = (char*)shmat(id, (char*)NULL, (optype == O_SHMREAD) ? SHM_RDONLY : 0);
if (shm == (char *)-1) /* I hate System V IPC, I really do */
return -1;
mbuf = str_get(mstr);
if (optype == O_SHMREAD) {
if (mstr->str_cur < msize) {
STR_GROW(mstr, msize+1);
mbuf = str_get(mstr);
}
Copy(shm + mpos, mbuf, msize, char);
mstr->str_cur = msize;
mstr->str_ptr[msize] = '\0';
}
else {
int n;
if ((n = mstr->str_cur) > msize)
n = msize;
Copy(mbuf, shm + mpos, n, char);
if (n < msize)
memzero(shm + mpos + n, msize - n);
}
return shmdt(shm);
#else
fatal("shm I/O not implemented");
#endif
}
|