Missing stdio features.

Julian Onions jpo at cs.nott.ac.uk
Mon Jul 28 21:42:19 AEST 1986


I try to use stdio as often as possible for several reasons (such as
portability, efficiency, laziness ...) but I feel it lacks one or two calls
to achieve some of the less every-day options.
Most of these can be got around if you `know' the internal stdio
structure - but that negates the portability win. Anyway, anyone care to
a) comment on these
b) mention their own pet stdio hates
c) show portable ways to implement these
d) tell me they're already implemented ==> RTFM.

-------------------------

Addition:	fpending(fp)

Synopsis:	returns the number of characters that are buffered up
		on the stream pointed to by fp.

Use:		Has several uses, in particular useful when you are
		applying select(2) to a FILE, select may say there is
		nothing to read when in actual fact there is stuff
		buffered up. The only other way around this is to
		set buffering to 0 which is rather inefficient.
		It's trivial to add this as another macro.

-------------------------

Addition:	finvbuf(fp)

Synopsis:	invalidate in-core stdio buffer for FILE *fp.

Use:		If you know by other information that a file you
		currently have open has changed then you need to
		force a reread of the file ensuring that all buffered
		information is junked. The only safe/portable alternative
		is to fclose/fopen the file descriptor again which is
		reasonably expensive. It should probably not guarantee
		anything about the offset or else do a rewind(3).

-------------------------

Addition:	fslam(fp)

Synopsis:	shut the given descriptor in a hurry without flushing it.

Use:		If you know that flushing a descriptor will cause a block.
		e.g. you have a timeout writing to a terminal cos someones
		^S'd it, so you decide to ignore that terminal & carry on.
		How can you shut it, fclose will try and flush the data
		and will hang the program again. If finvbuf() is in place
		this could be used instead. I suppose you could get around
		it with
			close(fileno(fp));
			fclose(fp);
		but it seems a bit messy and may cause indigestion for
		the fclose routine.

-------------------------

Addition:	fbufsiz(fp), fbuftype(fp)

Synopsis:	fbuzsiz returns size of buffer in use, fbuftype indicates
		what sort of buffering is in use (none, line or full).

Use:		To determine whats going on dynamically. E.g.
		if( fbufsiz(stderr) == 0 || fbuftype(stderr) == _IONBF)
		{
			printf("stderr non buffered - fixing\n");
			fclose(stderr);
			stderr = fdopen(2, "w")
			setbuf(stderr, buffer);
		}
		basically, you can set lots of interesting buffering modes
		but you can't discover whats in use.

-------------------------

An inconsistency - why is there no ungetchar(c) defined??


Julian.

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



More information about the Comp.lang.c mailing list