canonical.c

text/x-csrc

Filename: canonical.c
Type: text/x-csrc
Part: 0
Message: Re: [ADMIN] pg_upgrade from 9.1.3 to 9.2 failed
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char **argv)
{
	char	   *save;
	char	   *res;
	int 		category;

	if (argc == 1)
	{
		printf("LC_COLLATE = %d\n", LC_COLLATE);
		printf("LC_CTYPE = %d\n", LC_CTYPE);
		return 0;
	}
	
	category = atoi(argv[1]);
	
	save = setlocale(category, NULL);
	if (!save)
	{
		printf("failed to get the current locale\n");
		return 0;
	}

	/* 'save' may be pointing at a modifiable scratch variable, so copy it. */
	save = strdup(save);

	/* set the locale with setlocale, to see if it accepts it. */
	res = setlocale(category, argv[2]);

	if (!res)
	{
		printf("failed to get system local name for \"%s\"\n", res);
		return 0;
	}

	res = strdup(res);

	/* restore old value. */
	if (!setlocale(category, save))
	{
		printf("failed to restore old locale \"%s\"\n", save);
		return 0;
	}
		
	free(save);

	puts(res);
	return 0;
}