/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: compat.c,v 1.1.1.1 2008/08/24 05:33:08 gmcgarry Exp $
* Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Klaus Klein and Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $NetBSD: compat.c,v 1.1.1.1 2008/08/24 05:33:08 gmcgarry Exp $
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $NetBSD: compat.c,v 1.1.1.1 2008/08/24 05:33:08 gmcgarry Exp $
*/
#include <LibConfig.h>
#include <string.h>
#include <fcntl.h>
#include <sys/syslimits.h>
#ifndef HAVE_GETOPT
char *optarg;
int
{
size_t n;
char cmd;
char rv;
for (n = 0; n < nlen; n++) {
if (args[n] == ':')
continue;
optind += 1;
} else {
optind += 2;
}
if (!optarg)
optarg="";
return rv;
} else {
optind += 1;
return rv;
}
}
}
}
return -1;
}
#endif
#ifdef HAVE_BASENAME
#ifndef PATH_MAX
#endif
char *
{
char *p, *lastp;
/*
* If `path' is a null pointer or points to an empty string,
* return a pointer to the string ".".
*/
return (singledot);
/* Strip trailing slashes, if any. */
lastp--;
/* Now find the beginning of this (final) component. */
p = lastp;
p--;
/* ...and copy the result into the result buffer. */
return (result);
}
#endif
#if !defined(HAVE_MKSTEMP) && !defined(WIN32)
int
{
unsigned int pid;
/* To guarantee multiple calls generate unique names even if
the file is not created. 676 different possibilities with 7
or more X's, 26 with 6 or less. */
int xcnt = 0;
/* Move to end of path and count trailing X's. */
if (*trv == 'X')
xcnt++;
else
xcnt = 0;
/* Use at least one from xtra. Use 2 if more than 6 X's. */
/* Set remaining X's to pid digits with 0's to the left. */
while (*--trv == 'X') {
pid /= 10;
}
/* update xtra for next call. */
if (xtra[0] != 'z')
xtra[0]++;
else {
xtra[0] = 'a';
xtra[1]++;
else
}
}
#endif
#ifdef HAVE_FFS
int
ffs(int x)
{
int r = 1;
if (!x) return 0;
if (!(x & 0xffff)) { x >>= 16; r += 16; }
if (!(x & 0xff)) { x >>= 8; r += 8; }
if (!(x & 0xf)) { x >>= 4; r += 4; }
if (!(x & 3)) { x >>= 2; r += 2; }
if (!(x & 1)) { x >>= 1; r += 1; }
return r;
}
#endif
/*
* Copyright Patrick Powell 1995
* This code is based on code written by Patrick Powell (papowell@astart.com)
* It may be used for any purpose as long as this notice remains intact
* on all source code distributions
*/
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
static void
static void
static void
static void
static void
/*
* dopr(): poor man's version of doprintf
*/
/* format read states */
#define DP_S_DEFAULT 0
/* format flags - Bits */
/* Conversion Flags */
#define abs_val(p) (p < 0 ? -p : p)
static void
{
long value;
long double fvalue;
switch(state) {
case DP_S_DEFAULT:
if (ch == '%')
state = DP_S_FLAGS;
else
break;
case DP_S_FLAGS:
switch (ch) {
case '-':
flags |= DP_F_MINUS;
break;
case '+':
break;
case ' ':
flags |= DP_F_SPACE;
break;
case '#':
break;
case '0':
break;
default:
break;
}
break;
case DP_S_MIN:
} else if (ch == '*') {
} else
break;
case DP_S_DOT:
if (ch == '.') {
} else
break;
case DP_S_MAX:
if (max < 0)
max = 0;
} else if (ch == '*') {
} else
break;
case DP_S_MOD:
switch (ch) {
case 'h':
cflags = DP_C_SHORT;
break;
case 'l':
if (ch == 'l') {
}
break;
case 'q':
break;
case 'L':
break;
default:
break;
}
break;
case DP_S_CONV:
switch (ch) {
case 'd':
case 'i':
if (cflags == DP_C_SHORT)
else if (cflags == DP_C_LONG_LONG)
else
break;
case 'o':
flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT)
else if (cflags == DP_C_LONG_LONG)
else
break;
case 'u':
flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT)
else if (cflags == DP_C_LONG_LONG)
else
break;
case 'X':
case 'x':
flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT)
else if (cflags == DP_C_LONG_LONG)
else
break;
case 'f':
if (cflags == DP_C_LDOUBLE)
else
/* um, floating point? */
break;
case 'E':
case 'e':
if (cflags == DP_C_LDOUBLE)
else
break;
case 'G':
case 'g':
if (cflags == DP_C_LDOUBLE)
else
break;
case 'c':
break;
case 's':
if (max < 0)
break;
case 'p':
break;
case 'n':
if (cflags == DP_C_SHORT) {
short int *num;
long int *num;
} else if (cflags == DP_C_LONG_LONG) {
long long *num;
} else {
int *num;
}
break;
case '%':
break;
case 'w': /* not supported yet, treat as next char */
break;
default: /* Unknown, skip */
break;
}
max = -1;
break;
case DP_S_DONE:
break;
default: /* hmm? */
break; /* some picky compilers need this */
}
}
else
}
static void
{
if (value == 0)
value = "<NULL>";
if (padlen < 0)
padlen = 0;
if (flags & DP_F_MINUS)
--padlen;
++cnt;
}
++cnt;
}
++padlen;
++cnt;
}
}
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
static void
{
unsigned long uvalue;
#define PADMAX(x,y) ((x) > (y) ? (x) : (y))
if (max < 0)
max = 0;
if (!(flags & DP_F_UNSIGNED)) {
if (value < 0) {
signvalue = '-';
signvalue = '+';
else if (flags & DP_F_SPACE)
signvalue = ' ';
}
do {
if (place == 20)
place--;
if (zpadlen < 0)
zpadlen = 0;
if (spadlen < 0)
spadlen = 0;
spadlen = 0;
}
if (flags & DP_F_MINUS)
/* Spaces */
while (spadlen > 0) {
--spadlen;
}
/* Sign */
if (signvalue)
/* Zeros */
if (zpadlen > 0) {
while (zpadlen > 0) {
--zpadlen;
}
}
/* Digits */
while (place > 0)
/* Left Justified spaces */
while (spadlen < 0) {
++spadlen;
}
}
static long double
{
while (exp) {
result *= 10;
exp--;
}
return result;
}
static long
{
if (value >= 0.5)
intpart++;
return intpart;
}
static void
{
long double ufvalue;
/*
* AIX manpage says the default is 0, but Solaris says the default
* is 6, and sprintf on AIX defaults to 6
*/
if (max < 0)
max = 6;
if (fvalue < 0)
signvalue = '-';
signvalue = '+';
else if (flags & DP_F_SPACE)
signvalue = ' ';
/*
* Sorry, we only support 9 digits past the decimal because of our
* conversion method
*/
if (max > 9)
max = 9;
/* We "cheat" by converting the fractional part to integer by
* multiplying by a factor of 10
*/
intpart++;
}
/* Convert integer part */
do {
[intpart % 10];
if (iplace == 20)
iplace--;
/* Convert fractional part */
do {
[fracpart % 10];
if (fplace == 20)
fplace--;
/* -1 for decimal point, another -1 if we are printing a sign */
if (zpadlen < 0)
zpadlen = 0;
if (padlen < 0)
padlen = 0;
if (flags & DP_F_MINUS)
if (signvalue) {
--padlen;
signvalue = 0;
}
while (padlen > 0) {
--padlen;
}
}
while (padlen > 0) {
--padlen;
}
if (signvalue)
while (iplace > 0)
/*
* Decimal point. This should probably use locale to find the
* correct char to print out.
*/
while (fplace > 0)
while (zpadlen > 0) {
--zpadlen;
}
while (padlen < 0) {
++padlen;
}
}
static void
{
}
#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
#ifndef HAVE_VSNPRINTF
int
{
str[0] = 0;
}
#endif /* !HAVE_VSNPRINTF */
#ifndef HAVE_SNPRINTF
int
{
}
#endif /* !HAVE_SNPRINTF */