Guest User

Untitled

a guest
Oct 31st, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. bool checkChars(char arg[])
  7. {
  8. for(unsigned int i = 0; i < strlen(arg); i++)
  9. if(!strchr("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", arg[i]))
  10. return false;
  11. return true;
  12. }
  13.  
  14. bool isOkUser(char arg[])
  15. {
  16. if(checkChars(arg) == false)
  17. return false;
  18. return true;
  19. }
  20.  
  21. bool isOkPass(char arg[])
  22. {
  23. if(strlen(arg) < 3)
  24. {
  25. cout<<"Password is to short!";
  26. return false;
  27. }
  28. else
  29. {
  30. char aux[250];
  31. strcpy(aux, arg);
  32. for(int unsigned i = 0; i < strlen(aux); i++)
  33. aux[i] = tolower(aux[i]);
  34. if(strcmp(aux, arg) == 0)
  35. {
  36. cout<<"Password is insecure!";
  37. return false;
  38. }
  39. }
  40. return true;
  41. }
  42.  
  43. bool isOkEmail(char arg[])
  44. {
  45. bool isAt = false, isDot = false;
  46. char leftAux[250], rightAux[250], aux[250];
  47.  
  48. for (int unsigned i = 0; i < strlen(arg); i++)
  49. if(arg[i] == '@')
  50. isAt = true;
  51. else
  52. {
  53. leftAux[i] = arg[i];
  54. leftAux[i+1] = '\0';
  55. }
  56. if(isAt == false || strlen(leftAux) < 1 || checkChars(leftAux) == false)
  57. return false;
  58.  
  59. strcpy(rightAux, strstr(arg, "@"));
  60. strcpy(rightAux, rightAux + 1);
  61.  
  62. strcpy(aux, rightAux);
  63. leftAux[0] = rightAux[0] = '\0';
  64. for(int unsigned i = 0; i < strlen(aux); i++)
  65. if(aux[i] == '.')
  66. isDot = true ;
  67. else
  68. {
  69. leftAux[i] = aux[i];
  70. leftAux[i+1] = '\0';
  71. }
  72. if(isDot == false || strlen(leftAux) < 1 || checkChars(leftAux) == false)
  73. return false;
  74.  
  75. strcpy(rightAux, strstr(aux, "."));
  76. strcpy(rightAux, rightAux + 1);
  77. if(strlen(rightAux) < 2 || checkChars(rightAux) == false)
  78. return false;
  79. return true;
  80. }
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84. int option;
  85. string user, password, email;
  86. bool u = false, p = false, e = false;
  87.  
  88. while ((option = getopt (argc, argv, "u:p:e:")) != -1)
  89. switch (option)
  90. {
  91. case 'u':
  92. if(isOkUser(optarg) == false)
  93. {
  94. cout<<"Username is not valid!";
  95. return 1;
  96. }
  97. user = optarg;
  98. u = true;
  99. break;
  100. case 'p':
  101. if(isOkPass(optarg) == false)
  102. return 1;
  103. password = optarg;
  104. p = true;
  105. break;
  106. case 'e':
  107. if(isOkEmail(optarg) == false)
  108. {
  109. cout<<"E-mail is not valid!";
  110. return 1;
  111. }
  112. email = optarg;
  113. e = true;
  114. break;
  115. }
  116. if(u == false)
  117. {
  118. cout<<"Argument -u is mandatory";
  119. return 1;
  120. }
  121. if(e == true && p == true)
  122. cout<<"User "<<user<<" has the email "<<email<<" and the password "<<password;
  123. else if(p == false)
  124. cout<<"User "<<user<<" has email "<<email;
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment