Missing stdio features.

Julian Onions jpo at cs.nott.ac.uk
Mon Aug 18 20:40:47 AEST 1986


In article <479 at mcgill-vision.UUCP> mouse at mcgill-vision.UUCP writes:
>> Addition:	fpending(fp)
>(Henry Spencer comments that "the right solution to that is fselect(),
>not fpending().".  I disagree; suppose you want to select on a FILE*
>and on a file descriptor both?  How do you tell the difference?

OK, I'm greedy, I want both. fpending(fp) I wanted after some code
that checked the input for pending characters using FIONREAD.
fselect is rather involved requiring several lines of code,
fpending comes out as something like
#define fpending(fp) ((fp)->_cnt)
Give either fpending or fselect the other can be built and fpending seems
simplest, but maybe not the answer to the right question.
All in all, fselect is the right way to go (stdio features tracking
system calls), but fpending is so trivial it might as well be thrown in.

>> Addition:	fbufsiz(fp), fbuftype(fp)
>Never wanted 'em.
Neither have I, but for the sake of consistency it'd be nice to tell
what buffering is set as well as being able to set buffering.

>Here are the things I have wanted badly enough to add to stdio:
>FILE *fopenfxn(int (*fxn)(), char *mode)
>	Function-stream I/O.
Can't say I've ever thought about this one. I'm not sure when you'd
use it either.

>FILE *fopenstr(char *str, int len, char *mode)
>	An extension to sprintf() and sscanf().  This returns a stream
>	which performs I/O to a string (this makes sprintf() and
>	sscanf() unnecessay, though they are still convenient).
I like this one, C++ has something like this in its streams stuff,
and I liked the idea there. Being able to treat incore strings and
files the same seems a good idea.
e.g.
	if(argc>1)
		if(( fp = fopen(argv[1], "r")) == NULL)
			process(fp = fopenstr(argv[1], strlen(argv[1]), "r"));
		else	process(fp);
	else
		process(fp = stdin);
	fclose(fp);

>unfdopen(FILE *f)
>	Undoes fdopen(), that is, closes the FILE* without close()ing
>	the underlying file descriptor.
Yes, I'd thought of that, however as you say its trivial and can be
done reasonably portably e.g.
unfdopen(fp)
FILE *fp;
{	int oldf, tmpf;
	tmpf = dup(oldf = fileno(fp));
	fclose(fp);
	return dup2(tmpf, oldf);
}
Seems like a good idea. When you've finished playing around with
fprintf's etc. and just want the file descriptor for locking or
some other nefarious process and not all the expensive buffering space.
I'd prefer fdclose as the name though.

Julian.

jpo at cs.nott.ac.uk
jpo%cs.nott.ac.uk at ucl-cs.arpa
mcvax!ukc!nott-cs!jpo
-- 
Julian Onions



More information about the Comp.lang.c mailing list