Guest User

Untitled

a guest
Dec 3rd, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/sem.h>
  10. #define exit_on_error(s,m) if (s<0) { perror(m); exit(1);}
  11. #define exit_on_null(s,m) if (arrayUtilizadores<0) { perror(m); exit(1);}
  12.  
  13. typedef struct {
  14. int user_id;
  15. char password[100];
  16. char username[100];
  17. int ano;
  18. } Utilizador;
  19.  
  20.  
  21. int n_utilizadores=0;
  22. int id_control=100000;
  23.  
  24. int nop();
  25. int limpar_linha();
  26. int inserir_utilizador();
  27. int listar_utilizadores();
  28. int gravar();
  29. int ler();
  30. int apagar();
  31. int terminaServidor();
  32. int lerLastID();
  33.  
  34. int iniciar_memoria(){
  35. int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
  36. exit_on_error(id, "shmget");
  37.  
  38. Utilizador* arrayUtilizadores= (Utilizador *)shmat(id, 0, 0);
  39. exit_on_null(arrayUtilizadores, "shmget");
  40.  
  41. int i;
  42. for(i=0; i<20; i++){
  43. arrayUtilizadores[i].user_id= -1;
  44. strcpy(arrayUtilizadores[i].username, "[sem nada]");
  45. strcpy(arrayUtilizadores[i].password, "[sem nada]");
  46. arrayUtilizadores[i].ano =0;
  47.  
  48. }
  49. }
  50.  
  51. int main(){
  52. iniciar_memoria();
  53. ler();
  54. int opcao;
  55. do { printf ("MENU: \n");
  56. printf ("1. Criar um novo utilizadores\n" );
  57. printf ("2. Listar Utilizadores\n" );
  58. printf ("3. Apagar utilizadores\n" );
  59. printf ("4. Terminar servidor\n" );
  60. printf ("0. Sair\n" );
  61. printf ("Selecione a opção: ");
  62. scanf ("%d", &opcao );
  63. limpar_linha();
  64. if ( opcao == 1 ) inserir_utilizador (&id_control);
  65. if ( opcao == 2 ) listar_utilizadores ();
  66. if ( opcao == 3 ) apagar();
  67. if ( opcao == 4 ) terminaServidor();
  68. } while (opcao != 0 );
  69. gravar();
  70.  
  71. }
  72.  
  73. int terminaServidor() {
  74. int p;
  75. char linha [100];
  76. FILE *f = fopen ("servidor.lock", "r");
  77. if (f==NULL) {perror ("Não Existem Ficheiros: "); exit(1); };
  78. while ( fgets (linha, 100, f )!=NULL){
  79. p=atoi(linha);
  80. }
  81. fclose(f);
  82. kill(p, SIGUSR1);
  83.  
  84. remove("servidor.lock");
  85. printf("Servidor %d terminado e Ficheiro removido\n", p);
  86. }
  87.  
  88. int limpar_linha(){
  89. while(fgetc(stdin) != '\n');
  90. }
  91.  
  92. int inserir_utilizador (int *user_id) {
  93. int ano, i, existe;
  94. existe = 0;
  95. char temp [100];
  96. int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
  97. exit_on_error(id, "shmget");
  98.  
  99. Utilizador* arrayUtilizadores= (Utilizador *)shmat(id, 0, 0);
  100. exit_on_null(arrayUtilizadores, "shmget");
  101.  
  102.  
  103.  
  104. while (existe != 2){
  105. printf("Username:");
  106. fgets (temp, 100, stdin);
  107. temp [strlen(temp)-1] = '\0';
  108.  
  109. for (i=0; i<n_utilizadores; i++){
  110. if (strcmp(temp,arrayUtilizadores[i].username)==0)
  111. existe = 1;
  112. }
  113. if (existe == 1){
  114. printf("O utilizador %s já existe. \n Por favor indique outro:\n", temp);
  115. existe =0;
  116. } else {
  117. *user_id = *user_id+1;
  118. printf ("Ano de Nascimento: " );
  119. scanf ("%4d", &ano );
  120. limpar_linha();
  121.  
  122. arrayUtilizadores[n_utilizadores].user_id = *user_id;
  123. arrayUtilizadores[n_utilizadores].ano = ano;
  124. strcpy ( arrayUtilizadores[n_utilizadores].username, temp );
  125.  
  126. printf ("Password: ");
  127. fgets (temp, 100, stdin);
  128.  
  129. strcpy (arrayUtilizadores[n_utilizadores].password, temp );
  130. arrayUtilizadores[n_utilizadores].password
  131. [strlen(arrayUtilizadores[n_utilizadores].password) -1] = '\0';
  132. existe=2;
  133. n_utilizadores++;
  134. printf("O utilizador %s foi inserido com Sucesso.\n", arrayUtilizadores[n_utilizadores-1].username);
  135. }
  136. }
  137. existe=0;
  138.  
  139. }
  140.  
  141. int listar_utilizadores(){
  142. int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
  143. exit_on_error(id, "shmget");
  144.  
  145. Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
  146. exit_on_null(arrayUtilizadores, "shmget");
  147.  
  148. int i;
  149.  
  150. if (n_utilizadores != 0 ){
  151. printf("\nLista de Utilizadores: \n\n");
  152.  
  153. for (i = 0; i < 20; i++){
  154. printf ("%d %s %s %4d\n", arrayUtilizadores[i].user_id, arrayUtilizadores[i].username, arrayUtilizadores[i].password, arrayUtilizadores[i].ano);
  155. }
  156. printf("\n*******FIM DE LISTA*******\n\n");
  157. }
  158. else printf ("\nA lista está vazia.\n\n");
  159. }
  160.  
  161. int gravarLastID(){
  162. FILE *f=fopen ("lastId.dat", "w");
  163. FILE *f=fopen ("lastId.dat", "w");
  164. if (f==NULL) { perror ("Erro de escrita: "); exit(1);};
  165. int lastID=id_control;
  166. fwrite (&lastID, sizeof(int), 1,f);
  167. fclose(f);
  168. }
  169.  
  170. int gravar(){
  171. int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
  172. exit_on_error(id, "shmget");
  173.  
  174. Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
  175. exit_on_null(arrayUtilizadores, "shmget");
  176.  
  177. FILE *f = fopen ("utilizadores.dat", "w");
  178. if (f==NULL) { perror ("Erro de escrita: "); exit(1);};
  179. int i;
  180. for (i=0; (i<n_utilizadores) && (arrayUtilizadores[i].user_id != -1); i++){
  181. Utilizador a = arrayUtilizadores[i];
  182. fwrite (&a, sizeof(Utilizador), 1, f);
  183. }
  184. fclose(f);
  185. gravarLastID();
  186. }
  187.  
  188.  
  189. int ler(){
  190. int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
  191. exit_on_error(id, "shmget");
  192.  
  193. Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
  194. exit_on_null(arrayUtilizadores, "shmget");
  195.  
  196. FILE *f = fopen ("utilizadores.dat", "r");
  197. if (f==NULL) {f=fopen ("utilizadores.dat", "w"); };
  198. Utilizador a;
  199. while ( fread (&a, sizeof(a), 1, f) > 0){
  200. arrayUtilizadores[n_utilizadores]= a;
  201. n_utilizadores++;
  202. }
  203. fclose(f);
  204. lerLastID();
  205.  
  206. }
  207.  
  208. int lerLastID(){
  209.  
  210. FILE *f=fopen("lastId.dat", "r");
  211. if (f==NULL) {f=fopen ("lastId.dat", "w");};
  212. int lastId;
  213.  
  214. while (fread (&lastId, sizeof(int), 1, f) > 0){
  215. id_control=lastId;
  216. }
  217. fclose(f);
  218. }
  219.  
  220. int apagar(){
  221. int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
  222. exit_on_error(id, "shmget");
  223.  
  224. Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
  225. exit_on_null(arrayUtilizadores, "shmget");
  226.  
  227. int i, index, existe;
  228. existe = 0;
  229. char username[100];
  230. printf ("Insira Username que pretende apagar: ");
  231. fgets (username, 100, stdin);
  232. username [strlen(username) -1] = '\0';
  233.  
  234. for (i=0; (i<n_utilizadores) && (arrayUtilizadores[i].username != "(vazio)"); i++){
  235. if (strcmp(username, arrayUtilizadores[i].username)==0){
  236. index = i;
  237. existe = 1;
  238.  
  239. while (index<n_utilizadores){
  240. arrayUtilizadores[i]=arrayUtilizadores[i+1];
  241. //nao elimina o i+1
  242. index++;
  243. }
  244.  
  245. n_utilizadores = n_utilizadores - 1;
  246. printf("\nUtilizador %s eliminado com sucesso.\n\n", username);
  247. }
  248. }
  249. if (existe == 0)
  250. printf("\nO utilizador %s não existe.\nEscolha a opção pretendida no Menu.\n\n", username);
  251. }
Add Comment
Please, Sign In to add comment