Advertisement
Guest User

Untitled

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