[comp.sys.hp] BSD emulation library

ian@dms.cdc.com (Ian Hogg) (12/20/90)

A lot of people have lately been posting questions like "How do I implement this BSD routine in HPUX?".  I have done some of this and have answered a few about getdtablesize this month alone.  So I have decided to start distributing a bsd emulation library to aid in porting BSD programs to HP-UX.  This a minimal set of functions, mostly those I have encountered in porting applications.  It consists of some routines I wrote myself, the Berklib emulation routines from MIT X11r4, and an implementation of usle








ep that I have no idea who wrote.  I will maintain this library if there is sufficient interest in it.  I hope that people will send me any routines they have implemented or suggestions for others.

PS. Does anyone have any ideas of how to implement adjtime, or has ported the network time protocol daemon to HPUX?
============================= cut here =======================================
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Ian Hogg <ian@louie> on Wed Dec 19 12:34:14 1990
#
# This archive contains:
#	Berklib.c	bsdstuff.c	usleep.c	Makefile	
#	README		
#

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

echo x - Berklib.c
cat >Berklib.c <<'@EOF'
/* $XConsortium: Berklib.c,v 1.4 90/08/27 15:29:40 swick Exp $ */
/*
 * This file is used by System V based systems.
 */

#include <sys/types.h>

/*
 * These are routines found in BSD and not found in many SysV's.  They are
 * included so that some clients can compile.
 */

bcopy (b1, b2, length)
register unsigned char *b1, *b2;
register length;
{
    if (b1 < b2) {
	b2 += length;
	b1 += length;
	while (length--) {
	    *--b2 = *--b1;
	}
    }
    else {
	while (length--) {
	    *b2++ = *b1++;
	}
    }
}

bcmp (b1, b2, length)
register unsigned char *b1, *b2;
register length;
{
    while (length--) {
	if (*b1++ != *b2++) return 1;
    }
    return 0;
}

bzero (b, length)
register unsigned char *b;
register length;
{
    while (length--) {
	*b++ = '\0';
    }
}


/* Find the first set bit
 * i.e. least signifigant 1 bit:
 * 0 => 0
 * 1 => 1
 * 2 => 2
 * 3 => 1
 * 4 => 3
 */

int
ffs(mask)
unsigned int	mask;
{
    register i;

    if ( ! mask ) return 0;
    i = 1;
    while (! (mask & 1)) {
	i++;
	mask = mask >> 1;
    }
    return i;
}

char * 
index (s, c)
char *s, c;
{
    return ((char *) strchr (s, c));
}

char * 
rindex (s, c)
char *s, c;
{
    return ((char *) strrchr (s, c));
}

/*
 * insque, remque - insert/remove element from a queue
 *
 * DESCRIPTION
 *      Insque and remque manipulate queues built from doubly linked
 *      lists.  Each element in the queue must in the form of
 *      ``struct qelem''.  Insque inserts elem in a queue immedi-
 *      ately after pred; remque removes an entry elem from a queue.
 *
 * SEE ALSO
 *      ``VAX Architecture Handbook'', pp. 228-235.
 */

struct qelem {
    struct    qelem *q_forw;
    struct    qelem *q_back;
    char *q_data;
    };

insque(elem, pred)
register struct qelem *elem, *pred;
{
    register struct qelem *q;
    /* Insert locking code here */
    if ( elem->q_forw = q = (pred ? pred->q_forw : pred) )
	q->q_back = elem;
    if ( elem->q_back = pred )
	pred->q_forw = elem;
    /* Insert unlocking code here */
}

remque(elem)
register struct qelem *elem;
{
    register struct qelem *q;
    if ( ! elem ) return;
    /* Insert locking code here */

    if ( q = elem->q_back ) q->q_forw = elem->q_forw;
    if ( q = elem->q_forw ) q->q_back = elem->q_back;

    /* insert unlocking code here */
}


/*
 * Berkeley random()
 *
 * We simulate via System V's rand()
 */

int
random()
{
   return (rand());
}

/*
 * Berkeley srandom()
 *
 * We simulate via System V's rand()
 */

int
srandom(seed)
int seed;
{
   return (srand(seed));
}


#ifdef hpux

/** on hpux 5.n, readv/writev don't work on sockets;
 ** Even on 6.0, we'll keep these routines around for doing
 ** extra large writes; (> 4000); (this caused the Bezier
 ** demo to blow up.)
 **/

#include <sys/uio.h>

#define min(x,y) ((x)>(y)?(y):(x))

int swWritev(fildes, iov, iovcnt)
int fildes;
register struct iovec *iov;
register int iovcnt;
{
    while (iovcnt && iov->iov_len == 0)
	iovcnt--, iov++;

    if (iovcnt)
	return(write(fildes,iov->iov_base,min(iov->iov_len,4000)));
    else
	return(0);
}

int swReadv(fildes, iov, iovcnt)
int fildes;
register struct iovec *iov;
register int iovcnt;
{
    while (iovcnt && iov->iov_len == 0)
	iovcnt--, iov++;

    if (iovcnt)
	return(read(fildes,iov->iov_base,iov->iov_len));
    else
	return(0);
}

#endif /* hpux */

/*
 * gettimeofday emulation
 * Caution -- emulation is incomplete
 *  - has only second, not microsecond, resolution.
 *  - does not return timezone info.
 */

#if defined(USG) && !defined(CRAY)
int gettimeofday (tvp, tzp)
    struct timeval *tvp;
    struct timezone *tzp;
{
    time (&tvp->tv_sec);
    tvp->tv_usec = 0L;

    if (tzp) {
	fprintf( stderr,
		 "Warning: gettimeofday() emulation does not return timezone\n"
		);
    }
}
#endif

/*
 * gettimeofday emulation
 * Caution -- emulation is incomplete
 *  - has only second, not microsecond, resolution.
 *  - does not return timezone info.
 */

#if defined(USG) && !defined(CRAY)
int gettimeofday (tvp, tzp)
    struct timeval *tvp;
    struct timezone *tzp;
{
    time (&tvp->tv_sec);
    tvp->tv_usec = 0L;

    if (tzp) {
	fprintf( stderr,
		 "Warning: gettimeofday() emulation does not return timezone\n"
		);
    }
}
#endif

/*
 * gettimeofday emulation
 * Caution -- emulation is incomplete
 *  - has only second, not microsecond, resolution.
 *  - does not return timezone info.
 */

#if defined(USG) && !defined(CRAY)
int gettimeofday (tvp, tzp)
    struct timeval *tvp;
    struct timezone *tzp;
{
    time (&tvp->tv_sec);
    tvp->tv_usec = 0L;

    if (tzp) {
	fprintf( stderr,
		 "Warning: gettimeofday() emulation does not return timezone\n"
		);
    }
}
#endif
@EOF

chmod 666 Berklib.c

echo x - bsdstuff.c
cat >bsdstuff.c <<'@EOF'
/*
 *
 * bsdstuff.c - 
 *
 *   This file implements BSD routines that are missing in SYSV (specifically 
 *   HP-UX)
 *
 *   Written by Ian Hogg, Control Data Corp, Plymouth, MN
 */

#include <unistd.h>

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

#include <sys/param.h>
char *getwd(p)
char *p;
{
  extern char * getcwd();
  return(getcwd(p,MAXPATHLEN));
}

#include <time.h>
#include <sys/resource.h>

/* Assumes it is called as getpriority(PRIO_PROCESS,0) */

int getpriority(which, who)
int which, who;
{
  return nice(0);
}

/* Same as above */
int setpriority(which, who, prio)
int which, who, prio;
{
  return nice(prio-getpriority());
}

#include <stdio.h>
#include <malloc.h>
/* Implementation of the Berkely setlinebuf function call */

#define USE_HACK
setlinebuf(stream)
FILE *stream;
{
 /* Try a real hack */
#ifdef USE_HACK
 stream->__flag |= _IOLBF;
 return(0);
#else
 return(setvbuf(stream,NULL,_IOLBF,0));
#endif
}
@EOF

chmod 666 bsdstuff.c

echo x - usleep.c
cat >usleep.c <<'@EOF'
#include <signal.h>
#include <time.h>

static void stopme()
{
	signal(SIGALRM, SIG_DFL);
}

usleep(value)
long value;
{
	void stopme();
	struct itimerval ntval, otval;

	ntval.it_interval.tv_sec = 0;
	ntval.it_interval.tv_usec = 0;
	ntval.it_value.tv_sec = 0;
	ntval.it_value.tv_usec = value;
	signal(SIGALRM, stopme);
	setitimer(ITIMER_REAL, &ntval, &otval);
	pause();
}
@EOF

chmod 666 usleep.c

echo x - Makefile
cat >Makefile <<'@EOF'
OBJS = bsdstuff.o Berklib.o usleep.o

libberk.a: $(OBJS)
	ar r libberk.a $(OBJS)
@EOF

chmod 666 Makefile

echo x - README
cat >README <<'@EOF'
December 18, 1990

This directory is the beginning of maintaining a library of BSD routines that
are not implement in HP-UX.  They may be of interest to other SYSV based
systems.

The package contains the following files -

1. README - you're reading it now.
2. bsdstuff.c - bsd routines written by Ian Hogg of Control Data Corp.
3. Berklib.c  - Bsd emulation routines pulled from the MIT X11 sources.
4. usleep.c   - an implementation of usleep, I don't know where I got it from.
		If anyone recognizes this code, let me know I can give the
		author(s) proper credit.


The following routines are implemented.

Berklib.c:
	bcopy, bcmp, bzero, ffs, index, rindex, insqueue, remqueue, random,
	srandom, gettimeofday (not compiled for HP-UX)

bsdstuff.c:
	getdtablesize, getwd, getpriority, setpriority, setlinebuf

usleep.c:
	usleep


I have not made a comprehensive attempt to implement all routines missing.
Rather, I have implemented routines on an as needed basis.  This generally
occurs when porting software from Sun to HPUX.    I am willing to maintain this
library.  If you have any corrections, comments, suggestions, or additional
routines please mail them to ian@dms.cdc.com  and I will add them.




@EOF

chmod 666 README

exit 0

-- 
===============================================================================
Ian Hogg                                    email:  ian@dms.cdc.com
Empros/Control Data Corporation             phone:  (612)-553-4484
2300 Berkshire Lane N
Plymouth, MN 55441

-- 
===============================================================================
Ian Hogg                                    email:  ian@dms.cdc.com
Empros/Control Data Corporation             phone:  (612)-553-4484
2300 Berkshire Lane N