[comp.sys.atari.st.tech] the cookie jar: a sample program

7103_2622@uwovax.uwo.ca (Eric Smith) (10/01/90)

There have been several requests for information about the cookie jar.
Here's a sample program to print all the cookies in the system. It
illustrates how to read the cookie jar. Putting cookies in the jar is
somewhat more complicated, since under TOS 1.4 and lower you have to
install a reset vector to make sure the cookie jar pointer is cleared on
a warm boot. The MiNT source code has an example of how to do this.

If you have TOS <= 1.4, and no TSRs, there won't be any cookies. If you have
TOS 1.6 or higher, or are using MiNT, there will always be *some* cookies.
--
Eric R. Smith                     email:
Dept. of Mathematics            ersmith@uwovax.uwo.ca
University of Western Ontario   ersmith@uwovax.bitnet
London, Ont. Canada N6A 5B7
ph: (519) 661-3638
============================== cut here ===================================
/*
 * printjar: print all the cookies in the system. Written by Eric R. Smith and
 * placed in the public domain.
 */

#include <osbind.h>

/* we use a union because most cookie tags are 4 ASCII characters, so it
   may be useful to sometimes think of them as strings. On the other hand,
   they *are* longwords, so we may want to access them that way, too
 */

union clong {
	char	aschar[4];
	long	aslong;
};

/*
 * a cookie consists of 2 longwords; a tag and a value. The tag is normally
 * chosen to have some sort of significance when represented as 4 ascii
 * characters (see the union definition above). What the value represents
 * is dependent on the tag; it may be an address (for a TSR), or a version
 * number (e.g. MiNT does this), or whatever (it may not even have a meaning).
 */

struct cookie {
	union clong tag;
	long value;
};

typedef struct cookie COOKIE;

/*
 * A pointer to the cookie jar is found at 0x5a0. If there is no cookie jar
 * installed, this pointer will be 0. The cookie jar itself is an array
 * of cookies, with the last cookie having a "tag" of 0x00000000. (The value
 * of this cookie is the number of slots left in the cookie jar.)
 */

#define CJAR	((COOKIE **) 0x5a0)


/*
 * a simple program to print all the cookies in the system
 */

main()
{
	long ssp;
	char *p;
	COOKIE *cookie;

	ssp = Super(0L);
	cookie = *CJAR;

	if (cookie) {
		while (cookie->tag.aslong != 0) {
			p = cookie->tag.aschar;
			printf("%c%c%c%c = %lx\n",
				p[0],p[1],p[2],p[3],cookie->value);
			cookie++;
		}
	}
	exit(0);
}