Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <string.h>
- using namespace std;
- bool checkChars(char arg[])
- {
- for(unsigned int i = 0; i < strlen(arg); i++)
- if(!strchr("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", arg[i]))
- return false;
- return true;
- }
- bool isOkUser(char arg[])
- {
- if(checkChars(arg) == false)
- return false;
- return true;
- }
- bool isOkPass(char arg[])
- {
- if(strlen(arg) < 3)
- {
- cout<<"Password is to short!";
- return false;
- }
- else
- {
- char aux[250];
- strcpy(aux, arg);
- for(int unsigned i = 0; i < strlen(aux); i++)
- aux[i] = tolower(aux[i]);
- if(strcmp(aux, arg) == 0)
- {
- cout<<"Password is insecure!";
- return false;
- }
- }
- return true;
- }
- bool isOkEmail(char arg[])
- {
- bool isAt = false, isDot = false;
- char leftAux[250], rightAux[250], aux[250];
- for (int unsigned i = 0; i < strlen(arg); i++)
- if(arg[i] == '@')
- isAt = true;
- else
- {
- leftAux[i] = arg[i];
- leftAux[i+1] = '\0';
- }
- if(isAt == false || strlen(leftAux) < 1 || checkChars(leftAux) == false)
- return false;
- strcpy(rightAux, strstr(arg, "@"));
- strcpy(rightAux, rightAux + 1);
- strcpy(aux, rightAux);
- leftAux[0] = rightAux[0] = '\0';
- for(int unsigned i = 0; i < strlen(aux); i++)
- if(aux[i] == '.')
- isDot = true ;
- else
- {
- leftAux[i] = aux[i];
- leftAux[i+1] = '\0';
- }
- if(isDot == false || strlen(leftAux) < 1 || checkChars(leftAux) == false)
- return false;
- strcpy(rightAux, strstr(aux, "."));
- strcpy(rightAux, rightAux + 1);
- if(strlen(rightAux) < 2 || checkChars(rightAux) == false)
- return false;
- return true;
- }
- int main(int argc, char *argv[])
- {
- int option;
- string user, password, email;
- bool u = false, p = false, e = false;
- while ((option = getopt (argc, argv, "u:p:e:")) != -1)
- switch (option)
- {
- case 'u':
- if(isOkUser(optarg) == false)
- {
- cout<<"Username is not valid!";
- return 1;
- }
- user = optarg;
- u = true;
- break;
- case 'p':
- if(isOkPass(optarg) == false)
- return 1;
- password = optarg;
- p = true;
- break;
- case 'e':
- if(isOkEmail(optarg) == false)
- {
- cout<<"E-mail is not valid!";
- return 1;
- }
- email = optarg;
- e = true;
- break;
- }
- if(u == false)
- {
- cout<<"Argument -u is mandatory";
- return 1;
- }
- if(e == true && p == true)
- cout<<"User "<<user<<" has the email "<<email<<" and the password "<<password;
- else if(p == false)
- cout<<"User "<<user<<" has email "<<email;
- return 0;
- }
Add Comment
Please, Sign In to add comment