Is there a way to use vsnprintf_l? Would it possibly be considered for an addition one day, if missing?

ell1e kittens@wobble.ninja
Sat Oct 17 11:33:53 GMT 2020


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


More information about the Libc-help mailing list