/****************************************************************************
 *
 * $Source: /usr/local/cvsroot/gccsdk/unixlib/source/common/prefix.c,v $
 * $Date: 2004/01/14 23:16:59 $
 * $Revision: 1.1 $
 * $State: Exp $
 * $Author: joty $
 *
 ***************************************************************************/

#ifdef EMBED_RCSID
static const char rcs_id[] = "$Id: prefix.c,v 1.1 2004/01/14 23:16:59 joty Exp $";
#endif

#include <stdlib.h>
#include <string.h>
#include <swis.h>
#include <unixlib/local.h>
#include <unixlib/os.h>
#include <unixlib/unix.h>

/* Returns malloced block of memory holding DDEUtils_Prefix non zero-length
   string value or NULL when there isn't one set.  Caller needs to free
   memory after use.  */
const char *__get_dde_prefix (void)
{
  int regs[10];
  char *result;

  regs[0] = 0; /* Use the currently active object number.  */
  if (__os_swi (DDEUtils_ReadPrefix, regs) == NULL)
    {
      size_t size;
      const char *prefix;

      if ((prefix = (const char *)regs[0]) == NULL)
        return NULL;

      /* returned string can be control char terminated.  */
      while ((unsigned char)*prefix >= ' ')
        ++prefix;
      /* Filter out zero length results.  */
      if ((size = prefix - (const char *)regs[0]) == 0)
        return NULL;
      if ((result = (char *)malloc (size + 1)) == NULL)
        return NULL;
      memcpy (result, (const void *)regs[0], size);
      result[size] = '\0';
    }
  else
    {
      /* SWI DDEUtils_ReadPrefix is not known.  */
      result = __getenv_from_os ("Prefix$Dir", NULL, 0);

      /* Filter out zero length results.  */
      if (result[0] == '\0')
        {
          free (result);
          return NULL;
        }
    }

  return result;
}
