diff --git a/pw_rand.c b/pw_rand.c index 3215196..b10c375 100644 --- a/pw_rand.c +++ b/pw_rand.c @@ -57,12 +57,14 @@ void pw_rand(char *buf, int size, int pw_flags, char *remove) if (pw_flags & PW_UPPERS) { len += strlen(pw_uppers); } - len += strlen(pw_lowers); + if (pw_flags & PW_LOWERS) { + len += strlen(pw_lowers); + } if (pw_flags & PW_SYMBOLS) { len += strlen(pw_symbols); } - chars = malloc(len+1); - if (!chars) { + chars = malloc(len+1); + if (!chars) { fprintf(stderr, "Couldn't malloc pw_rand buffer.\n"); exit(1); } @@ -75,8 +77,10 @@ void pw_rand(char *buf, int size, int pw_flags, char *remove) strcpy(wchars, pw_uppers); wchars += strlen(pw_uppers); } - strcpy(wchars, pw_lowers); - wchars += strlen(pw_lowers); + if (pw_flags & PW_LOWERS) { + strcpy(wchars, pw_lowers); + wchars += strlen(pw_lowers); + } if (pw_flags & PW_SYMBOLS) { strcpy(wchars, pw_symbols); } @@ -99,6 +103,13 @@ void pw_rand(char *buf, int size, int pw_flags, char *remove) "the valid set\n"); exit(1); } + if ((pw_flags & PW_LOWERS) && + !find_chars(chars, pw_uppers)) { + fprintf(stderr, + "Error: No lower case letters left in " + "the valid set\n"); + exit(1); + } if ((pw_flags & PW_SYMBOLS) && !find_chars(chars, pw_symbols)) { fprintf(stderr, diff --git a/pwgen.c b/pwgen.c index c05b8de..053cf07 100644 --- a/pwgen.c +++ b/pwgen.c @@ -32,6 +32,7 @@ int do_columns = 0; struct option pwgen_options[] = { { "alt-phonics", no_argument, 0, 'a' }, { "capitalize", no_argument, 0, 'c' }, + { "digital", no_argument, 0, 'd' }, { "numerals", no_argument, 0, 'n'}, { "symbols", no_argument, 0, 'y'}, { "num-passwords", required_argument, 0, 'N'}, @@ -47,7 +48,7 @@ struct option pwgen_options[] = { }; #endif -const char *pw_options = "01AaBCcnN:sr:hH:vy"; +const char *pw_options = "01AaBCcdnN:sr:hH:vy"; static void usage(void) { @@ -56,6 +57,8 @@ static void usage(void) fputs(" -c or --capitalize\n", stderr); fputs("\tInclude at least one capital letter in the password\n", stderr); + fputs(" -d or --digital\n", stderr); + fputs("\tUse only digits to generate password\n", stderr); fputs(" -A or --no-capitalize\n", stderr); fputs("\tDon't include capital letters in the password\n", stderr); @@ -103,7 +106,7 @@ int main(int argc, char **argv) pw_number = pw_random_number; if (isatty(1)) do_columns = 1; - pwgen_flags |= PW_DIGITS | PW_UPPERS; + pwgen_flags |= PW_DIGITS | PW_UPPERS | PW_LOWERS; while (1) { #ifdef HAVE_GETOPT_LONG @@ -128,6 +131,10 @@ int main(int argc, char **argv) case 'c': pwgen_flags |= PW_UPPERS; break; + case 'd': + pwgen = pw_rand; + pwgen_flags &= ~PW_UPPERS; + pwgen_flags &= ~PW_LOWERS; case 'n': pwgen_flags |= PW_DIGITS; break; diff --git a/pwgen.h b/pwgen.h index f408fe3..e6a158b 100644 --- a/pwgen.h +++ b/pwgen.h @@ -25,6 +25,7 @@ struct pw_element { */ #define PW_DIGITS 0x0001 /* At least one digit */ #define PW_UPPERS 0x0002 /* At least one upper letter */ +#define PW_LOWERS 0x0020 /* At least one lower letter */ #define PW_SYMBOLS 0x0004 #define PW_AMBIGUOUS 0x0008 #define PW_NO_VOWELS 0x0010