Try to protect against wild langinfo() arguments.
[p5sagit/p5-mst-13.2.git] / ext / I18N / Langinfo / Langinfo.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #ifdef I_LANGINFO
6 #   include <langinfo.h>
7 #endif
8
9 #include "constants.c"
10
11 MODULE = I18N::Langinfo PACKAGE = I18N::Langinfo
12
13 PROTOTYPES: ENABLE
14
15 INCLUDE: constants.xs
16
17 SV*
18 langinfo(code)
19         int     code
20   CODE:
21 #ifdef HAS_NL_LANGINFO
22         char *s;
23         if (code) { /* bold assumption: all valid langinfo codes > 0 */
24 #ifdef _MAXSTRMSG
25             if (code >= _MAXSTRMSG
26                 RETVAL = &PL_sv_undef;
27             else
28 #else
29 #   ifdef _NL_NUM_ITEMS
30             if (code >= _NL_NUM_ITEMS)
31                 RETVAL = &PL_sv_undef;
32             else
33 #   else
34 #       ifdef _NL_NUM
35             if (code >= _NL_NUM)
36                 RETVAL = &PL_sv_undef;
37             else
38 #       endif
39 #   endif
40 #endif
41             {
42                 s = nl_langinfo(code);
43                 RETVAL = newSVpvn(s, strlen(s));
44             }
45         } else {
46             RETVAL = &PL_sv_undef;
47         }
48 #else
49         croak("nl_langinfo() not implemented on this architecture");
50 #endif
51   OUTPUT:
52         RETVAL