Unix Programming - snprintf macro test?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2004 > snprintf macro test?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author snprintf macro test?
Michael B Allen

2004-09-22, 9:22 pm

What is a good way to test for snprintf? I tried:

#if __STDC_VERSION__ >= 199901L
buf += snprintf(buf, blim - buf,
#else
buf += sprintf(buf,
#endif

but gcc doesn't set pick this up even with -D_ISOC99_SOURCE.

What's the best way?

Thanks,
Mike
James Antill

2004-09-22, 9:22 pm

On Wed, 22 Sep 2004 01:17:22 -0400, Michael B Allen wrote:

> What is a good way to test for snprintf? I tried:
>
> #if __STDC_VERSION__ >= 199901L
> buf += snprintf(buf, blim - buf,


This doesn't work...

char real_buf[4];
char *buf = real_buf;
char *blim = &real_buf[sizeof(real_buf)];

/* blim - buf == 4, ret = 2, written = 3 */
buf += snprintf(buf, blim - buf, "%s", "xy");

/* blim - buf == 2, ret = 2, written = 2 */
buf += snprintf(buf, blim - buf, "%s", "xy");

/* blim - buf == 0, ret = 2, written = 0 */
buf += snprintf(buf, blim - buf, "%s", "xy");

/* blim - buf == (size_t)-2, ret = 2, written = 3 */
buf += snprintf(buf, blim - buf, "%s", "xy");

....it also doesn't work when sprintf() returns -1.

> #else
> buf += sprintf(buf,
> #endif
>
> but gcc doesn't set pick this up even with -D_ISOC99_SOURCE.
>
> What's the best way?


autoconf, or something very similar as you'll often need to check for
things like _snprintf() and what the return value is.
A much better question though is what do you do if it isn't there?

I'd highly recommend just having your own sprintf() on a real string type
that way it's very simple to have an appending version.
See:

http://www.and.org/texts/simple_printf.c.html
http://www.and.org/vstr/printf_comparison.html

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

Michael B Allen

2004-09-23, 9:13 am

On Wed, 22 Sep 2004 12:52:17 -0400, James Antill wrote:
>
> This doesn't work...


True. You got me!

> I'd highly recommend just having your own sprintf() on a real string
> type
> that way it's very simple to have an appending version.


I've seen your vstr library but I'd rathing stick to plain strings at
the moment. Is there a good stand-alone snprintf that doesn't exibit
the odd c99 semantics?

Thanks,
Mike
Geoff Clare

2004-09-23, 9:13 am

"Michael B Allen" <mba2000@ioplex.com> wrote, on Wed, 22 Sep 2004:

> #if __STDC_VERSION__ >= 199901L

[...]
> but gcc doesn't set pick this up even with -D_ISOC99_SOURCE.


$ echo __STDC_VERSION__ | gcc -E - | tail -n 1
__STDC_VERSION__
$ echo __STDC_VERSION__ | gcc -std=c99 -E - | tail -n 1
199901L

--
Geoff Clare <nospam@gclare.org.uk>
Michael B Allen

2004-09-23, 9:57 am

On Thu, 23 Sep 2004 08:44:49 -0400, Geoff Clare wrote:

> "Michael B Allen" <mba2000@ioplex.com> wrote, on Wed, 22 Sep 2004:
>
> [...]
>
> $ echo __STDC_VERSION__ | gcc -E - | tail -n 1 __STDC_VERSION__ $ echo
> __STDC_VERSION__ | gcc -std=c99 -E - | tail -n 1 199901L
>


Thanks.

So what does _ISOC99_SOURCE do exactly?

Mike
James Antill

2004-09-23, 5:54 pm

On Thu, 23 Sep 2004 02:20:52 -0400, Michael B Allen wrote:

> On Wed, 22 Sep 2004 12:52:17 -0400, James Antill wrote:
>
> I've seen your vstr library but I'd rathing stick to plain strings at
> the moment. Is there a good stand-alone snprintf that doesn't exibit
> the odd c99 semantics?


Well the comparison page[1] isn't specific to Vstr, and there are
certainly other string libraries. But if you want one that you can just cp
into a project using C-style string then I'd probably recommend trio[2].

[1] http://www.and.org/vstr/printf_comparison.html

[2] http://www.and.org/vstr/printf_comparison.html#trio ;)

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

Geoff Clare

2004-09-24, 7:49 am

"Michael B Allen" <mba2000@ioplex.com> wrote, on Thu, 23 Sep 2004:

>
> Thanks.
>
> So what does _ISOC99_SOURCE do exactly?


It makes C99 symbols visible in the headers. However, __STDC_VERSION__
is defined internally by the compiler, it isn't defined in a header.

There shouldn't be any need to define _ISOC99_SOURCE explicitly with -D.
It will be turned on automatically if -std=c99 is used or if a superset
of C99 is selected, e.g. -D_XOPEN_SOURCE=600 for SUSv3.

--
Geoff Clare <nospam@gclare.org.uk>
Michael B Allen

2004-09-24, 5:51 pm

On Thu, 23 Sep 2004 10:42:12 -0400, James Antill wrote:
>
> Well the comparison page[1] isn't specific to Vstr, and there are
> certainly other string libraries. But if you want one that you can just
> cp into a project using C-style string then I'd probably recommend
> trio[2].
>
> [1] http://www.and.org/vstr/printf_comparison.html


Mmm, I didn't look at this very carefully the first time. It's a nice
list. I looked at the BSD code but unfortunately it looks intertwined
with the stdio code.

I'll look at trio.

Thanks,
Mike
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com