tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

RFC - add snscanf(3) to stdio



In using sscanf() on lines retrived with fgetln(), I ran into
an annoying bug with sscanf() - it calls strlen() on the buffer
being passed in. This makes it unsuitable for use on text that
is returned with fgetln() without whacking it with a gratutious
\0...ie. this will core dump:

line = fgetln(fp, &lsize)
if (line != NULL && lsize > 0)
   sscanf(line, "%d", &integer);

Having to what the input with \0 is not helpful and also means
you can't use sscanf() on files/memory mmap'd with PROT_READ.

Thus I'd like to introduce the following:
int snscanf(const char *str, const size_t buflen, const char *format, ...);
int vsnscanf(const char *str, const size_t buflen, const char *format, va_list ap);

The result would be the above code changed to look like this:

line = fgetln(fp, &lsize)
if (line != NULL && lsize > 0)
   snscanf(line, lsize, "%d", &integer);

and there would be no need for a gratutious \0 to be added.

Is there a need for fsnscanf()? I'm not convinced but could be...

Having snscanf is not without precedent - searching for it shows it to
be used at least in the linux kernel.

Comments?

Darren



Home | Main Index | Thread Index | Old Index