Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<time.h>
- #include"utility.c"
- #define TRUE 1
- #define FALSE 0
- #define SUCESS 1
- #define FAIL 0
- #define SN 20
- #define SL 15
- #define SP 10
- #define SE 30
- #define ST 50
- struct Name{
- char firstName[SN];
- char lastName[SN];
- };
- typedef struct{
- struct Name identification;
- char login[SL];
- char password[SP];
- char email[SE];
- int key; //Automatically generated
- }Registration;
- /* accountDat.dat | acessDat.dat | regisDat.dat */
- static void ResetString(char *);
- static int LoginConditions(char *, FILE *);
- static int PasswordConditions(char *);
- static int NameConditions(char *);
- static int EmailConditions(char *);
- static char *CurrentTime(char *);
- static int Register(int , FILE *);
- static int Login();
- static int QtLine(FILE *, char *);
- int main()
- {
- Login();
- return 0;
- }
- int Login()
- {
- char loginTry[SL];
- char passwordTry[SP];
- char stringTime[ST];
- char login[SL];
- char password[SP];
- int loginValid, passwordValid;
- int key, solicitacion;
- int i;
- FILE *accountDat;
- FILE *acessDat;
- if((accountDat = fopen("accountDat.dat", "a+")) == NULL){
- printf("The archieve \"accountDat.dat\" can not be acessed.\n");
- return FAIL;
- }
- do{
- do{
- loginValid = TRUE;
- printf("Join us -> type firsttime in Login to make a registration!\n");
- printf("%d people make part of this!\n", QtLine(accountDat, "accountDat.dat"));
- printf("Login: ");
- scanf("%s", loginTry);
- if(!strcmp(loginTry, "firsttime")){
- solicitacion = QtLine(accountDat, "accountDat.dat") + 1;
- Register(solicitacion, accountDat);
- loginValid = FALSE;
- }
- else{
- for(i=0; i<QtLine(accountDat, "accountDat.dat"); i++){
- fscanf(accountDat, "%d - %s %[^\n]s", &key, login, password);
- if(!strcmp(loginTry, login)){
- loginValid = TRUE;
- break;
- }
- }
- }
- }while(!loginValid);
- passwordValid = TRUE;
- printf (" - Sucess\n");
- printf("Password: ");
- scanf("%s", passwordTry);
- if(strcmp(passwordTry, password));
- passwordValid = FALSE;
- }while(!passwordValid);
- printf("You are logged in!\n");
- printf("Welcome %s\n", loginTry);
- if((acessDat = fopen("acessDat.dat", "a+")) == NULL){
- printf("The archieve \"acessDat.dat\" can not be acessed.\n");
- return FAIL;
- }
- else{
- fprintf(acessDat, "Key %d - %s\n", key, CurrentTime(stringTime));
- }
- fclose(acessDat);
- fclose(accountDat);
- return SUCESS;
- }
- int Register(int solicitacion, FILE *accountDat)
- {
- srandom(time(NULL));
- char passwordConfirm[SP];
- char emailConfirm[SE];
- int captcha = random()%10000;
- int captchaConfirm;
- int regisValid;
- FILE *regisDat;
- Registration accounts; /* Necessário expandir accounts */
- accounts.key = solicitacion;
- if((accountDat = fopen("accountDat.dat", "a")) == NULL){
- printf("The archieve \"accountDat.dat\" can not be acessed.\n");
- return FAIL;
- }
- if((regisDat = fopen("regisDat.dat", "a")) == NULL){
- printf("The archieve \"regisDat.dat\" can not be acessed.\n");
- return FAIL;
- }
- do{
- regisValid = TRUE;
- printf("First Name (until 20c): ");
- scanf("%s", accounts.identification.firstName);
- if(!NameConditions(accounts.identification.firstName))
- regisValid = FALSE;
- printf("Last Name (until 20c): ");
- scanf("%s", accounts.identification.lastName);
- if(!NameConditions(accounts.identification.lastName))
- regisValid = FALSE;
- printf("Login (until 15c) [a-Z, 0-9]: ");
- scanf("%s", accounts.login);
- if(!LoginConditions(accounts.login, accountDat))
- regisValid = FALSE;
- printf("Password (until 10c) [a-Z, 0-9]: ");
- scanf("%s", accounts.password);
- if(!PasswordConditions(accounts.password))
- regisValid = FALSE;
- printf("Confirm Password: ");
- scanf("%s", passwordConfirm);
- if(strcmp(passwordConfirm, accounts.password) != 0)
- regisValid = FALSE;
- printf("E-mail (until 40c): ");
- scanf("%s", accounts.email);
- if(!EmailConditions(accounts.email))
- regisValid = FALSE;
- printf("Confirm E-mail: ");
- scanf("%s", emailConfirm);
- if(strcmp(emailConfirm, accounts.email) != 0)
- regisValid = FALSE;
- printf("CAPTCHA %d: ", captcha);
- scanf("%d", &captchaConfirm);
- if(captcha != captchaConfirm)
- regisValid = FALSE;
- if(!regisValid)
- printf("ERROR, Try again\n");
- else{
- printf("Account made, key generated: %d\n", accounts.key);
- }
- }while(!regisValid);
- fprintf(regisDat,"Key: %d\n", accounts.key);
- fprintf(regisDat,"Name: %s\n", accounts.identification.firstName);
- fprintf(regisDat,"Last Name: %s\n", accounts.identification.lastName);
- fprintf(regisDat,"Login: %s\n", accounts.login);
- fprintf(regisDat,"Password: %s\n", accounts.password);
- fprintf(regisDat,"Email: %s\n\n", accounts.email);
- fprintf(accountDat, "%d - %s %s\n", accounts.key, accounts.login, accounts.password);
- fclose(regisDat);
- fclose(accountDat);
- return SUCESS;
- }
- void ResetString(char *string)
- {
- *string = '\0';
- }
- int NameConditions(char *name)
- {
- int i;
- for(i=0; *(name+i) != '\0'; i++)
- if(i>SN)
- return FALSE;
- return TRUE;
- }
- int LoginConditions(char *login, FILE *accountDat)
- {
- int i;
- char loginExist[SL];
- char passwordExist[SP];
- int key;
- if((accountDat = fopen("accountDat.dat", "r")) == NULL){
- printf("The archieve \"accountDat.dat\" can not be acessed.\n");
- return FAIL;
- }
- rewind(accountDat);
- for(i=0; i<QtLine(accountDat, "accountDat.dat"); i++){
- fscanf(accountDat, "%d - %s %[^\n]s", &key, loginExist, passwordExist);
- if(!strcmp(loginExist, login)){
- fclose(accountDat);
- return FALSE;
- }
- }
- for(i=0; *(login+i) != '\0'; i++)
- if(!(*(login+i) >= 'a' || *(login+i) <= 'z')
- || !(*(login+i) >= 'A' || *(login+i) <= 'Z')
- || !(*(login+i) >= '0' || *(login+i) <= '9')){
- fclose(accountDat);
- return FALSE;
- }
- if(i>SL){
- fclose(accountDat);
- return FALSE;
- }
- fclose(accountDat);
- return TRUE;
- }
- int PasswordConditions(char *password)
- {
- int i;
- for(i=0; *(password+i) != '\0'; i++)
- if(!(*(password+i) >= 'a' || *(password+i) <= 'z')
- || !(*(password+i) >= 'A' || *(password+i) <= 'Z')
- || !(*(password+i) >= '0' || *(password+i) <= '9'))
- return FALSE;
- if(i>SP)
- return FALSE;
- return TRUE;
- }
- int EmailConditions(char *email)
- {
- int i;
- for(i=0; *(email+i) != '\0'; i++)
- if(i>SE)
- return FALSE;
- return TRUE;
- }
- char *CurrentTime(char *str)
- {
- time_t rawtime;
- struct tm *timeinfo;
- time (&rawtime);
- timeinfo = localtime(&rawtime);
- strcat(str, "Current LOCAL time and date: ");
- strcat(str, asctime(timeinfo));
- strcat(str, "\n");
- return str;
- }
- int QtLine(FILE *search, char *archieve)
- {
- if((search = fopen(archieve, "r")) == NULL)
- return 0;
- int line = 0;
- char newlinesearch;
- while(newlinesearch != EOF)
- {
- newlinesearch = getc(search);
- if(newlinesearch == '\n')
- line++;
- }
- fclose(search);
- return line;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement