Im suppose to have this prompt me via stderr to enter two 32-bit keys. I use fgets tofrom stdin to read up the line the user inputs. Then im suppose to parse the line into tokens using strtok_r that converts them to unsigned longs. I already have a function strToULong.c that converts them. Display error messages and reprompt on any invalid input. If the user input Ctrl+D its to exit the program. If i get 2 good keys, then just pass them back to the keys[] referenced by the keys[] parameter.
This is what i have right now. I would appreciate any help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "mycrypt.h"
void getKeys( unsigned long keys[])
{
char keystring[BUFSIZ] = {'\0'};
char *savPtr;
int slen = 0;
int slen2 = 0;
char *keyPtr, *keyPtr2;
unsigned long key1, key2;
int error = 0;
char one[], two[];
(void) fprintf(stderr, "Enter the two 32-bit keys "
"[octal, decimal, or hax]\n"
"\tseparated by a space or tab: ");
if( fgets( keystring, sizeof(keystring), stdin) == NULL)
{
// error mesaage and exit Ctrl+D is pressed
fprintf(stderr, "\n");
exit(1);
}
slen = strlen(keystring);
while(error != 0 )
{
// Be sure to parse the string
keyPtr = strtok_r(keystring, WHITE_SPACE_CHARS, &savPtr);
keyPtr2 = strtok_r(NULL, WHITE_SPACE_CHARS, &savPtr);
slen = (unsigned) strlen(keyPtr);
slen2 =(unsigned) strlen(keyPtr2);
if( strlen(slen) == NULL && strlen(slen2) == NULL)
{
fprintf(stderr, "\n\tNo keys entered. You must enter 2 keys.\n");
error = 1;
}
if(strlen(slen2) == NULL)
{
fprintf(stderr, "\n\tOnly 1 key entered. You must enter 2 keys.\n");
error = 1;
}
// Need to utilize your strToULong
key1 = strToULong(keyPtr, 0);
key2 = strToULong(keyPtr2, 0);
}//end while loop
// If everything checks out, then store value into keys array
keys[0] = key1;
keys[1] = key2;
}
These are the instructions they gave us? This is my first C class. Any help would be appreciated. Thanks in advance.