HP TRICKS (home-grown and from news postings)
	=============================================

Things to watch out for:

TIOCNOTTY     - this is not available on HP-UX (use setsid()); trying
		to open '/dev/tty' from a "daemon" will also fail with
		status -1.
setpgrp       - this doesn't get rid of the controlling terminal
		if you use -lBSD (use setsid() instead).
BSD signals   - you almost certainly want them if your program came from
		a BSD system (e.g. Sun); this breaks 'setpgrp' (and
		'system' if your process has active child processes)
		among other things (see 'man bsdproc' and notes below).
		If all you need are BSD signals, try the replacement signal
		routine below which gives BSD semantics without all the
		other damage that using "-lBSD" causes.
/etc/utmp     - contains all sorts of records, not just current logins;
		old login records are found if file is read blindly.
setproctitle  - doesn't work as expected (see notes below for a version
		that really does work on HP-UX).
compiler defs - in K&R mode (the default), the HP C compiler defines "hpux";
		in ANSI mode (-Aa or -Ae), the HP C compiler defines "__hpux";
		the gcc (Gnu) C compiler defines "__hpux__". Thanks to
		Jarkko Hietaniemi  for this note.

***************************************************************************

John Agosta , from HP has contributed some notes on
the HP Porting Guide (which incorporates many of the things from this
posting).

Announcing the SunOS to HP-UX 9.05 Porting Guide
------------------------------------------------

If you are migrating software to HP-UX, or are just new to the HP-UX
development environment, HP has a new document which should help ease your
transition.  It is the "SunOS to HP-UX 9.05 Porting Guide".

This guide provides, in one easily readable document, information and
hints on porting from SunOS 4.1.3 to HP-UX 9.0x.  A porting experience
from SunOS to HP-UX need not, and should not be a painful one.  HP-UX has
many of the same system calls and tools found on SunOS.  This guide should
direct you through most of the known differences, and facilitate your
introduction to the development environment on HP-UX.

The guide is an assimilation of white papers, discussions on the Internet,
and most importantly, the real life porting experiences of customer and HP
engineers, channel partners, and ISVs porting from SunOS to HP-UX.

Even though this guide centers on the differences between SunOS 4.1.3 and
HP-UX, many customers who are new to HP-UX have found this document
extremely valuable.  This guide is highly recommended to anyone moving
from *SunOS, *Solaris, *AIX, or any other form of *UNIX to HP-UX.

How to Obtain the Porting Guide
-------------------------------

Electronic copies of the guide are available via the Interworks Library.
The Library may be accessed via:

	FTP:      www.interworks.org
	WWW URL:  ftp://www.interworks.org

/pub/comp.hp/porting_info/
	sun_hpux_port_ascii_0295 	ASCII version of the Porting Guide
	sun_hpux_port_html_0295.tar 	WWW HTML version of the Porting Guide
	sun_hpux_port_ps_0295.tar 	Postscript (level 3) version

Hard copies are available free of charge ONLY through your local HP Sales
Representative.  Please reference HP Literature Distribution Center
document number 5963-5416E.

I have appended the Table of Contents for the "SunOS to HP-UX 9.05
Porting Guide" to the end of this posting.

***************************************************************************


Individual routines you may find helpful:
-----------------------------------------


getwd:
------

/*
 * Replace 'SIZEOFARG-1' with the declared size of "arg", minus 1.
 *
 * For non-ANSI C, you can use -lBSD to load getwd, but see
 * 'man bsdproc' for other things that will been changed/damaged.
 */

#if defined(__hpux) || defined(__hpux__)
#include 
#define getwd(arg)	getcwd(arg, (size_t) SIZEOFARG-1)
#else
char	*getwd();
#endif


getrusage:
----------

~From: scot@pawnee.ugrad.ee.ufl.edu (Scott Miller)
~Newsgroups: comp.sys.hp
~Subject: Re: Where is getrusage()? (Summary)
Organization: UF EE Department

getrusage() is in the syscall includes

Here is the code fragment I used:

#if defined(hpux) || defined(__hpux) || defined(__hpux__)
#include 
#define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
#endif /* hpux */


srandom, random:
----------------

For a conversion to a "better" generator, use:

#define srandom srand48
#define random lrand48

For a simple conversion, use:

~From: mjo@snclib.snc.edu (Mike O'Connor)
~Newsgroups: comp.sys.hp
~Subject: Re: random and srandom on HP9000/720 with HPUX-8.07

#define srandom srand
#define random rand

~From: misar@rbg.informatik.th-darmstadt.de (Walter Misar)

> #define srandom srand48
> #define random lrand48

This can have very strange effects since lrand() returns numbers between
0 and 2^31, but random() is supposed to return 31 bit random numbers.
Perhaps a better solution would be:

#define random() ((long)(drand48()*(unsigned)(1<<31)))


getdtablesize:
--------------

/*
 * getdtablesize ()
 *
 * Returns the maximum number of file descriptors allowed.
 */

#include 

	int
getdtablesize ()
{
	return(sysconf(_SC_OPEN_MAX));
}


usleep:
-------

/*
 *  NAME:
 *      usleep     -- This is the precision timer for Test Set
 *                    Automation. It uses the select(2) system
 *                    call to delay for the desired number of
 *                    micro-seconds. This call returns ZERO
 *                    (which is usually ignored) on successful
 *                    completion, -1 otherwise.
 *
 *  ALGORITHM:
 *      1) We range check the passed in microseconds and log a
 *         warning message if appropriate. We then return without
 *         delay, flagging an error.
 *      2) Load the Seconds and micro-seconds portion of the
 *         interval timer structure.
 *      3) Call select(2) with no file descriptors set, just the
 *         timer, this results in either delaying the proper
 *         ammount of time or being interupted early by a signal.
 *
 *  HISTORY:
 *      Added when the need for a subsecond timer was evident.
 *
 *  AUTHOR:
 *      Michael J. Dyer                   Telephone:   AT&T 414.647.4044
 *      General Electric Medical Systems        GE DialComm  8 *767.4044
 *      P.O. Box 414  Mail Stop 12-27         Sect'y   AT&T 414.647.4584
 *      Milwaukee, Wisconsin  USA 53201                      8 *767.4584
 *      internet:  mike@sherlock.med.ge.com     GEMS WIZARD e-mail: DYER
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int     usleep( unsigned long int microSeconds )
{
        unsigned int            Seconds, uSec;
        int                     nfds, readfds, writefds, exceptfds;
        struct  timeval         Timer;

        nfds = readfds = writefds = exceptfds = 0;

        if( (microSeconds == (unsigned long) 0)
                || microSeconds > (unsigned long) 4000000 )
        {
                errno = ERANGE;         /* value out of range */
                perror( "usleep time out of range ( 0 -> 4000000 ) " );
                return -1;
        }

        Seconds = microSeconds / (unsigned long) 1000000;
        uSec    = microSeconds % (unsigned long) 1000000;

        Timer.tv_sec            = Seconds;
        Timer.tv_usec           = uSec;

        if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
        {
                perror( "usleep (select) failed" );
                return -1;
        }

        return 0;
}


flock:
------

/*
 * flock (fd, operation)
 *
 * This routine performs some file locking like the BSD 'flock'
 * on the object described by the int file descriptor 'fd',
 * which must already be open.
 *
 * The operations that are available are:
 *
 * LOCK_SH  -  get a shared lock.
 * LOCK_EX  -  get an exclusive lock.
 * LOCK_NB  -  don't block (must be ORed with LOCK_SH or LOCK_EX).
 * LOCK_UN  -  release a lock.
 *
 * Return value: 0 if lock successful, -1 if failed.
 *
 * Note that whether the locks are enforced or advisory is
 * controlled by the presence or absence of the SETGID bit on
 * the executable.
 *
 * Note that there is no difference between shared and exclusive
 * locks, since the 'lockf' system call in SYSV doesn't make any
 * distinction.
 *
 * The file "" should be modified to contain the definitions
 * of the available operations, which must be added manually (see below
 * for the values).
 */

#include 
#include 
#include 

#ifndef LOCK_SH
#define LOCK_SH 1
#endif
#ifndef LOCK_EX
#define LOCK_EX 2
#endif
#ifndef LOCK_NB
#define LOCK_NB 4
#endif
#ifndef LOCK_UN
#define LOCK_UN 8
#endif

extern int errno;

	int
flock (int fd, int operation)
{
	int i;

	switch (operation) {

	/* LOCK_SH - get a shared lock */
	case LOCK_SH:
	/* LOCK_EX - get an exclusive lock */
	case LOCK_EX:
		i = lockf (fd, F_LOCK, 0);
		break;

	/* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
	case LOCK_SH|LOCK_NB:
	/* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
	case LOCK_EX|LOCK_NB:
		i = lockf (fd, F_TLOCK, 0);
		if (i == -1)
			if ((errno == EAGAIN) || (errno == EACCES))
				errno = EWOULDBLOCK;
		break;

	/* LOCK_UN - unlock */
	case LOCK_UN:
		i = lockf (fd, F_ULOCK, 0);
		break;

	/* Default - can't decipher operation */
	default:
		i = -1;
		errno = EINVAL;
		break;
	}

	return (i);
}

/*
 * An alternative version was posted by James Gritton
 * (gritton@byu.edu) in comp.sys.hp.
 * As far as I can tell, it works the same as the above
 * except for the "errno" values returned (and it defaults
 * an invalid operation to "unlock").
 * The definitions of LOCK_xx should be put into  and/or
 * .
 * Note: this was typed in, so it may not work as given.
 */

/*
 *#include 
 *#define LOCK_SH 1
 *#define LOCK_EX 2
 *#define LOCK_NB 4
 *#define LOCK_UN 8
 *
 *	int
 *flock (int fd, int operation)
 *{
 *	struct flock fl;
 *
 *	switch (operation & ~LOCK_NB) {
 *	case LOCK_SH:
 *		fl.l_type = F_RDLCK;
 *		break;
 *	case LOCK_EX:
 *		fl.l_type = F_WRLCK;
 *		break;
 *	default:
 *		fl.l_type = F_UNLCK;
 *		break;
 *	}
 *
 *	fl.l_whence = SEEK_SET;
 *	fl.l_start = fl.l_len = 0L;
 *
 *	return fcntl (fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &fl);
 *}
 */


getclktck:
----------

/*
 * getclktck ()
 *
 * Returns the value of CLK_TCK (timer resolution).
 */

#include 

	int
getclktck ()
{
	return(sysconf(_SC_CLK_TCK));
}


getloadavg (works on a wide variety of systems):
------------------------------------------------

/*
 * getloadavg (ave, n)
 *
 * This routine returns 'n' double precision floats containing
 * the load averages in 'ave'; at most 3 values will be returned.
 *
 * Return value: 0 if successful, -1 if failed (and all load
 * averages are returned as 0).
 */

#include 
#include 
#include 
#include 

extern int errno;

#define STRSIZ	512			/* Sprintf buffer size */
static char errstr[STRSIZ];		/* Global sprintf buffer */

int ugetloads(float *a);
static void mperror(char *s);
static char *syserr();

#define merror(a1)		fprintf(stderr,"%s",a1)
#define merror1(fmt,a1)		{ sprintf(errstr,fmt,a1); merror(errstr); }

struct	nlist nl[] = {
#ifdef stardent
# define unixpath "/unix"
	{ "avenrun" },
#else
#if defined(hpux) || defined(__hpux) || defined(__hpux__)
# define unixpath "/hp-ux"
#ifdef __hppa       /* series 700 & 800 */
	{ "avenrun" },
#else               /* series 300 & 400 */
	{ "_avenrun" },
#endif
#else
# define unixpath "/vmunix"
	{ "_avenrun" },
#endif
#endif
	{ 0 },
};

#ifndef RISCos
	int
getloadavg (double *a, int na)
{
	int i, nreturn;
	static int kmem = -1;
#if defined(vax) || defined(hpux) || defined(__hpux) || defined(__hpux__)
	double avenrun[3];
#else
	long avenrun[3];
#endif
#ifdef NOKMEM
	float aves[3];
#endif /* NOKMEM */

	nreturn = na;
	if ( nreturn < 0 )
		nreturn = 0;
	if ( nreturn > 3 )
		nreturn = 3;

#ifdef NOKMEM
/* Use 'uptime' output for BSD-like systems with no /dev/kmem */

	i = ugetloads(aves);
	if( i == -1 ){
		merror("ugetloads failed\n");
		goto failed;
	}
	for (i = 0; i < nreturn; i++)
		a[i] = aves[i];

#else /*NOKMEM*/

	if(kmem == -1) {
#ifdef sgi
# include 
	nl[0].n_value = sysmp(MP_KERNADDR, MPKA_AVENRUN) & 0x7fffffff;
#else
		nlist(unixpath, nl);
		if (nl[0].n_type==0) {
			merror1("%s: No namelist\n", unixpath);
			goto failed;
		}
#ifdef stardent
		nl[0].n_value &= 0x7fffffff;
#endif
#endif
		if((kmem = open("/dev/kmem", 0)) == -1) {
			mperror("Can't open(/dev/kmem)");
			goto failed;
		}
	}
	if( lseek(kmem, (off_t)nl[0].n_value, 0) == -1 ){
		mperror("Can't lseek in kmem");
		goto failed;
	}
	if( read(kmem, (char *)avenrun, sizeof(avenrun)) != sizeof(avenrun) ){
		mperror("Can't read kmem");
		goto failed;
	}
	for (i = 0; i < nreturn; i++)
#if defined(sun) || defined(sequent)
		a[i] = avenrun[i] / FSCALE;
#else
#ifdef sgi
		a[i] = avenrun[i] / 1024;
#else
#if defined(BSD4_2) || defined(hpux) || defined(__hpux) || defined(__hpux__)
		a[i] = avenrun[i];
#else
#ifdef stardent
		a[i] = (double)avenrun[i] / (1<<16);
#else
		a[i] = avenrun[i] / 1024;
#endif /*stardent*/
#endif /*BSD4_2*/
#endif /*sgi*/
#endif /*sun*/
#endif /*NOKMEM*/
	return(0);
failed:;
	for (i = 0; i < nreturn; i++)
		a[i] = 0;
	return(-1);
}
#else /*RISCos*/
#include 
	static
getloadavg (double *a, int na)
{
	int i, nreturn;
	static int kmem = -1;
	fix avenrun[3];

	nreturn = na;
	if ( nreturn < 0 )
		nreturn = 0;
	if ( nreturn > 3 )
		nreturn = 3;

	if(kmem == -1) {
		nlist("/unix", nl);
		if (nl[0].n_type==0) {
			merror("/unix: No namelist\n");
			goto failed;
		}
		if((kmem = open("/dev/kmem", 0)) == -1) {
			mperror("Can't open(/dev/kmem)");
			goto failed;
		}
	}
	if( lseek(kmem, (off_t)nl[0].n_value, 0) == -1 ){
		mperror("Can't lseek in kmem");
		goto failed;
	}
	if( read(kmem, (char *)avenrun, sizeof(avenrun)) != sizeof(avenrun) ){
		mperror("Can't read kmem");
		goto failed;
	}
	for (i = 0; i < nreturn; i++)
	        a[i] = (int) FIX_TO_INT(avenrun[i]) + .5;
	return(0);
failed:;
	for (i = 0; i < nreturn; i++)
		a[i] = 0;
	return(-1);
}
#endif /* RISCOS */

/* ugetloads(ls)
 * float ld[3];
 *
 * Puts the 1, 5, and 15 minute load averages in the float
 * array passed to it.  This program calls upon uptime(1)
 * which could have different ways of printing ie. with bsd4.2
 * "   9:34pm  up 11 hrs,  3 users,  load average: 0.25, 0.22, 0.24  "
 *                                notice the commas -- ^ --- ^.
 * while bsd4.1 does not print commas.  The BSD41 define will
 * take care of this if that is your system, it defaults to
 * the 4.2 version.
 *
 * Author:
 *  John Bien
 *  {ihnp4 | ucbvax | decvax}!trwrb!jsb
 *
 * This routine taken from comp.sources.unix: Volume 4, Issue 78
 */

FILE *popen();

ugetloads(ld)
float ld[3];
{
    FILE *stream;
    int i;

    if((stream = popen("uptime","r")) == NULL)
	return(-1);

#ifdef BSD41
    i = fscanf(stream,"%*[^l] load average: %f %f %f", &ld[0],&ld[1],&ld[2]);
#else
    i = fscanf(stream,"%*[^l] load average: %f, %f, %f", &ld[0],&ld[1],&ld[2]);
#endif /* BSD41 */
    pclose(stream);
    return i == 3 ? 0 : -1;
}

/* Routine to print messages to stderr, appending the system error message */

	static void
mperror(char *s)
{
	char *p;
	char str[STRSIZ];	/* must have own internal buffer */

	if( (p=index(s,'\n')) != NULL )
		*p = '\0';
	sprintf(str,"%s: %s\n", s, syserr());
	if( p )
		*p = '\n';
	merror(str);
}

/* Routine to get the last system error message */

extern int sys_nerr;
extern char *sys_errlist[];

	static char *
syserr()
{
	static char buf[80];

	if (errno >= 0 && errno < sys_nerr)
		return(sys_errlist[errno]);
	sprintf(buf,"Unknown error %d", errno);
	return(buf);
}


getloadavg (works only on HP-UX 9.x):
-------------------------------------

/*
 * From: Daniel Hannigan 
 *
 * The will only work on HPUX > 9.0
 */

#include 

getloadavg (ave, na)
double *ave;
int na;
{
        struct pst_dynamic pstd;
        int i;

        if (pstat_getdynamic (&pstd, sizeof (struct pst_dynamic), 0, 0)) {
                for (i=0; i< na; ++i)
                        *(ave + i) = 0.0;
                return (-1);
        }

        switch (na) {
                case 3:
                        *(ave + 2) = pstd.psd_avg_15_min;
                case 2:
                        *(ave + 1) = pstd.psd_avg_5_min;
                case 1:
                        *(ave) = pstd.psd_avg_1_min;
                default:
                        break;
        }
        return (0);
}


Dissociate from controlling terminal:
-------------------------------------

~From: jhd@irfu.se (Jan D.)
Organization: Swedish Institute of Space Physics, Uppsala, Sweden

The code above should look like this on HP-UX (POSIX ?):

	#include 	/* For _SC_OPEN_MAX */

	long	tblsiz = sysconf(_SC_OPEN_MAX);

 	if (fork())
 	    exit(0);

	setsid();	/* Disassociate from controlling terminal */
 	for (c = 0; c < tblsiz; c++)
 	    (void) close(c);
 	(void) open("/", O_RDONLY);
 	(void) dup2(0, 1);
 	(void) dup2(0, 2);


Here's the deal regarding '-lBSD':

setpgrp() (in libc) is equivalent to setsid().
setpgrp(pid, pgrp) (in -lBSD) is equivalent to POSIX setpgid(pid, pgrp).
setpgrp2(pid, pgrp) is also equivalent to POSIX setpgid(pid, pgrp).

If you don't link with -lBSD you can replace setsid() in with setpgrp()
if you wan't. They both will get rid of the controlling terminal.

setpgrp(pid, pgrp) (-lBSD style), setpgid(pid, pgrp)
and setpgrp2(pid, pgrp) will NOT remove the controlling terminal.

Thus: The only way (I know of) in HP-UX to remove the controlling terminal
is with setsid() or setpgrp() in libc. The only way in POSIX to get
rid of the controlling terminal is with setsid().


setlinebuf:
-----------

/*
 * setlinebuf (FILE *fp)
 *
 * Routine to set line buffering on "fp".
 */

#include 

	int
setlinebuf (FILE *fp)
{
	(void) setvbuf (fp, NULL, _IOLBF, 0);
	return(0);
}


alloca:
-------

/*
	alloca -- (mostly) portable public-domain implementation -- D A Gwyn

	last edit:	86/05/30	rms
	   include config.h, since on VMS it renames some symbols.
	   Use xmalloc instead of malloc.

	This implementation of the PWB library alloca() function,
	which is used to allocate space off the run-time stack so
	that it is automatically reclaimed upon procedure exit,
	was inspired by discussions with J. Q. Johnson of Cornell.

	It should work under any C implementation that uses an
	actual procedure stack (as opposed to a linked list of
	frames).  There are some preprocessor constants that can
	be defined when compiling for your specific system, for
	improved efficiency; however, the defaults should be okay.

	The general concept of this implementation is to keep
	track of all alloca()-allocated blocks, and reclaim any
	that are found to be deeper in the stack than the current
	invocation.  This heuristic does not reclaim storage as
	soon as it becomes invalid, but it will do so eventually.

	As a special case, alloca(0) reclaims storage without
	allocating any.  It is a good idea to use alloca(0) in
	your main control loop, etc. to force garbage collection.
*/
#ifndef lint
static char	SCCSid[] = "@(#)alloca.c	1.1";	/* for the "what" utility */
#endif

#ifdef emacs
#include "config.h"
#ifdef static
/* actually, only want this if static is defined as ""
   -- this is for usg, in which emacs must undefine static
   in order to make unexec workable
   */
#ifndef STACK_DIRECTION
you
lose
-- must know STACK_DIRECTION at compile-time
#endif /* STACK_DIRECTION undefined */
#endif static
#else
#define xmalloc malloc
#endif emacs

#if defined(X3J11) || defined(__STDC__)
typedef void	*pointer;		/* generic pointer type */
#else
typedef char	*pointer;		/* generic pointer type */
#endif

#define	NULL	0			/* null pointer constant */

extern void	free();
extern pointer	xmalloc();

/*
	Define STACK_DIRECTION if you know the direction of stack
	growth for your system; otherwise it will be automatically
	deduced at run-time.

	STACK_DIRECTION > 0 => grows toward higher addresses
	STACK_DIRECTION < 0 => grows toward lower addresses
	STACK_DIRECTION = 0 => direction of growth unknown
*/

#ifndef STACK_DIRECTION
#define	STACK_DIRECTION	0		/* direction unknown */
#endif

#if STACK_DIRECTION != 0

#define	STACK_DIR	STACK_DIRECTION	/* known at compile-time */

#else	/* STACK_DIRECTION == 0; need run-time code */

static int	stack_dir;		/* 1 or -1 once known */
#define	STACK_DIR	stack_dir

static void
find_stack_direction (/* void */)
{
  static char	*addr = NULL;	/* address of first
				   `dummy', once known */
  auto char	dummy;		/* to get stack address */

  if (addr == NULL)
    {				/* initial entry */
      addr = &dummy;

      find_stack_direction ();	/* recurse once */
    }
  else				/* second entry */
    if (&dummy > addr)
      stack_dir = 1;		/* stack grew upward */
    else
      stack_dir = -1;		/* stack grew downward */
}

#endif	/* STACK_DIRECTION == 0 */

/*
	An "alloca header" is used to:
	(a) chain together all alloca()ed blocks;
	(b) keep track of stack depth.

	It is very important that sizeof(header) agree with malloc()
	alignment chunk size.  The following default should work okay.
*/

#ifndef	ALIGN_SIZE
#define	ALIGN_SIZE	sizeof(double)
#endif

typedef union hdr
{
  char	align[ALIGN_SIZE];	/* to force sizeof(header) */
  struct
    {
      union hdr *next;		/* for chaining headers */
      char *deep;		/* for stack depth measure */
    } h;
} header;

/*
	alloca( size ) returns a pointer to at least `size' bytes of
	storage which will be automatically reclaimed upon exit from
	the procedure that called alloca().  Originally, this space
	was supposed to be taken from the current stack frame of the
	caller, but that method cannot be made to work for some
	implementations of C, for example under Gould's UTX/32.
*/

static header *last_alloca_header = NULL; /* -> last alloca header */

pointer
alloca (size)			/* returns pointer to storage */
     unsigned	size;		/* # bytes to allocate */
{
  auto char	probe;		/* probes stack depth: */
  register char	*depth = &probe;

#if STACK_DIRECTION == 0
  if (STACK_DIR == 0)		/* unknown growth direction */
    find_stack_direction ();
#endif

				/* Reclaim garbage, defined as all alloca()ed storage that
				   was allocated from deeper in the stack than currently. */

  {
    register header	*hp;	/* traverses linked list */

    for (hp = last_alloca_header; hp != NULL;)
      if (STACK_DIR > 0 && hp->h.deep > depth
	  || STACK_DIR < 0 && hp->h.deep < depth)
	{
	  register header	*np = hp->h.next;

	  free ((pointer) hp);	/* collect garbage */

	  hp = np;		/* -> next header */
	}
      else
	break;			/* rest are not deeper */

    last_alloca_header = hp;	/* -> last valid storage */
  }

  if (size == 0)
    return NULL;		/* no allocation required */

  /* Allocate combined header + user data storage. */

  {
    register pointer	new = xmalloc (sizeof (header) + size);
    /* address of header */

    ((header *)new)->h.next = last_alloca_header;
    ((header *)new)->h.deep = depth;

    last_alloca_header = (header *)new;

    /* User storage begins just after header. */

    return (pointer)((char *)new + sizeof(header));
  }
}


setproctitle:
-------------

~From: Tor Lillqvist (tml@tik.vtt.fi),
      Technical Research Centre of Finland,
      Laboratory for Information Processing (VTT/TIK).

Q: How can I write to the argv[] strings in a program so that the
   altered strings show up in 'ps'?

A: In HP-UX you can't do it by clobbering the argv strings, but with the
   undocumented pstat syscall.  This code is from sendmail 5.65c (routine
   conf.c). Modify to your taste (especially remove the " (sendmail)").

/*
**  SETPROCTITLE -- set process title for ps
**
**	Parameters:
**		fmt -- a printf style format string.
**		a, b, c -- possible parameters to fmt.
**
**	Returns:
**		none.
**
**	Side Effects:
**		Clobbers argv of our main procedure so ps(1) will
**		display the title.
*/

/*VARARGS1*/
void
#ifdef __STDC__
setproctitle(const char *fmt, ...)
#else /* !__STDC__ */
setproctitle(fmt, va_alist)
	const char *fmt;
va_dcl
#endif /* __STDC__ */
{
#if defined(SETPROCTITLE) && !defined(SYSV)
	va_list	args;
	register char *p;
	register int i;
#if (defined(hpux) || defined(__hpux) || defined(__hpux__)) && defined(PSTAT_SETCMD)
	union pstun un;
#else
	extern char **Argv;
	extern char *LastArgv;
#endif
	char buf[MAXLINE];

# ifdef __STDC__
	va_start(args, fmt);
# else /* !__STDC__ */
	va_start(args);
# endif /* __STDC__ */
	(void) vsprintf(buf, fmt, args);
	va_end(args);

#if (defined(hpux) || defined(__hpux) || defined(__hpux__)) && defined(PSTAT_SETCMD)
	(void) sprintf(buf + strlen(buf), " (sendmail)");
	un.pst_command = buf;
	pstat(PSTAT_SETCMD, un, strlen(buf), 0, 0);
#else
	/* make ps print "(sendmail)" */
	p = Argv[0];
	*p++ = '-';

	i = strlen(buf);
	if (i > LastArgv - p - 2)
	{
		i = LastArgv - p - 2;
		buf[i] = '\0';
	}
	(void) strcpy(p, buf);
	p += i;
	while (p < LastArgv)
		*p++ = ' ';
#endif
#endif /* SETPROCTITLE && !SYSV */
}


utimes:
-------

/*
 * utimes (BSD equivalent of utime(2) - set file mod and access times)
 * (No attempt to reproduce same error code expect that they both do
 * return -1 on error and 0 on success)
 *
 * From: corrigan@weber.ucsd.edu (Michael J. Corrigan)
 */

#include 
#include 
#include 

int utimes(file,tvp) char *file; struct timeval *tvp;
{
	struct utimbuf ut;
	time_t now;

	now = time((time_t *)NULL);
	if (tvp == (struct timeval *)NULL) {
		ut.actime = now;
		ut.modtime = now;
	} else {
		ut.actime = tvp++->tv_sec;
		ut.modtime = tvp->tv_sec;
	}
	return(utime(file,&ut));
}


insque:
-------

/*
 * For insque() functionality, the insque.c from emacs 18.59
 * compiles/works under HP-UX 8.x and 9.x.
 *
 * From: mjo@iao.ford.com (Mike O'Connor, Ford Motor Company)
 */

/* This file implements the insque and remque functions of BSD.
   It is not compiled by default, because that change would be too risky
   to install right now.  If you find that HAVE_X_MENU leads to linker errors
   because these functions are undefined, then compile this file
   and arrange to link it in.  */

struct qelem {
  struct    qelem *q_forw;
  struct    qelem *q_back;
  char q_data[1];
};

/* Insert ELEM into a doubly-linked list, after PREV.  */

void
insque (elem, prev)
     struct qelem *elem, *prev;
{
  struct qelem *next = prev->q_forw;
  prev->q_forw = elem;
  if (next)
    next->q_back = elem;
  elem->q_forw = next;
  elem->q_back = prev;
}

/* Unlink ELEM from the doubly-linked list that it is in.  */

remque (elem)
     struct qelem *elem;
{
  struct qelem *next = elem->q_forw;
  struct qelem *prev = elem->q_back;
  if (next)
    next->q_back = prev;
  if (prev)
    prev->q_forw = next;
}


gethostid:
----------

/*
 * From: dd@mv.us.adobe.com (David DiGiacomo)
 */

#define _INCLUDE_HPUX_SOURCE
#include 

int
gethostid()
{
	struct utsname uts;

	if (uname(&uts) < 0)
		return 0;

	return atoi(uts.idnumber);
}


rmdir:
------

From Pat Lynch (Pat_Lynch@stortek.com):

rmdir(2) (when attempting to remove a directory that still has
files in it) returns -1 with errno = ENOTEMPTY on Domain/OS BSD,
but errno = EEXIST on HPUX.


signal:
-------

I've come across a few situations where linking in the bsd library
has caused more problems than solved them (bash comes to mind).
This is especially true when the only real problem is the code to be
ported assumes BSD signal semantics and gets unreliable signals from HPUX.
In this situation, I compile and link in the following function:

/*
 * 	signal.c
 *
 * PURPOSE
 * 	provide berkeley signals with signal interface
 *
 * Created On      : Wed Jun 23 08:47:42 1993
 * Author          : Andre Srinivasan (andre@neuronet.pitt.edu)
 *
 * HISTORY
 *
 */
#include 

void (*signal(int iSig, void (*pAction)(int)))(int)
{
struct sigaction saNew, saOld;

saNew.sa_handler = pAction;
sigemptyset(&saNew.sa_mask);
saNew.sa_flags = 0;

if (sigaction(iSig, &saNew, &saOld) < 0)
     return(SIG_ERR);

return(saOld.sa_handler);
}


***************************************************************************


"SunOS to HP-UX 9.05 Porting Guide" Table of Contents
-----------------------------------------------------


	Forward

	Introduction

	Development Environment Overview ................................... 1 
	 
	   Compilers 	                                            1-1
	      Compiler Versions                                     1-1
	      Compiler Licensing                                    1-2
	      Location of Compilers                                 1-2
	      Compiler Directives                                   1-3

	   Location of Files and Utilities                          1-3
	      C and C++ Header Files                                1-3
	      X11 and Motif Header Files                            1-4
	      System Libraries                                      1-4
	      X11 and Motif Libraries                               1-4
	      Other Utilities                                       1-5
	      Debug Environment                                     1-6

	   HP LaserROM/UX                                           1-6

	   HP-UX Patches                                            1-7

	   SunOS to HP-UX Porting References                        1-7


	Compilers .......................................................... 2

	   C Compiler                                               2-1
	      Product Overview                                      2-1
	      Key Features                                          2-2
	      Performance and Optimization                          2-2
	      Shared Library Support                                2-3
	      C Compiler Ordering Information                       2-3

	   C Language Tips and Traps                                2-4
	      ANSI C Compiler Compile Line                          2-4
	      Compiler Warning: Could Not Find 
		   Include File sys/filio.h 	                    2-5
	      PATH_MAX Not Defined                                  2-5
	      SIG_PF Not Found                                      2-5
	      S_un Undefined Member on HP-UX                        2-5
	       Problems                                  2-6
	      Unexpected Symbols: u_long, u_short                   2-6
	      ANSI C and Include File                    2-6
	      Compiler Fails With "Not Enough Space" Message        2-6
	      Large Executable Dumps Core                           2-6
	      Error Message: Too Many Defines                       2-6
	      Null Pointer Dereferencing                            2-7

	   MIN and MAX header file differences                      2-7

	   TCGETS and TCSETS                                        2-7

	   SunOS to HP-UX C Compiler Options Cross Reference        2-8
	      C Compiler Options Cross Reference                    2-9

	   C++ Compiler                                             2-12
	      Overview of HP C++ Version A.03.50                    2-12
	      HP C++ Run-Time Libraries                             2-13
	      HP C++ Compiler Ordering Information                  2-13
	      C++ Compiler Tips and Traps                           2-14
	      SunOS to HP-UX C++ Compiler Options Cross Reference   2-16
	      SunOS C++ and HP-UX C++ Compiler Options              2-17

	   FORTRAN Compiler                                         2-20
	      FORTRAN Compiler Tips and Traps                       2-20

	   Pascal Compiler                                          2-22
	      Product Overview                                      2-22
	      Optimization                                          2-22
	      Shared Libraries                                      2-22
	      Comments Differences                                  2-23
	      Data Type Differences                                 2-23
	   Pascal Compiler Options Cross Reference                  2-24

	   GNU gcc/g++                                              2-27
	      g++ Language Tips and Traps                           2-27
	      C++ Functional Prototypes                             2-27

	   Compiler Environment Variables                           2-28
	      CCOPTS Environment Variable                           2-28
	      TMPDIR Environment Variable                           2-29


	Other Development Tools & Information .............................. 3

	   make 	                                            3-1
	      make patch: PHCO_4762                                 3-1
	      make Environment Variable Differences                 3-1

	   imake                                                    3-2
	      imake and Long Continuation Lines                     3-3

	   GNU gmake                                                3-3

	   Linker                                                   3-3

	   Incremental Linking                                      3-4
	      Blink Link                                            3-4

	   HP-UX Debug Environment                                  3-5
	      HP Distributed Debugging Environment 
		   (HP/DDE) Version 3.05                            3-6
	      GNU Debugging                                         3-7
	      dbx Debug Interfaces                                  3-7

	   Notes on Debugging Shared Libraries with HP/DDE          3-7
	      Loading Debugging Information for Shared Libraries    3-8
	      Making Shared Libraries Writable                      3-8
	      A Shared Library Debugging Session                    3-9

	   Notes on Debugging Shared Libraries with xdb             3-10
	      Debugging an Application Using Shared Libraries       3-10
	      Accessing a Function Within a Shared Library          3-10
	      Debugging a Dynamically Loaded Shared Library         3-10
	      Adopting a Program                                    3-10

	   Software Performance Analysis Tools                      3-11
	      HP/PAK Version 6.05                                   3-11
	      HP GlancePlus/UX                                      3-11
	      Prof and Gprof                                        3-12
	      Profile-based Optimization                            3-13

	   HP SoftBench                                             3-13

	   RCS                                                      3-14

	   SCCS                                                     3-14

	   trace (public domain)                                    3-14


	Libraries .......................................................... 4

	   Archived Libraries                                       4-1
	      ar                                                    4-1
	      ranlib                                                4-1

	   Shared Libraries                                         4-1
	      When Are the Objects Attached? (binding)              4-1
	      Creating Position-Independent Code (PIC) 
		   and Shared Libraries                             4-2
	      Finding Shared Libraries: $SHLIB_PATH                 4-3
	      Enabling Dynamic Path Searching                       4-3
		 With chatr(1)                                      4-3
		 With the Linker                                    4-3
	      Absolute Paths to Libraries                           4-4
	      Shared Library Performance Considerations             4-5
	      Shared Library Debugging                              4-5

	   Overview of HP-UX Math Libraries                         4-6
	      How to Select Versions of the Math Libraries          4-6


	Graphics ........................................................... 5

	   OPEN LOOK to OSF/Motif                                   5-1
	      Learning OSF/Motif                                    5-2
	      Convincing Your Users There Is Another Way            5-2

	   Your Existing Code                                       5-2

	   Porting Strategy                                         5-3
	      Porting Utilities                                     5-4
	      Graphics Tool Positioning                             5-4

	   Where to Get OSF/Motif for SunOS                         5-5

	   Guidelines for Converting OLIT to OSF/Motif              5-5
	      Resolving Conflicting Resources                       5-7
	      Window Manager Differences                            5-8
	      Other OLIT Port Issues                                5-8

	   Curses                                                   5-9


	Signals	............................................................ 6

	   BSD4.2 Compatibility                                     6-2

	   Asynchronous I/O                                         6-3
	      Examples                                              6-3
		 BSD4.3 Example                                     6-3
		 HP-UX Example                                      6-4

	   select() and poll()                                      6-4


	Interprocess Communication (IPC) ................................... 7

	   FIFOs                                                    7-1

	   Messages                                                 7-1

	   Pipes                                                    7-1

	   RPC                                                      7-2
	      ONC RPC                                               7-2
	      DCE RPC                                               7-3

	   Semaphores                                               7-3

	   Shared Memory                                            7-3

	   Sockets                                                  7-3
	      TCP Socket Performance                                7-5

	   Streams                                                  7-6
	      STREAMS Porting Concerns                              7-6

	   BMS and Tooltalk                                         7-7

	   DCE Threads                                              7-7


	Miscellaneous ...................................................... 8

	   Making Daemons                                           8-1

	   Unique CPU Identification                                8-2

	   Stack Dump Function                                      8-2

	   Programmatically Obtaining Process Status                8-2

	   Programmatically Obtaining Ethernet Addresses            8-4

	   PS Instruction "Test and Set"                            8-4
	   
	   Porting Device Drivers 	                            8-5
	      Compile the Driver                                    8-5
	      Add the Driver to the Configuration Files             8-5
	      Generate the conf.c and config.mk Files               8-6
	      Build and Install a New Kernel                        8-6

	   Process Virtual Address Space                            8-7
	      Dealing With Data Alignment                           8-7
		 Alternatives for Dealing with Alignment Bus Errors 8-7
		 Examples of Code Expansion with Alignment Options  8-8

	   Large Data Space Processes                               8-11
	      System default test case                              8-12
	      Increase MAXDSIZ test case                            8-12
	      Add an Additional Swap Device Test Case               8-12
	      Increase MAXSWAPCHUNKS Size Test Case                 8-13
	      Relink the Programs with -N Test Case                 8-15

	   Hint for Debugging an X11 Application                    8-15

	   Eliminating Core Dumps                                   8-15

	   Obtaining data from /etc/utmp                            8-16

	   Floating Point                                           8-17
	      Exception Conditions                                  8-17
	      Inexact Result (Rounding)                             8-18


	HP on the Net ...................................................... 9

	   University of Liverpool Archive                          9-1

	   Interworks Software Archive                              9-2

	   GNU Software Archive                                     9-2

	   HP World Wide Web Sites                                  9-3
	      HP's WWW Home Page                                    9-3
	      HP WWW SupportLine Page                               9-3
	      HP WWW Patch Server Page                              9-3
	      Other HP-Related WWW Sites                            9-3

	   User Groups                                              9-4
	      Interworks                                            9-4
	      Interex                                               9-4

	   News Groups                                              9-4
	      The comp.sys.hp.* hierarchy                           9-4


	Public Domain Made Easy ............................................ A

	   Building Public Domain Software on HP-UX                 A-1

	   Overview                                                 A-1

	   C Compiler                                               A-2

	   make                                                     A-2

	   imake                                                    A-2
	      imake distribution                                    A-2
	      Location of imake Configuration Files                 A-3
	      Modifications to the Configuration File site.def      A-3
	      Location of Certain Include Files and Libraries       A-4

	   Motif and Xt                                             A-5

	   Xaw and Xmu                                              A-6

	   Example: Building the Cutview Program                    A-7
	      Simple Imakefile Containing a ComplexProgramTarget    A-7
	      Steps to Building the Software                        A-7


	System and Library Command Cross Reference ......................... B

	   SunOS and HP-UX System Call Differences 	            B-1

	   SunOS and HP-UX Library Routine Differences              B-24


	pstat(2) Man Page .................................................. C

	DBX to XDB Command Summary Cross-Reference ......................... D

	   Execution and Tracing                                    D-1

	   Displaying and Naming Data                               D-2

	   Accessing Source Files                                   D-3

	   Miscellaneous                                            D-3

	   Machine Level                                            D-4


	Series 700/800 Compatibility ....................................... E

	   Compatibility: What Does It Mean?                        E-1
	      What is Not Common                                    E-1

	   Non-Kernel Differences Between Series 700 and Series 800 E-3
	      New Math Libraries                                    E-3
	      X11R5 Server                                          E-4
	      Building One Executable for Series 700/800            E-4
	      Building Two Executables for Series 700/800           E-4


	Product Information ................................................ F

	   Accent STP                                               F-1

	   ALSYS, INC.                                              F-2

	   Bluestone                                                F-2

	   Hewlett-Packard Company (HP)                             F-3

	   Integrated Computer Solutions, Inc. (ICS)                F-5

	   iXOS Software                                            F-6

	   Melillo Consulting                                       F-6

	   Qualix                                                   F-7

	   UIM/X                                                    F-7

	   UniPress Software, Inc.                                  F-8

	   V. I. Corporation                                        F-9

	   XView Library                                            F-9


* Sun, SunOS, Solaris, Sun Microsystems are registered trademarks of Sun
  Microsystems, Inc.

* UNIX is registered trademarks of UNIX Systems Laboratories, Inc.
  (Novell) in the USA and other countries.

* AIX is registered trademarks of IBM

* All other trademarks and registered trademarks are the property of their
  respective holders.

***************************************************************************

End of BSD to HP-UX porting tricks.