main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "main.h"
  2. #include "argument.h"
  3. #include <stdio.h>
  4. char *key = NULL;
  5. char *name = NULL;
  6. struct arg_define arg[] = {
  7. {.ch='v', .name="version", .flat='v', .argument=no_argument},
  8. {.ch='h', .name="help", .flat='h', .argument=no_argument},
  9. {.ch='s', .name="set-pw", .flat='s', .argument=no_argument},
  10. {.ch='g', .name="get-pw", .flat='g', .argument=no_argument},
  11. {.ch=0},
  12. };
  13. enum {
  14. no = 0,
  15. set_pw,
  16. get_pw,
  17. } work = no;
  18. void printVersion(void);
  19. void printHelp(void);
  20. bool setPassWd(void);
  21. bool getPassWd(void);
  22. int main(int argc, char **argv) {
  23. name = argv[0];
  24. randomInit();
  25. initOpt(true, argc, argv, arg);
  26. for (getOpt(); opt_flat != 0; getOpt()) {
  27. switch (opt_flat) {
  28. case 'h':
  29. printHelp();
  30. exit(EXIT_SUCCESS);
  31. case 'v':
  32. printVersion();
  33. exit(EXIT_SUCCESS);
  34. case 's':
  35. if (work != no)
  36. exit(EXIT_FAILURE);
  37. work = set_pw;
  38. break;
  39. case 'g':
  40. if (work != no)
  41. exit(EXIT_FAILURE);
  42. work = get_pw;
  43. break;
  44. case 0:
  45. break;
  46. case '?':
  47. default:
  48. exit(EXIT_FAILURE);
  49. }
  50. }
  51. if (work == no) {
  52. fprintf(stderr, "What should I do?\n");
  53. printHelp();
  54. exit(EXIT_FAILURE);
  55. }
  56. if (argc - opt_i > 1)
  57. exit(EXIT_FAILURE);
  58. else if (argc - opt_i == 1) {
  59. if (key != NULL)
  60. exit(EXIT_FAILURE);
  61. key = calloc(strlen(argv[opt_i]) + 1, sizeof(char ));
  62. strcpy(key, argv[opt_i]);
  63. opt_i++;
  64. } else if (key == NULL) {
  65. printf("Please Enter The Key:\n");
  66. key = calloc(KEY_MAX_LEN + 10, sizeof(char ));
  67. fgets(key, KEY_MAX_LEN + 10, stdin);
  68. if (strchr(key, '\n') == NULL) {
  69. fprintf(stderr, "Key too long for stdin.\n");
  70. exit(EXIT_FAILURE);
  71. }
  72. }
  73. if (!isLegalKey(key))
  74. exit(EXIT_FAILURE);
  75. initBase64(key);
  76. bool status = false;
  77. if (work == set_pw)
  78. status = setPassWd();
  79. else
  80. status = getPassWd();
  81. free(key);
  82. if (!status)
  83. return EXIT_FAILURE;
  84. return EXIT_SUCCESS;
  85. }
  86. void printVersion(void) {
  87. printf("%s Version:\n%s\n%s\n", name, VERSION, VERSION_INFO);
  88. }
  89. void printHelp(void) {
  90. printf("Usage: %s <[option]..>\n", name);
  91. printf("Option: \n");
  92. printf(" -v --version Show version.\n");
  93. printf(" -h --help Show help.\n");
  94. printf(" -s --set-pw Set Password.\n");
  95. printf(" -g --get-pw Get Password.\n\n");
  96. printf("How to use?\n");
  97. printf( " You can choose a key and remember it. \n"
  98. "With set-pw, a tag is generated after you \n"
  99. "enter your key and the account information \n"
  100. "you want to store. Using get-pw, you can enter \n"
  101. "your own key and label to obtain the stored \n"
  102. "account information.\n"
  103. " Account information includes the account, \n"
  104. "password, and remarks. The account and password \n"
  105. "can only be visible non-whitespace characters. \n"
  106. "Remarks Can be visible non-whitespace characters \n"
  107. "and Spaces. All three must be set and the total \n"
  108. "length must not exceed 100.\n\n");
  109. printf("Length limit:\n");
  110. printf("Key: [%d - %d]\n\n", KEY_MIN_LEN, KEY_MAX_LEN);
  111. printf("Name origin:\n");
  112. printf("I'm super Huan. H in h-password is Huan in superhuan. \n"
  113. "Password means that the software is password software.\n\n");
  114. }
  115. #define READ_WORD(key, len, key_name, re) do { printf("Please Enter The " key_name ":"); \
  116. (key) = calloc((len) + 10, sizeof(char )); \
  117. fgets((key), (len) + 10, stdin); \
  118. if (strchr((key), '\n') == NULL) { \
  119. fprintf(stderr, key_name " too long for stdin.\n"); \
  120. goto re; \
  121. } else {*strchr((key), '\n') = 0;} \
  122. }while(0)
  123. bool setPassWd(void) {
  124. char *account = NULL;
  125. char *passwd = NULL;
  126. char *note = NULL;
  127. char *passwd_str = NULL;
  128. READ_WORD(account, 100, "Your account", ERROR1);
  129. READ_WORD(passwd, 100, "Your password", ERROR2);
  130. if (*passwd == 0) { // 未输入密码内容
  131. free(passwd);
  132. passwd = randomPasswd(); // 随机密码
  133. printf("random password: '%s'\n", passwd);
  134. }
  135. READ_WORD(note, 100, "Your note", ERROR3);
  136. passwd_str = makePasswordString(account, passwd, note);
  137. if (passwd_str == NULL)
  138. goto ERROR4;
  139. printPasswdStr(account, passwd, note, passwd_str);
  140. free(account);
  141. free(passwd);
  142. free(note);
  143. free(passwd_str);
  144. return true;
  145. ERROR4: free(note);
  146. ERROR3: free(passwd);
  147. ERROR2: free(account);
  148. ERROR1: return false;
  149. }
  150. bool getPassWd(void) {
  151. char *account = NULL;
  152. char *passwd = NULL;
  153. char *note = NULL;
  154. char *passwd_str = NULL;
  155. READ_WORD(passwd_str, 200, "Your Label", ERROR1);
  156. if (!getInfoFromPasswordString(passwd_str, &account, &passwd, &note))
  157. goto ERROR2;
  158. printInfo(account, passwd, note);
  159. free(account);
  160. free(passwd);
  161. free(note);
  162. free(passwd_str);
  163. return true;
  164. ERROR2: free(passwd_str);
  165. ERROR1: return false;
  166. }