9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] snprint(), getfields() specification
@ 2001-05-10 13:56 rob pike
  2001-05-10 14:25 ` Greg Hudson
  0 siblings, 1 reply; 16+ messages in thread
From: rob pike @ 2001-05-10 13:56 UTC (permalink / raw)
  To: 9fans

Really?  Unix sprintf tells you what it printed and snprintf tells you
what it *might* have printed?  That's a scary inconsistency.

-rob



^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: [9fans] snprint(), getfields() specification
@ 2001-05-10 17:56 rob pike
  0 siblings, 0 replies; 16+ messages in thread
From: rob pike @ 2001-05-10 17:56 UTC (permalink / raw)
  To: 9fans

> would it not also avoid the proliferation of special interfaces for each possible
> routine that does formatting (including the ones users invent)?

yes it would. maybe i'll bite the bullet. sean dorward has been reworking
our print library; i'll talk to him about it.

-rob



^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: [9fans] snprint(), getfields() specification
@ 2001-05-10 17:51 forsyth
  0 siblings, 0 replies; 16+ messages in thread
From: forsyth @ 2001-05-10 17:51 UTC (permalink / raw)
  To: 9fans

>>If I had to pick one, I'd go for the length routine.  If you really
>>care, always use it; if you never care, you want the actual number of
>>bytes and the snprint (not snprintf) result seems better.

would it not also avoid the proliferation of special interfaces for each possible
routine that does formatting (including the ones users invent)?



^ permalink raw reply	[flat|nested] 16+ messages in thread
* [9fans] snprint(), getfields() specification
@ 2001-05-10 17:40 erik quanstrom
  0 siblings, 0 replies; 16+ messages in thread
From: erik quanstrom @ 2001-05-10 17:40 UTC (permalink / raw)
  To: 9fans

> The getfields() manpage says,
> 
> 	If there are more than maxargs fields, only the first maxargs
> 	fields will be set.
> 	...
> 	Getfields return the number of tokens processed.
> 
> snprint() says something similar.
> 
> In both cases, if the limit is too small, there's no way to find out
> how much space you actually needed.  Unix snprintf returns that value;
> compare with the limit to know if you need to allocate more space,
> or emit an error, or just use what you got.
> 
> I think we might be better off adopting that convention.

actually, the unix snprint (linux) says:

RETURN VALUE
       If the output was truncated, the return value is -1, otherwise it is the
       number of characters stored, not including the terminating null.


i don't see a problem with snprint() truncating the output -- you
shouldn't be using it if you don't have a maximum size in mind to begin with.
the n in snprint is just a fencepost.

what would be nice, though, is mprint(), which scott's very own print
library provides. mprint() allocates enough memory for whatever string
you want to format, and returns the buffer. very nice.

as an aside, i've added "long" formats to scott's library. they're
of the form "%{list %s}", for example. we were using installable
formats on a largish project where we had a lot of need to print out
this or that. it was quite a help to expand the namespace, in our
project. also, %{md5sum} is quite a lot more readable and unambiguous
than %M (which is probablly used anyway).

if there's any interest, i'd be happy to package up my mods & post them.

erik



^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: [9fans] snprint(), getfields() specification
@ 2001-05-10 16:56 rob pike
  0 siblings, 0 replies; 16+ messages in thread
From: rob pike @ 2001-05-10 16:56 UTC (permalink / raw)
  To: 9fans

The 9P2000 version of the Plan 9, with all its variable-sized pieces,
has this problem in many places.  I don't think a single best solution
exists, nor that this idea works for snprintf, but for example: In the
new stat, the buffer might not be long enough for the full entry.  In
that case, the return value is 2, which could never otherwise be the
right answer, and the first two bytes of the buffer contain the
required length.  (The first two bytes of any stat buffer hold the
length of the entire object.)  In the manner intended, I believe, for
snprintf, you have a chance to reallocate and retry.

A word for consistency: the return value is, as traditional, the
number of bytes affected by the operation that actually happened.

If you want to know big a buffer is required for snprintf, two methods
strike me as better than the C99 approach: some sort of length routine
that in practice you could always call (avoiding the retry case), or a
separate return path, such as an int*.  In practice, I probably
wouldn't use either, since I use snprint purely for safety, almost
never expecting a truncated answer and not really caring if I get one.
The C99 definition makes my standard usage more convoluted to achieve.

If I had to pick one, I'd go for the length routine.  If you really
care, always use it; if you never care, you want the actual number of
bytes and the snprint (not snprintf) result seems better.

-rob



^ permalink raw reply	[flat|nested] 16+ messages in thread
[parent not found: <rob@plan9.bell-labs.com>]
* Re: [9fans] snprint(), getfields() specification
@ 2001-05-10 14:30 rob pike
  2001-05-11  8:33 ` Douglas A. Gwyn
  2001-05-14  8:36 ` David Rubin
  0 siblings, 2 replies; 16+ messages in thread
From: rob pike @ 2001-05-10 14:30 UTC (permalink / raw)
  To: 9fans

By tradition, the return value of functions report what they did,
not what they considered doing.

-rob



^ permalink raw reply	[flat|nested] 16+ messages in thread
* [9fans] snprint(), getfields() specification
@ 2001-05-10  5:11 Scott Schwartz
  0 siblings, 0 replies; 16+ messages in thread
From: Scott Schwartz @ 2001-05-10  5:11 UTC (permalink / raw)
  To: 9fans

The getfields() manpage says,

	If there are more than maxargs fields, only the first maxargs
	fields will be set.
	...
	Getfields return the number of tokens processed.

snprint() says something similar.

In both cases, if the limit is too small, there's no way to find out
how much space you actually needed.  Unix snprintf returns that value;
compare with the limit to know if you need to allocate more space,
or emit an error, or just use what you got.

I think we might be better off adopting that convention.



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

end of thread, other threads:[~2001-05-14  8:36 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-10 13:56 [9fans] snprint(), getfields() specification rob pike
2001-05-10 14:25 ` Greg Hudson
2001-05-10 14:44   ` William Josephson
  -- strict thread matches above, loose matches on Subject: below --
2001-05-10 17:56 rob pike
2001-05-10 17:51 forsyth
2001-05-10 17:40 erik quanstrom
2001-05-10 16:56 rob pike
     [not found] <rob@plan9.bell-labs.com>
2001-05-10 14:59 ` rob pike
2001-05-10 16:42   ` Scott Schwartz
2001-05-10 18:13   ` Steve Kilbane
2001-05-10 21:38     ` Boyd Roberts
2001-05-11  6:51       ` Steve Kilbane
2001-05-10 14:30 rob pike
2001-05-11  8:33 ` Douglas A. Gwyn
2001-05-14  8:36 ` David Rubin
2001-05-10  5:11 Scott Schwartz

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 NNTP newsgroup(s).