From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Nils M Holm" Date: Sat, 27 Jun 2015 23:19:43 +0200 To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Message-ID: <20150627211943.GA5035@ananda.local> References: <20150627203011.GB2121@ananda.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: Re: [9fans] Help with interrupting fgetc() Topicbox-Message-UUID: 5c4dd558-ead9-11e9-9d60-3106f5b1d025 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 #include #include 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