#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

struct data {
    int id;
    int num;
};


main() {
    int id, i;
    key_t key;
    char pathbuf[256], *ptr;
    int size = 0x5000; /* 20 kbytes */
    int flags = (SHM_R |SHM_W);
    struct data *daddr;

    if((ptr = getenv("HOME")) == NULL) {
	fprintf(stderr, "Cannot find a value for HOME\n");
	exit(1);
    }
    if((strncpy(pathbuf, ptr, sizeof(pathbuf))) == NULL) {
	fprintf(stderr, "String copy error\n");
	exit(1);
    }
    strcat(pathbuf, "/ipckey"); /* Assuming we have enough space in pathbuf */

    key = ftok(pathbuf, (int)'K');

    id = shmget(key, size, flags);
    if(id == -1) 
	perror("shmget:");

    
    daddr = shmat(id, 0, 0);
    if(daddr == (struct data *)-1) 
	perror("shmat:");
    
    for(i = 0; i < 10 ; i++) {
	fprintf(stdout, "%d %d\n", daddr[i].id, daddr[i].num);
    }

    
}
