Missing stdio features.

karl at haddock karl at haddock
Tue Sep 9 04:54:00 AEST 1986

umcp-cs!chris (Chris Torek) writes:
>In article <86900034 at haddock> karl at haddock writes:
>>>>FILE *fopenfxn(int (*fxn)(), char *mode)
>>Actually, the function argument should probably be analogous to read/write
>>rather than getc/putc.  But there should be one more argument to fopenfxn(),
>>viz. a (void *) argument to be passed to fxn() to distinguish streams.
>Indeed, there would be a certain symmetry to the whole thing if one
>could write
>    reader(f, buf, len) FILE *f; { return (read(fileno(f), buf, len)); }
>    FILE *f = fopenrf(reader, "r");
>    fileno(f) = fd;
>instead of
>    FILE *f = fdopen(fd, "r");

As I mentioned, I think it has to be (void *) in general, thus
   reader(void *v, char *buf, int len) {
       return (read(*(int *)v, buf, len));
   FILE *f = fopenrf(&fd, reader, "r");
so that the more general cases could be supported, e.g. read from string:
   typedef struct { char *t_start, *t_end; } tbuf;
   reader(void *v, char *buf, int len) {
       register tbuf *t = (tbuf *)v;
       len = min(len, t->t_end - t->t_start);
       memcpy(buf, t->t_start, len);
       t->start += len;
       return (len);
   FILE *f = fopenrf(&t, reader, "r");

You could get away with having the first arg to reader() be "FILE *", but in
any case "void *_id" needs to replace or supplement the existing "int _file"
in the FILE structure.

If there are separate functions fopenrf() and fopenwf(), is there any need
for the third argument?

Karl W. Z. Heuer (ima!haddock!karl; karl at haddock.isc.com), The Walking Lint

More information about the Comp.lang.c mailing list