/* 23 Mar 1991 by Eric Grosse. Copyright (c) 1991 by AT&T. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ /* function protypes, for snarfing... char* shm_create(char*, mode_t, int); char* shm_open(char*); int shm_close(char*); */ #include #include #include #include #define BAD (char *)(-1) /* returns 0 on failure */ char* shm_create(char* name, mode_t mode, int nbytes) { int shmid; char* shm = 0; shmid = shmget(IPC_PRIVATE, 128+nbytes, mode|IPC_CREAT); if (shmid >= 0) { shm = (char *)shmat(shmid, 0, 0); if (shm == BAD) { fprintf(stderr,"couldn't attach %d\n",shmid); shm = 0; } else { sprintf(shm, "%d %.100s%c\n", shmid, name, 0); } } return(shm); } /* returns 0 on failure */ char* shm_open(char* id) { int shmid, n; char* shm = 0; n = sscanf(id, "%d", &shmid); if (n == 1) { shm = (char*)shmat(shmid, 0, SHM_RDONLY); if (shm == BAD || shm == 0) { shm = 0; } else { /* finally, check that shm matches id */ n = strlen(id); if (id[n-1] == '\n') n--; if (strncmp(shm, id, n) != 0) shm = 0; } } return(shm); } /* returns non-0 on failure */ int shm_close(char* shm) { return( shmdt(shm) ); }