Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!rutgers!ames!ptsfa!ihnp4!inuxc!iuvax!bsu-cs!dhesi
From: dhesi@bsu-cs.UUCP (Rahul Dhesi)
Newsgroups: comp.unix.questions
Subject: Re: write (fildes, buf, nbytes) -- is "nbytes" int or unsigned?
Message-ID: <895@bsu-cs.UUCP>
Date: Mon, 27-Jul-87 21:54:21 EDT
Article-I.D.: bsu-cs.895
Posted: Mon Jul 27 21:54:21 1987
Date-Received: Wed, 29-Jul-87 06:31:52 EDT
References: <868@bsu-cs.UUCP>
Reply-To: dhesi@bsu-cs.UUCP (Rahul Dhesi)
Organization: CS Dept, Ball St U, Muncie, Indiana
Lines: 45
Summary: nbytes is unsigned iff write() is unsigned

In article <868@bsu-cs.UUCP> I said that in the declaration for 
"write (fildes, buf, nbytes)", nbytes should be int because write()
returns an int value.  Several people disagreed, one or two quite
strongly.

I looked around in a few places to see what others have written.  
Everybody seems to agree that write() returns an int value. But:

      nbytes is unsigned              nbytes is int
      ------ -- --------              ------ -- ---
System V Release 2 manual             4.2BSD and 4.3BSD manual
entry for write()                     entry for write()

Description of write() by Marc        Description of write() by Maurice
J. Rochkind in "Advanced UNIX         J. Bach in "The Design of the
Programming", p 28.                   UNIX Operating System", p 453.

Microsoft C 3.0 manual entry          Borland's Turbo C 1.0 manual
for write()                           entry for write()

I suspect that the left column represents the way things have been,
and the right column represents the way some people feel things 
ought to be.

Also, I looked at "Reliable Data Structures in C" by Thomas Plum.
He is mostly discussing draft ANSI C, and that does not define write().
He shows how to implement an equivalent function bin_write() portably.
And he uses a signed variable for nbytes.

But on a machine with 16-bit ints, using a signed nbytes unnecessarily
restricts the size of the largest block that can be written.

One possible solution:

     size_t write (fildes, buf, nbytes)
     int fildes; char *buf; size_t nbytes;

     The value supplied for `nbytes' must be in the range
     0 through (((size_t) -1) - 1), where size_t is an unsigned type
     defined in a header file.  Up to `nbytes' bytes of data are
     written each time write() is called.  The return value is the
     actual number of bytes written, unless an error occurs, in which
     case the value returned is ((size_t) -1).
-- 
Rahul Dhesi         UUCP:  {ihnp4,seismo}!{iuvax,pur-ee}!bsu-cs!dhesi