FreeBSD/usr.bin/locale/locale.c
/*
 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
 * environment variable is set)
 */
Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
environment variable is set)
Retrieve sorted list of system locales ( or user locales if PATH_LOCALE environment variable is set )
void
init_locales_list(void)
{
	DIR *dirp;
	struct dirent *dp;
	size_t i;
	int bogus;
	if (locales != NULL)
		return;
	locales = sl_init();
	if (locales == NULL)
		err(1, "could not allocate memory");
	if (__detect_path_locale() != 0)
		err(1, "unable to find locales storage");
	dirp = opendir(_PathLocale);
	if (dirp == NULL)
		err(1, "could not open directory '%s'", _PathLocale);
	while ((dp = readdir(dirp)) != NULL) {
		if (*(dp->d_name) == '.')
			continue;		
		for (bogus = i = 0; i < NBOGUS; i++)
			if (strncmp(dp->d_name, boguslocales[i],
			    strlen(boguslocales[i])) == 0)
				bogus = 1;
		if (!bogus)
			sl_add(locales, strdup(dp->d_name));
	}
	closedir(dirp);
	if (sl_find(locales, "POSIX") == NULL)
		sl_add(locales, "POSIX");
	if (sl_find(locales, "C") == NULL)
		sl_add(locales, "C");
	qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp);
}
void init_locales_list ( void ) { DIR * dirp ; struct dirent * dp ; size_t i ; int bogus ; if ( locales != NULL ) return ; locales = sl_init ( ) ; if ( locales == NULL ) err ( 1 , " " ) ; if ( __detect_path_locale ( ) != 0 ) err ( 1 , " " ) ; dirp = opendir ( _PathLocale ) ; if ( dirp == NULL ) err ( 1 , " " , _PathLocale ) ; while ( ( dp = readdir ( dirp ) ) != NULL ) { if ( * ( dp -> d_name ) == ' ' ) continue ; for ( bogus = i = 0 ; i < NBOGUS ; i ++ ) if ( strncmp ( dp -> d_name , boguslocales [ i ] , strlen ( boguslocales [ i ] ) ) == 0 ) bogus = 1 ; if ( ! bogus ) sl_add ( locales , strdup ( dp -> d_name ) ) ; } closedir ( dirp ) ; if ( sl_find ( locales , " " ) == NULL ) sl_add ( locales , " " ) ; if ( sl_find ( locales , " " ) == NULL ) sl_add ( locales , " " ) ; qsort ( locales -> sl_str , locales -> sl_cur , sizeof ( char * ) , scmp ) ; }
Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
 environment variable is set)
Retrieve sorted list of system locales ( or user locales if PATH_LOCALE environment variable is set )
/* why call this function twice ? */ /* initialize StringList */ /* get actual locales directory name */ /* open locales directory */ /* scan directory and store its contents except "." and ".." */ /* exclude "." and ".." */ /* make sure that 'POSIX' and 'C' locales are present in the list.
	 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
	 * we also list 'C' for constistency
	 */ /* make output nicer, sort the list */