Advertisement
Ollivvr

Untitled

Sep 20th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.01 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<time.h>
  5. #include"utility.c"
  6.  
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define SUCESS 1
  10. #define FAIL 0
  11.  
  12. #define SN 20
  13. #define SL 15
  14. #define SP 10
  15. #define SE 30
  16. #define ST 50
  17.  
  18. struct Name{
  19.     char firstName[SN];
  20.     char lastName[SN];
  21. };
  22.  
  23. typedef struct{
  24.     struct Name identification;
  25.     char login[SL];
  26.     char password[SP];
  27.     char email[SE];
  28.     int key;    //Automatically generated
  29. }Registration;
  30.  
  31. /* accountDat.dat | acessDat.dat | regisDat.dat */
  32.  
  33. static void ResetString(char *);
  34. static int LoginConditions(char *, FILE *);
  35. static int PasswordConditions(char *);
  36. static int NameConditions(char *);
  37. static int EmailConditions(char *);
  38. static char *CurrentTime(char *);
  39. static int Register(int , FILE *);
  40. static int Login();
  41. static int QtLine(FILE *, char *);
  42.  
  43. int main()
  44. {
  45.     Login();
  46.     return 0;
  47. }
  48.  
  49. int Login()
  50. {
  51.     char loginTry[SL];
  52.     char passwordTry[SP];
  53.     char stringTime[ST];
  54.     char login[SL];
  55.     char password[SP];
  56.     int loginValid, passwordValid;
  57.     int key, solicitacion;
  58.     int i;
  59.    
  60.     FILE *accountDat;
  61.     FILE *acessDat;
  62.    
  63.     if((accountDat = fopen("accountDat.dat", "a+")) == NULL){
  64.         printf("The archieve \"accountDat.dat\" can not be acessed.\n");
  65.         return FAIL;
  66.     }
  67.    
  68.     do{
  69.         do{
  70.             loginValid = TRUE;
  71.            
  72.             printf("Join us -> type firsttime in Login to make a registration!\n");
  73.             printf("%d people make part of this!\n", QtLine(accountDat, "accountDat.dat"));
  74.    
  75.             printf("Login: ");
  76.             scanf("%s", loginTry);
  77.  
  78.             if(!strcmp(loginTry, "firsttime")){
  79.                 solicitacion = QtLine(accountDat, "accountDat.dat") + 1;
  80.                 Register(solicitacion, accountDat);
  81.                 loginValid = FALSE;
  82.             }
  83.                        
  84.             else{
  85.                 for(i=0; i<QtLine(accountDat, "accountDat.dat"); i++){
  86.                     fscanf(accountDat, "%d - %s %[^\n]s", &key, login, password);
  87.                     if(!strcmp(loginTry, login)){
  88.                         loginValid = TRUE;
  89.                         break;
  90.                     }
  91.                 }
  92.             }
  93.         }while(!loginValid);
  94.        
  95.         passwordValid = TRUE;
  96.         printf (" - Sucess\n");
  97.         printf("Password: ");
  98.         scanf("%s", passwordTry);
  99.            
  100.         if(strcmp(passwordTry, password));
  101.             passwordValid = FALSE;
  102.            
  103.    }while(!passwordValid);
  104.    
  105.    printf("You are logged in!\n");
  106.    printf("Welcome %s\n", loginTry);
  107.    
  108.    if((acessDat = fopen("acessDat.dat", "a+")) == NULL){
  109.         printf("The archieve \"acessDat.dat\" can not be acessed.\n");
  110.         return FAIL;
  111.     }
  112.     else{
  113.         fprintf(acessDat, "Key %d - %s\n", key, CurrentTime(stringTime));
  114.     }
  115.    
  116.     fclose(acessDat);
  117.     fclose(accountDat);
  118.    
  119.     return SUCESS;
  120. }
  121.  
  122. int Register(int solicitacion, FILE *accountDat)
  123. {
  124.     srandom(time(NULL));
  125.     char passwordConfirm[SP];
  126.     char emailConfirm[SE];
  127.     int captcha = random()%10000;
  128.     int captchaConfirm;
  129.     int regisValid;
  130.     FILE *regisDat;
  131.     Registration accounts; /* Necessário expandir accounts */
  132.    
  133.     accounts.key = solicitacion;
  134.    
  135.     if((accountDat = fopen("accountDat.dat", "a")) == NULL){
  136.         printf("The archieve \"accountDat.dat\" can not be acessed.\n");
  137.         return FAIL;
  138.     }
  139.    
  140.     if((regisDat = fopen("regisDat.dat", "a")) == NULL){
  141.         printf("The archieve \"regisDat.dat\" can not be acessed.\n");
  142.         return FAIL;
  143.     }
  144.    
  145.     do{
  146.         regisValid = TRUE;
  147.        
  148.         printf("First Name (until 20c): ");
  149.         scanf("%s", accounts.identification.firstName);
  150.             if(!NameConditions(accounts.identification.firstName))
  151.                 regisValid = FALSE;    
  152.         printf("Last Name (until 20c): ");
  153.         scanf("%s", accounts.identification.lastName);
  154.             if(!NameConditions(accounts.identification.lastName))
  155.                 regisValid = FALSE;
  156.         printf("Login (until 15c) [a-Z, 0-9]: ");
  157.         scanf("%s", accounts.login);
  158.             if(!LoginConditions(accounts.login, accountDat))
  159.                 regisValid = FALSE;
  160.         printf("Password (until 10c) [a-Z, 0-9]: ");
  161.         scanf("%s", accounts.password);
  162.             if(!PasswordConditions(accounts.password))
  163.                 regisValid = FALSE;
  164.         printf("Confirm Password: ");
  165.         scanf("%s", passwordConfirm);
  166.             if(strcmp(passwordConfirm, accounts.password) != 0)
  167.                 regisValid = FALSE;
  168.         printf("E-mail (until 40c): ");
  169.         scanf("%s", accounts.email);
  170.             if(!EmailConditions(accounts.email))
  171.                 regisValid = FALSE;
  172.         printf("Confirm E-mail: ");
  173.         scanf("%s", emailConfirm);
  174.             if(strcmp(emailConfirm, accounts.email) != 0)
  175.                 regisValid = FALSE;
  176.         printf("CAPTCHA %d: ", captcha);
  177.         scanf("%d", &captchaConfirm);
  178.             if(captcha != captchaConfirm)
  179.                 regisValid = FALSE;
  180.        
  181.         if(!regisValid)
  182.             printf("ERROR, Try again\n");
  183.         else{
  184.             printf("Account made, key generated: %d\n", accounts.key);
  185.         }
  186.            
  187.     }while(!regisValid);
  188.        
  189.     fprintf(regisDat,"Key: %d\n", accounts.key);
  190.     fprintf(regisDat,"Name: %s\n", accounts.identification.firstName);
  191.     fprintf(regisDat,"Last Name: %s\n", accounts.identification.lastName);
  192.     fprintf(regisDat,"Login: %s\n", accounts.login);
  193.     fprintf(regisDat,"Password: %s\n", accounts.password);
  194.     fprintf(regisDat,"Email: %s\n\n", accounts.email);
  195.        
  196.     fprintf(accountDat, "%d - %s %s\n", accounts.key, accounts.login, accounts.password);
  197.        
  198.     fclose(regisDat);
  199.     fclose(accountDat);
  200.    
  201.     return SUCESS;
  202. }
  203.  
  204. void ResetString(char *string)
  205. {
  206.     *string = '\0';
  207. }
  208.  
  209. int NameConditions(char *name)
  210. {
  211.     int i;
  212.    
  213.     for(i=0; *(name+i) != '\0'; i++)
  214.         if(i>SN)
  215.             return FALSE;
  216.    
  217.     return TRUE;
  218. }
  219.  
  220. int LoginConditions(char *login, FILE *accountDat)
  221. {
  222.     int i;
  223.     char loginExist[SL];
  224.     char passwordExist[SP];
  225.     int key;
  226.    
  227.    
  228.     if((accountDat = fopen("accountDat.dat", "r")) == NULL){
  229.         printf("The archieve \"accountDat.dat\" can not be acessed.\n");
  230.         return FAIL;
  231.     }
  232.    
  233.     rewind(accountDat);
  234.    
  235.     for(i=0; i<QtLine(accountDat, "accountDat.dat"); i++){
  236.         fscanf(accountDat, "%d - %s %[^\n]s", &key, loginExist, passwordExist);
  237.         if(!strcmp(loginExist, login)){
  238.             fclose(accountDat);
  239.             return FALSE;
  240.         }
  241.     }
  242.        
  243.     for(i=0; *(login+i) != '\0'; i++)
  244.         if(!(*(login+i) >= 'a' || *(login+i) <= 'z')
  245.         || !(*(login+i) >= 'A' || *(login+i) <= 'Z')
  246.         || !(*(login+i) >= '0' || *(login+i) <= '9')){
  247.             fclose(accountDat);
  248.             return FALSE;
  249.         }
  250.    
  251.     if(i>SL){
  252.         fclose(accountDat);
  253.         return FALSE;
  254.     }
  255.    
  256.     fclose(accountDat);
  257.     return TRUE;
  258. }
  259.  
  260. int PasswordConditions(char *password)
  261. {
  262.     int i;
  263.    
  264.     for(i=0; *(password+i) != '\0'; i++)
  265.         if(!(*(password+i) >= 'a' || *(password+i) <= 'z')
  266.         || !(*(password+i) >= 'A' || *(password+i) <= 'Z')
  267.         || !(*(password+i) >= '0' || *(password+i) <= '9'))
  268.             return FALSE;
  269.    
  270.     if(i>SP)
  271.         return FALSE;
  272.    
  273.     return TRUE;       
  274. }
  275.  
  276. int EmailConditions(char *email)
  277. {
  278.     int i;
  279.    
  280.     for(i=0; *(email+i) != '\0'; i++)
  281.         if(i>SE)
  282.             return FALSE;
  283.    
  284.     return TRUE;
  285. }
  286.  
  287. char *CurrentTime(char *str)
  288. {
  289.     time_t rawtime;
  290.     struct tm *timeinfo;
  291.    
  292.     time (&rawtime);
  293.     timeinfo = localtime(&rawtime);
  294.    
  295.     strcat(str, "Current LOCAL time and date: ");
  296.     strcat(str, asctime(timeinfo));
  297.     strcat(str, "\n");
  298.    
  299.     return str;
  300. }
  301.  
  302. int QtLine(FILE *search, char *archieve)
  303. {
  304.     if((search = fopen(archieve, "r")) == NULL)
  305.         return 0;
  306.    
  307.     int line = 0;
  308.     char newlinesearch;
  309.    
  310.     while(newlinesearch != EOF)
  311.     {  
  312.         newlinesearch = getc(search);
  313.         if(newlinesearch == '\n')
  314.             line++;
  315.     }
  316.    
  317.     fclose(search);
  318.     return line;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement