1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
//Generator.c
#include "Generator.h"
char randomChar ()
{
char Chars [] =
"abcdefghijkmnlopqrstuvwxz"
"ABCDEFGHIJKMNLOPQRSTUVWXYZ"
"1234567890";
return Chars [rand () % strlen (Chars)];
}
void appendChar (char* Buffer , char Char)
{
//Buffer size
int Size = strlen (Buffer);
//Add the character and the null character
Buffer [Size] = Char;
}
int genPassword (char** Arguments , char* Buffer , int Size)
{
//Password Size
int PasswordSize = getPasswordSize (Arguments , Size);
//Check if the size is valid
if (PasswordSize == NO_PASSWORD)
{
return NO_PASSWORD;
}
if (PasswordSize > MAX_PASSWORD_SIZE)
{
return NO_PASSWORD;
}
//Iterate and append a character the end of the string
int i = 0;
for (i = 0 ; i < PasswordSize ; ++i)
{
appendChar (Buffer , randomChar ());
}
return 0;
}
void printPassword (char** Arguments , int Size)
{
char Buffer [FILENAME_MAX] = "";
//Check if we could generate a password
if (genPassword (Arguments , Buffer , Size) == NO_PASSWORD)
{
fprintf (stderr , "Invalid password size!! \n");
return;
}
//Check where we are printing to
if (getFilePos (Arguments , Size) == NO_FILE)
{
fprintf (stdout , "%s \n" , Buffer);
}
else
{
//File path
char FilePath [FILENAME_MAX] = "";
//Generate the path
getFile (Arguments , FilePath , Size);
//Stream to print to
FILE* Stream = fopen (FilePath , "a");
//Check if the stream is valid
if (Stream == NULL)
{
return;
}
//We can write to the file
else
{
//Print it
fprintf (Stream , "%s \n" , Buffer);
//Close the stream
fclose (Stream);
}
}
}
|