#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <errno.h>
#include <limits.h>

/*
 * This test calls wcstoul() to convert a string to an
 * unsigned long integer. wcstoul outputs the resulting
 * integer and any characters that could not be converted. 
 */

#define MAX_STRING 128

main() {
    int base = 10,
        errno;
    char *input_string = "1234.56";
    wchar_t string_array[MAX_STRING],
           *ptr;
    size_t size;
    unsigned long int val;
    
    printf("base = [%d]\n", base);
    printf("String to convert = %s\n", input_string);
    
    if ((size = mbstowcs(string_array, input_string, MAX_STRING)) == (size_t)-1) {
        perror("mbstowcs");
        exit(-1);
    }
    
    printf("wchar_t string is = [%S]\n", string_array);

    errno = 0;
    val = wcstoul(string_array, &ptr, base);
    
    if (errno == 0) {
      printf("returned unsigned long int from wcstoul = [%u]\n", val);
      printf("wide char terminating scan(ptr) = [%S]\n\n", ptr);
    }
    
    if (errno == ERANGE) {
        perror("error value is :");
        printf("ULONG_MAX = [%u]\n", ULONG_MAX);
        printf("wcstoul failed, val = [%d]\n\n", val);
    }
  
    return (0);
}

