/*  IPC Port Lister program 90:11:18 */

/***************************************************************
 * This program has just one function:
 *
 *  It prints a list of all
 *  the IPCPorts currently defined, with the number of
 *  references (Clients or Servers) to each, and any state
 *  flags set.
 *
 **************************************************************/


#include <stdio.h>
#include "IPCPorts.h"
#include "IPC_proto.h"
#include "IPC.h"

#include "exec/memory.h"


ULONG IPCBase;

struct IPCPort *testport;

char tportname[]= " CleaNeR  PoRt --";  /* a VERY odd name... */


void showport(struct Node * np)
{
    struct IPCPort * ip = (struct IPCPort *) np;
    int refs = CheckIPCPort(ip, 0);
    printf("%s: %d reference%s\n", ip->ipp_Name, refs, refs == 1 ? "" : "s");
    if (ip->ipp_Flags)
        printf("  -- marked as %s%s%s%s%s\n",
                ip->ipp_Flags & IPP_LOADING ? "LOADING " : "",
                ip->ipp_Flags & IPP_SERVED ? "SERVED " : "",
                ip->ipp_Flags & IPP_REOPEN ? "REOPEN " : "",
                ip->ipp_Flags & IPP_SHUT ? "SHUT " : "",
                ip->ipp_Flags & IPP_NOTIFY ? "NOTIFY " : "");
}

void main(int argc, char ** argv)
{
    struct Node *np; /* actually an IPCPort (but changed back when needed) */

    IPCBase = OpenLibrary("ppipc.library",0);
    if (!IPCBase) {
        puts("couldn't find IPC Library -- TTFN...\n");
        exit(20);
    }

    testport = GetIPCPort(tportname);   /* now at head of list */
    for (np = (struct Node *)testport->ipp_Port.mp_Node.ln_Succ;
                     /* don't include testport...*/
                np->ln_Succ; np = np->ln_Succ)
            showport(np);
    DropIPCPort(testport);

    CloseLibrary(IPCBase);
}


