"Fossies" - the Fresh Open Source Software Archive

Member "passwdqc-2.0.3/pwqgen.c" (23 Jun 2023, 1760 Bytes) of package /linux/privat/passwdqc-2.0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pwqgen.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.4.0_vs_2.0.0.

    1 /*
    2  * Copyright (c) 2008,2009 by Dmitry V. Levin
    3  * Copyright (c) 2016,2021 by Solar Designer
    4  * See LICENSE
    5  */
    6 
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <string.h>
   10 
   11 #include "passwdqc.h"
   12 
   13 static void
   14 print_help(void)
   15 {
   16     puts("Generate quality controllable passphrase.\n"
   17         "\nUsage: pwqgen [options]\n"
   18         "\nValid options are:\n"
   19         "  random=N\n"
   20         "       set size of randomly-generated passphrase in bits;\n"
   21         "  config=FILE\n"
   22         "       load config FILE in passwdqc.conf format;\n"
   23         "  --version\n"
   24         "       print program version and exit;\n"
   25         "  -h or --help\n"
   26         "       print this help text and exit.");
   27 }
   28 
   29 int main(int argc, const char **argv)
   30 {
   31     passwdqc_params_t params;
   32     char *reason, *pass;
   33     int retval;
   34 
   35     if (argc > 1 && argv[1][0] == '-') {
   36         if (!strcmp("-h", argv[1]) || !strcmp("--help", argv[1])) {
   37             print_help();
   38             return 0;
   39         }
   40 
   41         if (!strcmp("--version", argv[1])) {
   42             printf("pwqgen version %s\n", PASSWDQC_VERSION);
   43             return 0;
   44         }
   45     }
   46 
   47     passwdqc_params_reset(&params);
   48     if (argc > 1 &&
   49         passwdqc_params_parse(&params, &reason, argc - 1, argv + 1)) {
   50         fprintf(stderr, "pwqgen: %s\n",
   51             (reason ? reason : "Out of memory"));
   52         free(reason);
   53         return 1;
   54     }
   55 
   56     pass = passwdqc_random(&params.qc);
   57     passwdqc_params_free(&params);
   58     if (!pass) {
   59         fprintf(stderr, "pwqgen: Failed to generate a passphrase.\n"
   60             "This could happen for a number of reasons: you could have requested\n"
   61             "an impossible passphrase length, or the access to kernel random number\n"
   62             "pool could have failed.\n");
   63         return 1;
   64     }
   65 
   66     setvbuf(stdout, NULL, _IONBF, 0);
   67 
   68     retval = (puts(pass) >= 0 && fflush(stdout) == 0) ? 0 : 1;
   69 
   70     _passwdqc_memzero(pass, strlen(pass));
   71     free(pass);
   72 
   73     return retval;
   74 }