Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "defines.h"
- typedef struct {
- int user_id;
- char password[100];
- char username[100];
- int ano;
- } Utilizador;
- Utilizador *arrayUtilizadores;
- int n_utilizadores=0;
- int id_control=100000;
- int nop();
- int limpar_linha();
- int inserir_utilizador();
- int listar_utilizadores();
- int gravar();
- int ler();
- int apagar();
- int terminaServidor();
- int semaforo();
- struct sembuf DOWN = {0, -1, 0};
- struct sembuf UP = {0, 1, 0};
- int iniciar_memoria(){
- int id = shmget (7102, 50 * sizeof(Utilizador) , IPC_CREAT | 0666 );
- exit_on_error(id, "shmget");
- semaforo();
- int sem_id = semget (7102, 50 * sizeof(Utilizador) , IPC_CREAT | 0666 );
- exit_on_error(sem_id, "semget");
- arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
- if (arrayUtilizadores == NULL) { perror("erro no attach"); exit (1);}
- int status;
- status = semop(sem_id, &DOWN, 1);
- exit_on_error( status, "DOWN");
- int i;
- //FILE *f = fopen ("utilizadores.dat", "w");
- // if (f==NULL) { perror ("Erro de escrita: "); exit(1);};
- for(i=0; i<50; i++){
- arrayUtilizadores[i].user_id = -1;
- arrayUtilizadores[i].ano = -1;
- strcpy(arrayUtilizadores[i].password, "(vazio)");
- strcpy(arrayUtilizadores[i].username, "(vazio)");
- }
- status = semop(sem_id, &UP, 1);
- exit_on_error( status, "UP");
- }
- int semaforo(){
- int id = semget (7102, 50 * sizeof(Utilizador) , IPC_CREAT | 0666 );
- exit_on_error(id, "semget");
- int status;
- status = semctl(id,0,SETVAL,1);
- exit_on_error(status, "SETVAL");
- }
- int existeMemoria(){
- int id = shmget( 7102, 50 * sizeof (Utilizador), IPC_CREAT | 0666);
- if (id<0)
- iniciar_memoria();//se este e quando nao existe memoria entao la em cima no inicilizar tenho de preencher com utilizadores.dat
- else{
- arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
- if (arrayUtilizadores == NULL) { perror("erro no attach"); exit (1);}
- }
- }
- int teste_memoria (){
- int i;
- for (i=0; i<50; i++)
- printf("Pos %d: %d %d %s %s\n", i, arrayUtilizadores[i].user_id, arrayUtilizadores[i].ano, arrayUtilizadores[i].password, arrayUtilizadores[i].username);
- }
- int main(){
- existeMemoria();
- ler();
- int opcao;
- do{ printf ("MENU: \n");
- printf ("1. Criar um novo utilizadores\n" );
- printf ("2. Listar Utilizadores\n" );
- printf ("3. Apagar utilizadores\n" );
- printf ("4. Terminar servidor\n" );
- printf ("0. Sair\n" );
- printf ("Selecione a opção: ");
- scanf ("%d", &opcao );
- limpar_linha();
- if ( opcao == 1 ) inserir_utilizador (&id_control);
- if ( opcao == 2 ) teste_memoria ();
- if ( opcao == 3 ) apagar();
- if ( opcao == 4 ) terminaServidor();
- } while (opcao != 0 );
- gravar();
- }
- int terminaServidor() {
- int p;
- char linha [100];
- FILE *f = fopen ("servidor.lock", "r");
- if (f==NULL) {perror ("Não Existem Ficheiros: "); exit(1); };
- while ( fgets (linha, 100, f )!=NULL){
- p=atoi(linha);
- }
- fclose(f);
- kill(p, SIGUSR1);
- remove("servidor.lock");
- printf("Servidor %d terminado e Ficheiro removido\n", p);
- }
- int main(){
- existeMemoria();
- ler();
- int opcao;
- do{ printf ("MENU: \n");
- printf ("1. Criar um novo utilizadores\n" );
- printf ("2. Listar Utilizadores\n" );
- printf ("3. Apagar utilizadores\n" );
- printf ("4. Terminar servidor\n" );
- printf ("0. Sair\n" );
- printf ("Selecione a opção: ");
- scanf ("%d", &opcao );
- limpar_linha();
- if ( opcao == 1 ) inserir_utilizador (&id_control);
- if ( opcao == 2 ) teste_memoria ();
- if ( opcao == 3 ) apagar();
- if ( opcao == 4 ) terminaServidor();
- } while (opcao != 0 );
- gravar();
- }
- int terminaServidor() {
- int p;
- char linha [100];
- FILE *f = fopen ("servidor.lock", "r");
- if (f==NULL) {perror ("Não Existem Ficheiros: "); exit(1); };
- while ( fgets (linha, 100, f )!=NULL){
- p=atoi(linha);
- }
- fclose(f);
- kill(p, SIGUSR1);
- remove("servidor.lock");
- printf("Servidor %d terminado e Ficheiro removido\n", p);
- }
- int limpar_linha(){
- while(fgetc(stdin) != '\n');
- }
- int inserir_utilizador (int *user_id) {
- int ano, i, existe;
- existe = 0;
- char temp [100];
- while (existe != 2){
- printf("Username:");
- fgets (temp, 100, stdin);
- temp [strlen(temp)-1] = '\0';
- for (i=0; i<n_utilizadores; i++){
- if (strcmp(temp, arrayUtilizadores[i].username)==0)
- existe = 1;
- }
- if (existe == 1){
- printf("O utilizador %s já existe. \n Por favor indique outro:\n", temp);
- existe =0;
- } else {
- *user_id = *user_id+1;
- printf ("Ano de Nascimento: " );
- scanf ("%4d", &ano );
- limpar_linha();
- arrayUtilizadores[n_utilizadores].user_id = *user_id;
- arrayUtilizadores[n_utilizadores].ano = ano;
- strcpy ( arrayUtilizadores[n_utilizadores].username, temp );
- printf ("Password: ");
- fgets (temp, 100, stdin);
- strcpy (arrayUtilizadores[n_utilizadores].password, temp );
- arrayUtilizadores[n_utilizadores].password [strlen(arrayUtilizadores[n_utilizadores].password) -1] = '\0';
- existe=2;
- n_utilizadores++;
- printf("O utilizador %s foi inserido com Sucesso.\n", arrayUtilizadores[n_utilizadores-1].username);
- }
- }
- existe=0;
- }
- int gravarLastID(){
- FILE *f=fopen ("lastId.dat", "w");
- if (f==NULL) { perror ("Erro de escrita: "); exit(1);};
- int lastID=id_control;
- fwrite (&lastID, sizeof(int), 1,f);
- fclose(f);
- }
- int gravar(){
- FILE *f = fopen ("utilizadores.dat", "w");
- if (f==NULL) { perror ("Erro de escrita: "); exit(1);};
- int i;
- for (i=0; (i<n_utilizadores) && (arrayUtilizadores[i].user_id != -1); i++){
- Utilizador a = arrayUtilizadores[i];
- fwrite (&a, sizeof(Utilizador), 1, f);
- }
- fclose(f);
- gravarLastID();
- }
- int lerLastID();
- int ler(){
- FILE *f = fopen ("utilizadores.dat", "r");
- if (f==NULL) {f=fopen ("utilizadores.dat", "w"); };
- Utilizador a;
- while ( fread (&a, sizeof(a), 1, f) > 0){
- arrayUtilizadores[n_utilizadores]= a;
- n_utilizadores++;
- }
- fclose(f);
- lerLastID();
- }
- int lerLastID(){
- FILE *f=fopen("lastId.dat", "r");
- if (f==NULL) {f=fopen ("lastId.dat", "w");};
- int lastId;
- while (fread (&lastId, sizeof(int), 1, f) > 0){
- id_control=lastId;
- }
- fclose(f);
- }
- int apagar(){
- int i, index, existe;
- existe = 0;
- char username[100];
- printf ("Insira Username que pretende apagar: ");
- fgets (username, 100, stdin);
- username [strlen(username) -1] = '\0';
- for (i=0; (i<n_utilizadores) && (arrayUtilizadores[i].username != "(vazio)"); i++){
- if (strcmp(username, arrayUtilizadores[i].username)==0){
- index = i;
- existe = 1;
- while (index<n_utilizadores){
- arrayUtilizadores[i]=arrayUtilizadores[i+1];
- index++;
- }
- arrayUtilizadores[n_utilizadores].user_id = -1;
- arrayUtilizadores[i].ano = -1;
- strcpy(arrayUtilizadores[i].password, "(vazio)");
- strcpy(arrayUtilizadores[i].username, "(vazio)");
- n_utilizadores = n_utilizadores - 1;
- printf("\nUtilizador %s eliminado com sucesso.\n\n", username);
- }
- }
- if (existe == 0)
- printf("\nO utilizador %s não existe.\nEscolha a opção pretendida no Menu.\n\n", username);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement