Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option -d (--digital) to generate password from digits only #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pw_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions pwgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand All @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions pwgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down