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