Back to index

nagios-plugins  1.4.16
nl_langinfo.c
Go to the documentation of this file.
00001 /* nl_langinfo() replacement: query locale dependent information.
00002 
00003    Copyright (C) 2007-2010 Free Software Foundation, Inc.
00004 
00005    This program is free software: you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 #include <config.h>
00019 
00020 /* Specification.  */
00021 #include <langinfo.h>
00022 
00023 #if REPLACE_NL_LANGINFO
00024 
00025 /* Override nl_langinfo with support for added nl_item values.  */
00026 
00027 # include <locale.h>
00028 # include <string.h>
00029 
00030 # undef nl_langinfo
00031 
00032 char *
00033 rpl_nl_langinfo (nl_item item)
00034 {
00035   switch (item)
00036     {
00037 # if GNULIB_defined_CODESET
00038     case CODESET:
00039       {
00040         const char *locale;
00041         static char buf[2 + 10 + 1];
00042 
00043         locale = setlocale (LC_CTYPE, NULL);
00044         if (locale != NULL && locale[0] != '\0')
00045           {
00046             /* If the locale name contains an encoding after the dot, return
00047                it.  */
00048             const char *dot = strchr (locale, '.');
00049 
00050             if (dot != NULL)
00051               {
00052                 const char *modifier;
00053 
00054                 dot++;
00055                 /* Look for the possible @... trailer and remove it, if any.  */
00056                 modifier = strchr (dot, '@');
00057                 if (modifier == NULL)
00058                   return dot;
00059                 if (modifier - dot < sizeof (buf))
00060                   {
00061                     memcpy (buf, dot, modifier - dot);
00062                     buf [modifier - dot] = '\0';
00063                     return buf;
00064                   }
00065               }
00066           }
00067         return "";
00068       }
00069 # endif
00070 # if GNULIB_defined_ERA
00071     case ERA:
00072       /* The format is not standardized.  In glibc it is a sequence of strings
00073          of the form "direction:offset:start_date:end_date:era_name:era_format"
00074          with an empty string at the end.  */
00075       return "";
00076     case ERA_D_FMT:
00077       /* The %Ex conversion in strftime behaves like %x if the locale does not
00078          have an alternative time format.  */
00079       item = D_FMT;
00080       break;
00081     case ERA_D_T_FMT:
00082       /* The %Ec conversion in strftime behaves like %c if the locale does not
00083          have an alternative time format.  */
00084       item = D_T_FMT;
00085       break;
00086     case ERA_T_FMT:
00087       /* The %EX conversion in strftime behaves like %X if the locale does not
00088          have an alternative time format.  */
00089       item = T_FMT;
00090       break;
00091     case ALT_DIGITS:
00092       /* The format is not standardized.  In glibc it is a sequence of 10
00093          strings, appended in memory.  */
00094       return "\0\0\0\0\0\0\0\0\0\0";
00095 # endif
00096     default:
00097       break;
00098     }
00099   return nl_langinfo (item);
00100 }
00101 
00102 #else
00103 
00104 /* Provide nl_langinfo from scratch.  */
00105 
00106 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00107 
00108 /* Native Windows platforms.  */
00109 
00110 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
00111 #  include <windows.h>
00112 
00113 #  include <stdio.h>
00114 
00115 # else
00116 
00117 /* An old Unix platform without locales, such as Linux libc5 or BeOS.  */
00118 
00119 # endif
00120 
00121 # include <locale.h>
00122 
00123 char *
00124 nl_langinfo (nl_item item)
00125 {
00126   switch (item)
00127     {
00128     /* nl_langinfo items of the LC_CTYPE category */
00129     case CODESET:
00130 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00131       {
00132         static char buf[2 + 10 + 1];
00133 
00134         /* Woe32 has a function returning the locale's codepage as a number.  */
00135         sprintf (buf, "CP%u", GetACP ());
00136         return buf;
00137       }
00138 # elif defined __BEOS__
00139       return "UTF-8";
00140 # else
00141       return "ISO-8859-1";
00142 # endif
00143     /* nl_langinfo items of the LC_NUMERIC category */
00144     case RADIXCHAR:
00145       return localeconv () ->decimal_point;
00146     case THOUSEP:
00147       return localeconv () ->thousands_sep;
00148     /* nl_langinfo items of the LC_TIME category.
00149        TODO: Really use the locale.  */
00150     case D_T_FMT:
00151     case ERA_D_T_FMT:
00152       return "%a %b %e %H:%M:%S %Y";
00153     case D_FMT:
00154     case ERA_D_FMT:
00155       return "%m/%d/%y";
00156     case T_FMT:
00157     case ERA_T_FMT:
00158       return "%H:%M:%S";
00159     case T_FMT_AMPM:
00160       return "%I:%M:%S %p";
00161     case AM_STR:
00162       return "AM";
00163     case PM_STR:
00164       return "PM";
00165     case DAY_1:
00166       return "Sunday";
00167     case DAY_2:
00168       return "Monday";
00169     case DAY_3:
00170       return "Tuesday";
00171     case DAY_4:
00172       return "Wednesday";
00173     case DAY_5:
00174       return "Thursday";
00175     case DAY_6:
00176       return "Friday";
00177     case DAY_7:
00178       return "Saturday";
00179     case ABDAY_1:
00180       return "Sun";
00181     case ABDAY_2:
00182       return "Mon";
00183     case ABDAY_3:
00184       return "Tue";
00185     case ABDAY_4:
00186       return "Wed";
00187     case ABDAY_5:
00188       return "Thu";
00189     case ABDAY_6:
00190       return "Fri";
00191     case ABDAY_7:
00192       return "Sat";
00193     case MON_1:
00194       return "January";
00195     case MON_2:
00196       return "February";
00197     case MON_3:
00198       return "March";
00199     case MON_4:
00200       return "April";
00201     case MON_5:
00202       return "May";
00203     case MON_6:
00204       return "June";
00205     case MON_7:
00206       return "July";
00207     case MON_8:
00208       return "August";
00209     case MON_9:
00210       return "September";
00211     case MON_10:
00212       return "October";
00213     case MON_11:
00214       return "November";
00215     case MON_12:
00216       return "December";
00217     case ABMON_1:
00218       return "Jan";
00219     case ABMON_2:
00220       return "Feb";
00221     case ABMON_3:
00222       return "Mar";
00223     case ABMON_4:
00224       return "Apr";
00225     case ABMON_5:
00226       return "May";
00227     case ABMON_6:
00228       return "Jun";
00229     case ABMON_7:
00230       return "Jul";
00231     case ABMON_8:
00232       return "Aug";
00233     case ABMON_9:
00234       return "Sep";
00235     case ABMON_10:
00236       return "Oct";
00237     case ABMON_11:
00238       return "Nov";
00239     case ABMON_12:
00240       return "Dec";
00241     case ERA:
00242       return "";
00243     case ALT_DIGITS:
00244       return "\0\0\0\0\0\0\0\0\0\0";
00245     /* nl_langinfo items of the LC_MONETARY category
00246        TODO: Really use the locale. */
00247     case CRNCYSTR:
00248       return "-";
00249     /* nl_langinfo items of the LC_MESSAGES category
00250        TODO: Really use the locale. */
00251     case YESEXPR:
00252       return "^[yY]";
00253     case NOEXPR:
00254       return "^[nN]";
00255     default:
00256       return "";
00257     }
00258 }
00259 
00260 #endif