#include #include #include #include #include #define SHMSZ 27 #define KEY 5625 #define PERM 0600 void scrivi(void) { int indirizzo = 0; char c; int shmid; key_t key; char * shm, * s, * res; char str[80]; int i; printf("Inserisci indirizzo.\n"); scanf("%d",&indirizzo); printf("Indirizzo: %d \n",indirizzo); //key = KEY; key = (key_t)indirizzo; /* * Create the segment. */ if ((shmid = shmget(key, SHMSZ, IPC_CREAT | PERM)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char * ) - 1) { perror("shmat"); exit(1); } res = getpass("password:"); s = shm; int j = strlen(res); for (i = 0; i <= j; i++) * s++ = * res++; * s = NULL; printf("Scrittura terminata.\n"); exit(0); } void cancella(void) { int indirizzo = 0; int shmid; key_t key; char *shm, *s; struct shmid_ds * shmid_ds; int rtrn; printf("Inserisci indirizzo (x per usare il default).\n"); rtrn = scanf("%d",&indirizzo); if(rtrn!=0) { printf("Indirizzo: %d \n",indirizzo); key = (key_t)indirizzo; } else { printf("Indirizzo: %d \n",KEY); key = KEY; } /* * Locate the segment. */ if ((shmid = shmget(key, SHMSZ, PERM)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } if ((rtrn = shmctl(shmid, IPC_RMID, shmid_ds)) == -1) { perror("shmctl"); exit(1); } printf("Dati cancellati.\n"); exit(0); } main(int argc, char * argv[]) { // int i; // // printf("argc = %d\n", argc); // // for (i = 0; i < argc; i++) // printf("argv[%d] = \"%s\"\n", i, argv[i]); // // if ((argc!=2) || // ((argc==2) && (argv[1][0]!='s') && (argv[1][0]!='c'))) { // printf("Usage: shm_server [s|c]\n"); // } // else { // printf("1\n"); // if (argv[1][0] == 's') // scrivi(); // else { // printf("2\n"); // cancella(); // } // } cancella(); }