/**********************************************************************
 * ti library's function for determining the number of parents a node has
 *
 * $Author: brlewis $
 * $Source: /mit/techinfodev/src/libti/RCS/nparents.c,v $
 * $Header: /mit/techinfodev/src/libti/RCS/nparents.c,v 4.1 1995/04/10 20:21:57 brlewis Exp $
 *
 * Copyright 1993 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_ti_nparents_c[] = "$Header: /mit/techinfodev/src/libti/RCS/nparents.c,v 4.1 1995/04/10 20:21:57 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_nparents(TI *tp, long id, int *np)
 *
 * - puts number of parents in np if there's no error
 * - returns error code
 **********************************************************************/

long
ti_nparents(tp, id, np)
     TI *tp;
     long id;
     int *np;
{
  long code=0L;
  char tosend[16], torecv[BUFSIZ];
  char *ptr;

  /* construct techinfo command */
  sprintf(tosend, "s:%ld\n", id);

  /* send command and receive reply */
  if (code = _ti_talk(tp, tosend, strlen(tosend),
		      torecv, BUFSIZ-1)) return(code);

  if (!(ptr=strrchr(torecv, ':')))
    {
      strncpy(ti_context, torecv, 126);
      ti_context[127]='\0';
      return(TI_ERR_SRVHUH);
    }

  if (*(--ptr) == ':') return(0); /* no parents */

  /* one or more parents */
  *np = 1;
  while(*ptr != ':' && ptr > torecv)
    if (*(ptr--) == ',') (*np)++;

  return(0L);
}
