[comp.sys.atari.st] Did I come from the desktop?

jmw@tasis.utas.oz.au@munnari.oz (John Williams) (11/26/89)

A recent article suggested three possible methods by which a process  could
determine whether it was being run from the desktop or from a shell. I'd like
to suggest another method which works for me. I don't suggest that it is
foolproof however. It simply requires moving the basepage ptr up to its parent
until it becomes null. If this requires three iterations then the program
is running from the desktop. Any more and it is running from a shell.

The following piece of code works with MWC:

int	fromdesk()
{
	BASEPAGE	*bp;
	int		i = 0;

	bp = (BASEPAGE **) BP->p_parent;
	while (bp != 0) {
		i++;
		bp = (BASEPAGE **) bp->p_parent;
	}
	return (i == 3);
}

John M Williams
ACSnet: jmw@tasis.utas.oz	ARPA: 	jmw%tasis.utas.oz@uunet.uu.net
UUCP: 	{enea,hplabs,mcvax,uunet,ukc}!munnari!tasis.utas.oz!jmw

dal@syntel.mn.org (Dale Schumacher) (11/30/89)

[jmw@tasis.utas.oz.au@munnari.oz (John Williams) writes...]
> A recent article suggested three possible methods by which a process  could
> determine whether it was being run from the desktop or from a shell. I'd like
> to suggest another method which works for me. I don't suggest that it is
> foolproof however. It simply requires moving the basepage ptr up to its parent
> until it becomes null. If this requires three iterations then the program
> is running from the desktop. Any more and it is running from a shell.

Alright, I guess it's time to post this message again (is this the 3rd
time now?).  The quoted message is mostly correct, but incomplete.  The
situation is slightly different if you're in the /AUTO/ folder programs
at boot-time.  I check for the text-base pointer pointing to ROM, as
recommended by Alan Pratt @ Atari.  The following code (which, like John's
code, assumes access to a basepage pointer) uses this "approved" method.
I've also included a copy of <basepage.h> from dLibs for those who don't
know the layout of the basepage structure.

/***********************************************************************/
#include <basepage.h>

#define	FROM_AUTO	0
#define	FROM_DESKTOP	1
#define	FROM_SHELL	2

camefrom()
	{
	register BASEPAGE *p = _base;
	register int n = 0;

	p = p->p_parent;
	if((((long) (p->p_tbase)) & 0x800000L) == 0L)
		return(FROM_SHELL);
	while(p = p->p_parent)
		++n;
	return((n == 1) ? FROM_AUTO : ((n == 2) ? FROM_DESKTOP : FROM_SHELL));
	}
/***********************************************************************/
/*
 *	BASEPAGE.H	Definition of the basepage structure
 */

#ifndef BASEP_H
#define	BASEP_H

typedef struct basep
	{
	char		*p_lowtpa;	/* pointer to self (bottom of TPA) */
	char		*p_hitpa;	/* pointer to top of TPA + 1 */
	char		*p_tbase;	/* base of text segment */
	long		p_tlen;		/* length of text segment */
	char		*p_dbase;	/* base of data segment */
	long		p_dlen;		/* length of data segment */
	char		*p_bbase;	/* base of BSS segment */
	long		p_blen;		/* length of BSS segment */
	char		*p_dta;		/* pointer to current DTA */
	struct basep	*p_parent;	/* pointer to parent's basepage */
	char		*p_reserved;	/* reserved for future use */
	char		*p_env;		/* pointer to environment string */
	long		p_undef[20];	/* scratch area... don't touch */
	char		p_cmdlin[128];	/* command line image */
	}
	BASEPAGE;

extern	BASEPAGE	*_base;

#endif BASEP_H
/***********************************************************************/

\\   /  Dale Schumacher                         399 Beacon Ave.
 \\ /   (alias: Dalnefre')                      St. Paul, MN  55104-3527
  ><    ...umn-cs!midgard.mn.org!syntel!dal     United States of America
 / \\   "What is wanted is not the will to believe, but the will to find out,
/   \\  which is the exact opposite." -Bertrand Russell

apratt@atari.UUCP (Allan Pratt) (12/02/89)

dal@syntel.mn.org (Dale Schumacher) writes:
>I check for the text-base pointer pointing to ROM, as
>recommended by Alan Pratt @ Atari.

Yeah, well, that was before there were RAM TOSes running around. Caveat
hacker.  No RAM TOS is supported by Atari (at this time) and most are
illegal copies, making their users pirates, so it's not that great a
hardship.

============================================
Opinions expressed above do not necessarily	-- Allan Pratt, Atari Corp.
reflect those of Atari Corp. or anyone else.	  ...ames!atari!apratt

VBRANDT@DBNUAMA1.BITNET (12/20/89)

Hello all,

[I'm sorry that I reply only now to this message, but the recent amount of
 Info-Atari16 Digests gave me a hard time catching up.]

In Info-Atari16 #705, John M Williams (jmw%tasis.utas.oz@uunet.uu.net) said
in reference to my earlier posting:

>A recent article suggested three possible methods by which a process  could
>determine whether it was being run from the desktop or from a shell. I'd like
>to suggest another method which works for me. I don't suggest that it is
>foolproof however. It simply requires moving the basepage ptr up to its parent
>until it becomes null. If this requires three iterations then the program
>is running from the desktop. Any more and it is running from a shell.
[Code example omitted]

In Digest #734, nis!pwcs!stag!daemon@UMN-CS.CS.UMN.EDU (Dale Schumacher)
replies:

>Alright, I guess it's time to post this message again (is this the 3rd
>time now?).  The quoted message is mostly correct, but incomplete.  The
>situation is slightly different if you're in the /AUTO/ folder programs
>at boot-time.  I check for the text-base pointer pointing to ROM, as
>recommended by Allan Pratt @ Atari.  The following code (which, like John's
>code, assumes access to a basepage pointer) uses this "approved" method.
>I've also included a copy of <basepage.h> from dLibs for those who don't
>know the layout of the basepage structure.
[Code example omitted]

Finally, in Digest #740, portal!atari!apratt@uunet.uu.net (Allan Pratt) joins
the discussion:

>dal@syntel.mn.org (Dale Schumacher) writes:
>>I check for the text-base pointer pointing to ROM, as
>>recommended by Allan Pratt @ Atari.
>
>Yeah, well, that was before there were RAM TOSes running around. Caveat
>hacker.  No RAM TOS is supported by Atari (at this time) and most are
>illegal copies, making their users pirates, so it's not that great a
>hardship.

  Thank you all for replying.  One of the things I wanted to find out by posting
my original code samples was whether one can rely on the 'somewhat-documented'
facts one has to exploit to find out where the process came from.

   Is the three-times-iteration method approved?  Will it hold true in later
TOS revisions?  Same goes for my method, checking the os_magic pointer.  I took
pains to ensure that the code samples I sent out also work with TOS 1.6 (STE)
and TOS 3.0 (TT).  Dales version has the advantage of also detecting that the
program was started from the auto folder.  My version also works with RAM and
STE/TT TOSes.

   Maybe Allan can tell us if one or the other method will be supported by
Atari, or if there will be some kind of system variable that tells the current
process if his parent is a shell or the desktop.  Such a variable would also
make life easier for programs like Neodesk etc.

   A side note to Allan:  Take the ROM TOS 1.4, disassemble it, make it fully
relocatable, write a little RAM loader, and you have a RAM TOS.  Does that make
me a pirate???   [BTW: Such a RAM TOS is great for testing out little OS tweaks
and patches ... :-)]


----------------------------------------------------------------------------
Bitnet:  VBRANDT@DBNUAMA1 (will go away some day ...)  Volker A. Brandt
          UNM409@DBNRHRZ1 (alternative)                Angewandte Mathematik
UUCP:    ...!unido!DBNUAMA1.bitnet!vbrandt             (Bonn, West Germany)
ARPAnet: VBRANDT%DBNUAMA1.BITNET@CUNYVM.CUNY.EDU