/*
**  ShowPorts.c by Daniel Balster
**
**  lists all message ports by name to stdout
**
**  useful to get a quick overview of all ports or if you are searching for
**  the name of an arexxport of an unknown application.
**
**  needs os20 or higher, terminates quitely if the requested OS is not available.
**  must be run from cli/shell or the wb's "execute command" ability.
*/

#include <exec/exec.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

extern struct ExecBase *SysBase;

/*  no use of init/startup/exit code to gain bytes */

void _main( void )
{
    struct Library  *DOSBase;

    if( DOSBase = OpenLibrary( "dos.library", 37L ) )
    {
	struct Process *process = FindTask( 0 );
	if( process->pr_CLI )
	{
	    Forbid();

	    struct Node *node = (struct Node *) SysBase->PortList.lh_Head;

	    do {
		VPrintf( "%s\n", &(node->ln_Name) );
		node = node->ln_Succ;
	    } while( node );

	    Permit();
	}
	else
	{
	    /*	workbench stuff.
		do not add an icon, it should only be used by shell */

	    WaitPort( &process->pr_MsgPort );
	    ReplyMsg( GetMsg( &process->pr_MsgPort ) );
	}

	CloseLibrary( DOSBase );
    }
}
