Some hints for programming ATARI TOS programs with SOZOBON C

1. Accessories

1.1 streams
Never use 'printf()' or scanf()' function in Accessories. Don't access to
stdin, stdout or stderr streams.

1.2 environment
Cause not all TOS versions set the environment pointer in the Basepage of
an accessory process, I recommend to insert one of following functions
in your 'main' module. (the one where function 'main()' is to find.)

If you don't need any environment strings, and you are shure that library
functions don't do so:

char *getenv(char *var)
{
	return(NULL);
}

else:

char *getenv( char *var)
{
	char *string;

	if (gl_apversion == 0)	/* don't call before appl_init()	*/
		return(NULL);
	shel_envrn(&string, var)
	if (string) {
		if (*string == '=')	/* is this if/else okay ??	*/
			return(++string);
		else
			return("");
	} else
		return NULL;
		
}	/* acc getenv() routine	*/

1.3 memory

Another problem is access to dynamic memory. Accessories can't keep
their allocated memory passing an AC_CLOSE event. On the other hand
it is to detect wether the memory is to free or already freed.

There is function
extern BASEPAGE *Getbpact_pd();
in the library file 'extended.lib'.

You call it after your first malloc() or Malloc() and save the returned
value. 
   BASEPAGE *m_act_pd;	/* global variable */

   /* your malloc() succeded, and now: */
   if(!m_act_pd)
	   m_act_pd = Getbpact_pd();

After your accessory gets an AC_CLOSE, you have to free you memory or at
least delete all references into this memory blocks. With following example
code you can test wether you have to free or not:

   BASEPAGE *c_act_pd;
   
   if ((gl_apid >= 0x300) || (m_act_pd == (c_act_pd = Getbpact_pd()) )||
		(m_act_pd == c_act_pd->p_parent) ){
       /* free all allocated blocks */
   }
   /* delete all references to the memory blocks */

   m_act_pd = NULL;
   /* set to NULL, to get the current value after next malloc()/Malloc()
      call. */

Of course you can figure out wether you may keep the blocks after this
AC_CLOSE, but this would be a little more complicate, and you'ld need a
special memory handling for this.


