9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Nils M Holm" <nmh@t3x.org>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] Help with interrupting fgetc()
Date: Sat, 27 Jun 2015 23:19:43 +0200	[thread overview]
Message-ID: <20150627211943.GA5035@ananda.local> (raw)
In-Reply-To: <CAOw7k5gd8NLYukayhHNh_aOAPDc_QEy293mJEqmk2fCJ0rfnJg@mail.gmail.com>

On 2015-06-27T21:58:47+0100, Charles Forsyth wrote:
> if interrupted, fgetc returns EOF because the underlying read system call
> returns -1 (with error string "interrupted").
> System calls are interrupted by a note (see notify(2)). Your loop will
> therefore stop and exit.
>
> Also, see /sys/doc/comp.ms for some other details of the Plan 9 C
> environment. For instance, main is
>       void main(int, char**);
> and you need an explicit call to exits [sic] at the end of main, not a
> return 0 (or you'll get an error status "main"
> returned to the shell).

Thanks! I updated my code accordingly, and it works fine now. It seems
like a clearerr(stdin) is necessary to restore the original behavior of
fgetc() after an interrupted system call. Would the below code be
acceptable?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#include <u.h>
#include <libc.h>
#include <stdio.h>

volatile int intr = 0;

void n(void *x, char *s) {
	if (!strcmp(s, "interrupt")) {
		print("oopsie!\n");
		intr = 1;
		noted(NCONT);
	}
	else {
		noted(NDFLT);
	}
}

void main(int argc, char **argv) {
	int	c;

	notify(n);
	c = fgetc(stdin);
	while (c != EOF || intr) {
		if (intr)
			clearerr(stdin);
		else
			fputc(c, stdout);
		intr = 0;
		c = fgetc(stdin);
	}
	exits(NULL);
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

--
Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org



  reply	other threads:[~2015-06-27 21:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-27 20:30 Nils M Holm
2015-06-27 20:58 ` Charles Forsyth
2015-06-27 21:19   ` Nils M Holm [this message]
2015-06-27 21:12 ` Charles Forsyth
2015-06-27 21:21   ` Nils M Holm
2015-06-28  1:29 ` erik quanstrom
2015-06-28  7:29   ` Nils M Holm

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=20150627211943.GA5035@ananda.local \
    --to=nmh@t3x.org \
    --cc=9fans@9fans.net \
    /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).