...
 
Commits (2)
all: fix-filename
fix-filename: fix-filename.c
gcc -Wall --std=c11 -D_POSIX_C_SOURCE=200809L -o fix-filename fix-filename.c
mrproper: clean
clean:
rm -f fix-filename fix-filename*~ fix-filename*.o
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
static const size_t maxstrlen = 8192;
int main(int argc, char *argv[])
{
int error = 0;
for (int i = 1; error == 0 && i < argc; ++i) {
char *origname = argv[i];
char *newname = strndup(origname, maxstrlen);
size_t len = strlen(newname);
for (int p = 0; p < len;) {
if (p < len - 1) {
if (newname[p] == '\xc2') {
if (newname[p + 1] == '\x94') {
newname[p] = '\xc3';
newname[p + 1] = '\xb6';
p += 2;
} else if (newname[p + 1] == '\x84') {
newname[p] = '\xc3';
newname[p + 1] = '\xa4';
p += 2;
} else
++p;
} else if (newname[p] == '+') {
if (newname[p + 1] == '\xd1') {
newname[p] = '\xc3';
newname[p + 1] = '\xa5';
p += 2;
} else if (newname[p] == '+' && newname[p + 1] == '\xf1') {
newname[p] = '\xc3';
newname[p + 1] = '\xa4';
p += 2;
} else if (newname[p] == '+' && newname[p + 1] == '\xc2') {
newname[p] = '\xc3';
newname[p + 1] = '\xb6';
p += 2;
} else
++p;
} else
++p;
} else
++p;
}
if (strncmp(newname, origname, maxstrlen) != 0) {
error = rename(origname, newname);
}
free(newname);
}
return 0;
}
/* Copyright (c) 2018, Thomas Fischer <[email protected]>
/* Copyright (c) 2018-2019, Thomas Fischer <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
......@@ -28,9 +28,18 @@
#include <stdlib.h>
#include <string.h>
static const char *binary = "01";
static const unsigned int mod_for_binary = 2;
static const size_t bits_for_binary = 1;
static const char *numbers = "0123456789";
static const unsigned int mod_for_numbers = 10;
static const size_t bits_for_numbers = 4;
static const char *hex = "0123456789ABCDEF";
static const unsigned int mod_for_hex = 16;
static const size_t bits_for_hex = 4;
static const char *alpha = "qaywsxedcrfvtgbzhnujmikolpQAYWSXEDCRFVTGBZHNUJMIKOLP";
static const unsigned int mod_for_alpha = 52;
static const size_t bits_for_alpha = 6;
static const char *alphanum = "0123456789qaywsxedcrfvtgbzhnujmikolpQAYWSXEDCRFVTGBZHNUJMIKOLP";
static const unsigned int mod_for_alphanum = 62;
static const size_t bits_for_alphanum = 6;
......@@ -43,6 +52,16 @@ static const size_t bits_for_keyboardsafe = 6;
static const unsigned int ERROR_RANDOM_BITS = 0xffff;
int num_characters = 8, count_passwords = 1;
enum CharSet {Binary, Numbers, Hex, Alpha, AlphaNum, AlphaNumSpecial, KeyboardSafe};
enum CharSet charset = KeyboardSafe;
void print_version(FILE *output)
{
fprintf(output, "makepasswd v0.2\nCopyright 2018-2019 by Thomas Fischer <[email protected]>\n");
}
/**
* Print help on how to use this program.
* Output is written to a file device as specified by
......@@ -50,12 +69,19 @@ static const unsigned int ERROR_RANDOM_BITS = 0xffff;
*/
void print_help(FILE *output)
{
fprintf(output, "makepasswd v0.1\nCopyright 2018 by Thomas Fischer <[email protected]>\n\n");
fprintf(output, "Usage: makepasswd [--help] [--length N] [--num|--alpha|--special|--keyboard]\n");
print_version(output);
fprintf(output, "\nUsage: makepasswd [--help] [--length N] [--binary|--num|--alpha|--alphanum|--special|--keyboard|--weak|--medium|--strong]\n");
fprintf(output, " --help Print this help and exit without computing a password\n");
fprintf(output, " --length N Computed password shall be N characters long\n");
fprintf(output, " --version Print version and copyright information\n");
fprintf(output, " --length N Computed password shall be N characters long (default: N=%i)\n", num_characters);
fprintf(output, " --count N Number of passwords to be generated; one per line will be printed (default: N=%i)\n", count_passwords);
fprintf(output, " --binary Computed password shall contain only digits 0 and 1\n");
fprintf(output, " --num Computed password shall contain only digits 0 to 9\n");
fprintf(output, " --alpha Computed password shall contain only digits 0 to 9\n");
fprintf(output, " --hex Computed password shall contain only digits 0 to 9\n");
fprintf(output, " and letters A to F (only upper case)\n");
fprintf(output, " --alpha Computed password shall contain only letters A to Z\n");
fprintf(output, " (both upper and lower case)\n");
fprintf(output, " --alphanum Computed password shall contain only digits 0 to 9\n");
fprintf(output, " and letters A to Z (both upper and lower case)\n");
fprintf(output, " --special Computed password shall contain only digits 0 to 9,\n");
fprintf(output, " letters A to Z (both upper and lower case),\n");
......@@ -63,7 +89,11 @@ void print_help(FILE *output)
fprintf(output, " --keyboard Computed password shall contain only characters\n");
fprintf(output, " that have the same position on English, German,\n");
fprintf(output, " and Swedish keyboard layouts, such as 1, g, and !\n");
fprintf(output, "\nArguments --num, --alpha, --special, and --keyboard are mutually exclusive.\n");
fprintf(output, " --weak Apply default settings to generate a weak password\n");
fprintf(output, " --medium Apply default settings to generate a medium password\n");
fprintf(output, " --strong Apply default settings to generate a strong password\n");
fprintf(output, "\nArguments --binary, --num, --hex, --alpha, --special, --keyboard, --weak, --medium, and --strong are mutually exclusive.\n");
fprintf(output, "Default password mode is %s.\n", (charset == Binary ? "--binary" : (charset == Numbers ? "--num" : (charset == Hex ? "--hex" : (charset == Alpha ? "--alpha" : (charset == AlphaNum ? "--alphanum" : (charset == AlphaNumSpecial ? "--special" : (charset == KeyboardSafe ? "--keyboard" : "unset"))))))));
}
/**
......@@ -74,14 +104,14 @@ void print_help(FILE *output)
*/
unsigned int get_random_bits(const size_t queried_bits)
{
/// Check input arguments for sanity
/// Check input arguments for sanity
if (queried_bits >= (sizeof(unsigned int) * 8)) return ERROR_RANDOM_BITS;
/// Declare and initialize internal buffer of random bits
/// Declare and initialize internal buffer of random bits
static unsigned int buffered_random_bits = 0;
static size_t remaining_bits = 0;
/// Check if more random bits need to be retrieved from a random device
/// Check if more random bits need to be retrieved from a random device
if (remaining_bits < queried_bits) {
FILE *devrandom = fopen("/dev/urandom", "r");
if (devrandom != NULL) {
......@@ -110,46 +140,72 @@ unsigned int get_random_bits(const size_t queried_bits)
int main(int argc, char *argv[])
{
int num_characters = 8;
enum CharSet {Numbers, AlphaNum, AlphaNumSpecial, KeyboardSafe};
enum CharSet charset = KeyboardSafe;
/// Process command line options, overwriting default values
for (int i = 1; i < argc; ++i) {
if (i < argc - 1 && strlen(argv[i]) == 7 && strncmp(argv[i], "--length", 8) == 0) {
if (i < argc - 1 && strlen(argv[i]) == 8 && strncmp(argv[i], "--length", 8) == 0) {
num_characters = atoi(argv[i + 1]);
++i;
} else if (strlen(argv[i]) > 9 && strncmp(argv[i], "--length=", 9) == 0)
num_characters = atoi(argv[i] + 9);
else if (i < argc - 1 && strlen(argv[i]) == 7 && strncmp(argv[i], "--count", 7) == 0) {
count_passwords = atoi(argv[i + 1]);
++i;
} else if (strlen(argv[i]) > 8 && strncmp(argv[i], "--count=", 8) == 0)
count_passwords = atoi(argv[i] + 8);
else if (strncmp(argv[i], "--binary", 8) == 0)
charset = Binary;
else if (strncmp(argv[i], "--num", 5) == 0)
charset = Numbers;
else if (strncmp(argv[i], "--hex", 5) == 0)
charset = Hex;
else if (strncmp(argv[i], "--alpha", 7) == 0)
charset = Alpha;
else if (strncmp(argv[i], "--alphanum", 10) == 0)
charset = AlphaNum;
else if (strncmp(argv[i], "--special", 9) == 0)
charset = AlphaNumSpecial;
else if (strncmp(argv[i], "--keyboard", 10) == 0)
charset = KeyboardSafe;
else if (strncmp(argv[i], "--help", 6) == 0) {
else if (strncmp(argv[i], "--weak", 6) == 0) {
charset = Alpha;
num_characters = 6;
} else if (strncmp(argv[i], "--medium", 8) == 0) {
charset = AlphaNum;
num_characters = 10;
} else if (strncmp(argv[i], "--strong", 8) == 0) {
charset = AlphaNumSpecial;
num_characters = 16;
} else if (strncmp(argv[i], "--help", 6) == 0) {
print_help(stdout);
return 1;
} else if (strncmp(argv[i], "--version", 9) == 0) {
print_version(stdout);
return 1;
} else {
fprintf(stderr, "Unknown or incomplete argument: %s\n", argv[i]);
return 1;
}
}
if (num_characters <= 0 || num_characters > 512) {
fprintf(stderr, "Invalid number of characters given\n");
return 1;
} else if (count_passwords <= 0 || count_passwords > 512) {
fprintf(stderr, "Invalid count of passwords given\n");
return 1;
}
const size_t bits = (charset == Numbers) ? bits_for_numbers : (charset == AlphaNum ? bits_for_alphanum : (charset == AlphaNumSpecial ? bits_for_alphanumspecial : bits_for_keyboardsafe));
const unsigned int mod = (charset == Numbers) ? mod_for_numbers : (charset == AlphaNum ? mod_for_alphanum : (charset == AlphaNumSpecial ? mod_for_alphanumspecial : mod_for_keyboardsafe));
const char *chars = (charset == Numbers) ? numbers : (charset == AlphaNum ? alphanum : (charset == AlphaNumSpecial ? alphanumspecial : keyboardsafe));
for (int i = 0; i < num_characters; ++i) {
const unsigned int r = get_random_bits(bits);
if (r == ERROR_RANDOM_BITS) break;
printf("%c", chars[r % mod]);
const size_t bits = charset == Binary ? bits_for_binary : (charset == Numbers ? bits_for_numbers : (charset == Hex ? bits_for_hex : (charset == Alpha ? bits_for_alpha : (charset == AlphaNum ? bits_for_alphanum : (charset == AlphaNumSpecial ? bits_for_alphanumspecial : bits_for_keyboardsafe)))));
const unsigned int mod = charset == Binary ? mod_for_binary : (charset == Numbers ? mod_for_numbers : (charset == Hex ? mod_for_hex : (charset == Alpha ? mod_for_alpha : (charset == AlphaNum ? mod_for_alphanum : (charset == AlphaNumSpecial ? mod_for_alphanumspecial : mod_for_keyboardsafe)))));
const char *chars = charset == Binary ? binary : (charset == Numbers ? numbers : (charset == Hex ? hex : (charset == Alpha ? alpha : (charset == AlphaNum ? alphanum : (charset == AlphaNumSpecial ? alphanumspecial : keyboardsafe)))));
for (int k = 0; k < count_passwords; ++k) {
for (int i = 0; i < num_characters; ++i) {
const unsigned int r = get_random_bits(bits);
if (r == ERROR_RANDOM_BITS) break;
printf("%c", chars[r % mod]);
}
printf("\n");
}
printf("\n");
return 0;
}