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