9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "rob pike" <rob@plan9.bell-labs.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] snprint(), getfields() specification
Date: Thu, 10 May 2001 12:56:20 -0400	[thread overview]
Message-ID: <20010510165622.E3C3C199F0@mail.cse.psu.edu> (raw)

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



             reply	other threads:[~2001-05-10 16:56 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-10 16:56 rob pike [this message]
  -- 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
     [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 13:56 rob pike
2001-05-10 14:25 ` Greg Hudson
2001-05-10 14:44   ` William Josephson
2001-05-10  5:11 Scott Schwartz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010510165622.E3C3C199F0@mail.cse.psu.edu \
    --to=rob@plan9.bell-labs.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).