Advertisement
Guest User

TGB2

a guest
Nov 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. //
  2. // main.c
  3. // TGB2
  4. //
  5. // Created by Gabriel Schütz and Vinicius Müller on 16.11.17.
  6. // Copyright © 2017 Gabriel Schütz and Vinicius Müller. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <strings.h>
  11. #include <time.h>
  12. #include <locale.h>
  13.  
  14.  
  15.  
  16. typedef struct elemento {
  17. char dado[50];
  18. struct elemento *proximo;
  19. } elemento;
  20.  
  21. elemento *topo = NULL;
  22. elemento *alocar = NULL;
  23.  
  24. int push(char *dado) {
  25. alocar = (struct elemento *) malloc(sizeof(struct elemento));
  26.  
  27. if (alocar == NULL) {
  28. printf("Falta de memória\n");
  29. exit(0);
  30. }
  31.  
  32. sprintf(alocar->dado,"%s",dado);
  33.  
  34. if (topo == NULL) {
  35. topo = alocar;
  36. topo->proximo = NULL;
  37. }
  38. else
  39. {
  40. alocar->proximo = topo;
  41. topo = alocar;
  42. }
  43. }
  44.  
  45. int logar(char * usu, char * sen){
  46. FILE *pArq;
  47. if((pArq = fopen("db.txt", "r")) == NULL){
  48. return 0;
  49. }
  50. char user[100], pass[100];
  51. while(fgetc(pArq) != EOF){
  52. fseek(pArq, -1, SEEK_CUR );
  53. fscanf(pArq, "%[^,], %s\n", user, pass);
  54. if (strcmp(usu, user) == 0){
  55. if (strcmp(pass, sen) == 0){
  56. return 1;
  57. }
  58. }
  59. }
  60. return 0;
  61. }
  62.  
  63. void mostrar(void)
  64. {
  65. elemento *aux = topo;
  66. while(aux)
  67. {
  68. printf("%s\n", aux->dado);
  69. aux = aux->proximo;
  70. }
  71.  
  72. }
  73.  
  74. int main(int argc, const char * argv[]) {
  75. printf("Bem-vindo. Digite:\n1 - Para entrar.\n2 - Para mostrar usuários autenticados.\n");
  76. char a, usuario[100], senha[30];
  77. while(1)
  78. {
  79. a = getchar();
  80. fflush(stdin);
  81. switch (a)
  82. {
  83. case '1':
  84. printf("Entrar\nUsuario: ");
  85. fflush(stdin);
  86. gets(usuario);
  87. printf("senha: ");
  88. fflush(stdin);
  89. gets(senha);
  90. if(logar(usuario, senha) == 1)
  91. {
  92. time_t t = time(NULL);
  93. time(&t);
  94. struct tm *timeinfo = localtime (&t);
  95. sprintf(usuario,"%s %02d/%02d/%02d %02d:%02d\n", usuario,timeinfo->tm_mday,
  96. timeinfo->tm_mon+1, (timeinfo->tm_year - 100), timeinfo->tm_hour, timeinfo->tm_min);
  97. push(usuario);
  98. printf("Usuário autenticado com sucesso \n");
  99.  
  100. }else
  101. printf("ACESSO NEGADO\n");
  102. printf("Digite:\n1 - Para entrar.\n2 - Para mostrar usuários autenticados.\n");
  103. break;
  104. case '2':
  105. printf("Mostrar\n");
  106.  
  107. mostrar();
  108. printf("Digite:\n1 - Para entrar.\n2 - Para mostrar usuários autenticados.\n");
  109. default:
  110. break;
  111. }
  112. }
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement