#include <locale.h>
#include <monetary.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define BUFFER_SIZE 100


void get_my_money_string(char **ptr, double money, int buf_size)
{
  char buffer[buf_size];
  if (strfmon(buffer, sizeof(buffer), "%.2n", money) < 0)
    return;

  *ptr = malloc(buf_size);
  strcpy(*ptr, buffer);
}


double get_amount(char *ptr)
{
  char *buffer;
  double result;
  result = strtod(ptr, &buffer);
  return result;
}


int main(int argc, char *argv[])
{
  if (argc < 2) {
    puts("Must supply only 1 value.");
    return -3;
  }
  if (argc > 2) {
    puts("Must supply only 1 value.");
    return -2;
  }
  if (setlocale(LC_MONETARY, "en_US.utf8") == NULL) {
    puts("Cannot set locale.");
    return -1;
  }

  double money;
  char *well_formed_money;

  money = get_amount(argv[1]);
  get_my_money_string(&well_formed_money, money, BUFFER_SIZE);
  printf("Result: %s\n", well_formed_money);

  return 0;
}
