Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #define exit_on_error(s,m) if (s<0) { perror(m); exit(1);}
- #define exit_on_null(s,m) if (arrayUtilizadores<0) { perror(m); exit(1);}
- typedef struct {
- int user_id;
- char password[100];
- char username[100];
- int ano;
- } Utilizador;
- 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 lerLastID();
- int iniciar_memoria(){
- int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
- exit_on_error(id, "shmget");
- Utilizador* arrayUtilizadores= (Utilizador *)shmat(id, 0, 0);
- exit_on_null(arrayUtilizadores, "shmget");
- int i;
- for(i=0; i<20; i++){
- arrayUtilizadores[i].user_id= -1;
- strcpy(arrayUtilizadores[i].username, "[sem nada]");
- strcpy(arrayUtilizadores[i].password, "[sem nada]");
- arrayUtilizadores[i].ano =0;
- }
- }
- int main(){
- iniciar_memoria();
- 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 ) listar_utilizadores ();
- 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];
- int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
- exit_on_error(id, "shmget");
- Utilizador* arrayUtilizadores= (Utilizador *)shmat(id, 0, 0);
- exit_on_null(arrayUtilizadores, "shmget");
- 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 listar_utilizadores(){
- int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
- exit_on_error(id, "shmget");
- Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
- exit_on_null(arrayUtilizadores, "shmget");
- int i;
- if (n_utilizadores != 0 ){
- printf("\nLista de Utilizadores: \n\n");
- for (i = 0; i < 20; i++){
- printf ("%d %s %s %4d\n", arrayUtilizadores[i].user_id, arrayUtilizadores[i].username, arrayUtilizadores[i].password, arrayUtilizadores[i].ano);
- }
- printf("\n*******FIM DE LISTA*******\n\n");
- }
- else printf ("\nA lista está vazia.\n\n");
- }
- int gravarLastID(){
- FILE *f=fopen ("lastId.dat", "w");
- 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(){
- int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
- exit_on_error(id, "shmget");
- Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
- exit_on_null(arrayUtilizadores, "shmget");
- 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 ler(){
- int id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
- exit_on_error(id, "shmget");
- Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
- exit_on_null(arrayUtilizadores, "shmget");
- 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 id = shmget(7102, 20* sizeof(Utilizador), IPC_CREAT | 0666);
- exit_on_error(id, "shmget");
- Utilizador* arrayUtilizadores = (Utilizador *)shmat(id, 0, 0);
- exit_on_null(arrayUtilizadores, "shmget");
- 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];
- //nao elimina o i+1
- index++;
- }
- 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);
- }
Add Comment
Please, Sign In to add comment