/***************
 *
 * Testing the "crypt" function.
 *
 * In my original versions I had lines like
 *    encrypt=crypt(plain,salt)
 * which stomps on the encrypt buffer, putting
 * into it the address of memory allocated from within crypt().
 * It turns out that crypt() uses this same buffer repeatedly.
 * Thus subsequent calls like
 *    encrypt2=crypt(plain,salt)
 * would return a pointer to the same buffer, thus also changing
 * the encrypt script (which was already a pointer to the same buffer).
 * This was confusing me...
 *
 * The version below uses (correctly) the "strcpy" function to 
 * copy the characters that crypt() returns into the local string buffers.
 * 
 *
 **********************/

#define _XOPEN_SOURCE
#include <unistd.h>
#include <string.h>

int main(){

  char encrypt[36];
  char plain[36];
  char salt[36];
  char guess[36];
  char encryptGuess[36];

  printf("   -- Testing the password crypt() function -- \n");
  printf(" Enter a secret word (<32 chars, no spaces) to encrypt: ");
  scanf("%s", plain);

  strcpy( salt,    "a7");
  strcpy( encrypt, crypt(plain,salt) );
  printf(" With salt='%s', the encrypted version of '%s' is '%s'.\n",
         salt, plain, encrypt);
  printf("\n");

  while(1==1){
    printf(" Enter a guess: ");
    scanf("%s", guess);
    strcpy( encryptGuess, crypt(guess, encrypt) );
    printf(" With salt='%s', encrypted version of '%s' is '%s'.\n",
           encrypt, guess, encryptGuess);

    if ( strcmp(encryptGuess,encrypt)==0 ){
      printf(" Yup, that's the secret word all right.\n");
      break;
    }
    else {
      printf(" Nope, that's not it.  Try again.\n\n");
    }
    
  }


  /*** testing ***********8

  // --- These declarations are equivalent, but allocate
  //     memory on the heap rather than the stack.
  // char* encrypt  = (char*)malloc(sizeof(char)*32);
  // char* encryptGuess = (char*)malloc(sizeof(char)*32);
  // char* plain    = (char*)malloc(sizeof(char)*32);
  // char* salt     = (char*)malloc(sizeof(char)*32);
  // char* guess    = (char*)malloc(sizeof(char)*32);

  // Copy characters into the string buffers.
  strcpy(encrypt,  "");
  strcpy(encryptGuess,  "");
  strcpy(plain,    "secret");
  strcpy(salt,     "a7123");
  strcpy(guess,    "WRONG");

  printf(" encrypt = '%s', address=%p \n", encrypt, encrypt);
  printf(" encryptGuess = '%s', address=%p \n", encryptGuess, encryptGuess);
  printf(" plain   = '%s', address=%p \n", plain, plain);
  printf(" salt    = '%s', address=%p \n", salt, salt);
  printf(" guess   = '%s', address=%p \n", guess, guess);
  printf("\n");

  strcpy(encrypt, crypt(plain,salt));

  printf(" encrypt = '%s', address=%p \n", encrypt, encrypt);
  printf(" encryptGuess = '%s', address=%p \n", encryptGuess, encryptGuess);
  printf(" plain   = '%s', address=%p \n", plain, plain);
  printf(" salt    = '%s', address=%p \n", salt, salt);
  printf(" guess   = '%s', address=%p \n", guess, guess);
  printf("\n");

  strcpy(encryptGuess, crypt(guess,salt));

  printf(" encrypt = '%s', address=%p \n", encrypt, encrypt);
  printf(" encryptGuess = '%s', address=%p \n", encryptGuess, encryptGuess);
  printf(" plain   = '%s', address=%p \n", plain, plain);
  printf(" salt    = '%s', address=%p \n", salt, salt);
  printf(" guess   = '%s', address=%p \n", guess, guess);
  printf("\n");


  ****************/

}


syntax highlighted by Code2HTML, v. 0.9.1