Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #define _WITH_GETLINE
  2. #define _SVID_SOURCE 1
  3. #define _GNU_SOURCE
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. #include<sys/types.h>
  8. #include<sys/ipc.h>
  9. #include<sys/shm.h>
  10. #include<errno.h>
  11. #include<sys/stat.h>
  12. #include<pwd.h>
  13. #include<unistd.h>
  14.  
  15.  
  16. #define MY_MSG_SIZE 101
  17.  
  18. key_t shmkey;
  19. int shmid;
  20. struct my_data {
  21. int typ;
  22. int ile;
  23. int ilu_klientow;
  24. int stala_ile;
  25. int z;
  26. char txt[100][MY_MSG_SIZE];
  27. char user[100][MY_MSG_SIZE];
  28. } *shared_data;
  29.  
  30. char *buf = NULL;
  31. char *buf2 = NULL;
  32. size_t bufsize = MY_MSG_SIZE;
  33. int i;
  34.  
  35. int main(int argc, char * argv[]) {
  36. char tab[100];
  37. struct passwd *pw;
  38. uid_t uid;
  39. pw = getpwuid(uid = geteuid());
  40. if (pw == NULL)
  41. {
  42. perror("nie znalazlem euid\n");
  43. exit(1);
  44. }
  45. /* shmkey = ftok(argv[1], 1); */
  46. if ((shmkey = ftok(argv[1], 1)) == -1)
  47. {
  48. perror("ftok\n");
  49. exit(1);
  50. }
  51. printf("Klient skargi i wnioskow wita!\n");
  52.  
  53.  
  54. if( (shmid = shmget(shmkey, 0, 0)) == -1 ) {
  55. printf(" blad shmget\n");
  56. exit(1);
  57. }
  58. shared_data = (struct my_data *) shmat(shmid, (void *)0, 0);
  59. if(shared_data == (struct my_data *)-1) {
  60. printf(" blad shmat!\n");
  61. exit(1);
  62. }
  63.  
  64. if(shared_data -> ile < 1)
  65. {
  66. perror("brak miejsca!\n");
  67. exit(1);
  68. }
  69. else
  70. {
  71. if (shared_data -> ile == 1)
  72. printf ("%s%d%s%d%s\n", "[Wolny ", shared_data -> ile, " wpis (na ", shared_data->stala_ile, ")]");
  73. else
  74. if (shared_data -> ile >= 2 && shared_data -> ile <= 4)
  75. printf ("%s%d%s%d%s\n", "[Wolne ", shared_data -> ile, " wpisy (na ", shared_data -> stala_ile, ")]");
  76. else
  77. printf ("%s%d%s%d%s\n", "[Wolnych ", shared_data -> ile, " wpisow (na ", shared_data->stala_ile, ")]");
  78. }
  79. if (shared_data -> ile >= 1)
  80. {
  81.  
  82. printf("Napisz co ci lezy na watrobie:\n");
  83.  
  84.  
  85. getline(&buf, &bufsize, stdin);
  86.  
  87. if(strlen(buf) > MY_MSG_SIZE)
  88. {
  89. perror("Za dluga wiadomosc!");
  90. exit(1);
  91. }
  92. else
  93. shared_data -> ile--;
  94.  
  95. /* wpisywanie do pamieci dzielonej */
  96.  
  97.  
  98. shared_data->typ = 1;
  99. buf[strlen(buf)-1] = '\0'; /* techniczne: usuwam koniec linii */
  100.  
  101. strcpy(tab, pw->pw_name);
  102. shared_data -> z++;
  103. shared_data -> ilu_klientow++;
  104. for(i = shared_data->z-1; i<shared_data->z; i++)
  105. {
  106. strcpy(shared_data -> user[i], tab);
  107. strcpy(shared_data -> txt[i], buf);
  108. }
  109. printf("Dziekuje za dokonanie wpisu do ksiegi\n");
  110.  
  111.  
  112. if (shmdt(shared_data)==-1)
  113. {
  114. perror("blad shmdt\n");
  115. exit(1);
  116. }
  117. else
  118. shmdt(shared_data);
  119. }
  120. return 0;
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement