Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WITH_GETLINE
- #define _SVID_SOURCE 1
- #define _GNU_SOURCE
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<sys/types.h>
- #include<sys/ipc.h>
- #include<sys/shm.h>
- #include<errno.h>
- #include<sys/stat.h>
- #include<pwd.h>
- #include<unistd.h>
- #define MY_MSG_SIZE 101
- key_t shmkey;
- int shmid;
- struct my_data {
- int typ;
- int ile;
- int ilu_klientow;
- int stala_ile;
- int z;
- char txt[100][MY_MSG_SIZE];
- char user[100][MY_MSG_SIZE];
- } *shared_data;
- char *buf = NULL;
- char *buf2 = NULL;
- size_t bufsize = MY_MSG_SIZE;
- int i;
- int main(int argc, char * argv[]) {
- char tab[100];
- struct passwd *pw;
- uid_t uid;
- pw = getpwuid(uid = geteuid());
- if (pw == NULL)
- {
- perror("nie znalazlem euid\n");
- exit(1);
- }
- /* shmkey = ftok(argv[1], 1); */
- if ((shmkey = ftok(argv[1], 1)) == -1)
- {
- perror("ftok\n");
- exit(1);
- }
- printf("Klient skargi i wnioskow wita!\n");
- if( (shmid = shmget(shmkey, 0, 0)) == -1 ) {
- printf(" blad shmget\n");
- exit(1);
- }
- shared_data = (struct my_data *) shmat(shmid, (void *)0, 0);
- if(shared_data == (struct my_data *)-1) {
- printf(" blad shmat!\n");
- exit(1);
- }
- if(shared_data -> ile < 1)
- {
- perror("brak miejsca!\n");
- exit(1);
- }
- else
- {
- if (shared_data -> ile == 1)
- printf ("%s%d%s%d%s\n", "[Wolny ", shared_data -> ile, " wpis (na ", shared_data->stala_ile, ")]");
- else
- if (shared_data -> ile >= 2 && shared_data -> ile <= 4)
- printf ("%s%d%s%d%s\n", "[Wolne ", shared_data -> ile, " wpisy (na ", shared_data -> stala_ile, ")]");
- else
- printf ("%s%d%s%d%s\n", "[Wolnych ", shared_data -> ile, " wpisow (na ", shared_data->stala_ile, ")]");
- }
- if (shared_data -> ile >= 1)
- {
- printf("Napisz co ci lezy na watrobie:\n");
- getline(&buf, &bufsize, stdin);
- if(strlen(buf) > MY_MSG_SIZE)
- {
- perror("Za dluga wiadomosc!");
- exit(1);
- }
- else
- shared_data -> ile--;
- /* wpisywanie do pamieci dzielonej */
- shared_data->typ = 1;
- buf[strlen(buf)-1] = '\0'; /* techniczne: usuwam koniec linii */
- strcpy(tab, pw->pw_name);
- shared_data -> z++;
- shared_data -> ilu_klientow++;
- for(i = shared_data->z-1; i<shared_data->z; i++)
- {
- strcpy(shared_data -> user[i], tab);
- strcpy(shared_data -> txt[i], buf);
- }
- printf("Dziekuje za dokonanie wpisu do ksiegi\n");
- if (shmdt(shared_data)==-1)
- {
- perror("blad shmdt\n");
- exit(1);
- }
- else
- shmdt(shared_data);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement