public inbox for linux-serial@vger.kernel.org
 help / color / mirror / Atom feed
From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-serial@vger.kernel.org
Subject: Re: result=read(fd,buffer,200);
Date: Tue, 20 Jul 2004 19:09:32 +0200	[thread overview]
Message-ID: <20040720170931.GB4690@lug-owl.de> (raw)
In-Reply-To: <20040720174142.7b8b6525@emig4.heisch.inka.de>

[-- Attachment #1: Type: text/plain, Size: 1384 bytes --]

On Tue, 2004-07-20 17:41:42 +0200, Gerald Emig <gme@emig-software.de>
wrote in message <20040720174142.7b8b6525@emig4.heisch.inka.de>:
> According to the documentation provided in info:libc, read 
> returns the number of character it actually has read.
> Especially if you are using serial ports, the amount read is often fewer
> as you expected.

That's a general thing to keep in mind.

Additionally, keep in mind that c_cc[VMIN] will control how long to
wait, too...

> Additionally, checking errno is stronly recommended.

As always:)

> Use something like the following to read exactly 200 chars:
> 
> 
> int read_amount (int iwantread)

This int should have been size_t.

> {
> 	int readpointer, fetch;

...and fetch needs to be ssize_t.

> readpointer=0;
> while (iwantread>0) {
> 	fetch=read (fd,&buffer[readpointer],iwantread);
> 	if (fetch >= 0) {
> 		readpointer += fetch;
> 		iwantread -= fetch;
> 	} else 	if ((errno!=EINTR) && (errno!=EAGAIN)) return -1;
> 	}
> 	return iwantread; // should be 0 on success
> }

MfG, JBG

-- 
   Jan-Benedict Glaw       jbglaw@lug-owl.de    . +49-172-7608481
   "Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg
    fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!
   ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

      reply	other threads:[~2004-07-20 17:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-20 13:12 result=read(fd,buffer,200); Sayang Oin
2004-07-20 15:41 ` result=read(fd,buffer,200); Gerald Emig
2004-07-20 17:09   ` Jan-Benedict Glaw [this message]

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=20040720170931.GB4690@lug-owl.de \
    --to=jbglaw@lug-owl.de \
    --cc=linux-serial@vger.kernel.org \
    /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