magic cookies given back by ftell, and used in fseek

Chris Torek chris at mimsy.UUCP
Sat May 28 05:51:03 AEST 1988


In article <11695 at mimsy.UUCP> I proposed making `seek cookies' in
the following manner:
>Allocate a block (`array') of real information, and return indicies
>into this block (a la file descriptors).  The block can be expanded as
>necessary by using realloc().

I then noted that

>The Unix kernel simplifies the task by fixing the number of descriptors
>(usually at something like 20 or 64 or 256).

I stopped here because the fire alarm was going off in my apartment
building.  (False alarm, fortunately.)  There is a more serious
problem with this scheme, and that is that there is no decent way
to tell when an ftell cookie is dead and can be freed.  A small,
limited number of cookies is insufficient.

Of course, one can flush all the cookies for a file when that file
is closed, but it is easy to imagine a file that stays open for
weeks at a time, seeking all the while.

Various solutions exist, none particularly clean.  The most obvious
are to add a function for disposing of seek cookies (`fkillseek()'?),
and to deem that a cookie dies when it is used:

	long save_pos;

	save_pos = ftell(f);
	// work on f
	(void) fseek(f, save_pos, 0);

	/* if we need it again, we must reactivate it: */
	save_pos = ftell(f);

The former requires an incompatible addition to stdio, while the
latter requires an incompatible change to those things that use
stdio.  Which would be easier remains uncertain, although at least
the latter rule has the virtue of simplicity.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list