public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Is there a way to use vsnprintf_l? Would it possibly be considered for an addition one day, if missing?
@ 2020-10-17 11:33 ell1e
  2020-10-19 21:01 ` Adhemerval Zanella
  0 siblings, 1 reply; 6+ messages in thread
From: ell1e @ 2020-10-17 11:33 UTC (permalink / raw)
  To: libc-help

Hello everyone, I hope I'm sending this to the right place!

I write C stuff on linux with GLIBC, and I've discovered I prefer to use 
a locale-independent version of snprintf to avoid issues with third 
party plugins using a process-wide setlocale() thoughtlessly. While I am 
aware a uselocale() in the local thread would also help with this, I 
don't really want to put that everywhere after I call into an external 
plugin just in case to guarantee this (or every time before I call 
snprintf).

The typical choice from what I can see for a locale-independent snprintf 
would be snprintf_l. However, always specifying that I really want a "C" 
locale as an additional parameter is tedious, especially since I have no 
desire to ever use anything else. In addition, this function isn't 
available on all platforms or might be named differently/have a 
different argument order (e.g. Windows, or MinGW). Therefore, I wrapped 
it to have the regular snprintf signature again:

static int mysnprintf(char *buf, size_t size, const char *format, ...)

... as a three line inline header function (to give it a chance of being 
inlined/optimized away), where the natural function to call inside would 
now be vsnprintf_l as far as I can tell.

However, sadly it seems glibc is lacking vsnprintf_l. I grepped the 
headers, and just couldn't find it.

Does it happen to be available in some ways I didn't anticipate? If not, 
is there a possibility to petition for it to be added?

Right now I'm calling uselocale()+vsnprintf() in a pair every time 
instead. While I don't anticipate any notable performance problems from 
the additional call, it still seems needlessly tedious, and vsnprintf_l 
just feeks like a better match. In addition, vsnprintf_l is available on 
the popular BSDs, macOS, and even Windows 10! (As a variant with 
slightly different argument order, _vsnprintf_l.) So I think it would be 
great to have it on Linux as well for parity.

For a similar reasoning, vprintf_l (in addition to vsnprintf_l) would 
also be a function I'd really love to use. I already use strtod_l, which 
thankfully does seem to be available in glibc already.

Best regards and happy C writing,

ell1e

PS: I hope I didn't typo the function names anywhere, I tend to mess up 
the letters sometimes xD I apologize if that happened at any point

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-10-29 16:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-17 11:33 Is there a way to use vsnprintf_l? Would it possibly be considered for an addition one day, if missing? ell1e
2020-10-19 21:01 ` Adhemerval Zanella
2020-10-20  5:45   ` Florian Weimer
2020-10-20 13:30     ` Adhemerval Zanella
2020-10-29 15:48       ` Florian Weimer
2020-10-29 16:02       ` ell1e

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).