瀏覽代碼

feat: hpd文件可以设置密钥提示

SongZihuan 3 年之前
父節點
當前提交
b9084eb07b
共有 4 個文件被更改,包括 125 次插入13 次删除
  1. 2 2
      argument.c
  2. 80 11
      main.c
  3. 2 0
      main.h
  4. 41 0
      passwd_file.c

+ 2 - 2
argument.c

@@ -42,13 +42,13 @@ static int getReturn(struct arg_define *arg) {
         case no_argument:
             return arg->flat;
         case can_argument:
-            if (!exchange_one && (status != continue_flat || argv_[opt_i][continue_index + 1] == '\0') && opt_i + 1 < argc_) {
+            if (!exchange_one && (status != continue_flat || argv_[opt_i][continue_index + 1] == '\0') && opt_i + 1 < argc_ && argv_[opt_i + 1][0] != '-') {
                 opt_val = argv_[opt_i + 1];
             } else
                 opt_val = NULL;
             return arg->flat;
         case must_argument:
-            if (!exchange_one && (status != continue_flat && argv_[opt_i][continue_index + 1] == '\0') || opt_i + 1 >= argc_) {
+            if (!exchange_one && (status != continue_flat && argv_[opt_i][continue_index + 1] == '\0') || opt_i + 1 >= argc_ && argv_[opt_i + 1][0] != '-') {
                 fprintf(stderr, "-%c --%s : Lack of argument.\n", arg->ch, arg->name);
                 return '?';
             }

+ 80 - 11
main.c

@@ -8,15 +8,21 @@ char *key = NULL;
 char *key = INCLUDE_KEY;
 #endif
 
+#define TIPS_SIZE (30)
+
 char *name = NULL;
 struct arg_define arg[] = {
         {.ch='v', .name="version", .flat='v', .argument=no_argument},
         {.ch='h', .name="help", .flat='h', .argument=no_argument},
         {.ch='s', .name="set-pw", .flat='s', .argument=no_argument},
         {.ch='g', .name="get-pw", .flat='g', .argument=no_argument},
-        {.ch='i', .name="in-file", .flat='i', .argument=no_argument},
 #ifdef INCLUDE_KEY
         {.ch='c', .name="check-key", .flat='c', .argument=must_argument},
+#else
+        {.ch='i', .name="in-file", .flat='i', .argument=no_argument},
+        {.ch='t', .name="tips", .flat='t', .argument=no_argument},
+        {.ch='?', .name="set-tips", .flat='w', .argument=no_argument},
+        {.ch='p', .name="print-label", .flat='p', .argument=no_argument},
 #endif
         {.ch=0},
 };
@@ -26,13 +32,18 @@ enum {
     get_pw,
 } work = no;
 bool in_file = false;  // 是否在文件中工作
+bool print_passwd = false;  // 是否打印content
 
 void printVersion(void);
 void printHelp(void);
 bool setPassWd(void);
 bool getPassWd(void);
+char *getTipsFromStdin(void);
 
 int main(int argc, char **argv) {
+    bool print_tips = false;
+    bool set_tips = false;
+
     printf("Welecome to use H-Password.\n");
 #ifdef INCLUDE_KEY
     printf("Exclusive custom user: %s\n", UserName);
@@ -71,6 +82,19 @@ int main(int argc, char **argv) {
             case 'i':
                 in_file = true;
                 break;
+            case 't':
+#ifdef INCLUDE_KEY
+                printf("Tips: %s\n", KeyTips);
+#else
+                print_tips = true;
+#endif
+                break;
+            case 'w':
+                set_tips = true;
+                break;
+            case 'p':
+                print_passwd = true;
+                break;
             case 0:
                 break;
             case '?':
@@ -79,16 +103,18 @@ int main(int argc, char **argv) {
         }
     }
 
-    if (work == no) {
+    if (work == no && print_tips == false && set_tips == false && print_passwd == false) {
         what_do:
         fprintf(stderr, "What should I do?\n");
+        fflush(stderr);
         printHelp();
         exit(EXIT_FAILURE);
     }
 
-    if (argc - opt_i > 1)
+    if (argc - opt_i > 1) {
+        fprintf(stderr, "Too many argument.");
         exit(EXIT_FAILURE);
-    else if (argc - opt_i == 1) {
+    } else if (argc - opt_i == 1) {
         if (key != NULL)
             exit(EXIT_FAILURE);
         key = calloc(strlen(argv[opt_i]) + 1, sizeof(char ));
@@ -110,14 +136,32 @@ int main(int argc, char **argv) {
         exit(EXIT_FAILURE);
     initBase64(key);
 
-    if (in_file)
-        initPasswdInit("passwd.hpd");
+    if (in_file) {
+        if (!initPasswdInit("passwd.hpd")) {
+            fprintf(stderr, "Unable to open HPD file.\n");
+            exit(EXIT_FAILURE);
+        }
+        if (print_tips)
+            printFileTips();
 
-    bool status = false;
+        if (set_tips) {
+            char *tips = getTipsFromStdin();
+            setFileTips(tips);
+            free(tips);
+        }
+
+    } else if (print_tips || set_tips || print_passwd) {
+        fprintf(stderr, "The -t/-p/--set-tips option relies on the -i option.\n");
+        exit(EXIT_FAILURE);
+    }
+
+    bool status = true;
     if (work == set_pw)
         status = setPassWd();
-    else
+    else if (work == get_pw)
         status = getPassWd();
+    else if (in_file && print_passwd)
+        printContent();
 
 #ifndef INCLUDE_KEY
     free(key);
@@ -166,7 +210,8 @@ void printHelp(void) {
            "length must not exceed 100.\n\n");
 
     printf("Length limit:\n");
-    printf("Key: [%d - %d]\n\n", KEY_MIN_LEN, KEY_MAX_LEN);
+    printf("Key: [%d - %d]\n", KEY_MIN_LEN, KEY_MAX_LEN);
+    printf("Tips: [0 - %d]\n\n", TIPS_SIZE);
 
     printf("Name origin:\n");
     printf("I'm super Huan. H in h-password is Huan in superhuan. \n"
@@ -207,12 +252,16 @@ bool setPassWd(void) {
     passwd_str = makePasswordString(account, passwd, note);
     if (passwd_str == NULL)
         goto ERROR5;
-    printf("***********\n");
+
+    printf("********************\n");
+    fflush(stdout);
     printPasswdStr(account, passwd, note, passwd_str);
 
     if (in_file) {
         addConnect(in_file_name, passwd_str);
         printf("The label has been written to the file. (name: %s)\n", in_file_name);
+        if (print_passwd)
+            printContent();
     }
 
     if (in_file_name != NULL)
@@ -239,7 +288,10 @@ bool getPassWd(void) {
     char *in_file_name = NULL;
     char *passwd_str = NULL;
 
-    if (in_file) {
+    if (in_file && print_passwd)
+        printContent();
+
+    if (in_file && !print_passwd) {
         READ_WORD(in_file_name, 50, "Your Label", ERROR1);
         passwd_str = findConnect(in_file_name);
         if (passwd_str == NULL) {
@@ -265,3 +317,20 @@ bool getPassWd(void) {
     ERROR2: free(passwd_str);
     ERROR1: return false;
 }
+
+char *getTipsFromStdin(void) {
+    char *tips = calloc(TIPS_SIZE + 1, sizeof(char));
+    char *enter_flat;
+
+    printf("Enter the tips [< %d]:", TIPS_SIZE);
+    fgets(tips, TIPS_SIZE + 1, stdin);
+
+    if ((enter_flat = strchr(tips, '\n')) == NULL) {
+        fprintf(stderr, "Tips too long [> %d].\n", TIPS_SIZE);
+        free(tips);
+        return NULL;
+    }
+
+    *enter_flat = 0;
+    return tips;
+}

+ 2 - 0
main.h

@@ -48,6 +48,8 @@ char *randomPasswd(void);
 bool initPasswdInit(const char *path_);
 void addConnect(char *name, char *passwd_str);
 char *findConnect(char *name);
+void printFileTips(void);
+void setFileTips(char *tips);
 void printContent(void);
 
 #endif //H_PASSWD_MAIN_H

+ 41 - 0
passwd_file.c

@@ -8,6 +8,7 @@
 typedef uint64_t h_size_t;
 typedef char h_char;
 const char *path = NULL;  // 文件地址
+h_char *key_tips = NULL;
 
 struct Content {
     h_char *name;
@@ -49,6 +50,7 @@ static bool put_fwrite_enter(FILE *fp) {
 
 static bool readFileHead(FILE *fp, h_char *file_md5) {
     size_t ret;
+    h_size_t tips_size;
     ret = fread_str(file_md5, MD5_STR_LEN + 1, fp);  // 先读取md5码, 包括NUL
     if (ret < MD5_STR_LEN + 1)
         return false;
@@ -56,6 +58,14 @@ static bool readFileHead(FILE *fp, h_char *file_md5) {
     if (fread_size_t(&content_size, fp) != 1)
         return false;
 
+    if (fread_size_t(&tips_size, fp) != 1)
+        return false;
+
+    key_tips = calloc(tips_size, sizeof(char));
+    ret = fread_str(key_tips, tips_size, fp);
+    if (ret < tips_size)
+        return false;
+
     if (get_fread_char(fp) != '\n')
         return false;
 
@@ -64,6 +74,10 @@ static bool readFileHead(FILE *fp, h_char *file_md5) {
 
 static bool writeFileHead(FILE *fp, h_char *md5str) {
     size_t ret;
+    h_char *tips = "There isn't tips.";
+    if (key_tips != NULL)
+        tips = key_tips;
+
     ret = fwrite_str(md5str, MD5_STR_LEN + 1, fp);  // 写入NUL
     if (ret < MD5_STR_LEN + 1)
         return false;
@@ -71,6 +85,13 @@ static bool writeFileHead(FILE *fp, h_char *md5str) {
     if (fwrite_size_t(content_size, fp) != 1)
         return false;
 
+    if (fwrite_size_t(strlen(tips) + 1, fp) != 1)
+        return false;
+
+    ret = fwrite_str(tips, strlen(tips) + 1, fp);  // 写入NULL
+    if (ret < strlen(tips) + 1)
+        return false;
+
     if (!put_fwrite_enter(fp))
         return false;
 
@@ -205,8 +226,13 @@ static void atEnd(void) {  // 写入数据
 
 void printContent(void) {
     struct Content *con = content;
+    printf("********************\n");
+    printf("Print all label in file.\n");
+    printf("total: %d\n", content_size);
+    printf("--------------------\n", content_size);
     for (int i = 0; i < content_size; i++, con = con->next)
         printf("%d. name: %s, label: %s\n", i, con->name, con->passwd_str);
+    printf("********************\n");
 }
 
 bool initPasswdInit(const char *path_) {
@@ -274,3 +300,18 @@ char *findConnect(char *name) {
     }
     return NULL;
 }
+
+void printFileTips(void) {
+    if (key_tips != NULL)
+        printf("Tips in file: %s\n", key_tips);
+    else
+        printf("Tips in file: There isn't tips.");
+}
+
+void setFileTips(char *tips) {
+    if (key_tips != NULL)
+        free(key_tips);
+
+    key_tips = calloc(strlen(tips) + 1, sizeof(char));
+    strcpy(key_tips, tips);
+}