
/* Used by both Maelstrom and the sound server for Maelstrom */

#include <stdio.h>
#include <stdlib.h>

#ifdef TESTING
#define LIBDIR	"."
#else
#ifndef LIBDIR
#define LIBDIR	"/usr/local/lib/Maelstrom"
#endif
#endif /* TESTING */

char *file2libpath(char *filename)
{
	char *libdir;
	static char fullpath[256];

	if ( (libdir=getenv("MAELSTROM_LIB")) == NULL )
		libdir=LIBDIR;
	sprintf(fullpath, "%s/%s", libdir, filename);
	return(fullpath);
}

#ifdef _SGI_SOURCE
#include <unistd.h>
#include <limits.h>
void usleep(unsigned long usec)
{
	sginap((CLK_TCK*usec)/1000000);
}
#else
#ifdef _INCLUDE_HPUX_SOURCE

/* Sort of a kludgy usleep().  It assumes that there will be no SIGALRM
   pending besides ours.  Support is there for better SIGALRM handling.
*/
#include <unistd.h>
#include <time.h>
#include <signal.h>

static void (*old_alarmhandler)(__harg);
static struct itimerval old_itimer;
static int alarmed;
static void new_alarmhandler(__harg unused)
{
	alarmed=1;
}
void usleep(unsigned long usec)
{
	struct itimerval usleep_itimer;

	old_alarmhandler=signal(SIGALRM, new_alarmhandler);
	usleep_itimer.it_interval.tv_sec = 0;
	usleep_itimer.it_interval.tv_usec = 0;
	usleep_itimer.it_value.tv_sec = (usec/1000000);
	usleep_itimer.it_value.tv_usec = (usec%1000000);
	setitimer(ITIMER_REAL, &usleep_itimer, &old_itimer);
	alarmed=0;
	while ( ! alarmed )
		pause();
	signal(SIGALRM, old_alarmhandler);
}
#endif /* HPUX */
#endif /* SGI */
