Thanks for your feedback everyone. Was laughing at the person who thought this was homework help, I only with i was learning C at school. Currently we are 'learning' how to make websites in HTML, complete with <font> tag.
Also, to the person who suggested Python, thanks but as you said this is just a practise in learning C. I already have a Python password generator script (which interestingly enough ran just 0.03 seconds slower than my C code when generating 100, 1000 char passwords).
Anyway here is a 'fixed' version that should address some of the problems you guys raised:
Code:
/* passgen.c: Generate pseudo-random passwords
Copyright (C) David Futcher (bobbo) 2008
<bobbocanfly@gmail.com> - rev 2
Release under the GNU GPL License v2 or above */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv)
{
int bytes, pass, n, i;
char * buffer;
/* Get user args */
if (argc >= 3)
{
pass = atoi(argv[1]);
bytes = atoi(argv[2]);
/* Detects incorrect or stupid input */
if (pass < 1)
{
printf("%s <no. passwords to generate> <length of passwords>\n", argv[0]);
return 1;
}
else if (bytes < 1)
{
printf("%s <no. passwords to generate> <length of passwords>\n", argv[0]);
return 1;
}
}
else
{
printf("%s <no. passwords to generate> <length of passwords>\n", argv[0]);
return 1;
}
/* Set up dynamic memory + rand() seed */
buffer = malloc(bytes+1);
if (buffer == NULL)
{
printf("Could not allocate memory...exiting\n");
return 1;
}
srand(time(NULL));
/* Actual generation loop */
for (i=0; i<pass; i++)
{
for (n=0; n<bytes; n++)
{
buffer[n] = 'a' + (31.0 * (rand() / (RAND_MAX + 1.0)));
}
buffer[bytes] = '\0';
printf ("%s\n", buffer);
}
/* It worked! Get ready to exit */
free(buffer);
return 0;
}
Bookmarks