Commit | Line | Data |
f2ab5a41 |
1 | ################################################################################ |
2 | ## |
bfc37ff7 |
3 | ## $Revision: 7 $ |
f2ab5a41 |
4 | ## $Author: mhx $ |
bfc37ff7 |
5 | ## $Date: 2010/03/07 13:15:45 +0100 $ |
f2ab5a41 |
6 | ## |
7 | ################################################################################ |
8 | ## |
bfc37ff7 |
9 | ## Version 3.x, Copyright (C) 2004-2010, Marcus Holland-Moritz. |
f2ab5a41 |
10 | ## Version 2.x, Copyright (C) 2001, Paul Marquess. |
11 | ## Version 1.x, Copyright (C) 1999, Kenneth Albanowski. |
12 | ## |
13 | ## This program is free software; you can redistribute it and/or |
14 | ## modify it under the same terms as Perl itself. |
15 | ## |
16 | ################################################################################ |
17 | |
18 | =provides |
19 | |
20 | my_snprintf |
21 | |
22 | =implementation |
23 | |
24 | #if !defined(my_snprintf) |
25 | #if { NEED my_snprintf } |
26 | |
27 | int |
28 | my_snprintf(char *buffer, const Size_t len, const char *format, ...) |
29 | { |
30 | dTHX; |
31 | int retval; |
32 | va_list ap; |
33 | va_start(ap, format); |
34 | #ifdef HAS_VSNPRINTF |
35 | retval = vsnprintf(buffer, len, format, ap); |
36 | #else |
37 | retval = vsprintf(buffer, format, ap); |
38 | #endif |
39 | va_end(ap); |
c01be2ce |
40 | if (retval < 0 || (len > 0 && (Size_t)retval >= len)) |
f2ab5a41 |
41 | Perl_croak(aTHX_ "panic: my_snprintf buffer overflow"); |
42 | return retval; |
43 | } |
44 | |
45 | #endif |
46 | #endif |
47 | |
48 | =xsinit |
49 | |
50 | #define NEED_my_snprintf |
51 | |
52 | =xsubs |
53 | |
54 | void |
55 | my_snprintf() |
56 | PREINIT: |
57 | char buf[128]; |
58 | int len; |
59 | PPCODE: |
60 | len = my_snprintf(buf, sizeof buf, "foo%s%d", "bar", 42); |
c1a049cb |
61 | mXPUSHi(len); |
62 | mXPUSHs(newSVpv(buf, 0)); |
f2ab5a41 |
63 | XSRETURN(2); |
64 | |
65 | =tests plan => 2 |
66 | |
67 | my($l, $s) = Devel::PPPort::my_snprintf(); |
68 | ok($l, 8); |
69 | ok($s, "foobar42"); |
70 | |