@@ -252,7 +252,7 @@ static void trapsig(int signum);
252
252
static void check_ok (void );
253
253
static char * escape_quotes (const char * src );
254
254
static int locale_date_order (const char * locale );
255
- static bool check_locale_name (int category , const char * locale ,
255
+ static void check_locale_name (int category , const char * locale ,
256
256
char * * canonname );
257
257
static bool check_locale_encoding (const char * locale , int encoding );
258
258
static void setlocales (void );
@@ -2529,7 +2529,7 @@ locale_date_order(const char *locale)
2529
2529
}
2530
2530
2531
2531
/*
2532
- * Is the locale name valid for the locale category?
2532
+ * Verify that locale name is valid for the locale category.
2533
2533
*
2534
2534
* If successful, and canonname isn't NULL, a malloc'd copy of the locale's
2535
2535
* canonical name is stored there. This is especially useful for figuring out
@@ -2540,7 +2540,7 @@ locale_date_order(const char *locale)
2540
2540
*
2541
2541
* this should match the backend's check_locale() function
2542
2542
*/
2543
- static bool
2543
+ static void
2544
2544
check_locale_name (int category , const char * locale , char * * canonname )
2545
2545
{
2546
2546
char * save ;
@@ -2551,7 +2551,11 @@ check_locale_name(int category, const char *locale, char **canonname)
2551
2551
2552
2552
save = setlocale (category , NULL );
2553
2553
if (!save )
2554
- return false; /* won't happen, we hope */
2554
+ {
2555
+ fprintf (stderr , _ ("%s: setlocale failed\n" ),
2556
+ progname );
2557
+ exit (1 );
2558
+ }
2555
2559
2556
2560
/* save may be pointing at a modifiable scratch variable, so copy it. */
2557
2561
save = pg_strdup (save );
@@ -2565,16 +2569,34 @@ check_locale_name(int category, const char *locale, char **canonname)
2565
2569
2566
2570
/* restore old value. */
2567
2571
if (!setlocale (category , save ))
2572
+ {
2568
2573
fprintf (stderr , _ ("%s: failed to restore old locale \"%s\"\n" ),
2569
2574
progname , save );
2575
+ exit (1 );
2576
+ }
2570
2577
free (save );
2571
2578
2572
- /* should we exit here? */
2579
+ /* complain if locale wasn't valid */
2573
2580
if (res == NULL )
2574
- fprintf (stderr , _ ("%s: invalid locale name \"%s\"\n" ),
2575
- progname , locale );
2576
-
2577
- return (res != NULL );
2581
+ {
2582
+ if (* locale )
2583
+ fprintf (stderr , _ ("%s: invalid locale name \"%s\"\n" ),
2584
+ progname , locale );
2585
+ else
2586
+ {
2587
+ /*
2588
+ * If no relevant switch was given on command line, locale is an
2589
+ * empty string, which is not too helpful to report. Presumably
2590
+ * setlocale() found something it did not like in the environment.
2591
+ * Ideally we'd report the bad environment variable, but since
2592
+ * setlocale's behavior is implementation-specific, it's hard to
2593
+ * be sure what it didn't like. Print a safe generic message.
2594
+ */
2595
+ fprintf (stderr , _ ("%s: invalid locale settings; check LANG and LC_* environment variables\n" ),
2596
+ progname );
2597
+ }
2598
+ exit (1 );
2599
+ }
2578
2600
}
2579
2601
2580
2602
/*
@@ -2642,41 +2664,27 @@ setlocales(void)
2642
2664
}
2643
2665
2644
2666
/*
2645
- * canonicalize locale names, and override any missing/invalid values from
2646
- * our current environment
2667
+ * canonicalize locale names, and obtain any missing values from our
2668
+ * current environment
2647
2669
*/
2648
2670
2649
- if (check_locale_name (LC_CTYPE , lc_ctype , & canonname ))
2650
- lc_ctype = canonname ;
2651
- else
2652
- lc_ctype = pg_strdup (setlocale (LC_CTYPE , NULL ));
2653
- if (check_locale_name (LC_COLLATE , lc_collate , & canonname ))
2654
- lc_collate = canonname ;
2655
- else
2656
- lc_collate = pg_strdup (setlocale (LC_COLLATE , NULL ));
2657
- if (check_locale_name (LC_NUMERIC , lc_numeric , & canonname ))
2658
- lc_numeric = canonname ;
2659
- else
2660
- lc_numeric = pg_strdup (setlocale (LC_NUMERIC , NULL ));
2661
- if (check_locale_name (LC_TIME , lc_time , & canonname ))
2662
- lc_time = canonname ;
2663
- else
2664
- lc_time = pg_strdup (setlocale (LC_TIME , NULL ));
2665
- if (check_locale_name (LC_MONETARY , lc_monetary , & canonname ))
2666
- lc_monetary = canonname ;
2667
- else
2668
- lc_monetary = pg_strdup (setlocale (LC_MONETARY , NULL ));
2671
+ check_locale_name (LC_CTYPE , lc_ctype , & canonname );
2672
+ lc_ctype = canonname ;
2673
+ check_locale_name (LC_COLLATE , lc_collate , & canonname );
2674
+ lc_collate = canonname ;
2675
+ check_locale_name (LC_NUMERIC , lc_numeric , & canonname );
2676
+ lc_numeric = canonname ;
2677
+ check_locale_name (LC_TIME , lc_time , & canonname );
2678
+ lc_time = canonname ;
2679
+ check_locale_name (LC_MONETARY , lc_monetary , & canonname );
2680
+ lc_monetary = canonname ;
2669
2681
#if defined(LC_MESSAGES ) && !defined(WIN32 )
2670
- if (check_locale_name (LC_MESSAGES , lc_messages , & canonname ))
2671
- lc_messages = canonname ;
2672
- else
2673
- lc_messages = pg_strdup (setlocale (LC_MESSAGES , NULL ));
2682
+ check_locale_name (LC_MESSAGES , lc_messages , & canonname );
2683
+ lc_messages = canonname ;
2674
2684
#else
2675
2685
/* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2676
- if (check_locale_name (LC_CTYPE , lc_messages , & canonname ))
2677
- lc_messages = canonname ;
2678
- else
2679
- lc_messages = pg_strdup (setlocale (LC_CTYPE , NULL ));
2686
+ check_locale_name (LC_CTYPE , lc_messages , & canonname );
2687
+ lc_messages = canonname ;
2680
2688
#endif
2681
2689
}
2682
2690
0 commit comments