From 5ae662b1af35bb0935dd2afd509d3fb73e39bf5d Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Thu, 2 Dec 2021 18:16:01 +1100 Subject: CRLF -> LF --- Makefile | 36 +++++------ README.md | 14 ++--- config.h | 20 +++---- passgen.c | 202 +++++++++++++++++++++++++++++++------------------------------- 4 files changed, 136 insertions(+), 136 deletions(-) diff --git a/Makefile b/Makefile index 6eb2abb..2bc2752 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,18 @@ -NAME = passgen -PREFIX = $(HOME)/.local - -CC = gcc -CFLAGS += -std=c99 -Wall -Wextra -Wshadow -Werror -pedantic - -default: $(NAME) - -$(NAME): $(NAME).c config.h - $(CC) $(CFLAGS) -o $(NAME) $(NAME).c - -install: $(NAME) - install -d $(DESTDIR)$(PREFIX)/bin/ - install -m 755 $(NAME) $(DESTDIR)$(PREFIX)/bin/ - -clean: - rm -f *.o - rm -f $(NAME) +NAME = passgen +PREFIX = $(HOME)/.local + +CC = gcc +CFLAGS += -std=c99 -Wall -Wextra -Wshadow -Werror -pedantic + +default: $(NAME) + +$(NAME): $(NAME).c config.h + $(CC) $(CFLAGS) -o $(NAME) $(NAME).c + +install: $(NAME) + install -d $(DESTDIR)$(PREFIX)/bin/ + install -m 755 $(NAME) $(DESTDIR)$(PREFIX)/bin/ + +clean: + rm -f *.o + rm -f $(NAME) diff --git a/README.md b/README.md index 570ca2e..aa29a66 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# passgen - -Just a password generator that makes [passwds.ninja](https://passwds.ninja/)-style passwords. - -Heavily inspired by a generator I used before at work, just that this time in C. (This is my first C project, please be gentle...) - -Licence: Zlib +# passgen + +Just a password generator that makes [passwds.ninja](https://passwds.ninja/)-style passwords. + +Heavily inspired by a generator I used before at work, just that this time in C. (This is my first C project, please be gentle...) + +Licence: Zlib diff --git a/config.h b/config.h index 1160580..815ef1e 100644 --- a/config.h +++ b/config.h @@ -1,10 +1,10 @@ -#define DEFAULT_GRAMMAR "Cvccvc!##" - -// For vowels: -// i, o excluded due to potentially confusing 1/l/i + 0/o -// y included as a vowel because it kinda is one -#define CLASSES \ - CLASS('v', "aeuy") \ - CLASS('c', "bcdfghkmnprstvwxz") \ - CLASS('#', "1234567890") \ - CLASS('!', "@#$%^&*_-+=()[]{}") +#define DEFAULT_GRAMMAR "Cvccvc!##" + +// For vowels: +// i, o excluded due to potentially confusing 1/l/i + 0/o +// y included as a vowel because it kinda is one +#define CLASSES \ + CLASS('v', "aeuy") \ + CLASS('c', "bcdfghkmnprstvwxz") \ + CLASS('#', "1234567890") \ + CLASS('!', "@#$%^&*_-+=()[]{}") diff --git a/passgen.c b/passgen.c index 2c560b0..d60b1f4 100644 --- a/passgen.c +++ b/passgen.c @@ -1,101 +1,101 @@ -#include "config.h" - -#include -#include -#include -#include - -#ifdef __linux__ -#include -#else -#include -#include -#endif - -#define CLASS(c, chars) \ - case c: \ - class = chars; \ - class_size = sizeof(chars)-1; \ - break; - - -int main(int argc, char *argv[]) -{ - char *grammar = DEFAULT_GRAMMAR; - int grammar_size = sizeof(DEFAULT_GRAMMAR)-1; - - if (argc == 2) { - // Take first argument as the grammar - grammar = argv[1]; - grammar_size = strlen(grammar); - } else if (argc == 4) { - // Take arguments as triplets, specials, numbers - // atoi might be scuffed but so be it (it just goes = 0 if invalid input) - int triplets = atoi(argv[1]); - int specials = atoi(argv[2]); - int numbers = atoi(argv[3]); - - if (triplets < 1) { - printf("ERROR: Cannot have less than one triplet."); - return 1; - } - - grammar_size = triplets * 3 + specials + numbers; - grammar = malloc(grammar_size + 1); - grammar[grammar_size] = 0; - - memcpy(grammar, "Cvc", 3); - for (int i = 1; i < triplets; ++i) - memcpy(grammar + (i * 3), "cvc", 3); - - memset(grammar + (triplets * 3), '!', specials); - memset(grammar + (triplets * 3) + specials, '#', numbers); - //printf("Custom: %s\n", grammar); - } - - char password[grammar_size+1]; - password[grammar_size] = 0; - -#ifndef __linux__ - // seed RNG; this isn't very good, but it's enough(?) - srand(time(NULL) + getpid() % 420 - 69); -#endif - - for (int i = 0; i < grammar_size; ++i) { - char c = grammar[i]; - - bool caps = false; - if (c >= 'A' && c <= 'Z') { - caps = true; - c += 'a' - 'A'; - } - - char *class; - int class_size = 0; - switch (c) { - CLASSES - default: - printf("ERROR: Invalid grammar character '%c'.\n", c); - if (grammar != (char*)DEFAULT_GRAMMAR) - free(grammar); - return 1; - } - - do { -#ifdef __linux__ - unsigned int r; - getrandom(&r, sizeof(r), 0); -#else - long r = rand(); -#endif - password[i] = class[r % class_size] - (caps ? 'a' - 'A' : 0); - } while (i != 0 && password[i] == password[i-1]); - } - - if (grammar != (char*)DEFAULT_GRAMMAR) - free(grammar); - - printf("%s\n", password); - - return 0; -} +#include "config.h" + +#include +#include +#include +#include + +#ifdef __linux__ +#include +#else +#include +#include +#endif + +#define CLASS(c, chars) \ + case c: \ + class = chars; \ + class_size = sizeof(chars)-1; \ + break; + + +int main(int argc, char *argv[]) +{ + char *grammar = DEFAULT_GRAMMAR; + int grammar_size = sizeof(DEFAULT_GRAMMAR)-1; + + if (argc == 2) { + // Take first argument as the grammar + grammar = argv[1]; + grammar_size = strlen(grammar); + } else if (argc == 4) { + // Take arguments as triplets, specials, numbers + // atoi might be scuffed but so be it (it just goes = 0 if invalid input) + int triplets = atoi(argv[1]); + int specials = atoi(argv[2]); + int numbers = atoi(argv[3]); + + if (triplets < 1) { + printf("ERROR: Cannot have less than one triplet."); + return 1; + } + + grammar_size = triplets * 3 + specials + numbers; + grammar = malloc(grammar_size + 1); + grammar[grammar_size] = 0; + + memcpy(grammar, "Cvc", 3); + for (int i = 1; i < triplets; ++i) + memcpy(grammar + (i * 3), "cvc", 3); + + memset(grammar + (triplets * 3), '!', specials); + memset(grammar + (triplets * 3) + specials, '#', numbers); + //printf("Custom: %s\n", grammar); + } + + char password[grammar_size+1]; + password[grammar_size] = 0; + +#ifndef __linux__ + // seed RNG; this isn't very good, but it's enough(?) + srand(time(NULL) + getpid() % 420 - 69); +#endif + + for (int i = 0; i < grammar_size; ++i) { + char c = grammar[i]; + + bool caps = false; + if (c >= 'A' && c <= 'Z') { + caps = true; + c += 'a' - 'A'; + } + + char *class; + int class_size = 0; + switch (c) { + CLASSES + default: + printf("ERROR: Invalid grammar character '%c'.\n", c); + if (grammar != (char*)DEFAULT_GRAMMAR) + free(grammar); + return 1; + } + + do { +#ifdef __linux__ + unsigned int r; + getrandom(&r, sizeof(r), 0); +#else + long r = rand(); +#endif + password[i] = class[r % class_size] - (caps ? 'a' - 'A' : 0); + } while (i != 0 && password[i] == password[i-1]); + } + + if (grammar != (char*)DEFAULT_GRAMMAR) + free(grammar); + + printf("%s\n", password); + + return 0; +} -- cgit