Initial import of libnbcompat-20030331, a portable NetBSD compatibility

library for use by the pkgsrc tools and other sources from NetBSD.
This commit is contained in:
grant 2003-03-31 05:02:17 +00:00
parent ba62a04bd7
commit 706e3fa728
74 changed files with 26330 additions and 0 deletions

View file

@ -0,0 +1,2 @@
libnbcompat is a portable NetBSD compatibility library for use by
the pkgsrc tools and other sources from NetBSD.

View file

@ -0,0 +1,22 @@
# $NetBSD: Makefile,v 1.1.1.1 2003/03/31 05:02:17 grant Exp $
#
DISTNAME= libnbcompat-20030331
CATEGORIES= pkgtools
MASTER_SITES= # empty
DISTFILES= # empty
MAINTAINER= grant@NetBSD.org
HOMEPAGE= http://www.NetBSD.org/
COMMENT= Portable NetBSD compatibility library
GNU_CONFIGURE= # defined
NO_PKGTOOLS_REQD_CHECK= # defined
NO_CHECKSUM= # defined
NO_PATCH= # defined
do-extract:
@${CP} -Rp ${FILESDIR} ${WRKSRC}
.include "../../mk/bsd.pkg.mk"

View file

@ -0,0 +1,19 @@
@comment $NetBSD: PLIST,v 1.1.1.1 2003/03/31 05:02:18 grant Exp $
include/libnbcompat/extern.h
include/libnbcompat/ftpglob.h
include/libnbcompat/fts.h
include/libnbcompat/md5.h
include/libnbcompat/mtree.h
include/libnbcompat/namespace.h
include/libnbcompat/nbcompat.h
include/libnbcompat/nbtypes.h
include/libnbcompat/pack_dev.h
include/libnbcompat/pwcache.h
include/libnbcompat/rmd160.h
include/libnbcompat/sha1.h
include/libnbcompat/sha2.h
include/libnbcompat/stat_flags.h
include/libnbcompat/util.h
include/libnbcompat/vis.h
@dirrm include/libnbcompat
lib/libnbcompat.a

View file

@ -0,0 +1,40 @@
# $NetBSD: buildlink2.mk,v 1.1.1.1 2003/03/31 05:02:18 grant Exp $
#
.if !defined(LIBPKGSRC_BUILDLINK2_MK)
LIBPKGSRC_BUILDLINK2_MK= # defined
BUILDLINK_DEPMETHOD.libpkgsrc= build
.include "../../mk/bsd.prefs.mk"
BUILDLINK_DEPENDS.libpkgsrc?= libpkgsrc>=20030331
BUILDLINK_PKGSRCDIR.libpkgsrc?= ../../pkgtools/libpkgsrc
BUILDLINK_PACKAGES+= libpkgsrc
EVAL_PREFIX+= BUILDLINK_PREFIX.libpkgsrc=libpkgsrc
BUILDLINK_PREFIX.libpkgsrc_DEFAULT= ${LOCALBASE}
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/extern.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/ftpglob.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/fts.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/md5.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/mtree.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/namespace.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/nbcompat.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/nb-types.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/pack_dev.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/pwcache.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/rmd160.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/sha1.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/sha2.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/stat_flags.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/util.h
BUILDLINK_FILES.libpkgsrc+= include/libpkgsrc/vis.h
BUILDLINK_FILES.libpkgsrc+= lib/libpkgsrc.a
BUILDLINK_TARGETS+= libpkgsrc-buildlink
libpkgsrc-buildlink: _BUILDLINK_USE
.endif # LIBPKGSRC_BUILDLINK2_MK

View file

@ -0,0 +1,70 @@
# $NetBSD: Makefile.in,v 1.1.1.1 2003/03/31 05:02:20 grant Exp $
#
srcdir = @srcdir@
prefix = @prefix@
VPATH = @srcdir@
SHELL = /bin/sh
CC = @CC@
CFLAGS = -I${srcdir} -I. @INCLUDES@ @CFLAGS@
CPPFLAGS= @CPPFLAGS@
DEFS = @DEFS@
INSTALL = @INSTALL@
LDFLAGS = @LDFLAGS@
AR = @AR@
RANLIB = @RANLIB@
LIB = libnbcompat.a
OBJS = @LIBOBJS@ \
md5c.o md5hl.o rmd160.o rmd160hl.o sha1.o sha1hl.o \
sha2.o sha2hl.o unvis.o vis.o setmode.o __fts13.o \
getid.o misc.o pack_dev.o spec.o stat_flags.o \
fparseln.o pwcache.o
LINK= = $(CCLD) $(CFLAGS) $(LDFLAGS) -o $@
COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS)
.c.o: nbtypes.h
$(COMPILE) $(DEFS) -c $< -o $@
all: ${LIB}
${LIB}: ${OBJS}
${AR} cr $@ ${OBJS}
${RANLIB} $@
nbtypes.h: bits
./bits nbtypes.h
bits: bits.c
${CC} -o bits bits.c
install:
${INSTALL} -m 555 libnbcompat.a ${prefix}/lib
${INSTALL} -m 755 -d ${prefix}/include/libnbcompat
${INSTALL} -m 444 extern.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 ftpglob.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 fts.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 md5.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 mtree.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 namespace.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 nbcompat.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 nbtypes.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 pack_dev.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 pwcache.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 rmd160.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 sha1.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 sha2.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 stat_flags.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 util.h ${prefix}/include/libnbcompat
${INSTALL} -m 444 vis.h ${prefix}/include/libnbcompat
clean:
rm -f *.a *.o bits nbtypes.h config.h
distclean: clean
rm -f Makefile config.log config.status configure.lineno

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
/* $Id: acconfig.h,v 1.1.1.1 2003/03/31 05:02:22 grant Exp $ */
/* Define if *printf() uses %qd to print `long long' (otherwise uses %lld) */
#undef HAVE_PRINTF_QD
/* Define if `long long' is supported and sizeof(off_t) >= 8 */
#undef HAVE_QUAD_SUPPORT

141
pkgtools/libnbcompat/files/aclocal.m4 vendored Normal file
View file

@ -0,0 +1,141 @@
dnl $Id: aclocal.m4,v 1.1.1.1 2003/03/31 05:02:22 grant Exp $
dnl
dnl
dnl AC_MSG_TRY_COMPILE
dnl
dnl Written by Luke Mewburn <lukem@netbsd.org>
dnl
dnl Usage:
dnl AC_MSG_TRY_COMPILE(Message, CacheVar, Includes, Code,
dnl ActionPass [,ActionFail] )
dnl
dnl effectively does:
dnl AC_CACHE_CHECK(Message, CacheVar,
dnl AC_TRY_COMPILE(Includes, Code, CacheVar = yes, CacheVar = no)
dnl if CacheVar == yes
dnl AC_MESSAGE_RESULT(yes)
dnl ActionPass
dnl else
dnl AC_MESSAGE_RESULT(no)
dnl ActionFail
dnl )
dnl
AC_DEFUN(AC_MSG_TRY_COMPILE, [
AC_CACHE_CHECK($1, $2, [
AC_TRY_COMPILE([ $3 ], [ $4; ], [ $2=yes ], [ $2=no ])
])
if test "x[$]$2" = "xyes"; then
$5
else
$6
:
fi
])
dnl
dnl AC_MSG_TRY_LINK
dnl
dnl Usage:
dnl AC_MSG_TRY_LINK(Message, CacheVar, Includes, Code,
dnl ActionPass [,ActionFail] )
dnl
dnl as AC_MSG_TRY_COMPILE, but uses AC_TRY_LINK instead of AC_TRY_COMPILE
dnl
AC_DEFUN(AC_MSG_TRY_LINK, [
AC_CACHE_CHECK($1, $2, [
AC_TRY_LINK([ $3 ], [ $4; ], [ $2=yes ], [ $2=no ])
])
if test "x[$]$2" = "xyes"; then
$5
else
$6
:
fi
])
dnl
dnl AC_LIBRARY_NET: #Id: net.m4,v 1.5 1997/11/09 21:36:54 jhawk Exp #
dnl
dnl Written by John Hawkinson <jhawk@mit.edu>. This code is in the Public
dnl Domain.
dnl
dnl This test is for network applications that need socket() and
dnl gethostbyname() -ish functions. Under Solaris, those applications need to
dnl link with "-lsocket -lnsl". Under IRIX, they should *not* link with
dnl "-lsocket" because libsocket.a breaks a number of things (for instance:
dnl gethostbyname() under IRIX 5.2, and snoop sockets under most versions of
dnl IRIX).
dnl
dnl Unfortunately, many application developers are not aware of this, and
dnl mistakenly write tests that cause -lsocket to be used under IRIX. It is
dnl also easy to write tests that cause -lnsl to be used under operating
dnl systems where neither are necessary (or useful), such as SunOS 4.1.4, which
dnl uses -lnsl for TLI.
dnl
dnl This test exists so that every application developer does not test this in
dnl a different, and subtly broken fashion.
dnl
dnl It has been argued that this test should be broken up into two seperate
dnl tests, one for the resolver libraries, and one for the libraries necessary
dnl for using Sockets API. Unfortunately, the two are carefully intertwined and
dnl allowing the autoconf user to use them independantly potentially results in
dnl unfortunate ordering dependancies -- as such, such component macros would
dnl have to carefully use indirection and be aware if the other components were
dnl executed. Since other autoconf macros do not go to this trouble, and almost
dnl no applications use sockets without the resolver, this complexity has not
dnl been implemented.
dnl
dnl The check for libresolv is in case you are attempting to link statically
dnl and happen to have a libresolv.a lying around (and no libnsl.a).
dnl
AC_DEFUN(AC_LIBRARY_NET, [
# Most operating systems have gethostbyname() in the default searched
# libraries (i.e. libc):
AC_CHECK_FUNC(gethostbyname, ,
# Some OSes (eg. Solaris) place it in libnsl:
AC_CHECK_LIB(nsl, gethostbyname, ,
# Some strange OSes (SINIX) have it in libsocket:
AC_CHECK_LIB(socket, gethostbyname, ,
# Unfortunately libsocket sometimes depends on libnsl.
# AC_CHECK_LIB's API is essentially broken so the following
# ugliness is necessary:
AC_CHECK_LIB(socket, gethostbyname,
LIBS="-lsocket -lnsl $LIBS",
AC_CHECK_LIB(resolv, gethostbyname),
-lnsl)
)
)
)
AC_CHECK_FUNC(socket, , AC_CHECK_LIB(socket, socket, ,
AC_CHECK_LIB(socket, socket, LIBS="-lsocket -lnsl $LIBS", , -lnsl)))
])
dnl
dnl Test for __attribute__
dnl
AC_DEFUN(AC_C___ATTRIBUTE__, [
AC_MSG_CHECKING(for __attribute__)
AC_CACHE_VAL(ac_cv___attribute__, [
AC_TRY_COMPILE([
#include <stdlib.h>
],
[
static void foo(void) __attribute__ ((noreturn));
static void
foo(void)
{
exit(1);
}
],
ac_cv___attribute__=yes,
ac_cv___attribute__=no)])
if test "$ac_cv___attribute__" = "yes"; then
AC_DEFINE(HAVE___ATTRIBUTE__, 1, [define if your compiler has __attribute__])
fi
AC_MSG_RESULT($ac_cv___attribute__)
])

View file

@ -0,0 +1,184 @@
/*
* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* 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
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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.
*/
#include "config.h"
#if 0
__RCSID("$Id: bits.c,v 1.1.1.1 2003/03/31 05:02:23 grant Exp $");
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define BITSIZE(TYPE) \
{ \
int b = 0; TYPE x = 1, zero = 0; char *pre = "u"; \
char tmp[128], tmp2[128]; \
while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
if(b >= len){ \
int tabs; \
sprintf(tmp, "%sint%d_t" , pre, len); \
sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
tabs = 5 - strlen(tmp2) / 8; \
fprintf(f, "%s", tmp2); \
while(tabs-- > 0) fprintf(f, "\t"); \
fprintf(f, "/* %2d bits */\n", b); \
return; \
} \
}
#ifndef HAVE___ATTRIBUTE__
#define __attribute__(x)
#endif
static void
try_signed(FILE *f, int len) __attribute__ ((unused));
static void
try_unsigned(FILE *f, int len) __attribute__ ((unused));
static int
print_bt(FILE *f, int flag) __attribute__ ((unused));
static void
try_signed(FILE *f, int len)
{
BITSIZE(signed char);
BITSIZE(short);
BITSIZE(int);
BITSIZE(long);
#ifdef HAVE_LONG_LONG
BITSIZE(long long);
#endif
fprintf(f, "/* There is no %d bit type */\n", len);
}
static void
try_unsigned(FILE *f, int len)
{
BITSIZE(unsigned char);
BITSIZE(unsigned short);
BITSIZE(unsigned int);
BITSIZE(unsigned long);
#ifdef HAVE_LONG_LONG
BITSIZE(unsigned long long);
#endif
fprintf(f, "/* There is no %d bit type */\n", len);
}
int main(int argc, char **argv)
{
FILE *f;
char *fn, *hb;
if(argc < 2){
fn = "bits.h";
hb = "__BITS_H__";
f = stdout;
} else {
char *p;
fn = argv[1];
hb = malloc(strlen(fn) + 5);
sprintf(hb, "__%s__", fn);
for(p = hb; *p; p++){
if(!isalnum((unsigned char)*p))
*p = '_';
}
f = fopen(argv[1], "w");
}
fprintf(f, "/* %s -- this file was generated by\n", fn);
fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "",
"$Id: bits.c,v 1.1.1.1 2003/03/31 05:02:23 grant Exp $");
fprintf(f, "#ifndef %s\n", hb);
fprintf(f, "#define %s\n", hb);
fprintf(f, "\n");
#ifdef HAVE_SYS_TYPES_H
fprintf(f, "#include <sys/types.h>\n");
#endif
#ifdef HAVE_INTTYPES_H
fprintf(f, "#include <inttypes.h>\n");
#endif
#ifdef HAVE_SYS_BITYPES_H
fprintf(f, "#include <sys/bitypes.h>\n");
#endif
#ifdef HAVE_BIND_BITYPES_H
fprintf(f, "#include <bind/bitypes.h>\n");
#endif
#ifdef HAVE_NETINET_IN6_MACHTYPES_H
fprintf(f, "#include <netinet/in6_machtypes.h>\n");
#endif
fprintf(f, "\n");
#ifndef HAVE_INT8_T
try_signed (f, 8);
#endif /* HAVE_INT8_T */
#ifndef HAVE_INT16_T
try_signed (f, 16);
#endif /* HAVE_INT16_T */
#ifndef HAVE_INT32_T
try_signed (f, 32);
#endif /* HAVE_INT32_T */
#ifndef HAVE_INT64_T
try_signed (f, 64);
#endif /* HAVE_INT64_T */
#ifndef HAVE_UINT8_T
try_unsigned (f, 8);
#endif /* HAVE_UINT8_T */
#ifndef HAVE_UINT16_T
try_unsigned (f, 16);
#endif /* HAVE_UINT16_T */
#ifndef HAVE_UINT32_T
try_unsigned (f, 32);
#endif /* HAVE_UINT32_T */
#ifndef HAVE_UINT64_T
try_unsigned (f, 64);
#endif /* HAVE_UINT64_T */
#define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
#ifndef HAVE_U_INT8_T
X(8);
#endif /* HAVE_U_INT8_T */
#ifndef HAVE_U_INT16_T
X(16);
#endif /* HAVE_U_INT16_T */
#ifndef HAVE_U_INT32_T
X(32);
#endif /* HAVE_U_INT32_T */
#ifndef HAVE_U_INT64_T
X(64);
#endif /* HAVE_U_INT64_T */
fprintf(f, "#endif /* %s */\n", hb);
return 0;
}

1354
pkgtools/libnbcompat/files/config.guess vendored Executable file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,506 @@
/* config.h.in. Generated from configure.ac by autoheader. */
/* $Id: config.h.in,v 1.1.1.1 2003/03/31 05:02:20 grant Exp $ */
/* Define if *printf() uses %qd to print `long long' (otherwise uses %lld) */
#undef HAVE_PRINTF_QD
/* Define if `long long' is supported and sizeof(off_t) >= 8 */
#undef HAVE_QUAD_SUPPORT
/* Define to 1 if the `getpgrp' function requires zero arguments. */
#undef GETPGRP_VOID
/* Define to 1 if you have the <arpa/nameser.h> header file. */
#undef HAVE_ARPA_NAMESER_H
/* Define to 1 if you have the <assert.h> header file. */
#undef HAVE_ASSERT_H
/* Define to 1 if you have the `bcopy' function. */
#undef HAVE_BCOPY
/* Define to 1 if you have the <bind/bitypes.h> header file. */
#undef HAVE_BIND_BITYPES_H
/* Define to 1 if you have the `bzero' function. */
#undef HAVE_BZERO
/* Define to 1 if you have the declaration of `strerror_r', and to 0 if you
don't. */
#undef HAVE_DECL_STRERROR_R
/* Define to 1 if you have the <dirent.h> header file. */
#undef HAVE_DIRENT_H
/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
#undef HAVE_DOPRNT
/* Define to 1 if you have the `err' function. */
#undef HAVE_ERR
/* Define to 1 if you have the <errno.h> header file. */
#undef HAVE_ERRNO_H
/* Define to 1 if you have the <err.h> header file. */
#undef HAVE_ERR_H
/* Define to 1 if you have the <fcntl.h> header file. */
#undef HAVE_FCNTL_H
/* Define to 1 if you have the `fgetln' function. */
#undef HAVE_FGETLN
/* Define to 1 if you have the `fparseln' function. */
#undef HAVE_FPARSELN
/* Define to 1 if you have the `fseeko' function. */
#undef HAVE_FSEEKO
/* Define to 1 if you have the <fts.h> header file. */
#undef HAVE_FTS_H
/* Define to 1 if you have the `getenv' function. */
#undef HAVE_GETENV
/* Define to 1 if you have the `getpassphrase' function. */
#undef HAVE_GETPASSPHRASE
/* Define to 1 if you have the `getpgrp' function. */
#undef HAVE_GETPGRP
/* Define to 1 if you have the `gid_from_group' function. */
#undef HAVE_GID_FROM_GROUP
/* Define to 1 if you have the `glob' function. */
#undef HAVE_GLOB
/* Define to 1 if you have the `group_from_gid' function. */
#undef HAVE_GROUP_FROM_GID
/* Define to 1 if you have the `inet_ntop' function. */
#undef HAVE_INET_NTOP
/* Define to 1 if you have the `inet_pton' function. */
#undef HAVE_INET_PTON
/* Define to 1 if the system has the type `int16_t'. */
#undef HAVE_INT16_T
/* Define to 1 if the system has the type `int32_t'. */
#undef HAVE_INT32_T
/* Define to 1 if the system has the type `int64_t'. */
#undef HAVE_INT64_T
/* Define to 1 if the system has the type `int8_t'. */
#undef HAVE_INT8_T
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the `lchflags' function. */
#undef HAVE_LCHFLAGS
/* Define to 1 if you have the `lchmod' function. */
#undef HAVE_LCHMOD
/* Define to 1 if you have the `lchown' function. */
#undef HAVE_LCHOWN
/* Define to 1 if you have the <libutil.h> header file. */
#undef HAVE_LIBUTIL_H
/* Define to 1 if you have the <limits.h> header file. */
#undef HAVE_LIMITS_H
/* Define to 1 if you have the <locale.h> header file. */
#undef HAVE_LOCALE_H
/* Define to 1 if the system has the type `long long'. */
#undef HAVE_LONG_LONG
/* Define to 1 if you have the `lutimes' function. */
#undef HAVE_LUTIMES
/* Define to 1 if you have the <machine/endian.h> header file. */
#undef HAVE_MACHINE_ENDIAN_H
/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
to 0 otherwise. */
#undef HAVE_MALLOC
/* Define to 1 if you have the `MD5File' function. */
#undef HAVE_MD5FILE
/* Define to 1 if you have the `memcpy' function. */
#undef HAVE_MEMCPY
/* Define to 1 if you have the `memmove' function. */
#undef HAVE_MEMMOVE
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the `memset' function. */
#undef HAVE_MEMSET
/* Define to 1 if you have the `mkstemp' function. */
#undef HAVE_MKSTEMP
/* Define to 1 if you have the <param.h> header file. */
#undef HAVE_PARAM_H
/* Define to 1 if you have the <paths.h> header file. */
#undef HAVE_PATHS_H
/* Define to 1 if you have the `poll' function. */
#undef HAVE_POLL
/* Define to 1 if you have the <poll.h> header file. */
#undef HAVE_POLL_H
/* Define to 1 if you have the `pwcache_groupdb' function. */
#undef HAVE_PWCACHE_GROUPDB
/* Define to 1 if you have the `pwcache_userdb' function. */
#undef HAVE_PWCACHE_USERDB
/* Define to 1 if you have the <regex.h> header file. */
#undef HAVE_REGEX_H
/* Define to 1 if you have the `RMD160File' function. */
#undef HAVE_RMD160FILE
/* Define to 1 if you have the `select' function. */
#undef HAVE_SELECT
/* Define to 1 if you have the `setenv' function. */
#undef HAVE_SETENV
/* Define to 1 if you have the `setgrent' function. */
#undef HAVE_SETGRENT
/* Define to 1 if you have the `setgroupent' function. */
#undef HAVE_SETGROUPENT
/* Define to 1 if you have the `setlocale' function. */
#undef HAVE_SETLOCALE
/* Define to 1 if you have the `setpassent' function. */
#undef HAVE_SETPASSENT
/* Define to 1 if you have the `setprogname' function. */
#undef HAVE_SETPROGNAME
/* Define to 1 if you have the `setpwent' function. */
#undef HAVE_SETPWENT
/* Define to 1 if you have the `SHA1File' function. */
#undef HAVE_SHA1FILE
/* Define to 1 if you have the `sl_init' function. */
#undef HAVE_SL_INIT
/* Define to 1 if you have the `snprintf' function. */
#undef HAVE_SNPRINTF
/* Define to 1 if you have the `statfs' function. */
#undef HAVE_STATFS
/* Define to 1 if you have the `statvfs' function. */
#undef HAVE_STATVFS
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the `strdup' function. */
#undef HAVE_STRDUP
/* Define to 1 if you have the `strerror' function. */
#undef HAVE_STRERROR
/* Define to 1 if you have the `strerror_r' function. */
#undef HAVE_STRERROR_R
/* Define to 1 if you have the <stringlist.h> header file. */
#undef HAVE_STRINGLIST_H
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the `strlcat' function. */
#undef HAVE_STRLCAT
/* Define to 1 if you have the `strlcpy' function. */
#undef HAVE_STRLCPY
/* Define to 1 if you have the `strptime' function. */
#undef HAVE_STRPTIME
/* Define to 1 if you have the `strsep' function. */
#undef HAVE_STRSEP
/* Define to 1 if you have the `strsvis' function. */
#undef HAVE_STRSVIS
/* Define to 1 if you have the `strtoll' function. */
#undef HAVE_STRTOLL
/* Define to 1 if you have the `strunvis' function. */
#undef HAVE_STRUNVIS
/* Define to 1 if you have the `strvis' function. */
#undef HAVE_STRVIS
/* Define to 1 if you have the <sys/bitypes.h> header file. */
#undef HAVE_SYS_BITYPES_H
/* Define to 1 if you have the <sys/byteorder.h> header file. */
#undef HAVE_SYS_BYTEORDER_H
/* Define to 1 if you have the <sys/cdefs.h> header file. */
#undef HAVE_SYS_CDEFS_H
/* Define to 1 if you have the <sys/endian.h> header file. */
#undef HAVE_SYS_ENDIAN_H
/* Define to 1 if you have the <sys/file.h> header file. */
#undef HAVE_SYS_FILE_H
/* Define to 1 if you have the <sys/mkdev.h> header file. */
#undef HAVE_SYS_MKDEV_H
/* Define to 1 if you have the <sys/mount.h> header file. */
#undef HAVE_SYS_MOUNT_H
/* Define to 1 if you have the <sys/param.h> header file. */
#undef HAVE_SYS_PARAM_H
/* Define to 1 if you have the <sys/poll.h> header file. */
#undef HAVE_SYS_POLL_H
/* Define to 1 if you have the <sys/queue.h> header file. */
#undef HAVE_SYS_QUEUE_H
/* Define to 1 if you have the <sys/statvfs.h> header file. */
#undef HAVE_SYS_STATVFS_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <sys/vfs.h> header file. */
#undef HAVE_SYS_VFS_H
/* Define to 1 if you have the <termcap.h> header file. */
#undef HAVE_TERMCAP_H
/* Define to 1 if you have the `timegm' function. */
#undef HAVE_TIMEGM
/* Define to 1 if you have the `uid_from_user' function. */
#undef HAVE_UID_FROM_USER
/* Define to 1 if the system has the type `uint16_t'. */
#undef HAVE_UINT16_T
/* Define to 1 if the system has the type `uint32_t'. */
#undef HAVE_UINT32_T
/* Define to 1 if the system has the type `uint64_t'. */
#undef HAVE_UINT64_T
/* Define to 1 if the system has the type `uint8_t'. */
#undef HAVE_UINT8_T
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to 1 if you have the `user_from_uid' function. */
#undef HAVE_USER_FROM_UID
/* Define to 1 if you have the `usleep' function. */
#undef HAVE_USLEEP
/* Define to 1 if you have the <util.h> header file. */
#undef HAVE_UTIL_H
/* Define to 1 if the system has the type `u_int16_t'. */
#undef HAVE_U_INT16_T
/* Define to 1 if the system has the type `u_int32_t'. */
#undef HAVE_U_INT32_T
/* Define to 1 if the system has the type `u_int64_t'. */
#undef HAVE_U_INT64_T
/* Define to 1 if the system has the type `u_int8_t'. */
#undef HAVE_U_INT8_T
/* Define to 1 if you have the <vis.h> header file. */
#undef HAVE_VIS_H
/* Define to 1 if you have the `vprintf' function. */
#undef HAVE_VPRINTF
/* define if your compiler has __attribute__ */
#undef HAVE___ATTRIBUTE__
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* The size of a `off_t', as computed by sizeof. */
#undef SIZEOF_OFF_T
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Define to 1 if strerror_r returns char *. */
#undef STRERROR_R_CHAR_P
/* Define to 1 if your processor stores words with the most significant byte
first (like Motorola and SPARC, unlike Intel and VAX). */
#undef WORDS_BIGENDIAN
/* Define to rpl_malloc if the replacement function should be used. */
#undef malloc
/* Define to `unsigned' if <sys/types.h> does not define. */
#undef size_t
#ifndef HAVE___ATTRIBUTE__
# define __attribute__(x)
#endif
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#ifndef __IDSTRING
# define __IDSTRING(name,string) \
static const char name[] __attribute__((__unused__)) = string
#endif
#ifndef __RCSID
# define __RCSID(_s) __IDSTRING(rcsid,_s)
#endif
#ifndef __COPYRIGHT
# define __COPYRIGHT(_s) __IDSTRING(copyright,_s)
#endif
#ifndef __P
# if defined(__STDC__) || defined(__cplusplus)
# define __P(protos) protos /* full-blown ANSI C */
# else
# define __P(protos) () /* traditional C preprocessor */
# endif
#endif
#ifndef __CONCAT
# if defined(__STDC__) || defined(__cplusplus)
# define __CONCAT(x,y) x ## y
# else
# define __CONCAT(x,y) x/**/y
# endif
#endif
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# else
# define __BEGIN_DECLS
# endif
#endif
#ifndef __END_DECLS
# if defined(__cplusplus)
# define __END_DECLS };
# else
# define __END_DECLS
# endif
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif
#ifdef HAVE_MACHINE_ENDIAN_H
#include <machine/endian.h>
#endif
#ifdef HAVE_SYS_BYTEORDER_H
#include <sys/byteorder.h>
#endif
#ifndef LITTLE_ENDIAN
# define LITTLE_ENDIAN 1234
#endif
#ifndef BIG_ENDIAN
# define BIG_ENDIAN 4321
#endif
#ifndef UINT_MAX
# define UINT_MAX 4294967295U
#endif
#ifndef BYTE_ORDER
# if defined(_BIG_ENDIAN)
# define BYTE_ORDER BIG_ENDIAN
# elif defined(_LITTE_ENDIAN)
# define BYTE_ORDER LITTLE_ENDIAN
# elif defined(WORDS_BIGENDIAN)
# define BYTE_ORDER BIG_ENDIAN
# else
# define BYTE_ORDER LITTLE_ENDIAN
# endif
#endif
#if defined(HAVE_MEMSET)
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
#else
# if defined(HAVE_BZERO)
#define MEMSET_BZERO(p,l) bzero((p), (l))
# else
#error You need either memset or bzero
# endif
#endif
#if defined(HAVE_MEMCPY)
#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
#else
# if defined(HAVE_BCOPY)
#define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
# else
#error You need either memcpy or bcopy
# endif
#endif

1463
pkgtools/libnbcompat/files/config.sub vendored Executable file

File diff suppressed because it is too large Load diff

7857
pkgtools/libnbcompat/files/configure vendored Executable file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,267 @@
dnl Process this file with autoconf to produce a configure script.
AC_PREREQ(2.52)
AC_INIT([libnbcompat], [20030331], [grant@NetBSD.org])
AC_CONFIG_SRCDIR([bits.c])
AC_CONFIG_HEADER(config.h)
AC_ARG_PROGRAM
AC_CANONICAL_HOST
CANONICAL_HOST=$host
AC_SUBST(CANONICAL_HOST)
AC_SUBST(INCLUDES)
# Checks for programs.
AC_PROG_MAKE_SET
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_RANLIB
AC_CHECK_PROG(AR, ar, ar)
AUTOCONF=${AUTOCONF-"$srcdir/missing --run autoconf"}
AC_SUBST(AUTOCONF)
AUTOHEADER=${AUTOHEADER-"$srcdir/missing --run autoheader"}
AC_SUBST(AUTOHEADER)
dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([sys/endian.h sys/file.h sys/mkdev.h sys/param.h])
AC_CHECK_HEADERS([assert.h arpa/nameser.h dirent.h err.h errno.h fcntl.h \
fts.h libutil.h locale.h machine/endian.h param.h paths.h \
poll.h regex.h stringlist.h sys/byteorder.h sys/poll.h \
termcap.h unistd.h util.h vis.h limits.h])
AC_CHECK_HEADERS([bind/bitypes.h inttypes.h sys/bitypes.h sys/cdefs.h \
sys/endian.h sys/mkdev.h sys/mount.h sys/queue.h \
sys/stat.h sys/statvfs.h sys/vfs.h sys/types.h \
sys/byteorder.h])
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C___ATTRIBUTE__
AC_TYPE_SIZE_T
AC_CHECK_TYPES([long long, int8_t, int16_t, int32_t, int64_t, u_int8_t, u_int16_t, u_int32_t, u_int64_t, uint8_t, uint16_t, uint32_t, uint64_t],[],[],
[#if STDC_HEADERS
#include <stdlib.h>
#include <stddef.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_BITYPES_H
#include <sys/bitypes.h>
#endif])
AC_C_BIGENDIAN
AC_CHECK_SIZEOF(off_t, 0)
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_STRERROR_R
AC_FUNC_VPRINTF
AC_REPLACE_FUNCS([err fgetln fparseln fseeko getenv glob inet_ntop \
inet_pton lchflags lchmod lchown lutimes mkstemp setenv \
setprogname sl_init snprintf strdup strerror strlcat \
strlcpy strptime strsep strtoll timegm usleep])
AC_CHECK_FUNCS([MD5File RMD160File SHA1File])
AC_CHECK_FUNCS([setgroupent setgrent setpassent setpwent])
AC_CHECK_FUNCS([bcopy bzero memset memcpy setlocale])
AC_CHECK_FUNCS([statfs statvfs])
AC_CHECK_FUNCS([gid_from_group group_from_gid pwcache_groupdb pwcache_userdb \
uid_from_user user_from_uid])
AC_CHECK_FUNCS([strunvis strvis strsvis])
AC_CHECK_FUNCS([getpassphrase getpgrp memmove poll select])
if test $ac_cv_func_getpgrp = yes; then
AC_FUNC_GETPGRP
fi
dnl if test $ac_cv_lib_util_fparseln != yes; then
dnl AC_REPLACE_FUNCS([fparseln])
dnl fi
if test $ac_cv_type_long_long = yes -a $ac_cv_sizeof_off_t -ge 8; then
dnl We assume that if sprintf() supports %lld or %qd,
dnl then all of *printf() does. If not, disable long long
dnl support because we don't know how to display it.
AC_MSG_CHECKING(*printf() support for %lld)
can_printf_longlong=no
AC_TRY_RUN([
#include <stdio.h>
int main() {
char buf[100];
sprintf(buf, "%lld", 4294967300LL);
return (strcmp(buf, "4294967300"));
}
], [
AC_MSG_RESULT(yes)
can_printf_longlong=yes
], [
AC_MSG_RESULT(no)
], [ : ])
if test $can_printf_longlong != yes; then
AC_MSG_CHECKING(*printf() support for %qd)
AC_TRY_RUN([
#include <stdio.h>
int main() {
char buf[100];
sprintf(buf, "%qd", 4294967300LL);
return (strcmp(buf, "4294967300"));
}
], [
AC_MSG_RESULT(yes)
can_printf_longlong=yes
AC_DEFINE(HAVE_PRINTF_QD, 1)
], [
AC_MSG_RESULT(no)
], [ : ])
fi
if test $can_printf_longlong = yes; then
AC_DEFINE(HAVE_QUAD_SUPPORT, 1)
AC_REPLACE_FUNCS(strtoll)
fi
fi
if test $ac_cv_func_sl_init = yes; then
AC_MSG_TRY_COMPILE(if sl_add() returns int, pkg_cv_INT_SL_ADD, [
#include <stringlist.h> ], [ int f = sl_add((StringList *)0, "foo") ],
[:] , [AC_LIBOBJ="$AC_LIBOBJ sl_init.o"])
fi
if test $ac_cv_header_vis_h = yes; then
AC_MSG_TRY_COMPILE(for VIS_WHITE in vis.h,
pkg_cv_have_vis_white, [
#include <sys/types.h>
#include <vis.h> ], [ int X = VIS_WHITE ], [:],
ac_cv_header_vis_h=no )
fi
AH_BOTTOM([
#ifndef HAVE___ATTRIBUTE__
# define __attribute__(x)
#endif
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#ifndef __IDSTRING
# define __IDSTRING(name,string) \
static const char name[] __attribute__((__unused__)) = string
#endif
#ifndef __RCSID
# define __RCSID(_s) __IDSTRING(rcsid,_s)
#endif
#ifndef __COPYRIGHT
# define __COPYRIGHT(_s) __IDSTRING(copyright,_s)
#endif
#ifndef __P
# if defined(__STDC__) || defined(__cplusplus)
# define __P(protos) protos /* full-blown ANSI C */
# else
# define __P(protos) () /* traditional C preprocessor */
# endif
#endif
#ifndef __CONCAT
# if defined(__STDC__) || defined(__cplusplus)
# define __CONCAT(x,y) x ## y
# else
# define __CONCAT(x,y) x/**/y
# endif
#endif
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# else
# define __BEGIN_DECLS
# endif
#endif
#ifndef __END_DECLS
# if defined(__cplusplus)
# define __END_DECLS };
# else
# define __END_DECLS
# endif
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif
#ifdef HAVE_MACHINE_ENDIAN_H
#include <machine/endian.h>
#endif
#ifdef HAVE_SYS_BYTEORDER_H
#include <sys/byteorder.h>
#endif
#ifndef LITTLE_ENDIAN
# define LITTLE_ENDIAN 1234
#endif
#ifndef BIG_ENDIAN
# define BIG_ENDIAN 4321
#endif
#ifndef UINT_MAX
# define UINT_MAX 4294967295U
#endif
#ifndef BYTE_ORDER
# if defined(_BIG_ENDIAN)
# define BYTE_ORDER BIG_ENDIAN
# elif defined(_LITTE_ENDIAN)
# define BYTE_ORDER LITTLE_ENDIAN
# elif defined(WORDS_BIGENDIAN)
# define BYTE_ORDER BIG_ENDIAN
# else
# define BYTE_ORDER LITTLE_ENDIAN
# endif
#endif
#if defined(HAVE_MEMSET)
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
#else
# if defined(HAVE_BZERO)
#define MEMSET_BZERO(p,l) bzero((p), (l))
# else
#error You need either memset or bzero
# endif
#endif
#if defined(HAVE_MEMCPY)
#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
#else
# if defined(HAVE_BCOPY)
#define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
# else
#error You need either memcpy or bcopy
# endif
#endif
])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

View file

@ -0,0 +1,92 @@
/* $Id: err.c,v 1.1.1.1 2003/03/31 05:02:43 grant Exp $ */
/*
* Copyright 1997-2000 Luke Mewburn <lukem@netbsd.org>.
* 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
* documentation and/or other materials provided with the distribution.
* 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 BY THE AUTHOR ``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.
*/
#include "nbcompat.h"
void
err(int eval, const char *fmt, ...)
{
va_list ap;
int sverrno;
sverrno = errno;
(void)fprintf(stderr, "%s: ", getprogname());
va_start(ap, fmt);
if (fmt != NULL) {
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, ": ");
}
va_end(ap);
(void)fprintf(stderr, "%s\n", strerror(sverrno));
exit(eval);
}
void
errx(int eval, const char *fmt, ...)
{
va_list ap;
(void)fprintf(stderr, "%s: ", getprogname());
va_start(ap, fmt);
if (fmt != NULL)
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
exit(eval);
}
void
warn(const char *fmt, ...)
{
va_list ap;
int sverrno;
sverrno = errno;
(void)fprintf(stderr, "%s: ", getprogname());
va_start(ap, fmt);
if (fmt != NULL) {
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, ": ");
}
va_end(ap);
(void)fprintf(stderr, "%s\n", strerror(sverrno));
}
void
warnx(const char *fmt, ...)
{
va_list ap;
(void)fprintf(stderr, "%s: ", getprogname());
va_start(ap, fmt);
if (fmt != NULL)
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
}

View file

@ -0,0 +1,95 @@
/* $NetBSD: extern.h,v 1.1.1.1 2003/03/31 05:02:43 grant Exp $ */
/*-
* Copyright (c) 1991, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)extern.h 8.1 (Berkeley) 6/6/93
*/
#include "nbtypes.h"
#include "mtree.h"
#if HAVE_CONFIG_H
#include "config.h"
#else
# ifdef netbsd
#define HAVE_LCHMOD 1
#define HAVE_STRUCT_STAT_ST_FLAGS 1
# endif
#endif
#ifdef HAVE_ERR_H
#include <err.h>
#endif
#include <fts.h>
#include <vis.h>
#if HAVE_NETDB_H
/* For MAXHOSTNAMELEN on some platforms. */
#include <netdb.h>
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
void addtag(slist_t *, char *);
int check_excludes(const char *, const char *);
int compare(NODE *, FTSENT *);
int crc(int, u_int32_t *, u_int32_t *);
void cwalk(void);
void dump_nodes(const char *, NODE *, int);
void init_excludes(void);
int matchtags(NODE *);
void mtree_err(const char *, ...)
__attribute__((__format__(__printf__, 1, 2)));
const char *nodetype(u_int);
u_int parsekey(const char *, int *);
void parsetags(slist_t *, char *);
u_int parsetype(const char *);
void read_excludes_file(const char *);
const char *rlink(const char *);
int verify(void);
extern int dflag, eflag, iflag, lflag, mflag, rflag, sflag, tflag, uflag;
extern int Wflag;
extern size_t mtree_lineno;
extern u_int32_t crc_total;
extern int ftsoptions, keys;
extern char fullpath[];
extern slist_t includetags, excludetags;
#include "stat_flags.h"

View file

@ -0,0 +1,84 @@
/* $NetBSD: fgetln.c,v 1.1.1.1 2003/03/31 05:02:43 grant Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
char *
fgetln(fp, len)
FILE *fp;
size_t *len;
{
static char *buf = NULL;
static size_t bufsiz = 0;
char *ptr;
if (buf == NULL) {
bufsiz = BUFSIZ;
if ((buf = malloc(bufsiz)) == NULL)
return NULL;
}
if (fgets(buf, bufsiz, fp) == NULL)
return NULL;
*len = 0;
while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
size_t nbufsiz = bufsiz + BUFSIZ;
char *nbuf = realloc(buf, nbufsiz);
if (nbuf == NULL) {
int oerrno = errno;
free(buf);
errno = oerrno;
buf = NULL;
return NULL;
} else
buf = nbuf;
*len = bufsiz;
if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL)
return buf;
bufsiz = nbufsiz;
}
*len = (ptr - buf) + 1;
return buf;
}

View file

@ -0,0 +1,207 @@
/* $Id: fparseln.c,v 1.1.1.1 2003/03/31 05:02:43 grant Exp $ */
/* $NetBSD: fparseln.c,v 1.1.1.1 2003/03/31 05:02:43 grant Exp $ */
/*
* Copyright (c) 1997 Christos Zoulas. 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christos Zoulas.
* 4. 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 BY THE AUTHOR ``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.
*/
#include "nbcompat.h"
static int isescaped(const char *, const char *, int);
/* isescaped():
* Return true if the character in *p that belongs to a string
* that starts in *sp, is escaped by the escape character esc.
*/
static int
isescaped(const char *sp, const char *p, int esc)
{
const char *cp;
size_t ne;
/* No escape character */
if (esc == '\0')
return 1;
/* Count the number of escape characters that precede ours */
for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
continue;
/* Return true if odd number of escape characters */
return (ne & 1) != 0;
}
/* fparseln():
* Read a line from a file parsing continuations ending in \
* and eliminating trailing newlines, or comments starting with
* the comment char.
*/
char *
fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
{
static const char dstr[3] = { '\\', '\\', '#' };
size_t s, len;
char *buf;
char *ptr, *cp;
int cnt;
char esc, con, nl, com;
len = 0;
buf = NULL;
cnt = 1;
if (str == NULL)
str = dstr;
esc = str[0];
con = str[1];
com = str[2];
/*
* XXX: it would be cool to be able to specify the newline character,
* but unfortunately, fgetln does not let us
*/
nl = '\n';
while (cnt) {
cnt = 0;
if (lineno)
(*lineno)++;
if ((ptr = fgetln(fp, &s)) == NULL)
break;
if (s && com) { /* Check and eliminate comments */
for (cp = ptr; cp < ptr + s; cp++)
if (*cp == com && !isescaped(ptr, cp, esc)) {
s = cp - ptr;
cnt = s == 0 && buf == NULL;
break;
}
}
if (s && nl) { /* Check and eliminate newlines */
cp = &ptr[s - 1];
if (*cp == nl)
s--; /* forget newline */
}
if (s && con) { /* Check and eliminate continuations */
cp = &ptr[s - 1];
if (*cp == con && !isescaped(ptr, cp, esc)) {
s--; /* forget escape */
cnt = 1;
}
}
if (s == 0 && buf != NULL)
continue;
if ((cp = realloc(buf, len + s + 1)) == NULL) {
free(buf);
return NULL;
}
buf = cp;
(void) memcpy(buf + len, ptr, s);
len += s;
buf[len] = '\0';
}
if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
strchr(buf, esc) != NULL) {
ptr = cp = buf;
while (cp[0] != '\0') {
int skipesc;
while (cp[0] != '\0' && cp[0] != esc)
*ptr++ = *cp++;
if (cp[0] == '\0' || cp[1] == '\0')
break;
skipesc = 0;
if (cp[1] == com)
skipesc += (flags & FPARSELN_UNESCCOMM);
if (cp[1] == con)
skipesc += (flags & FPARSELN_UNESCCONT);
if (cp[1] == esc)
skipesc += (flags & FPARSELN_UNESCESC);
if (cp[1] != com && cp[1] != con && cp[1] != esc)
skipesc = (flags & FPARSELN_UNESCREST);
if (skipesc)
cp++;
else
*ptr++ = *cp++;
*ptr++ = *cp++;
}
*ptr = '\0';
len = strlen(buf);
}
if (size)
*size = len;
return buf;
}
#ifdef TEST
int main(int, char *[]);
int
main(int argc, char *argv[])
{
char *ptr;
size_t size, line;
line = 0;
while ((ptr = fparseln(stdin, &size, &line, NULL,
FPARSELN_UNESCALL)) != NULL)
printf("line %d (%d) |%s|\n", line, size, ptr);
return 0;
}
/*
# This is a test
line 1
line 2 \
line 3 # Comment
line 4 \# Not comment \\\\
# And a comment \
line 5 \\\
line 6
*/
#endif /* TEST */

View file

@ -0,0 +1,40 @@
/* $Id: fseeko.c,v 1.1.1.1 2003/03/31 05:02:44 grant Exp $ */
/*
* Copyright 2002 Luke Mewburn <lukem@netbsd.org>.
* 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
* documentation and/or other materials provided with the distribution.
* 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 BY THE AUTHOR ``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.
*/
#include "nbcompat.h"
int
fseeko(FILE *stream, off_t offset, int whence)
{
if (offset > (off_t)LONG_MAX) {
errno = EINVAL;
return (-1);
}
return (fseek(stream, (long) offset, whence));
}

View file

@ -0,0 +1,92 @@
/* $Id: ftpglob.h,v 1.1.1.1 2003/03/31 05:02:45 grant Exp $ */
/* $NetBSD: ftpglob.h,v 1.1.1.1 2003/03/31 05:02:45 grant Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)glob.h 8.1 (Berkeley) 6/2/93
*/
#ifndef _GLOB_H_
#define _GLOB_H_
typedef struct {
int gl_pathc; /* Count of total paths so far. */
int gl_matchc; /* Count of paths matching pattern. */
int gl_offs; /* Reserved at beginning of gl_pathv. */
int gl_flags; /* Copy of flags parameter to glob. */
char **gl_pathv; /* List of paths matching pattern. */
/* Copy of errfunc parameter to glob. */
int (*gl_errfunc)(const char *, int);
/*
* Alternate filesystem access methods for glob; replacement
* versions of closedir(3), readdir(3), opendir(3), stat(2)
* and lstat(2).
*/
void (*gl_closedir)(void *);
struct dirent *(*gl_readdir)(void *);
void *(*gl_opendir)(const char *);
int (*gl_lstat)(const char *, struct stat *);
int (*gl_stat)(const char *, struct stat *);
} glob_t;
#define GLOB_APPEND 0x0001 /* Append to output from previous call. */
#define GLOB_DOOFFS 0x0002 /* Use gl_offs. */
#define GLOB_ERR 0x0004 /* Return on error. */
#define GLOB_MARK 0x0008 /* Append / to matching directories. */
#define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */
#define GLOB_NOSORT 0x0020 /* Don't sort. */
#define GLOB_NOESCAPE 0x1000 /* Disable backslash escaping. */
#define GLOB_NOSPACE (-1) /* Malloc call failed. */
#define GLOB_ABORTED (-2) /* Unignored error. */
#define GLOB_NOMATCH (-3) /* No match, and GLOB_NOCHECK was not set. */
#define GLOB_NOSYS (-4) /* Implementation does not support function. */
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
#define GLOB_BRACE 0x0080 /* Expand braces ala csh. */
#define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */
#define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */
#define GLOB_LIMIT 0x0400 /* Limit memory used by matches to ARG_MAX */
#define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */
#define GLOB_QUOTE 0 /* source compatibility */
#define GLOB_ABEND GLOB_ABORTED /* source compatibility */
int glob(const char *, int, int (*)(const char *, int), glob_t *);
void globfree(glob_t *);
#endif /* !_GLOB_H_ */

View file

@ -0,0 +1,131 @@
/* $NetBSD: fts.h,v 1.1.1.1 2003/03/31 05:02:44 grant Exp $ */
/*
* Copyright (c) 1989, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)fts.h 8.3 (Berkeley) 8/14/94
*/
#ifndef _FTS_H_
#define _FTS_H_
typedef struct {
struct _ftsent *fts_cur; /* current node */
struct _ftsent *fts_child; /* linked list of children */
struct _ftsent **fts_array; /* sort array */
dev_t fts_dev; /* starting device # */
char *fts_path; /* path for this descent */
int fts_rfd; /* fd for root */
int fts_pathlen; /* sizeof(path) */
int fts_nitems; /* elements in the sort array */
int (*fts_compar) /* compare function */
(const struct _ftsent **, const struct _ftsent **);
#define FTS_COMFOLLOW 0x001 /* follow command line symlinks */
#define FTS_LOGICAL 0x002 /* logical walk */
#define FTS_NOCHDIR 0x004 /* don't change directories */
#define FTS_NOSTAT 0x008 /* don't get stat info */
#define FTS_PHYSICAL 0x010 /* physical walk */
#define FTS_SEEDOT 0x020 /* return dot and dot-dot */
#define FTS_XDEV 0x040 /* don't cross devices */
#define FTS_WHITEOUT 0x080 /* return whiteout information */
#define FTS_OPTIONMASK 0x0ff /* valid user option mask */
#define FTS_NAMEONLY 0x100 /* (private) child names only */
#define FTS_STOP 0x200 /* (private) unrecoverable error */
int fts_options; /* fts_open options, global flags */
} FTS;
typedef struct _ftsent {
struct _ftsent *fts_cycle; /* cycle node */
struct _ftsent *fts_parent; /* parent directory */
struct _ftsent *fts_link; /* next file in directory */
long fts_number; /* local numeric value */
void *fts_pointer; /* local address value */
char *fts_accpath; /* access path */
char *fts_path; /* root path */
int fts_errno; /* errno for this node */
int fts_symfd; /* fd for symlink */
u_short fts_pathlen; /* strlen(fts_path) */
u_short fts_namelen; /* strlen(fts_name) */
ino_t fts_ino; /* inode */
dev_t fts_dev; /* device */
#ifdef __LIBC12_SOURCE__
u_int16_t fts_nlink; /* link count */
#else
nlink_t fts_nlink; /* link count */
#endif
#define FTS_ROOTPARENTLEVEL -1
#define FTS_ROOTLEVEL 0
short fts_level; /* depth (-1 to N) */
#define FTS_D 1 /* preorder directory */
#define FTS_DC 2 /* directory that causes cycles */
#define FTS_DEFAULT 3 /* none of the above */
#define FTS_DNR 4 /* unreadable directory */
#define FTS_DOT 5 /* dot or dot-dot */
#define FTS_DP 6 /* postorder directory */
#define FTS_ERR 7 /* error; errno is set */
#define FTS_F 8 /* regular file */
#define FTS_INIT 9 /* initialized only */
#define FTS_NS 10 /* stat(2) failed */
#define FTS_NSOK 11 /* no stat(2) requested */
#define FTS_SL 12 /* symbolic link */
#define FTS_SLNONE 13 /* symbolic link without target */
#define FTS_W 14 /* whiteout object */
u_short fts_info; /* user flags for FTSENT structure */
#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */
#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */
#define FTS_ISW 0x04 /* this is a whiteout object */
u_short fts_flags; /* private flags for FTSENT structure */
#define FTS_AGAIN 1 /* read node again */
#define FTS_FOLLOW 2 /* follow symbolic link */
#define FTS_NOINSTR 3 /* no instructions */
#define FTS_SKIP 4 /* discard node */
u_short fts_instr; /* fts_set() instructions */
struct stat *fts_statp; /* stat(2) information */
char fts_name[1]; /* file name */
} FTSENT;
FTSENT *fts_children (FTS *, int);
int fts_close (FTS *);
FTS *fts_open (char * const *, int,
int (*)(const FTSENT **, const FTSENT **));
FTSENT *fts_read (FTS *);
int fts_set (FTS *, FTSENT *, int);
#endif /* !_FTS_H_ */

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 1987 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)getenv.c 5.8 (Berkeley) 2/23/91";*/
static char *rcsid = "$Id: getenv.c,v 1.1.1.1 2003/03/31 05:02:46 grant Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/cdefs.h>
/*
* getenv --
* Returns ptr to value associated with name, if any, else NULL.
*/
char *
getenv(name)
const char *name;
{
int offset;
char *__findenv();
return(__findenv(name, &offset));
}
/*
* __findenv --
* Returns pointer to value associated with name, if any, else NULL.
* Sets offset to be the offset of the name/value combination in the
* environmental array, for use by setenv(3) and unsetenv(3).
* Explicitly removes '=' in argument name.
*
* This routine *should* be a static; don't use it.
*/
char *
__findenv(name, offset)
register char *name;
int *offset;
{
extern char **environ;
register int len;
register char **P, *C;
for (C = name, len = 0; *C && *C != '='; ++C, ++len);
for (P = environ; *P; ++P)
if (!strncmp(*P, name, len))
if (*(C = *P + len) == '=') {
*offset = P - environ;
return(++C);
}
return(NULL);
}

View file

@ -0,0 +1,462 @@
/* $NetBSD: getid.c,v 1.1.1.1 2003/03/31 05:02:46 grant Exp $ */
/* from: NetBSD: getpwent.c,v 1.48 2000/10/03 03:22:26 enami Exp */
/* from: NetBSD: getgrent.c,v 1.41 2002/01/12 23:51:30 lukem Exp */
/*
* Copyright (c) 1987, 1988, 1989, 1993, 1994, 1995
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn of Wasabi Systems.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include <sys/param.h>
#include <sys/time.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "extern.h"
#ifndef linux
static struct group * gi_getgrnam(const char *);
static struct group * gi_getgrgid(gid_t);
static int gi_setgroupent(int);
static int grscan(int, gid_t, const char *);
static int grmatchline(int, gid_t, const char *);
#endif
static void gi_endgrent(void);
static int grstart(void);
#ifndef linux
static struct passwd * gi_getpwnam(const char *);
static struct passwd * gi_getpwuid(uid_t);
static int gi_setpassent(int);
static int pwscan(int, uid_t, const char *);
static int pwmatchline(int, uid_t, const char *);
#endif
static void gi_endpwent(void);
static int pwstart(void);
#define MAXGRP 200
#define MAXLINELENGTH 1024
static FILE *_gr_fp;
#ifndef linux
static struct group _gr_group;
static int _gr_stayopen;
#endif
static int _gr_filesdone;
static FILE *_pw_fp;
#ifndef linux
static struct passwd _pw_passwd; /* password structure */
static int _pw_stayopen; /* keep fd's open */
#endif
static int _pw_filesdone;
static char grfile[MAXPATHLEN];
static char pwfile[MAXPATHLEN];
#ifndef linux
static char *members[MAXGRP];
static char grline[MAXLINELENGTH];
static char pwline[MAXLINELENGTH];
#endif
#ifndef GID_MAX
#define GID_MAX 2147483647U /* max value for a gid_t (2^31-2) */
#endif
#ifndef UID_MAX
#define UID_MAX 2147483647U /* max value for a uid_t (2^31-2) */
#endif
int
setup_getid(const char *dir)
{
if (dir == NULL)
return (0);
/* close existing databases */
gi_endgrent();
gi_endpwent();
/* build paths to new databases */
snprintf(grfile, sizeof(grfile), "%s/group", dir);
snprintf(pwfile, sizeof(pwfile), "%s/master.passwd", dir);
/* try to open new databases */
if (!grstart() || !pwstart())
return (0);
#ifdef netbsd
/* switch pwcache(3) lookup functions */
if (pwcache_groupdb(gi_setgroupent, gi_endgrent,
gi_getgrnam, gi_getgrgid) == -1
|| pwcache_userdb(gi_setpassent, gi_endpwent,
gi_getpwnam, gi_getpwuid) == -1)
return (0);
#endif
return (1);
}
/*
* group lookup functions
*/
#ifndef linux
static struct group *
gi_getgrnam(const char *name)
{
int rval;
if (!grstart())
return NULL;
rval = grscan(1, 0, name);
if (!_gr_stayopen)
endgrent();
return (rval) ? &_gr_group : NULL;
}
static struct group *
gi_getgrgid(gid_t gid)
{
int rval;
if (!grstart())
return NULL;
rval = grscan(1, gid, NULL);
if (!_gr_stayopen)
endgrent();
return (rval) ? &_gr_group : NULL;
}
static int
gi_setgroupent(int stayopen)
{
if (!grstart())
return 0;
_gr_stayopen = stayopen;
return 1;
}
#endif
static void
gi_endgrent(void)
{
_gr_filesdone = 0;
if (_gr_fp) {
(void)fclose(_gr_fp);
_gr_fp = NULL;
}
}
static int
grstart(void)
{
_gr_filesdone = 0;
if (_gr_fp) {
rewind(_gr_fp);
return 1;
}
if (grfile[0] == '\0') /* sanity check */
return 0;
return (_gr_fp = fopen(grfile, "r")) ? 1 : 0;
}
#ifndef linux
static int
grscan(int search, gid_t gid, const char *name)
{
if (_gr_filesdone)
return 0;
for (;;) {
if (!fgets(grline, sizeof(grline), _gr_fp)) {
if (!search)
_gr_filesdone = 1;
return 0;
}
/* skip lines that are too big */
if (!strchr(grline, '\n')) {
int ch;
while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
;
continue;
}
if (grmatchline(search, gid, name))
return 1;
}
/* NOTREACHED */
}
static int
grmatchline(int search, gid_t gid, const char *name)
{
unsigned long id;
char **m;
char *cp, *bp, *ep;
/* name may be NULL if search is nonzero */
bp = grline;
memset(&_gr_group, 0, sizeof(_gr_group));
_gr_group.gr_name = strsep(&bp, ":\n");
if (search && name && strcmp(_gr_group.gr_name, name))
return 0;
_gr_group.gr_passwd = strsep(&bp, ":\n");
if (!(cp = strsep(&bp, ":\n")))
return 0;
id = strtoul(cp, &ep, 10);
if (id > GID_MAX || *ep != '\0')
return 0;
_gr_group.gr_gid = (gid_t)id;
if (search && name == NULL && _gr_group.gr_gid != gid)
return 0;
cp = NULL;
if (bp == NULL)
return 0;
for (_gr_group.gr_mem = m = members;; bp++) {
if (m == &members[MAXGRP - 1])
break;
if (*bp == ',') {
if (cp) {
*bp = '\0';
*m++ = cp;
cp = NULL;
}
} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
if (cp) {
*bp = '\0';
*m++ = cp;
}
break;
} else if (cp == NULL)
cp = bp;
}
*m = NULL;
return 1;
}
#endif
/*
* user lookup functions
*/
#ifndef linux
static struct passwd *
gi_getpwnam(const char *name)
{
int rval;
if (!pwstart())
return NULL;
rval = pwscan(1, 0, name);
if (!_pw_stayopen)
endpwent();
return (rval) ? &_pw_passwd : NULL;
}
static struct passwd *
gi_getpwuid(uid_t uid)
{
int rval;
if (!pwstart())
return NULL;
rval = pwscan(1, uid, NULL);
if (!_pw_stayopen)
endpwent();
return (rval) ? &_pw_passwd : NULL;
}
static int
gi_setpassent(int stayopen)
{
if (!pwstart())
return 0;
_pw_stayopen = stayopen;
return 1;
}
#endif
static void
gi_endpwent(void)
{
_pw_filesdone = 0;
if (_pw_fp) {
(void)fclose(_pw_fp);
_pw_fp = NULL;
}
}
static int
pwstart(void)
{
_pw_filesdone = 0;
if (_pw_fp) {
rewind(_pw_fp);
return 1;
}
if (pwfile[0] == '\0') /* sanity check */
return 0;
return (_pw_fp = fopen(pwfile, "r")) ? 1 : 0;
}
#ifndef linux
static int
pwscan(int search, uid_t uid, const char *name)
{
if (_pw_filesdone)
return 0;
for (;;) {
if (!fgets(pwline, sizeof(pwline), _pw_fp)) {
if (!search)
_pw_filesdone = 1;
return 0;
}
/* skip lines that are too big */
if (!strchr(pwline, '\n')) {
int ch;
while ((ch = getc(_pw_fp)) != '\n' && ch != EOF)
;
continue;
}
if (pwmatchline(search, uid, name))
return 1;
}
/* NOTREACHED */
}
static int
pwmatchline(int search, uid_t uid, const char *name)
{
unsigned long id;
char *cp, *bp, *ep;
/* name may be NULL if search is nonzero */
bp = pwline;
memset(&_pw_passwd, 0, sizeof(_pw_passwd));
_pw_passwd.pw_name = strsep(&bp, ":\n"); /* name */
if (search && name && strcmp(_pw_passwd.pw_name, name))
return 0;
_pw_passwd.pw_passwd = strsep(&bp, ":\n"); /* passwd */
if (!(cp = strsep(&bp, ":\n"))) /* uid */
return 0;
id = strtoul(cp, &ep, 10);
if (id > UID_MAX || *ep != '\0')
return 0;
_pw_passwd.pw_uid = (uid_t)id;
if (search && name == NULL && _pw_passwd.pw_uid != uid)
return 0;
if (!(cp = strsep(&bp, ":\n"))) /* gid */
return 0;
id = strtoul(cp, &ep, 10);
if (id > GID_MAX || *ep != '\0')
return 0;
_pw_passwd.pw_gid = (gid_t)id;
if (!(ep = strsep(&bp, ":"))) /* class */
return 0;
if (!(ep = strsep(&bp, ":"))) /* change */
return 0;
if (!(ep = strsep(&bp, ":"))) /* expire */
return 0;
if (!(_pw_passwd.pw_gecos = strsep(&bp, ":\n"))) /* gecos */
return 0;
if (!(_pw_passwd.pw_dir = strsep(&bp, ":\n"))) /* directory */
return 0;
if (!(_pw_passwd.pw_shell = strsep(&bp, ":\n"))) /* shell */
return 0;
if (strchr(bp, ':') != NULL)
return 0;
return 1;
}
#endif

View file

@ -0,0 +1,887 @@
/* $Id: glob.c,v 1.1.1.1 2003/03/31 05:02:48 grant Exp $ */
/* $NetBSD: glob.c,v 1.1.1.1 2003/03/31 05:02:48 grant Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
/*
* glob(3) -- a superset of the one defined in POSIX 1003.2.
*
* The [!...] convention to negate a range is supported (SysV, Posix, ksh).
*
* Optional extra services, controlled by flags not defined by POSIX:
*
* GLOB_MAGCHAR:
* Set in gl_flags if pattern contained a globbing character.
* GLOB_NOMAGIC:
* Same as GLOB_NOCHECK, but it will only append pattern if it did
* not contain any magic characters. [Used in csh style globbing]
* GLOB_ALTDIRFUNC:
* Use alternately specified directory access functions.
* GLOB_TILDE:
* expand ~user/foo to the /home/dir/of/user/foo
* GLOB_BRACE:
* expand {1,2}{a,b} to 1a 1b 2a 2b
* gl_matchc:
* Number of matches in the current invocation of glob.
*/
#include "nbcompat.h"
#undef TILDE /* XXX: AIX 4.1.5 has this in <sys/ioctl.h> */
#define DOLLAR '$'
#define DOT '.'
#define EOS '\0'
#define LBRACKET '['
#define NOT '!'
#define QUESTION '?'
#define QUOTE '\\'
#define RANGE '-'
#define RBRACKET ']'
#define SEP '/'
#define STAR '*'
#define TILDE '~'
#define UNDERSCORE '_'
#define LBRACE '{'
#define RBRACE '}'
#define SLASH '/'
#define COMMA ','
#ifndef DEBUG
#define M_QUOTE 0x8000
#define M_PROTECT 0x4000
#define M_MASK 0xffff
#define M_ASCII 0x00ff
typedef u_short Char;
#else
#define M_QUOTE 0x80
#define M_PROTECT 0x40
#define M_MASK 0xff
#define M_ASCII 0x7f
typedef char Char;
#endif
#define CHAR(c) ((Char)((c)&M_ASCII))
#define META(c) ((Char)((c)|M_QUOTE))
#define M_ALL META('*')
#define M_END META(']')
#define M_NOT META('!')
#define M_ONE META('?')
#define M_RNG META('-')
#define M_SET META('[')
#define ismeta(c) (((c)&M_QUOTE) != 0)
static int compare(const void *, const void *);
static int g_Ctoc(const Char *, char *, size_t);
static int g_lstat(Char *, struct stat *, glob_t *);
static DIR *g_opendir(Char *, glob_t *);
static Char *g_strchr(const Char *, int);
static int g_stat(Char *, struct stat *, glob_t *);
static int glob0(const Char *, glob_t *);
static int glob1(Char *, glob_t *, size_t *);
static int glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
size_t *);
static int globextend(const Char *, glob_t *, size_t *);
static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
static int globexp1(const Char *, glob_t *);
static int globexp2(const Char *, const Char *, glob_t *, int *);
static int match(Char *, Char *, Char *);
#ifdef DEBUG
static void qprintf(const char *, Char *);
#endif
int
glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
glob_t *pglob)
{
const u_char *patnext;
int c;
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
patnext = (const u_char *) pattern;
if (!(flags & GLOB_APPEND)) {
pglob->gl_pathc = 0;
pglob->gl_pathv = NULL;
if (!(flags & GLOB_DOOFFS))
pglob->gl_offs = 0;
}
pglob->gl_flags = flags & ~GLOB_MAGCHAR;
pglob->gl_errfunc = errfunc;
pglob->gl_matchc = 0;
bufnext = patbuf;
bufend = bufnext + MAXPATHLEN;
if (flags & GLOB_NOESCAPE) {
while (bufnext < bufend && (c = *patnext++) != EOS)
*bufnext++ = c;
} else {
/* Protect the quoted characters. */
while (bufnext < bufend && (c = *patnext++) != EOS)
if (c == QUOTE) {
if ((c = *patnext++) == EOS) {
c = QUOTE;
--patnext;
}
*bufnext++ = c | M_PROTECT;
}
else
*bufnext++ = c;
}
*bufnext = EOS;
if (flags & GLOB_BRACE)
return globexp1(patbuf, pglob);
else
return glob0(patbuf, pglob);
}
/*
* Expand recursively a glob {} pattern. When there is no more expansion
* invoke the standard globbing routine to glob the rest of the magic
* characters
*/
static int
globexp1(const Char *pattern, glob_t *pglob)
{
const Char* ptr = pattern;
int rv;
/* Protect a single {}, for find(1), like csh */
if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
return glob0(pattern, pglob);
while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
if (!globexp2(ptr, pattern, pglob, &rv))
return rv;
return glob0(pattern, pglob);
}
/*
* Recursive brace globbing helper. Tries to expand a single brace.
* If it succeeds then it invokes globexp1 with the new pattern.
* If it fails then it tries to glob the rest of the pattern and returns.
*/
static int
globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv)
{
int i;
Char *lm, *ls;
const Char *pe, *pm, *pl;
Char patbuf[MAXPATHLEN + 1];
/* copy part up to the brace */
for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
continue;
ls = lm;
/* Find the balanced brace */
for (i = 0, pe = ++ptr; *pe; pe++)
if (*pe == LBRACKET) {
/* Ignore everything between [] */
for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
continue;
if (*pe == EOS) {
/*
* We could not find a matching RBRACKET.
* Ignore and just look for RBRACE
*/
pe = pm;
}
}
else if (*pe == LBRACE)
i++;
else if (*pe == RBRACE) {
if (i == 0)
break;
i--;
}
/* Non matching braces; just glob the pattern */
if (i != 0 || *pe == EOS) {
/*
* we use `pattern', not `patbuf' here so that that
* unbalanced braces are passed to the match
*/
*rv = glob0(pattern, pglob);
return 0;
}
for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
switch (*pm) {
case LBRACKET:
/* Ignore everything between [] */
for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
continue;
if (*pm == EOS) {
/*
* We could not find a matching RBRACKET.
* Ignore and just look for RBRACE
*/
pm = pl;
}
break;
case LBRACE:
i++;
break;
case RBRACE:
if (i) {
i--;
break;
}
/* FALLTHROUGH */
case COMMA:
if (i && *pm == COMMA)
break;
else {
/* Append the current string */
for (lm = ls; (pl < pm); *lm++ = *pl++)
continue;
/*
* Append the rest of the pattern after the
* closing brace
*/
for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
continue;
/* Expand the current pattern */
#ifdef DEBUG
qprintf("globexp2:", patbuf);
#endif
*rv = globexp1(patbuf, pglob);
/* move after the comma, to the next string */
pl = pm + 1;
}
break;
default:
break;
}
}
*rv = 0;
return 0;
}
/*
* expand tilde from the passwd file.
*/
static const Char *
globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
{
struct passwd *pwd;
const char *h;
const Char *p;
Char *b;
char *d;
Char *pend = &patbuf[patsize / sizeof(Char)];
pend--;
if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
return pattern;
/* Copy up to the end of the string or / */
for (p = pattern + 1, d = (char *)(void *)patbuf;
d < (char *)(void *)pend && *p && *p != SLASH;
*d++ = *p++)
continue;
if (d == (char *)(void *)pend)
return NULL;
*d = EOS;
d = (char *)(void *)patbuf;
if (*d == EOS) {
/*
* handle a plain ~ or ~/ by expanding $HOME
* first and then trying the password file
*/
if ((h = getenv("HOME")) == NULL) {
if ((pwd = getpwuid(getuid())) == NULL)
return pattern;
else
h = pwd->pw_dir;
}
}
else {
/*
* Expand a ~user
*/
if ((pwd = getpwnam(d)) == NULL)
return pattern;
else
h = pwd->pw_dir;
}
/* Copy the home directory */
for (b = patbuf; b < pend && *h; *b++ = *h++)
continue;
if (b == pend)
return NULL;
/* Append the rest of the pattern */
while (b < pend && (*b++ = *p++) != EOS)
continue;
if (b == pend)
return NULL;
return patbuf;
}
/*
* The main glob() routine: compiles the pattern (optionally processing
* quotes), calls glob1() to do the real pattern matching, and finally
* sorts the list (unless unsorted operation is requested). Returns 0
* if things went well, nonzero if errors occurred. It is not an error
* to find no matches.
*/
static int
glob0(const Char *pattern, glob_t *pglob)
{
const Char *qpatnext;
int c, error, oldpathc;
Char *bufnext, patbuf[MAXPATHLEN+1];
size_t limit;
limit = 0;
if ((qpatnext = globtilde(pattern, patbuf, sizeof(patbuf),
pglob)) == NULL)
return GLOB_ABEND;
oldpathc = pglob->gl_pathc;
bufnext = patbuf;
/* We don't need to check for buffer overflow any more. */
while ((c = *qpatnext++) != EOS) {
switch (c) {
case LBRACKET:
c = *qpatnext;
if (c == NOT)
++qpatnext;
if (*qpatnext == EOS ||
g_strchr(qpatnext+1, RBRACKET) == NULL) {
*bufnext++ = LBRACKET;
if (c == NOT)
--qpatnext;
break;
}
*bufnext++ = M_SET;
if (c == NOT)
*bufnext++ = M_NOT;
c = *qpatnext++;
do {
*bufnext++ = CHAR(c);
if (*qpatnext == RANGE &&
(c = qpatnext[1]) != RBRACKET) {
*bufnext++ = M_RNG;
*bufnext++ = CHAR(c);
qpatnext += 2;
}
} while ((c = *qpatnext++) != RBRACKET);
pglob->gl_flags |= GLOB_MAGCHAR;
*bufnext++ = M_END;
break;
case QUESTION:
pglob->gl_flags |= GLOB_MAGCHAR;
*bufnext++ = M_ONE;
break;
case STAR:
pglob->gl_flags |= GLOB_MAGCHAR;
/* collapse adjacent stars to one,
* to avoid exponential behavior
*/
if (bufnext == patbuf || bufnext[-1] != M_ALL)
*bufnext++ = M_ALL;
break;
default:
*bufnext++ = CHAR(c);
break;
}
}
*bufnext = EOS;
#ifdef DEBUG
qprintf("glob0:", patbuf);
#endif
if ((error = glob1(patbuf, pglob, &limit)) != 0)
return(error);
if (pglob->gl_pathc == oldpathc) {
/*
* If there was no match we are going to append the pattern
* if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was
* specified and the pattern did not contain any magic
* characters GLOB_NOMAGIC is there just for compatibility
* with csh.
*/
if ((pglob->gl_flags & GLOB_NOCHECK) ||
((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR))
== GLOB_NOMAGIC)) {
return globextend(pattern, pglob, &limit);
} else {
return (GLOB_NOMATCH);
}
} else if (!(pglob->gl_flags & GLOB_NOSORT)) {
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
(size_t)pglob->gl_pathc - oldpathc, sizeof(char *),
compare);
}
return(0);
}
static int
compare(const void *p, const void *q)
{
return(strcoll(*(const char * const *)p, *(const char * const *)q));
}
static int
glob1(Char *pattern, glob_t *pglob, size_t *limit)
{
Char pathbuf[MAXPATHLEN+1];
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
if (*pattern == EOS)
return(0);
/*
* we save one character so that we can use ptr >= limit,
* in the general case when we are appending non nul chars only.
*/
return(glob2(pathbuf, pathbuf, pathbuf + sizeof(pathbuf) - 1, pattern,
pglob, limit));
}
/*
* The functions glob2 and glob3 are mutually recursive; there is one level
* of recursion for each segment in the pattern that contains one or more
* meta characters.
*/
static int
glob2(Char *pathbuf, Char *pathend, Char *pathlim,
Char *pattern, glob_t *pglob, size_t *limit)
{
struct stat sb;
Char *p, *q;
int anymeta;
/*
* Loop over pattern segments until end of pattern or until
* segment with meta character found.
*/
for (anymeta = 0;;) {
if (*pattern == EOS) { /* End of pattern? */
*pathend = EOS;
if (g_lstat(pathbuf, &sb, pglob))
return(0);
if (((pglob->gl_flags & GLOB_MARK) &&
pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
(S_ISLNK(sb.st_mode) &&
(g_stat(pathbuf, &sb, pglob) == 0) &&
S_ISDIR(sb.st_mode)))) {
if (pathend >= pathlim)
return (GLOB_ABORTED);
*pathend++ = SEP;
*pathend = EOS;
}
++pglob->gl_matchc;
return(globextend(pathbuf, pglob, limit));
}
/* Find end of next segment, copy tentatively to pathend. */
q = pathend;
p = pattern;
while (*p != EOS && *p != SEP) {
if (ismeta(*p))
anymeta = 1;
if (q >= pathlim)
return GLOB_ABORTED;
*q++ = *p++;
}
if (!anymeta) { /* No expansion, do next segment. */
pathend = q;
pattern = p;
while (*pattern == SEP) {
if (pathend >= pathlim)
return GLOB_ABORTED;
*pathend++ = *pattern++;
}
} else /* Need expansion, recurse. */
return(glob3(pathbuf, pathend, pathlim, pattern, p,
pglob, limit));
}
/* NOTREACHED */
}
static int
glob3(Char *pathbuf, Char *pathend, Char *pathlim,
Char *pattern, Char *restpattern, glob_t *pglob, size_t *limit)
{
struct dirent *dp;
DIR *dirp;
int error;
char buf[MAXPATHLEN];
/*
* The readdirfunc declaration can't be prototyped, because it is
* assigned, below, to two functions which are prototyped in glob.h
* and dirent.h as taking pointers to differently typed opaque
* structures.
*/
struct dirent *(*readdirfunc)(void *);
*pathend = EOS;
errno = 0;
if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
if (pglob->gl_errfunc) {
if (g_Ctoc(pathbuf, buf, sizeof(buf)))
return (GLOB_ABORTED);
if (pglob->gl_errfunc(buf, errno) ||
pglob->gl_flags & GLOB_ERR)
return (GLOB_ABORTED);
}
/*
* Posix/XOpen: glob should return when it encounters a
* directory that it cannot open or read
* XXX: Should we ignore ENOTDIR and ENOENT though?
* I think that Posix had in mind EPERM...
*/
if (pglob->gl_flags & GLOB_ERR)
return (GLOB_ABORTED);
return(0);
}
error = 0;
/* Search directory for matching names. */
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
readdirfunc = pglob->gl_readdir;
else
readdirfunc = (struct dirent *(*)(void *)) readdir;
while ((dp = (*readdirfunc)(dirp)) != NULL) {
u_char *sc;
Char *dc;
/* Initial DOT must be matched literally. */
if (dp->d_name[0] == DOT && *pattern != DOT)
continue;
/*
* The resulting string contains EOS, so we can
* use the pathlim character, if it is the nul
*/
for (sc = (u_char *) dp->d_name, dc = pathend;
dc <= pathlim && (*dc++ = *sc++) != EOS;)
continue;
/*
* Have we filled the buffer without seeing EOS?
*/
if (dc > pathlim && *pathlim != EOS) {
/*
* Abort when requested by caller, otherwise
* reset pathend back to last SEP and continue
* with next dir entry.
*/
if (pglob->gl_flags & GLOB_ERR) {
error = GLOB_ABORTED;
break;
}
else {
*pathend = EOS;
continue;
}
}
if (!match(pathend, pattern, restpattern)) {
*pathend = EOS;
continue;
}
error = glob2(pathbuf, --dc, pathlim, restpattern, pglob, limit);
if (error)
break;
}
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
(*pglob->gl_closedir)(dirp);
else
closedir(dirp);
/*
* Again Posix X/Open issue with regards to error handling.
*/
if ((error || errno) && (pglob->gl_flags & GLOB_ERR))
return (GLOB_ABORTED);
return(error);
}
/*
* Extend the gl_pathv member of a glob_t structure to accomodate a new item,
* add the new item, and update gl_pathc.
*
* This assumes the BSD realloc, which only copies the block when its size
* crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
* behavior.
*
* Return 0 if new item added, error code if memory couldn't be allocated.
*
* Invariant of the glob_t structure:
* Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
* gl_pathv points to (gl_offs + gl_pathc + 1) items.
*/
static int
globextend(const Char *path, glob_t *pglob, size_t *limit)
{
char **pathv;
int i;
size_t newsize, len;
char *copy;
const Char *p;
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) :
malloc(newsize);
if (pathv == NULL)
return(GLOB_NOSPACE);
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
/* first time around -- clear initial gl_offs items */
pathv += pglob->gl_offs;
for (i = pglob->gl_offs; --i >= 0; )
*--pathv = NULL;
}
pglob->gl_pathv = pathv;
for (p = path; *p++;)
continue;
len = (size_t)(p - path);
*limit += len;
if ((copy = malloc(len)) != NULL) {
if (g_Ctoc(path, copy, len)) {
free(copy);
return(GLOB_ABORTED);
}
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
}
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
if ((pglob->gl_flags & GLOB_LIMIT) && (newsize + *limit) >= ARG_MAX) {
errno = 0;
return(GLOB_NOSPACE);
}
return(copy == NULL ? GLOB_NOSPACE : 0);
}
/*
* pattern matching function for filenames. Each occurrence of the *
* pattern causes a recursion level.
*/
static int
match(Char *name, Char *pat, Char *patend)
{
int ok, negate_range;
Char c, k;
while (pat < patend) {
c = *pat++;
switch (c & M_MASK) {
case M_ALL:
if (pat == patend)
return(1);
do
if (match(name, pat, patend))
return(1);
while (*name++ != EOS);
return(0);
case M_ONE:
if (*name++ == EOS)
return(0);
break;
case M_SET:
ok = 0;
if ((k = *name++) == EOS)
return(0);
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
++pat;
while (((c = *pat++) & M_MASK) != M_END)
if ((*pat & M_MASK) == M_RNG) {
if (c <= k && k <= pat[1])
ok = 1;
pat += 2;
} else if (c == k)
ok = 1;
if (ok == negate_range)
return(0);
break;
default:
if (*name++ != c)
return(0);
break;
}
}
return(*name == EOS);
}
/* Free allocated data belonging to a glob_t structure. */
void
globfree(glob_t *pglob)
{
int i;
char **pp;
if (pglob->gl_pathv != NULL) {
pp = pglob->gl_pathv + pglob->gl_offs;
for (i = pglob->gl_pathc; i--; ++pp)
if (*pp)
free(*pp);
free(pglob->gl_pathv);
pglob->gl_pathv = NULL;
pglob->gl_pathc = 0;
}
}
static DIR *
g_opendir(Char *str, glob_t *pglob)
{
char buf[MAXPATHLEN];
if (!*str)
(void)strcpy(buf, ".");
else {
if (g_Ctoc(str, buf, sizeof(buf)))
return NULL;
}
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return((*pglob->gl_opendir)(buf));
return(opendir(buf));
}
static int
g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
{
char buf[MAXPATHLEN];
if (g_Ctoc(fn, buf, sizeof(buf)))
return -1;
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return((*pglob->gl_lstat)(buf, sb));
return(lstat(buf, sb));
}
static int
g_stat(Char *fn, struct stat *sb, glob_t *pglob)
{
char buf[MAXPATHLEN];
if (g_Ctoc(fn, buf, sizeof(buf)))
return -1;
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return((*pglob->gl_stat)(buf, sb));
return(stat(buf, sb));
}
static Char *
g_strchr(const Char *str, int ch)
{
do {
if (*str == ch)
/* LINTED this is libc's definition! */
return (Char *)str;
} while (*str++);
return (NULL);
}
static int
g_Ctoc(const Char *str, char *buf, size_t len)
{
char *dc;
if (len == 0)
return 1;
for (dc = buf; len && (*dc++ = *str++) != EOS; len--)
continue;
return len == 0;
}
#ifdef DEBUG
static void
qprintf(const char *str, Char *s)
{
Char *p;
(void)printf("%s:\n", str);
for (p = s; *p; p++)
(void)printf("%c", CHAR(*p));
(void)printf("\n");
for (p = s; *p; p++)
(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
(void)printf("\n");
for (p = s; *p; p++)
(void)printf("%c", ismeta(*p) ? '_' : ' ');
(void)printf("\n");
}
#endif

View file

@ -0,0 +1,192 @@
/* $Id: inet_ntop.c,v 1.1.1.1 2003/03/31 05:02:48 grant Exp $ */
/* $NetBSD: inet_ntop.c,v 1.1.1.1 2003/03/31 05:02:48 grant Exp $ */
/* Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include "nbcompat.h"
#if HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifndef IN6ADDRSZ
#define IN6ADDRSZ 16
#endif
#ifndef INT16SZ
#define INT16SZ 2
#endif
#ifdef SPRINTF_CHAR
# define SPRINTF(x) strlen(sprintf/**/x)
#else
# define SPRINTF(x) ((size_t)sprintf x)
#endif
/*
* WARNING: Don't even consider trying to compile this on a system where
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
*/
static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
/* char *
* inet_ntop(af, src, dst, size)
* convert a network format address to presentation format.
* return:
* pointer to presentation format address (`dst'), or NULL (see errno).
* author:
* Paul Vixie, 1996.
*/
const char *
inet_ntop(int af, const void *src, char *dst, size_t size)
{
switch (af) {
case AF_INET:
return (inet_ntop4(src, dst, size));
#ifdef INET6
case AF_INET6:
return (inet_ntop6(src, dst, size));
#endif
default:
errno = EAFNOSUPPORT;
return (NULL);
}
/* NOTREACHED */
}
/* const char *
* inet_ntop4(src, dst, size)
* format an IPv4 address, more or less like inet_ntoa()
* return:
* `dst' (as a const)
* notes:
* (1) uses no statics
* (2) takes a u_char* not an in_addr as input
* author:
* Paul Vixie, 1996.
*/
static const char *
inet_ntop4(const u_char *src, char *dst, size_t size)
{
static const char fmt[] = "%u.%u.%u.%u";
char tmp[sizeof "255.255.255.255"];
if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
errno = ENOSPC;
return (NULL);
}
strcpy(dst, tmp);
return (dst);
}
#ifdef INET6
/* const char *
* inet_ntop6(src, dst, size)
* convert IPv6 binary address into presentation (printable) format
* author:
* Paul Vixie, 1996.
*/
static const char *
inet_ntop6(const u_char *src, char *dst, size_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* to contain a value of the specified size. On some systems, like
* Crays, there is no such thing as an integer variable with 16 bits.
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
struct { int base, len; } best, cur;
u_int words[IN6ADDRSZ / INT16SZ];
int i;
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
memset(words, '\0', sizeof words);
for (i = 0; i < IN6ADDRSZ; i++)
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
best.base = -1;
cur.base = -1;
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
else
cur.len++;
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
}
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
}
if (best.base != -1 && best.len < 2)
best.base = -1;
/*
* Format the result.
*/
tp = tmp;
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
if (i == best.base)
*tp++ = ':';
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0)
*tp++ = ':';
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
return (NULL);
tp += strlen(tp);
break;
}
tp += SPRINTF((tp, "%x", words[i]));
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
*tp++ = ':';
*tp++ = '\0';
/*
* Check for overflow, copy, and we're done.
*/
if ((size_t)(tp - tmp) > size) {
errno = ENOSPC;
return (NULL);
}
strcpy(dst, tmp);
return (dst);
}
#endif

View file

@ -0,0 +1,286 @@
/* $Id: inet_pton.c,v 1.1.1.1 2003/03/31 05:02:49 grant Exp $ */
/* $NetBSD: inet_pton.c,v 1.1.1.1 2003/03/31 05:02:49 grant Exp $ */
/* Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include "nbcompat.h"
#if HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifndef INADDRSZ
#define INADDRSZ 4
#endif
/*
* WARNING: Don't even consider trying to compile this on a system where
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
*/
static int inet_pton4(const char *src, u_char *dst, int pton);
#ifdef INET6
static int inet_pton6(const char *src, u_char *dst);
#endif
/* int
* inet_pton(af, src, dst)
* convert from presentation format (which usually means ASCII printable)
* to network format (which is usually some kind of binary format).
* return:
* 1 if the address was valid for the specified address family
* 0 if the address wasn't valid (`dst' is untouched in this case)
* -1 if some other error occurred (`dst' is untouched in this case, too)
* author:
* Paul Vixie, 1996.
*/
int
inet_pton(int af, const char *src, void *dst)
{
switch (af) {
case AF_INET:
return (inet_pton4(src, dst, 1));
#ifdef INET6
case AF_INET6:
return (inet_pton6(src, dst));
#endif
default:
errno = EAFNOSUPPORT;
return (-1);
}
/* NOTREACHED */
}
/* int
* inet_pton4(src, dst, pton)
* when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand.
* when last arg is 1: inet_pton(). decimal dotted-quad only.
* return:
* 1 if `src' is a valid input, else 0.
* notice:
* does not touch `dst' unless it's returning 1.
* author:
* Paul Vixie, 1996.
*/
static int
inet_pton4(const char *src, u_char *dst, int pton)
{
u_int val;
u_int digit;
int base, n;
unsigned char c;
u_int parts[4];
register u_int *pp = parts;
c = *src;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, isdigit=decimal.
*/
if (!isdigit(c))
return (0);
val = 0; base = 10;
if (c == '0') {
c = *++src;
if (c == 'x' || c == 'X')
base = 16, c = *++src;
else if (isdigit(c) && c != '9')
base = 8;
}
/* inet_pton() takes decimal only */
if (pton && base != 10)
return (0);
for (;;) {
if (isdigit(c)) {
digit = c - '0';
if (digit >= base)
break;
val = (val * base) + digit;
c = *++src;
} else if (base == 16 && isxdigit(c)) {
digit = c + 10 - (islower(c) ? 'a' : 'A');
if (digit >= 16)
break;
val = (val << 4) | digit;
c = *++src;
} else
break;
}
if (c == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
* a (with a treated as 32 bits)
*/
if (pp >= parts + 3)
return (0);
*pp++ = val;
c = *++src;
} else
break;
}
/*
* Check for trailing characters.
*/
if (c != '\0' && !isspace(c))
return (0);
/*
* Concoct the address according to
* the number of parts specified.
*/
n = pp - parts + 1;
/* inet_pton() takes dotted-quad only. it does not take shorthand. */
if (pton && n != 4)
return (0);
switch (n) {
case 0:
return (0); /* initial nondigit */
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (parts[0] > 0xff || val > 0xffffff)
return (0);
val |= parts[0] << 24;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if ((parts[0] | parts[1]) > 0xff || val > 0xffff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
if (dst) {
val = htonl(val);
memcpy(dst, &val, INADDRSZ);
}
return (1);
}
#ifdef INET6
/* int
* inet_pton6(src, dst)
* convert presentation level address to network order binary form.
* return:
* 1 if `src' is a valid [RFC1884 2.2] address, else 0.
* notice:
* (1) does not touch `dst' unless it's returning 1.
* (2) :: in a full address is silently ignored.
* credit:
* inspired by Mark Andrews.
* author:
* Paul Vixie, 1996.
*/
static int
inet_pton6(const char *src, u_char *dst)
{
static const char xdigits_l[] = "0123456789abcdef",
xdigits_u[] = "0123456789ABCDEF";
u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
const char *xdigits, *curtok;
int ch, saw_xdigit;
u_int val;
memset((tp = tmp), '\0', IN6ADDRSZ);
endp = tp + IN6ADDRSZ;
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
if (*++src != ':')
return (0);
curtok = src;
saw_xdigit = 0;
val = 0;
while ((ch = *src++) != '\0') {
const char *pch;
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
pch = strchr((xdigits = xdigits_u), ch);
if (pch != NULL) {
val <<= 4;
val |= (pch - xdigits);
if (val > 0xffff)
return (0);
saw_xdigit = 1;
continue;
}
if (ch == ':') {
curtok = src;
if (!saw_xdigit) {
if (colonp)
return (0);
colonp = tp;
continue;
} else if (*src == '\0')
return (0);
if (tp + INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
saw_xdigit = 0;
val = 0;
continue;
}
if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
inet_pton4(curtok, tp, 1) > 0) {
tp += INADDRSZ;
saw_xdigit = 0;
break; /* '\0' was seen by inet_pton4(). */
}
return (0);
}
if (saw_xdigit) {
if (tp + INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
}
if (colonp != NULL) {
/*
* Since some memmove()'s erroneously fail to handle
* overlapping regions, we'll do the shift by hand.
*/
const int n = tp - colonp;
int i;
if (tp == endp)
return (0);
for (i = 1; i <= n; i++) {
endp[- i] = colonp[n - i];
colonp[n - i] = 0;
}
tp = endp;
}
if (tp != endp)
return (0);
memcpy(dst, tmp, IN6ADDRSZ);
return (1);
}
#endif

View file

@ -0,0 +1,251 @@
#!/bin/sh
#
# install - install a program, script, or datafile
# This comes from X11R5 (mit/util/scripts/install.sh).
#
# Copyright 1991 by the Massachusetts Institute of Technology
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of M.I.T. not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission. M.I.T. makes no representations about the
# suitability of this software for any purpose. It is provided "as is"
# without express or implied warranty.
#
# Calling this script install-sh is preferred over install.sh, to prevent
# `make' implicit rules from creating a file called install from it
# when there is no Makefile.
#
# This script is compatible with the BSD install script, but was written
# from scratch. It can only install one file at a time, a restriction
# shared with many OS's install programs.
# set DOITPROG to echo to test this script
# Don't use :- since 4.3BSD and earlier shells don't like it.
doit="${DOITPROG-}"
# put in absolute paths if you don't have them in your path; or use env. vars.
mvprog="${MVPROG-mv}"
cpprog="${CPPROG-cp}"
chmodprog="${CHMODPROG-chmod}"
chownprog="${CHOWNPROG-chown}"
chgrpprog="${CHGRPPROG-chgrp}"
stripprog="${STRIPPROG-strip}"
rmprog="${RMPROG-rm}"
mkdirprog="${MKDIRPROG-mkdir}"
transformbasename=""
transform_arg=""
instcmd="$mvprog"
chmodcmd="$chmodprog 0755"
chowncmd=""
chgrpcmd=""
stripcmd=""
rmcmd="$rmprog -f"
mvcmd="$mvprog"
src=""
dst=""
dir_arg=""
while [ x"$1" != x ]; do
case $1 in
-c) instcmd="$cpprog"
shift
continue;;
-d) dir_arg=true
shift
continue;;
-m) chmodcmd="$chmodprog $2"
shift
shift
continue;;
-o) chowncmd="$chownprog $2"
shift
shift
continue;;
-g) chgrpcmd="$chgrpprog $2"
shift
shift
continue;;
-s) stripcmd="$stripprog"
shift
continue;;
-t=*) transformarg=`echo $1 | sed 's/-t=//'`
shift
continue;;
-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
shift
continue;;
*) if [ x"$src" = x ]
then
src=$1
else
# this colon is to work around a 386BSD /bin/sh bug
:
dst=$1
fi
shift
continue;;
esac
done
if [ x"$src" = x ]
then
echo "install: no input file specified"
exit 1
else
true
fi
if [ x"$dir_arg" != x ]; then
dst=$src
src=""
if [ -d $dst ]; then
instcmd=:
chmodcmd=""
else
instcmd=mkdir
fi
else
# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
# might cause directories to be created, which would be especially bad
# if $src (and thus $dsttmp) contains '*'.
if [ -f $src -o -d $src ]
then
true
else
echo "install: $src does not exist"
exit 1
fi
if [ x"$dst" = x ]
then
echo "install: no destination specified"
exit 1
else
true
fi
# If destination is a directory, append the input filename; if your system
# does not like double slashes in filenames, you may need to add some logic
if [ -d $dst ]
then
dst="$dst"/`basename $src`
else
true
fi
fi
## this sed command emulates the dirname command
dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
# Make sure that the destination directory exists.
# this part is taken from Noah Friedman's mkinstalldirs script
# Skip lots of stat calls in the usual case.
if [ ! -d "$dstdir" ]; then
defaultIFS='
'
IFS="${IFS-${defaultIFS}}"
oIFS="${IFS}"
# Some sh's can't handle IFS=/ for some reason.
IFS='%'
set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
IFS="${oIFS}"
pathcomp=''
while [ $# -ne 0 ] ; do
pathcomp="${pathcomp}${1}"
shift
if [ ! -d "${pathcomp}" ] ;
then
$mkdirprog "${pathcomp}"
else
true
fi
pathcomp="${pathcomp}/"
done
fi
if [ x"$dir_arg" != x ]
then
$doit $instcmd $dst &&
if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
else
# If we're going to rename the final executable, determine the name now.
if [ x"$transformarg" = x ]
then
dstfile=`basename $dst`
else
dstfile=`basename $dst $transformbasename |
sed $transformarg`$transformbasename
fi
# don't allow the sed command to completely eliminate the filename
if [ x"$dstfile" = x ]
then
dstfile=`basename $dst`
else
true
fi
# Make a temp file name in the proper directory.
dsttmp=$dstdir/#inst.$$#
# Move or copy the file name to the temp name
$doit $instcmd $src $dsttmp &&
trap "rm -f ${dsttmp}" 0 &&
# and set any options; do chmod last to preserve setuid bits
# If any of these fail, we abort the whole thing. If we want to
# ignore errors from any of these, just make sure not to ignore
# errors from the above "$doit $instcmd $src $dsttmp" command.
if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
# Now rename the file to the real destination.
$doit $rmcmd -f $dstdir/$dstfile &&
$doit $mvcmd $dsttmp $dstdir/$dstfile
fi &&
exit 0

View file

@ -0,0 +1,58 @@
/* $NetBSD: lchflags.c,v 1.1.1.1 2003/03/31 05:02:49 grant Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
/* Emulate lchflags(2), checking path with lstat(2) first to ensure that
* it's not a symlink, and then call chflags(2) */
#include "nbcompat.h"
#include <sys/stat.h>
#include <unistd.h>
int
lchflags(const char *path, u_long flags)
{
struct stat psb;
if (lstat(path, &psb) == -1)
return -1;
if (S_ISLNK(psb.st_mode)) {
return 0;
}
return (chflags(path, flags));
}

View file

@ -0,0 +1,58 @@
/* $NetBSD: lchmod.c,v 1.1.1.1 2003/03/31 05:02:51 grant Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
/* Emulate lchmod(2), checking path with lstat(2) first to ensure that
* it's not a symlink, and then call chmod(2) */
#include "nbcompat.h"
#include <sys/stat.h>
#include <unistd.h>
int
lchmod(const char *path, mode_t mode)
{
struct stat psb;
if (lstat(path, &psb) == -1)
return -1;
if (S_ISLNK(psb.st_mode)) {
return 0;
}
return (chmod(path, mode));
}

View file

@ -0,0 +1,56 @@
/* $NetBSD: lchown.c,v 1.1.1.1 2003/03/31 05:02:52 grant Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
/* Emulate lchown(2), checking path with lstat(2) first to ensure that
* it's not a symlink, and then call chown(2) */
#include <sys/stat.h>
#include <unistd.h>
int
lchown(const char *path, uid_t owner, gid_t group)
{
struct stat psb;
if (lstat(path, &psb) == -1)
return -1;
if (S_ISLNK(psb.st_mode)) {
return 0;
}
return (chown(path, owner, group));
}

View file

@ -0,0 +1,58 @@
/* $NetBSD: lutimes.c,v 1.1.1.1 2003/03/31 05:02:54 grant Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Grant Beattie.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
/* Emulate lutimes(2), checking path with lstat(2) first to ensure that
* it's not a symlink, and then call chmod(2) */
#include "nbcompat.h"
#include <sys/stat.h>
#include <unistd.h>
int
lutimes(const char *path, const struct timeval *times)
{
struct stat psb;
if (lstat(path, &psb) == -1)
return -1;
if (S_ISLNK(psb.st_mode)) {
return 0;
}
return (utimes(path, times));
}

View file

@ -0,0 +1,15 @@
/* $NetBSD: malloc.c,v 1.1.1.1 2003/03/31 05:02:54 grant Exp $ */
#if ! HAVE_MALLOC
#undef malloc
#include <sys/types.h>
void *malloc();
void *rpl_malloc (size_t n)
{
if (n == 0) n = 1;
return malloc(n);
}
#endif

View file

@ -0,0 +1,53 @@
/* $NetBSD: md5.h,v 1.1.1.1 2003/03/31 05:02:55 grant Exp $ */
/*
* This file is derived from the RSA Data Security, Inc. MD5 Message-Digest
* Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.ORG>
* for portability and formatting.
*/
/*
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
* rights reserved.
*
* License to copy and use this software is granted provided that it
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
* Algorithm" in all material mentioning or referencing this software
* or this function.
*
* License is also granted to make and use derivative works provided
* that such works are identified as "derived from the RSA Data
* Security, Inc. MD5 Message-Digest Algorithm" in all material
* mentioning or referencing the derived work.
*
* RSA Data Security, Inc. makes no representations concerning either
* the merchantability of this software or the suitability of this
* software for any particular purpose. It is provided "as is"
* without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this
* documentation and/or software.
*/
#ifndef _SYS_MD5_H_
#define _SYS_MD5_H_
/* MD5 context. */
typedef struct MD5Context {
u_int32_t state[4]; /* state (ABCD) */
u_int32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
__BEGIN_DECLS
void MD5Init __P((MD5_CTX *));
void MD5Update __P((MD5_CTX *, const unsigned char *, unsigned int));
void MD5Final __P((unsigned char[16], MD5_CTX *));
#ifndef _KERNEL
char *MD5End __P((MD5_CTX *, char *));
char *MD5File __P((const char *, char *));
char *MD5Data __P((const unsigned char *, unsigned int, char *));
#endif /* _KERNEL */
__END_DECLS
#endif /* _SYS_MD5_H_ */

View file

@ -0,0 +1,375 @@
/* $NetBSD: md5c.c,v 1.1.1.1 2003/03/31 05:02:45 grant Exp $ */
/*
* This file is derived from the RSA Data Security, Inc. MD5 Message-Digest
* Algorithm and has been modifed by Jason R. Thorpe <thorpej@NetBSD.ORG>
* for portability and formatting.
*/
/*
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
* rights reserved.
*
* License to copy and use this software is granted provided that it
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
* Algorithm" in all material mentioning or referencing this software
* or this function.
*
* License is also granted to make and use derivative works provided
* that such works are identified as "derived from the RSA Data
* Security, Inc. MD5 Message-Digest Algorithm" in all material
* mentioning or referencing the derived work.
*
* RSA Data Security, Inc. makes no representations concerning either
* the merchantability of this software or the suitability of this
* software for any particular purpose. It is provided "as is"
* without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this
* documentation and/or software.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#if defined(_KERNEL) || defined(_STANDALONE)
#include <lib/libkern/libkern.h>
#include <sys/param.h>
#include <sys/md5.h>
#define _DIAGASSERT(x) (void)0
#else
/* #include "namespace.h" */
#include <assert.h>
#include <string.h>
#include <md5.h>
#endif /* _KERNEL || _STANDALONE */
#if defined(HAVE_MEMSET)
#define ZEROIZE(d, l) memset((d), 0, (l))
#else
# if defined(HAVE_BZERO)
#define ZEROIZE(d, l) bzero((d), (l))
# else
#error You need either memset or bzero
# endif
#endif
typedef unsigned char *POINTER;
typedef u_int16_t UINT2;
typedef u_int32_t UINT4;
/*
* Constants for MD5Transform routine.
*/
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
#if 0
#if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias)
__weak_alias(MD5Init,_MD5Init)
__weak_alias(MD5Update,_MD5Update)
__weak_alias(MD5Final,_MD5Final)
#endif
#endif
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
static void MD5Transform __P((UINT4 [4], const unsigned char [64]));
static void Encode __P((unsigned char *, UINT4 *, unsigned int));
static void Decode __P((UINT4 *, const unsigned char *, unsigned int));
/*
* Encodes input (UINT4) into output (unsigned char). Assumes len is
* a multiple of 4.
*/
static void
Encode (output, input, len)
unsigned char *output;
UINT4 *input;
unsigned int len;
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
/*
* Decodes input (unsigned char) into output (UINT4). Assumes len is
* a multiple of 4.
*/
static void
Decode (output, input, len)
UINT4 *output;
const unsigned char *input;
unsigned int len;
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
}
static const unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/*
* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/*
* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/*
* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/*
* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
void
MD5Init(context)
MD5_CTX *context; /* context */
{
_DIAGASSERT(context != 0);
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants. */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
/*
* MD5 block update operation. Continues an MD5 message-digest
* operation, processing another message block, and updating the
* context.
*/
void
MD5Update(context, input, inputLen)
MD5_CTX *context; /* context */
const unsigned char *input; /* input block */
unsigned int inputLen; /* length of input block */
{
unsigned int i, idx, partLen;
_DIAGASSERT(context != 0);
_DIAGASSERT(input != 0);
/* Compute number of bytes mod 64 */
idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context->count[0] += ((UINT4)inputLen << 3))
< ((UINT4)inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT4)inputLen >> 29);
partLen = 64 - idx;
/* Transform as many times as possible. */
if (inputLen >= partLen) {
/* LINTED const castaway ok */
memcpy((POINTER)&context->buffer[idx],
input, partLen);
MD5Transform(context->state, context->buffer);
for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform(context->state, &input[i]);
idx = 0;
} else
i = 0;
/* Buffer remaining input */
/* LINTED const castaway ok */
memcpy((POINTER)&context->buffer[idx], &input[i],
inputLen - i);
}
/*
* MD5 finalization. Ends an MD5 message-digest operation, writing the
* message digest and zeroing the context.
*/
void
MD5Final(digest, context)
unsigned char digest[16]; /* message digest */
MD5_CTX *context; /* context */
{
unsigned char bits[8];
unsigned int idx, padLen;
_DIAGASSERT(digest != 0);
_DIAGASSERT(context != 0);
/* Save number of bits */
Encode(bits, context->count, 8);
/* Pad out to 56 mod 64. */
idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
padLen = (idx < 56) ? (56 - idx) : (120 - idx);
MD5Update (context, PADDING, padLen);
/* Append length (before padding) */
MD5Update(context, bits, 8);
/* Store state in digest */
Encode(digest, context->state, 16);
/* Zeroize sensitive information. */
ZEROIZE((POINTER)(void *)context, sizeof(*context));
}
/*
* MD5 basic transformation. Transforms state based on block.
*/
static void
MD5Transform(state, block)
UINT4 state[4];
const unsigned char block[64];
{
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode(x, block, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information. */
ZEROIZE((POINTER)(void *)x, sizeof (x));
}

View file

@ -0,0 +1,126 @@
/* $NetBSD: md5hl.c,v 1.1.1.1 2003/03/31 05:02:45 grant Exp $ */
/*
* Written by Jason R. Thorpe <thorpej@netbsd.org>, April 29, 1997.
* Public domain.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* #include "namespace.h" */
#include <nbtypes.h>
#define MDALGORITHM MD5
#include <md5.h>
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
/* $NetBSD: md5hl.c,v 1.1.1.1 2003/03/31 05:02:45 grant Exp $ */
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp
*/
/*
* Modifed April 29, 1997 by Jason R. Thorpe <thorpej@netbsd.org>
*/
#include <assert.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define CONCAT(x,y) __CONCAT(x,y)
#define MDNAME(x) CONCAT(MDALGORITHM,x)
char *
MDNAME(End)(ctx, buf)
MDNAME(_CTX) *ctx;
char *buf;
{
int i;
unsigned char digest[16];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != 0);
if (buf == NULL)
buf = malloc(33);
if (buf == NULL)
return (NULL);
MDNAME(Final)(digest, ctx);
for (i = 0; i < 16; i++) {
buf[i+i] = hex[(u_int32_t)digest[i] >> 4];
buf[i+i+1] = hex[digest[i] & 0x0f];
}
buf[i+i] = '\0';
return (buf);
}
char *
MDNAME(File)(filename, buf)
const char *filename;
char *buf;
{
unsigned char buffer[BUFSIZ];
MDNAME(_CTX) ctx;
int f, i, j;
_DIAGASSERT(filename != 0);
/* buf may be NULL */
MDNAME(Init)(&ctx);
f = open(filename, O_RDONLY, 0666);
if (f < 0)
return NULL;
while ((i = read(f, buffer, sizeof(buffer))) > 0)
MDNAME(Update)(&ctx, buffer, (unsigned int)i);
j = errno;
close(f);
errno = j;
if (i < 0)
return NULL;
return (MDNAME(End)(&ctx, buf));
}
char *
MDNAME(Data)(data, len, buf)
const unsigned char *data;
unsigned int len;
char *buf;
{
MDNAME(_CTX) ctx;
_DIAGASSERT(data != 0);
MDNAME(Init)(&ctx);
MDNAME(Update)(&ctx, data, len);
return (MDNAME(End)(&ctx, buf));
}

View file

@ -0,0 +1,296 @@
/* $NetBSD: misc.c,v 1.1.1.1 2003/03/31 05:02:55 grant Exp $ */
/*-
* Copyright (c) 1991, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)misc.c 8.1 (Berkeley) 6/6/93
*/
#include "nbcompat.h"
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: misc.c,v 1.1.1.1 2003/03/31 05:02:55 grant Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "extern.h"
typedef struct _key {
const char *name; /* key name */
u_int val; /* value */
#define NEEDVALUE 0x01
u_int flags;
} KEY;
/* NB: the following tables must be sorted lexically. */
static KEY keylist[] = {
{"cksum", F_CKSUM, NEEDVALUE},
{"device", F_DEV, NEEDVALUE},
{"flags", F_FLAGS, NEEDVALUE},
{"gid", F_GID, NEEDVALUE},
{"gname", F_GNAME, NEEDVALUE},
{"ignore", F_IGN, 0},
{"link", F_SLINK, NEEDVALUE},
{"md5", F_MD5, NEEDVALUE},
{"md5digest", F_MD5, NEEDVALUE},
{"mode", F_MODE, NEEDVALUE},
{"nlink", F_NLINK, NEEDVALUE},
{"optional", F_OPT, 0},
{"rmd160", F_RMD160, NEEDVALUE},
{"rmd160digest",F_RMD160, NEEDVALUE},
{"sha1", F_SHA1, NEEDVALUE},
{"sha1digest", F_SHA1, NEEDVALUE},
{"size", F_SIZE, NEEDVALUE},
{"tags", F_TAGS, NEEDVALUE},
{"time", F_TIME, NEEDVALUE},
{"type", F_TYPE, NEEDVALUE},
{"uid", F_UID, NEEDVALUE},
{"uname", F_UNAME, NEEDVALUE}
};
static KEY typelist[] = {
{"block", F_BLOCK, },
{"char", F_CHAR, },
{"dir", F_DIR, },
{"fifo", F_FIFO, },
{"file", F_FILE, },
{"link", F_LINK, },
{"socket", F_SOCK, },
};
slist_t excludetags, includetags;
int keys = KEYDEFAULT;
int keycompare(const void *, const void *);
u_int
parsekey(const char *name, int *needvaluep)
{
static int allbits;
KEY *k, tmp;
if (allbits == 0) {
int i;
for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
allbits |= keylist[i].val;
}
tmp.name = name;
if (strcmp(name, "all") == 0)
return (allbits);
k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
sizeof(KEY), keycompare);
if (k == NULL)
mtree_err("unknown keyword `%s'", name);
if (needvaluep)
*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
return (k->val);
}
u_int
parsetype(const char *name)
{
KEY *k, tmp;
tmp.name = name;
k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
sizeof(KEY), keycompare);
if (k == NULL)
mtree_err("unknown file type `%s'", name);
return (k->val);
}
int
keycompare(const void *a, const void *b)
{
return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
}
void
mtree_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (mtree_lineno)
warnx("failed at line %lu of the specification",
(u_long) mtree_lineno);
exit(1);
/* NOTREACHED */
}
void
addtag(slist_t *list, char *elem)
{
#define TAG_CHUNK 20
if ((list->count % TAG_CHUNK) == 0) {
char **new;
new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
* sizeof(char *));
if (new == NULL)
mtree_err("memory allocation error");
list->list = new;
}
list->list[list->count] = elem;
list->count++;
}
void
parsetags(slist_t *list, char *args)
{
char *p, *e;
int len;
if (args == NULL) {
addtag(list, NULL);
return;
}
while ((p = strsep(&args, ",")) != NULL) {
if (*p == '\0')
continue;
len = strlen(p) + 3; /* "," + p + ",\0" */
if ((e = malloc(len)) == NULL)
mtree_err("memory allocation error");
snprintf(e, len, ",%s,", p);
addtag(list, e);
}
}
/*
* matchtags
* returns 0 if there's a match from the exclude list in the node's tags,
* or there's an include list and no match.
* return 1 otherwise.
*/
int
matchtags(NODE *node)
{
int i;
if (node->tags) {
for (i = 0; i < excludetags.count; i++)
if (strstr(node->tags, excludetags.list[i]))
break;
if (i < excludetags.count)
return (0);
for (i = 0; i < includetags.count; i++)
if (strstr(node->tags, includetags.list[i]))
break;
if (i > 0 && i == includetags.count)
return (0);
} else if (includetags.count > 0) {
return (0);
}
return (1);
}
u_int
nodetoino(u_int type)
{
switch (type) {
case F_BLOCK:
return S_IFBLK;
case F_CHAR:
return S_IFCHR;
case F_DIR:
return S_IFDIR;
case F_FIFO:
return S_IFIFO;
case F_FILE:
return S_IFREG;
case F_LINK:
return S_IFLNK;
case F_SOCK:
return S_IFSOCK;
default:
printf("unknown type %d", type);
abort();
}
/* NOTREACHED */
}
const char *
nodetype(u_int type)
{
return (inotype(nodetoino(type)));
}
const char *
inotype(u_int type)
{
switch (type & S_IFMT) {
case S_IFBLK:
return ("block");
case S_IFCHR:
return ("char");
case S_IFDIR:
return ("dir");
case S_IFIFO:
return ("fifo");
case S_IFREG:
return ("file");
case S_IFLNK:
return ("link");
case S_IFSOCK:
return ("socket");
default:
return ("unknown");
}
/* NOTREACHED */
}

View file

@ -0,0 +1,127 @@
/* $Id: mkstemp.c,v 1.1.1.1 2003/03/31 05:02:56 grant Exp $ */
/* $NetBSD: mkstemp.c,v 1.1.1.1 2003/03/31 05:02:56 grant 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#include "nbcompat.h"
int
mkstemp(char *path)
{
char *start, *trv;
struct stat sbuf;
u_int pid;
int fd;
/* 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. */
static char xtra[2] = "aa";
int xcnt = 0;
pid = getpid();
/* Move to end of path and count trailing X's. */
for (trv = path; *trv; ++trv)
if (*trv == 'X')
xcnt++;
else
xcnt = 0;
/* Use at least one from xtra. Use 2 if more than 6 X's. */
if (*(trv-1) == 'X')
*--trv = xtra[0];
if (xcnt > 6 && *(trv-1) == 'X')
*--trv = xtra[1];
/* Set remaining X's to pid digits with 0's to the left. */
while (*--trv == 'X') {
*trv = (pid % 10) + '0';
pid /= 10;
}
/* update xtra for next call. */
if (xtra[0] != 'z')
xtra[0]++;
else {
xtra[0] = 'a';
if (xtra[1] != 'z')
xtra[1]++;
else
xtra[1] = 'a';
}
/*
* check the target directory; if you have six X's and it
* doesn't exist this runs for a *very* long time.
*/
for (start = trv + 1;; --trv) {
if (trv <= path)
break;
if (*trv == '/') {
*trv = '\0';
if (stat(path, &sbuf))
return (-1);
if (!S_ISDIR(sbuf.st_mode)) {
errno = ENOTDIR;
return (-1);
}
*trv = '/';
break;
}
}
for (;;) {
if ((fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
return (fd);
if (errno != EEXIST)
return (-1);
/* tricky little algorithm for backward compatibility */
for (trv = start;;) {
if (!*trv)
return (-1);
if (*trv == 'z')
*trv++ = 'a';
else {
if (isdigit((unsigned char)*trv))
*trv = 'a';
else
++*trv;
break;
}
}
}
/*NOTREACHED*/
}

View file

@ -0,0 +1,136 @@
/* $NetBSD: mtree.h,v 1.1.1.1 2003/03/31 05:02:56 grant Exp $ */
/*-
* Copyright (c) 1990, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)mtree.h 8.1 (Berkeley) 6/6/93
*/
#ifndef _MTREE_H_
#define _MTREE_H_
#define KEYDEFAULT (F_GID | F_MODE | F_NLINK | F_SIZE | F_SLINK | \
F_TIME | F_TYPE | F_UID | F_FLAGS)
#define MISMATCHEXIT 2
typedef struct _node {
struct _node *parent, *child; /* up, down */
struct _node *prev, *next; /* left, right */
off_t st_size; /* size */
struct timespec st_mtimespec; /* last modification time */
char *slink; /* symbolic link reference */
uid_t st_uid; /* uid */
gid_t st_gid; /* gid */
#if !defined(S_ISTXT) && defined(S_ISVTX)
#define S_ISTXT S_ISVTX
#endif
#define MBITS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
mode_t st_mode; /* mode */
dev_t st_rdev; /* device type */
u_long st_flags; /* flags */
nlink_t st_nlink; /* link count */
u_long cksum; /* check sum */
char *md5digest; /* MD5 digest */
char *rmd160digest; /* RMD-160 digest */
char *sha1digest; /* SHA1 digest */
char *tags; /* tags, comma delimited */
size_t lineno; /* line # entry came from */
#define F_CKSUM 0x00000001 /* cksum(1) check sum */
#define F_DEV 0x00000002 /* device type */
#define F_DONE 0x00000004 /* directory done */
#define F_FLAGS 0x00000008 /* file flags */
#define F_GID 0x00000010 /* gid */
#define F_GNAME 0x00000020 /* group name */
#define F_IGN 0x00000040 /* ignore */
#define F_MAGIC 0x00000080 /* name has magic chars */
#define F_MD5 0x00000100 /* MD5 digest */
#define F_MODE 0x00000200 /* mode */
#define F_NLINK 0x00000400 /* number of links */
#define F_OPT 0x00000800 /* existence optional */
#define F_RMD160 0x00001000 /* RMD-160 digest */
#define F_SHA1 0x00002000 /* SHA1 digest */
#define F_SIZE 0x00004000 /* size */
#define F_SLINK 0x00008000 /* symbolic link */
#define F_TAGS 0x00010000 /* tags */
#define F_TIME 0x00020000 /* modification time */
#define F_TYPE 0x00040000 /* file type */
#define F_UID 0x00080000 /* uid */
#define F_UNAME 0x00100000 /* user name */
#define F_VISIT 0x00200000 /* file visited */
int flags; /* items set */
#define F_BLOCK 0x001 /* block special */
#define F_CHAR 0x002 /* char special */
#define F_DIR 0x004 /* directory */
#define F_FIFO 0x008 /* fifo */
#define F_FILE 0x010 /* regular file */
#define F_LINK 0x020 /* symbolic link */
#define F_SOCK 0x040 /* socket */
int type; /* file type */
char name[1]; /* file name (must be last) */
} NODE;
typedef struct {
char **list;
int count;
} slist_t;
/*
* prototypes for functions published to other programs which want to use
* the specfile parser but don't want to pull in all of "extern.h"
*/
const char *inotype(u_int);
u_int nodetoino(u_int);
int setup_getid(const char *);
NODE *spec(FILE *);
char *vispath(const char *);
#define RP(p) \
((p)->fts_path[0] == '.' && (p)->fts_path[1] == '/' ? \
(p)->fts_path + 2 : (p)->fts_path)
#define UF_MASK ((UF_NODUMP | UF_IMMUTABLE | \
UF_APPEND | UF_OPAQUE) \
& UF_SETTABLE) /* user settable flags */
#define SF_MASK ((SF_ARCHIVED | SF_IMMUTABLE | \
SF_APPEND) & SF_SETTABLE) /* root settable flags */
#define CH_MASK (UF_MASK | SF_MASK) /* all settable flags */
#define SP_FLGS (SF_IMMUTABLE | SF_APPEND) /* special flags */
#endif /* _MTREE_H_ */

View file

@ -0,0 +1,628 @@
/* $NetBSD: namespace.h,v 1.1.1.1 2003/03/31 05:02:52 grant Exp $ */
/*-
* Copyright (c) 1997-2002 The NetBSD Foundation, Inc.
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#ifndef _NAMESPACE_H_
#define _NAMESPACE_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#define brk _brk
#define catclose _catclose
#define catgets _catgets
#define catopen _catopen
#define daylight _daylight
#define err _err
#define errx _errx
#define fork _fork
#define fseeko _fseeko
#define ftello _ftello
#define inet_aton _inet_aton
#define inet_pton _inet_pton
#define pipe _pipe
#define sbrk _sbrk
#define strlcat _strlcat
#define strlcpy _strlcpy
#define strtoimax _strtoimax
#define strtoll _strtoll
#define strtoull _strtoull
#define strtoumax _strtoumax
#define sys_errlist _sys_errlist
#define sys_nerr _sys_nerr
#define sys_siglist _sys_siglist
#define sys_nsig _sys_nsig
#define sysconf __sysconf
#define verr _verr
#define verrx _verrx
#define vwarn _vwarn
#define vwarnx _vwarnx
#define warn _warn
#define warnx _warnx
#ifdef __weak_alias
#define MD2Final _MD2Final
#define MD2Init _MD2Init
#define MD2Update _MD2Update
#define MD4Final _MD4Final
#define MD4Init _MD4Init
#define MD4Update _MD4Update
#define MD5Final _MD5Final
#define MD5Init _MD5Init
#define MD5Update _MD5Update
#define RMD160Data _RMD160Data
#define RMD160End _RMD160End
#define RMD160File _RMD160File
#define RMD160Final _RMD160Final
#define RMD160Init _RMD160Init
#define RMD160Transform _RMD160Transform
#define RMD160Update _RMD160Update
#define SHA1Data _SHA1Data
#define SHA1End _SHA1End
#define SHA1File _SHA1File
#define SHA1Final _SHA1Final
#define SHA1Init _SHA1Init
#define SHA1Transform _SHA1Transform
#define SHA1Update _SHA1Update
#define a64l _a64l
#define adjtime _adjtime
#define alarm _alarm
#define alphasort _alphasort
#define asctime_r _asctime_r
#define asprintf _asprintf
#define atoll _atoll
#define authnone_create _authnone_create
#define authunix_create _authunix_create
#define authunix_create_default _authunix_create_default
#define basename _basename
#define bindresvport _bindresvport
#define bindresvport_sa _bindresvport_sa
#define bm_comp _bm_comp
#define bm_exec _bm_exec
#define bm_free _bm_free
#define callrpc _callrpc
#define cfgetispeed _cfgetispeed
#define cfgetospeed _cfgetospeed
#define cfmakeraw _cfmakeraw
#define cfsetispeed _cfsetispeed
#define cfsetospeed _cfsetospeed
#define cfsetspeed _cfsetspeed
#define cgetcap _cgetcap
#define cgetclose _cgetclose
#define cgetent _cgetent
#define cgetfirst _cgetfirst
#define cgetmatch _cgetmatch
#define cgetnext _cgetnext
#define cgetnum _cgetnum
#define cgetset _cgetset
#define cgetstr _cgetstr
#define cgetustr _cgetustr
#define clnt_broadcast _clnt_broadcast
#define clnt_create _clnt_create
#define clnt_create_vers _clnt_create_vers
#define clnt_dg_create _clnt_dg_create
#define clnt_pcreateerror _clnt_pcreateerror
#define clnt_perrno _clnt_perrno
#define clnt_perror _clnt_perror
#define clnt_raw_create _clnt_raw_create
#define clnt_tli_create _clnt_tli_create
#define clnt_tp_create _clnt_tp_create
#define clnt_spcreateerror _clnt_spcreateerror
#define clnt_sperrno _clnt_sperrno
#define clnt_sperror _clnt_sperror
#define clnt_vc_create _clnt_vc_create
#define clntraw_create _clntraw_create
#define clnttcp_create _clnttcp_create
#define clntudp_bufcreate _clntudp_bufcreate
#define clntudp_create _clntudp_create
#define clock_settime _clock_settime
#define closedir _closedir
#define closelog _closelog
#define confstr _confstr
#define ctermid _ctermid
#define ctime_r _ctime_r
#define daemon _daemon
#define dbm_clearerr _dbm_clearerr
#define dbm_close _dbm_close
#define dbm_delete _dbm_delete
#define dbm_dirfno _dbm_dirfno
#define dbm_error _dbm_error
#define dbm_fetch _dbm_fetch
#define dbm_firstkey _dbm_firstkey
#define dbm_nextkey _dbm_nextkey
#define dbm_open _dbm_open
#define dbm_store _dbm_store
#define dbopen _dbopen
#define devname _devname
#define dirname _dirname
#define dn_expand _dn_expand
#define drand48 _drand48
#define endfsent _endfsent
#define endgrent _endgrent
#define endhostent _endhostent
#define endnetconfig _endnetconfig
#define endnetent _endnetent
#define endnetgrent _endnetgrent
#define endprotoent _endprotoent
#define endpwent _endpwent
#define endrpcent _endrpcent
#define endservent _endservent
#define endttyent _endttyent
#define endusershell _endusershell
#define erand48 _erand48
#define ether_aton _ether_aton
#define ether_hostton _ether_hostton
#define ether_line _ether_line
#define ether_ntoa _ether_ntoa
#define ether_ntohost _ether_ntohost
#define execl _execl
#define execle _execle
#define execlp _execlp
#define execv _execv
#define execvp _execvp
#define fdopen _fdopen
#define fgetln _fgetln
#define fnmatch _fnmatch
#define fpgetmask _fpgetmask
#define fpgetround _fpgetround
#define fpgetsticky _fpgetsticky
#define fpsetmask _fpsetmask
#define fpsetround _fpsetround
#define fpsetsticky _fpsetsticky
#define freenetconfigent _freenetconfigent
#define freeaddrinfo _freeaddrinfo
#define freeifaddrs _freeifaddrs
#define ftok _ftok
#define ftruncate _ftruncate
#define fts_children _fts_children
#define fts_close _fts_close
#define fts_open _fts_open
#define fts_read _fts_read
#define fts_set _fts_set
#define gai_strerror _gai_strerror
#define get_myaddress _get_myaddress
#define getbsize _getbsize
#define getcwd _getcwd
#define getdiskbyname _getdiskbyname
#define getfsent _getfsent
#define getfsfile _getfsfile
#define getfsspec _getfsspec
#define getgrent _getgrent
#define getgrgid _getgrgid
#define getgrnam _getgrnam
#define getgrouplist _getgrouplist
#define getifaddrs _getifaddrs
#define getloadavg _getloadavg
#define getlogin _getlogin
#define getmntinfo _getmntinfo
#define getmode _getmode
#define getnameinfo _getnameinfo
#define getnetbyaddr _getnetbyaddr
#define getnetbyname _getnetbyname
#define getnetconfig _getnetconfig
#define getnetconfigent _getnetconfigent
#define getnetent _getnetent
#define getnetgrent _getnetgrent
#define getnetpath _getnetpath
#define getopt _getopt
#define getopt_long _getopt_long
#define getpagesize _getpagesize
#define getpass _getpass
#define getprogname _getprogname
#define getprotobyname _getprotobyname
#define getprotobynumber _getprotobynumber
#define getprotoent _getprotoent
#define getpwent _getpwent
#define getpwnam _getpwnam
#define getpwuid _getpwuid
#define getrpcbyname _getrpcbyname
#define getrpcbynumber _getrpcbynumber
#define getrpcent _getrpcent
#define getrpcport _getrpcport
#define getservbyname _getservbyname
#define getservbyport _getservbyport
#define getservent _getservent
#define getsubopt _getsubopt
#define getttyent _getttyent
#define getttynam _getttynam
#define getusershell _getusershell
#define glob _glob
#define globfree _globfree
#define gmtime_r _gmtime_r
#define group_from_gid _group_from_gid
#define heapsort _heapsort
#define herror _herror
#define hes_error _hes_error
#define hes_free _hes_free
#define hes_init _hes_init
#define hes_resolve _hes_resolve
#define hes_to_bind _hes_to_bind
#define hesiod_end _hesiod_end
#define hesiod_free_list _hesiod_free_list
#define hesiod_init _hesiod_init
#define hesiod_resolve _hesiod_resolve
#define hesiod_to_bind _hesiod_to_bind
#define if_freenameindex _if_freenameindex
#define if_indextoname _if_indextoname
#define if_nameindex _if_nameindex
#define if_nametoindex _if_nametoindex
#define in6addr_any _in6addr_any
#define in6addr_linklocal_allnodes _in6addr_linklocal_allnodes
#define in6addr_loopback _in6addr_loopback
#define in6addr_nodelocal_allnodes _in6addr_nodelocal_allnodes
#define inet6_option_alloc _inet6_option_alloc
#define inet6_option_append _inet6_option_append
#define inet6_option_find _inet6_option_find
#define inet6_option_init _inet6_option_init
#define inet6_option_next _inet6_option_next
#define inet6_option_space _inet6_option_space
#define inet6_rthdr_add _inet6_rthdr_add
#define inet6_rthdr_getaddr _inet6_rthdr_getaddr
#define inet6_rthdr_getflags _inet6_rthdr_getflags
#define inet6_rthdr_init _inet6_rthdr_init
#define inet6_rthdr_lasthop _inet6_rthdr_lasthop
#define inet6_rthdr_segments _inet6_rthdr_segments
#define inet6_rthdr_space _inet6_rthdr_space
#define inet_lnaof _inet_lnaof
#define inet_makeaddr _inet_makeaddr
#define inet_net_ntop _inet_net_ntop
#define inet_net_pton _inet_net_pton
#define inet_neta _inet_neta
#define inet_netof _inet_netof
#define inet_network _inet_network
#define inet_nsap_addr _inet_nsap_addr
#define inet_nsap_ntoa _inet_nsap_ntoa
#define inet_ntoa _inet_ntoa
#define inet_ntop _inet_ntop
#define initgroups _initgroups
#define initstate _initstate
#define innetgr _innetgr
#define isatty _isatty
#define isinf _isinf
#define isnan _isnan
#define jrand48 _jrand48
#define l64a _l64a
#define l64a_r _l64a_r
#define lcong48 _lcong48
#define llabs _llabs
#define lldiv _lldiv
#define localtime_r _localtime_r
#define lockf _lockf
#define lrand48 _lrand48
#define lseek _lseek
#define mergesort _mergesort
#define mmap _mmap
#define mpool_close _mpool_close
#define mpool_filter _mpool_filter
#define mpool_get _mpool_get
#define mpool_new _mpool_new
#define mpool_open _mpool_open
#define mpool_put _mpool_put
#define mpool_sync _mpool_sync
#define mrand48 _mrand48
#define nc_perror _nc_perror
#define nc_sperror _nc_sperror
#define nice _nice
#if 0
#define nlist _nlist
#endif
#define nrand48 _nrand48
#define ntp_adjtime _ntp_adjtime
#define nsdispatch _nsdispatch
#define offtime _offtime
#define opendir _opendir
#define openlog _openlog
#define pause _pause
#define pclose _pclose
#define pmap_getmaps _pmap_getmaps
#define pmap_getport _pmap_getport
#define pmap_rmtcall _pmap_rmtcall
#define pmap_set _pmap_set
#define pmap_unset _pmap_unset
#define popen _popen
#define posix2time _posix2time
#define pread _pread
#define psignal _psignal
#define putenv _putenv
#define pwcache_userdb _pwcache_userdb
#define pwcache_groupdb _pwcache_groupdb
#define pwrite _pwrite
#define qabs _qabs
#define qdiv _qdiv
#define radixsort _radixsort
#define random _random
#define readdir _readdir
#define realpath _realpath
#define regcomp _regcomp
#define regerror _regerror
#define regexec _regexec
#define regfree _regfree
#define registerrpc _registerrpc
#define res_init _res_init
#define res_mkquery _res_mkquery
#define res_query _res_query
#define res_search _res_search
#define rewinddir _rewinddir
#define rpc_broadcast _rpc_broadcast
#define rpc_broadcast_exp _rpc_broadcast_exp
#define rpc_call _rpc_call
#define rpc_reg _rpc_reg
#define rpcb_getmaps _rpcb_getmaps
#define rpcb_gettime _rpcb_gettime
#define rpcb_rmtcall _rpcb_rmtcall
#define rpcb_set _rpcb_set
#define rpcb_taddr2uaddr _rpcb_taddr2uaddr
#define rpcb_uaddr2taddr _rpcb_uaddr2taddr
#define rpcb_unset _rpcb_unset
#define scandir _scandir
#define seed48 _seed48
#define seekdir _seekdir
#define send _send
#define setdomainname _setdomainname
#define setenv _setenv
#define setfsent _setfsent
#define setgrent _setgrent
#define setgroupent _setgroupent
#define sethostent _sethostent
#define sethostname _sethostname
#define setlogmask _setlogmask
#define setmode _setmode
#define setnetconfig _setnetconfig
#define setnetent _setnetent
#define setnetgrent _setnetgrent
#define setpassent _setpassent
#define setnetpath _setnetpath
#define setproctitle _setproctitle
#define setprotoent _setprotoent
#define setpwent _setpwent
#define setrpcent _setrpcent
#define setservent _setservent
#define setstate _setstate
#define setttyent _setttyent
#define settimeofday _settimeofday
#define setusershell _setusershell
#define shm_open _shm_open
#define shm_unlink _shm_unlink
#define siginterrupt _siginterrupt
#define signal _signal
#define sl_add _sl_add
#define sl_find _sl_find
#define sl_free _sl_free
#define sl_init _sl_init
#define sleep _sleep
#define snprintf _snprintf
#define sradixsort _sradixsort
#define srand48 _srand48
#define srandom _srandom
#define strcasecmp _strcasecmp
#define strdup _strdup
#define strncasecmp _strncasecmp
#define strptime _strptime
#define strsep _strsep
#define strsignal _strsignal
#define strsvis _strsvis
#define strsvisx _strsvisx
#define strtok_r _strtok_r
#define strunvis _strunvis
#define strvis _strvis
#define strvisx _strvisx
#define svc_auth_reg _svc_auth_reg
#define svc_create _svc_create
#define svc_dg_create _svc_dg_create
#define svc_exit _svc_exit
#define svc_fd_create _svc_fd_create
#define svc_getreq _svc_getreq
#define svc_getreqset _svc_getreqset
#define svc_getreq_common _svc_getreq_common
#define svc_raw_create _svc_raw_create
#define svc_register _svc_register
#define svc_reg _svc_reg
#define svc_run _svc_run
#define svc_sendreply _svc_sendreply
#define svc_tli_create _svc_tli_create
#define svc_tp_create _svc_tp_create
#define svc_unregister _svc_unregister
#define svc_unreg _svc_unreg
#define svc_vc_create _svc_vc_create
#define svcerr_auth _svcerr_auth
#define svcerr_decode _svcerr_decode
#define svcerr_noproc _svcerr_noproc
#define svcerr_noprog _svcerr_noprog
#define svcerr_progvers _svcerr_progvers
#define svcerr_systemerr _svcerr_systemerr
#define svcerr_weakauth _svcerr_weakauth
#define svcfd_create _svcfd_create
#define svcraw_create _svcraw_create
#define svctcp_create _svctcp_create
#define svcudp_bufcreate _svcudp_bufcreate
#define svcudp_create _svcudp_create
#define svcudp_enablecache _svcudp_enablecache
#define svis _svis
#define sysarch _sysarch
#define sysctl _sysctl
#define syslog _syslog
#define tcdrain _tcdrain
#define tcflow _tcflow
#define tcflush _tcflush
#define tcgetattr _tcgetattr
#define tcgetpgrp _tcgetpgrp
#define tcgetsid _tcgetsid
#define tcsendbreak _tcsendbreak
#define tcsetattr _tcsetattr
#define tcsetpgrp _tcsetpgrp
#define telldir _telldir
#define time _time
#define time2posix _time2posix
#define timegm _timegm
#define timelocal _timelocal
#define timeoff _timeoff
#define times _times
#define ttyname _ttyname
#define ttyslot _ttyslot
#define tzname _tzname
#define tzset _tzset
#define tzsetwall _tzsetwall
#define ualarm _ualarm
#define uname _uname
#define unsetenv _unsetenv
#define unvis _unvis
#define user_from_uid _user_from_uid
#define usleep _usleep
#define utime _utime
#define valloc _valloc
#define vis _vis
#define vsnprintf _vsnprintf
#define vsyslog _vsyslog
#define wait _wait
#define wait3 _wait3
#define waitpid _waitpid
#define xdr_accepted_reply _xdr_accepted_reply
#define xdr_array _xdr_array
#define xdr_authunix_parms _xdr_authunix_parms
#define xdr_bool _xdr_bool
#define xdr_bytes _xdr_bytes
#define xdr_callhdr _xdr_callhdr
#define xdr_callmsg _xdr_callmsg
#define xdr_char _xdr_char
#define xdr_datum _xdr_datum
#define xdr_des_block _xdr_des_block
#define xdr_domainname _xdr_domainname
#define xdr_double _xdr_double
#define xdr_enum _xdr_enum
#define xdr_float _xdr_float
#define xdr_free _xdr_free
#define xdr_hyper _xdr_hyper
#define xdr_int _xdr_int
#define xdr_int16_t _xdr_int16_t
#define xdr_int32_t _xdr_int32_t
#define xdr_int64_t _xdr_int64_t
#define xdr_long _xdr_long
#define xdr_longlong_t _xdr_longlong_t
#define xdr_mapname _xdr_mapname
#define xdr_netbuf _xdr_netbuf
#define xdr_netobj _xdr_netobj
#define xdr_opaque _xdr_opaque
#define xdr_opaque_auth _xdr_opaque_auth
#define xdr_peername _xdr_peername
#define xdr_pmap _xdr_pmap
#define xdr_pmaplist _xdr_pmaplist
#define xdr_pointer _xdr_pointer
#define xdr_reference _xdr_reference
#define xdr_rejected_reply _xdr_rejected_reply
#define xdr_replymsg _xdr_replymsg
#define xdr_rmtcall_args _xdr_rmtcall_args
#define xdr_rmtcallres _xdr_rmtcallres
#define xdr_rpcb _xdr_rpcb
#define xdr_rpcb_entry _xdr_rpcb_entry
#define xdr_rpcb_entry_list_ptr _xdr_rpcb_entry_list_ptr
#define xdr_rpcb_rmtcallargs _xdr_rpcb_rmtcallargs
#define xdr_rpcb_rmtcallres _xdr_rpcb_rmtcallres
#define xdr_rpcb_stat _xdr_rpcb_stat
#define xdr_rpcb_stat_byvers _xdr_rpcb_stat_byvers
#define xdr_rpcblist _xdr_rpcblist
#define xdr_rpcblist_ptr _xdr_rpcblist_ptr
#define xdr_rpcbs_addrlist _xdr_rpcbs_addrlist
#define xdr_rpcbs_addrlist_ptr _xdr_rpcbs_addrlist_ptr
#define xdr_rpcbs_proc _xdr_rpcbs_proc
#define xdr_rpcbs_rmtcalllist _xdr_rpcbs_rmtcalllist
#define xdr_rpcbs_rmtcalllist_ptr _xdr_rpcbs_rmtcalllist_ptr
#define xdr_rpcbs _xdr_rpcbs
#define xdr_rpcbs _xdr_rpcbs
#define xdr_short _xdr_short
#define xdr_string _xdr_string
#define xdr_u_char _xdr_u_char
#define xdr_u_hyper _xdr_u_hyper
#define xdr_u_int _xdr_u_int
#define xdr_u_int16_t _xdr_u_int16_t
#define xdr_u_int32_t _xdr_u_int32_t
#define xdr_u_int64_t _xdr_u_int64_t
#define xdr_u_long _xdr_u_long
#define xdr_u_longlong_t _xdr_u_longlong_t
#define xdr_u_short _xdr_u_short
#define xdr_union _xdr_union
#define xdr_vector _xdr_vector
#define xdr_void _xdr_void
#define xdr_wrapstring _xdr_wrapstring
#define xdr_yp_inaddr _xdr_yp_inaddr
#define xdr_ypall _xdr_ypall
#define xdr_ypbind_resp _xdr_ypbind_resp
#define xdr_ypbind_setdom _xdr_ypbind_setdom
#define xdr_ypdomain_wrap_string _xdr_ypdomain_wrap_string
#define xdr_ypmap_parms _xdr_ypmap_parms
#define xdr_ypmap_wrap_string _xdr_ypmap_wrap_string
#define xdr_ypmaplist _xdr_ypmaplist
#define xdr_ypowner_wrap_string _xdr_ypowner_wrap_string
#define xdr_yppushresp_xfr _xdr_yppushresp_xfr
#define xdr_ypreq_key _xdr_ypreq_key
#define xdr_ypreq_nokey _xdr_ypreq_nokey
#define xdr_ypreq_xfr _xdr_ypreq_xfr
#define xdr_ypresp_key_val _xdr_ypresp_key_val
#define xdr_ypresp_maplist _xdr_ypresp_maplist
#define xdr_ypresp_master _xdr_ypresp_master
#define xdr_ypresp_order _xdr_ypresp_order
#define xdr_ypresp_val _xdr_ypresp_val
#define xdrmem_create _xdrmem_create
#define xdrrec_create _xdrrec_create
#define xdrrec_endofrecord _xdrrec_endofrecord
#define xdrrec_eof _xdrrec_eof
#define xdrrec_skiprecord _xdrrec_skiprecord
#define xdrstdio_create _xdrstdio_create
#define xprt_register _xprt_register
#define xprt_unregister _xprt_unregister
#define yp_all _yp_all
#define yp_bind _yp_bind
#define yp_first _yp_first
#define yp_get_default_domain _yp_get_default_domain
#define yp_maplist _yp_maplist
#define yp_master _yp_master
#define yp_match _yp_match
#define yp_next _yp_next
#define yp_order _yp_order
#define yp_unbind _yp_unbind
#define yperr_string _yperr_string
#define ypprot_err _ypprot_err
#define dlopen __dlopen
#define dlclose __dlclose
#define dlsym __dlsym
#define dlerror __dlerror
#define dladdr __dladdr
#define fmtcheck __fmtcheck
#endif /* __weak_alias */
#endif /* _NAMESPACE_H_ */

View file

@ -0,0 +1,330 @@
#include "config.h"
#include <nbtypes.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#if HAVE_POLL
# if HAVE_POLL_H
# include <poll.h>
# elif HAVE_SYS_POLL_H
# include <sys/poll.h>
# endif
#elif HAVE_SELECT
# define USE_SELECT
#else
# error "no poll() or select() found"
#endif
#if HAVE_DIRENT_H
# include <dirent.h>
#else
# define dirent direct
# if HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# if HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#if HAVE_SYS_QUEUE_H
# include <sys/queue.h>
#else
#define LIST_HEAD(name, type) \
struct name { \
struct type *lh_first; /* first element */ \
}
#define LIST_HEAD_INITIALIZER(head) \
{ NULL }
#define LIST_ENTRY(type) \
struct { \
struct type *le_next; /* next element */ \
struct type **le_prev; /* address of previous next element */ \
}
#define LIST_INSERT_HEAD(head, elm, field) do { \
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
(head)->lh_first = (elm); \
(elm)->field.le_prev = &(head)->lh_first; \
} while (/*CONSTCOND*/0)
#define LIST_INIT(head) do { \
(head)->lh_first = NULL; \
} while (/*CONSTCOND*/0)
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
#define LIST_FIRST(head) ((head)->lh_first)
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#endif
#if HAVE_ASSERT_H
# include <assert.h>
#endif
#if HAVE_ERR_H
# include <err.h>
#endif
#include "ftpglob.h"
#if HAVE_SYS_MOUNT_H
# include <sys/mount.h>
#endif
#if HAVE_PATHS_H
# include <paths.h>
#endif
#ifndef _PATH_BSHELL
#define _PATH_BSHELL "/bin/sh"
#endif
#ifndef _PATH_TMP
#define _PATH_TMP "/tmp/"
#endif
#if HAVE_STRINGLIST_H
# include <stringlist.h>
#else
typedef struct _stringlist {
char **sl_str;
size_t sl_max;
size_t sl_cur;
} StringList;
StringList *sl_init(void);
int sl_add(StringList *, char *);
void sl_free(StringList *, int);
char *sl_find(StringList *, char *);
#endif
#if HAVE_TERMCAP_H
# include <termcap.h>
#else
int tgetent(char *, const char *);
char *tgetstr(const char *, char **);
int tgetflag(const char *);
int tgetnum(const char *);
char *tgoto(const char *, int, int);
void tputs(const char *, int, int (*)(int));
#endif
#if HAVE_UTIL_H
# include <util.h>
#endif
#if HAVE_LIBUTIL_H
# include <libutil.h>
#endif
#if HAVE_VIS_H
# include <vis.h>
#else
# include "vis.h"
#endif
#if ! HAVE_D_NAMLEN
# define DIRENT_MISSING_D_NAMLEN
#endif
#if ! HAVE_H_ERRNO_D
extern int h_errno;
#endif
#define HAVE_H_ERRNO 1 /* XXX: an assumption for now... */
#if ! HAVE_FCLOSE_D
int fclose(FILE *);
#endif
#if ! HAVE_GETPASS_D
char *getpass(const char *);
#endif
#if ! HAVE_OPTARG_D
extern char *optarg;
#endif
#if ! HAVE_OPTIND_D
extern int optind;
#endif
#if ! HAVE_PCLOSE_D
int pclose(FILE *);
#endif
#if ! HAVE_ERR
void err(int, const char *, ...);
void errx(int, const char *, ...);
void warn(const char *, ...);
void warnx(const char *, ...);
#endif
#if ! HAVE_FGETLN
char *fgetln(FILE *, size_t *);
#endif
#if ! HAVE_FSEEKO
int fseeko(FILE *, off_t, int);
#endif
#if ! HAVE_FPARSELN
# define FPARSELN_UNESCESC 0x01
# define FPARSELN_UNESCCONT 0x02
# define FPARSELN_UNESCCOMM 0x04
# define FPARSELN_UNESCREST 0x08
# define FPARSELN_UNESCALL 0x0f
char *fparseln(FILE *, size_t *, size_t *, const char[3], int);
#endif
#if ! HAVE_INET_NTOP
const char *inet_ntop(int, const void *, char *, size_t);
#endif
#if ! HAVE_INET_PTON
int inet_pton(int, const char *, void *);
#endif
#if ! HAVE_MKSTEMP
int mkstemp(char *);
#endif
#if ! HAVE_SETPROGNAME
const char *getprogname(void);
void setprogname(const char *);
#endif
#if ! HAVE_SNPRINTF
int snprintf(char *, size_t, const char *, ...);
#endif
#if ! HAVE_STRDUP
char *strdup(const char *);
#endif
#if ! HAVE_STRERROR
char *strerror(int);
#endif
#if ! HAVE_STRPTIME || ! HAVE_STRPTIME_D
char *strptime(const char *, const char *, struct tm *);
#endif
#if HAVE_QUAD_SUPPORT
# if ! HAVE_STRTOLL && HAVE_LONG_LONG
long long strtoll(const char *, char **, int);
# if ! defined(QUAD_MIN)
# define QUAD_MIN (-0x7fffffffffffffffL-1)
# endif
# if ! defined(QUAD_MAX)
# define QUAD_MAX (0x7fffffffffffffffL)
# endif
# endif
#else /* ! HAVE_QUAD_SUPPORT */
# define NO_LONG_LONG 1
#endif /* ! HAVE_QUAD_SUPPORT */
#if ! HAVE_TIMEGM
time_t timegm(struct tm *);
#endif
#if ! HAVE_HSTRERROR
char *strerror(int);
#endif
#if ! HAVE_STRLCAT
size_t strlcat(char *, const char *, size_t);
#endif
#if ! HAVE_STRLCPY
size_t strlcpy(char *, const char *, size_t);
#endif
#if ! HAVE_STRSEP
char *strsep(char **stringp, const char *delim);
#endif
#if ! HAVE_MEMMOVE
# define memmove(a,b,c) bcopy((b),(a),(c))
/* XXX: add others #defines for borken systems? */
#endif
#if HAVE_GETPASSPHRASE
# define getpass getpassphrase
#endif
#if ! defined(MIN)
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#if ! defined(MAX)
# define MAX(a, b) ((a) < (b) ? (b) : (a))
#endif
#if ! defined(timersub)
# define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0)
#endif
#if ! defined(S_ISLNK)
# define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
#endif
#if ! defined(ALLPERMS)
# define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
#endif
#define DAYSPERNYEAR 365
#define EPOCH_YEAR 1970
#define HOURSPERDAY 24
#define MINSPERHOUR 60
#define SECSPERHOUR 3600
#define SECSPERMIN 60
#define SECSPERDAY 86400
#define TM_YEAR_BASE 1900

View file

@ -0,0 +1,313 @@
/* $NetBSD: pack_dev.c,v 1.1.1.1 2003/03/31 05:02:47 grant Exp $ */
/*-
* Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: pack_dev.c,v 1.1.1.1 2003/03/31 05:02:47 grant Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_ERR_H
#include <err.h>
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_SYS_MKDEV_H
#include <sys/mkdev.h>
#endif
#include "pack_dev.h"
static pack_t pack_netbsd;
static pack_t pack_freebsd;
static pack_t pack_8_8;
static pack_t pack_12_20;
static pack_t pack_14_18;
static pack_t pack_8_24;
static pack_t pack_bsdos;
static int compare_format(const void *, const void *);
/* exported */
dev_t
pack_native(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev(numbers[0], numbers[1]);
if (major(dev) != numbers[0])
errx(1, "invalid major number");
if (minor(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_netbsd(x) ((int32_t)((((x) & 0x000fff00) >> 8)))
#define minor_netbsd(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \
(((x) & 0x000000ff) >> 0)))
#define makedev_netbsd(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \
(((y) << 12) & 0xfff00000) | \
(((y) << 0) & 0x000000ff)))
static dev_t
pack_netbsd(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_netbsd(numbers[0], numbers[1]);
if (major_netbsd(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_netbsd(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
#define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
#define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
(((y) << 0) & 0xffff00ff)))
static dev_t
pack_freebsd(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_freebsd(numbers[0], numbers[1]);
if (major_freebsd(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_freebsd(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
#define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
#define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
(((y) << 0) & 0x000000ff)))
static dev_t
pack_8_8(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_8_8(numbers[0], numbers[1]);
if (major_8_8(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_8_8(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
#define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
#define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
(((y) << 0) & 0x000fffff)))
static dev_t
pack_12_20(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_12_20(numbers[0], numbers[1]);
if (major_12_20(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_12_20(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
#define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
#define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
(((y) << 0) & 0x0003ffff)))
static dev_t
pack_14_18(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_14_18(numbers[0], numbers[1]);
if (major_14_18(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_14_18(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
#define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
#define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
(((y) << 0) & 0x00ffffff)))
static dev_t
pack_8_24(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_8_24(numbers[0], numbers[1]);
if (major_8_24(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_8_24(dev) != numbers[1])
errx(1, "invalid minor number");
} else
errx(1, "too many fields for format");
return (dev);
}
#define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
#define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
#define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
#define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
(((y) << 8) & 0x000fff00) | \
(((z) << 0) & 0x000000ff)))
static dev_t
pack_bsdos(int n, u_long numbers[])
{
dev_t dev;
if (n == 2) {
dev = makedev_12_20(numbers[0], numbers[1]);
if (major_12_20(dev) != numbers[0])
errx(1, "invalid major number");
if (minor_12_20(dev) != numbers[1])
errx(1, "invalid minor number");
} else if (n == 3) {
dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
if (major_12_12_8(dev) != numbers[0])
errx(1, "invalid major number");
if (unit_12_12_8(dev) != numbers[1])
errx(1, "invalid unit number");
if (subunit_12_12_8(dev) != numbers[2])
errx(1, "invalid subunit number");
} else
errx(1, "too many fields for format");
return (dev);
}
/* list of formats and pack functions */
/* this list must be sorted lexically */
struct format {
const char *name;
pack_t *pack;
} formats[] = {
{"386bsd", pack_8_8},
{"4bsd", pack_8_8},
{"bsdos", pack_bsdos},
{"freebsd", pack_freebsd},
{"hpux", pack_8_24},
{"isc", pack_8_8},
{"linux", pack_8_8},
{"native", pack_native},
{"netbsd", pack_netbsd},
{"osf1", pack_12_20},
{"sco", pack_8_8},
{"solaris", pack_14_18},
{"sunos", pack_8_8},
{"svr3", pack_8_8},
{"svr4", pack_14_18},
{"ultrix", pack_8_8},
};
static int
compare_format(const void *key, const void *element)
{
const char *name;
const struct format *format;
name = key;
format = element;
return (strcmp(name, format->name));
}
pack_t *
pack_find(const char *name)
{
struct format *format;
format = bsearch(name, formats,
sizeof(formats)/sizeof(formats[0]),
sizeof(formats[0]), compare_format);
if (format == 0)
return (NULL);
return (format->pack);
}

View file

@ -0,0 +1,47 @@
/* $NetBSD: pack_dev.h,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $ */
/*-
* Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#ifndef _PACK_DEV_H
#define _PACK_DEV_H
typedef dev_t pack_t(int, u_long []);
pack_t *pack_find(const char *);
pack_t pack_native;
#endif /* _PACK_DEV_H */

View file

@ -0,0 +1,677 @@
/* $NetBSD: pwcache.c,v 1.1.1.1 2003/03/31 05:02:58 grant Exp $ */
/*-
* Copyright (c) 1992 Keith Muller.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego.
*
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "config.h"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: pwcache.c,v 1.1.1.1 2003/03/31 05:02:58 grant Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <assert.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef __ELF__
#define _user_from_uid user_from_uid
#define _group_from_gid group_from_gid
#define _pwcache_userdb pwcache_userdb
#define _pwcache_groupdb pwcache_groupdb
#else
#ifdef __weak_alias
__weak_alias(user_from_uid,_user_from_uid)
__weak_alias(group_from_gid,_group_from_gid)
__weak_alias(pwcache_userdb,_pwcache_userdb)
__weak_alias(pwcache_groupdb,_pwcache_groupdb)
#endif
#endif
#include "pwcache.h"
/*
* routines that control user, group, uid and gid caches (for the archive
* member print routine).
* IMPORTANT:
* these routines cache BOTH hits and misses, a major performance improvement
*/
/*
* function pointers to various name lookup routines.
* these may be changed as necessary.
*/
static int (*_pwcache_setgroupent)(int) =
#ifdef HAVE_SETGROUPENT
setgroupent;
#elif HAVE_SETGRENT
setgrent;
#else
NULL
#endif
static void (*_pwcache_endgrent)(void) = endgrent;
static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
static int (*_pwcache_setpassent)(int) =
#ifdef HAVE_SETPASSENT
setpassent;
#elif HAVE_SETGRENT
setpwent;
#else
NULL
#endif
static void (*_pwcache_endpwent)(void) = endpwent;
static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
/*
* internal state
*/
static int pwopn; /* is password file open */
static int gropn; /* is group file open */
static UIDC **uidtb; /* uid to name cache */
static GIDC **gidtb; /* gid to name cache */
static UIDC **usrtb; /* user name to uid cache */
static GIDC **grptb; /* group name to gid cache */
static int uidtb_fail; /* uidtb_start() failed ? */
static int gidtb_fail; /* gidtb_start() failed ? */
static int usrtb_fail; /* usrtb_start() failed ? */
static int grptb_fail; /* grptb_start() failed ? */
static u_int st_hash(const char *, size_t, int);
static int uidtb_start(void);
static int gidtb_start(void);
static int usrtb_start(void);
static int grptb_start(void);
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
static u_int
st_hash(const char *name, size_t len, int tabsz)
{
u_int key = 0;
_DIAGASSERT(name != NULL);
while (len--) {
key += *name++;
key = (key << 8) | (key >> 24);
}
return (key % tabsz);
}
/*
* uidtb_start
* creates an an empty uidtb
* Return:
* 0 if ok, -1 otherwise
*/
static int
uidtb_start(void)
{
if (uidtb != NULL)
return (0);
if (uidtb_fail)
return (-1);
if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
++uidtb_fail;
return (-1);
}
return (0);
}
/*
* gidtb_start
* creates an an empty gidtb
* Return:
* 0 if ok, -1 otherwise
*/
static int
gidtb_start(void)
{
if (gidtb != NULL)
return (0);
if (gidtb_fail)
return (-1);
if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
++gidtb_fail;
return (-1);
}
return (0);
}
/*
* usrtb_start
* creates an an empty usrtb
* Return:
* 0 if ok, -1 otherwise
*/
static int
usrtb_start(void)
{
if (usrtb != NULL)
return (0);
if (usrtb_fail)
return (-1);
if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
++usrtb_fail;
return (-1);
}
return (0);
}
/*
* grptb_start
* creates an an empty grptb
* Return:
* 0 if ok, -1 otherwise
*/
static int
grptb_start(void)
{
if (grptb != NULL)
return (0);
if (grptb_fail)
return (-1);
if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
++grptb_fail;
return (-1);
}
return (0);
}
/*
* user_from_uid()
* caches the name (if any) for the uid. If noname clear, we always
* return the the stored name (if valid or invalid match).
* We use a simple hash table.
* Return
* Pointer to stored name (or a empty string)
*/
#ifndef HAVE_USER_FROM_UID
const char *
user_from_uid(uid_t uid, int noname)
{
struct passwd *pw;
UIDC *ptr, **pptr;
if ((uidtb == NULL) && (uidtb_start() < 0))
return (NULL);
/*
* see if we have this uid cached
*/
pptr = uidtb + (uid % UID_SZ);
ptr = *pptr;
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
/*
* have an entry for this uid
*/
if (!noname || (ptr->valid == VALID))
return (ptr->name);
return (NULL);
}
/*
* No entry for this uid, we will add it
*/
if (!pwopn) {
if (_pwcache_setpassent != NULL)
(*_pwcache_setpassent)(1);
++pwopn;
}
if (ptr == NULL)
*pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
/*
* no match for this uid in the local password file
* a string that is the uid in numberic format
*/
if (ptr == NULL)
return (NULL);
ptr->uid = uid;
(void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
ptr->valid = INVALID;
if (noname)
return (NULL);
} else {
/*
* there is an entry for this uid in the password file
*/
if (ptr == NULL)
return (pw->pw_name);
ptr->uid = uid;
(void)strlcpy(ptr->name, pw->pw_name, UNMLEN);
ptr->valid = VALID;
}
return (ptr->name);
}
#endif
/*
* group_from_gid()
* caches the name (if any) for the gid. If noname clear, we always
* return the the stored name (if valid or invalid match).
* We use a simple hash table.
* Return
* Pointer to stored name (or a empty string)
*/
#ifndef HAVE_GROUP_FROM_GID
const char *
group_from_gid(gid_t gid, int noname)
{
struct group *gr;
GIDC *ptr, **pptr;
if ((gidtb == NULL) && (gidtb_start() < 0))
return (NULL);
/*
* see if we have this gid cached
*/
pptr = gidtb + (gid % GID_SZ);
ptr = *pptr;
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
/*
* have an entry for this gid
*/
if (!noname || (ptr->valid == VALID))
return (ptr->name);
return (NULL);
}
/*
* No entry for this gid, we will add it
*/
if (!gropn) {
if (_pwcache_setgroupent != NULL)
(*_pwcache_setgroupent)(1);
++gropn;
}
if (ptr == NULL)
*pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
/*
* no match for this gid in the local group file, put in
* a string that is the gid in numberic format
*/
if (ptr == NULL)
return (NULL);
ptr->gid = gid;
(void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
ptr->valid = INVALID;
if (noname)
return (NULL);
} else {
/*
* there is an entry for this group in the group file
*/
if (ptr == NULL)
return (gr->gr_name);
ptr->gid = gid;
(void)strlcpy(ptr->name, gr->gr_name, GNMLEN);
ptr->valid = VALID;
}
return (ptr->name);
}
#endif
/*
* uid_from_user()
* caches the uid for a given user name. We use a simple hash table.
* Return
* the uid (if any) for a user name, or a -1 if no match can be found
*/
#ifndef HAVE_UID_FROM_USER
int
uid_from_user(const char *name, uid_t *uid)
{
struct passwd *pw;
UIDC *ptr, **pptr;
size_t namelen;
/*
* return -1 for mangled names
*/
if (name == NULL || ((namelen = strlen(name)) == 0))
return (-1);
if ((usrtb == NULL) && (usrtb_start() < 0))
return (-1);
/*
* look up in hash table, if found and valid return the uid,
* if found and invalid, return a -1
*/
pptr = usrtb + st_hash(name, namelen, UNM_SZ);
ptr = *pptr;
if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
if (ptr->valid == INVALID)
return (-1);
*uid = ptr->uid;
return (0);
}
if (!pwopn) {
if (_pwcache_setpassent != NULL)
(*_pwcache_setpassent)(1);
++pwopn;
}
if (ptr == NULL)
*pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
/*
* no match, look it up, if no match store it as an invalid entry,
* or store the matching uid
*/
if (ptr == NULL) {
if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
return (-1);
*uid = pw->pw_uid;
return (0);
}
(void)strlcpy(ptr->name, name, UNMLEN);
if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
ptr->valid = INVALID;
return (-1);
}
ptr->valid = VALID;
*uid = ptr->uid = pw->pw_uid;
return (0);
}
#endif
/*
* gid_from_group()
* caches the gid for a given group name. We use a simple hash table.
* Return
* the gid (if any) for a group name, or a -1 if no match can be found
*/
#ifndef HAVE_GID_FROM_GROUP
int
gid_from_group(const char *name, gid_t *gid)
{
struct group *gr;
GIDC *ptr, **pptr;
size_t namelen;
/*
* return -1 for mangled names
*/
if (name == NULL || ((namelen = strlen(name)) == 0))
return (-1);
if ((grptb == NULL) && (grptb_start() < 0))
return (-1);
/*
* look up in hash table, if found and valid return the uid,
* if found and invalid, return a -1
*/
pptr = grptb + st_hash(name, namelen, GID_SZ);
ptr = *pptr;
if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
if (ptr->valid == INVALID)
return (-1);
*gid = ptr->gid;
return (0);
}
if (!gropn) {
if (_pwcache_setgroupent != NULL)
(*_pwcache_setgroupent)(1);
++gropn;
}
if (ptr == NULL)
*pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
/*
* no match, look it up, if no match store it as an invalid entry,
* or store the matching gid
*/
if (ptr == NULL) {
if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
return (-1);
*gid = gr->gr_gid;
return (0);
}
(void)strlcpy(ptr->name, name, GNMLEN);
if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
ptr->valid = INVALID;
return (-1);
}
ptr->valid = VALID;
*gid = ptr->gid = gr->gr_gid;
return (0);
}
#endif
#define FLUSHTB(arr, len, fail) \
do { \
if (arr != NULL) { \
for (i = 0; i < len; i++) \
if (arr[i] != NULL) \
free(arr[i]); \
arr = NULL; \
} \
fail = 0; \
} while (/* CONSTCOND */0);
#ifndef HAVE_PWCACHE_USERDB
int
pwcache_userdb(
int (*a_setpassent)(int),
void (*a_endpwent)(void),
struct passwd * (*a_getpwnam)(const char *),
struct passwd * (*a_getpwuid)(uid_t))
{
int i;
/* a_setpassent and a_endpwent may be NULL */
if (a_getpwnam == NULL || a_getpwuid == NULL)
return (-1);
if (_pwcache_endpwent != NULL)
(*_pwcache_endpwent)();
FLUSHTB(uidtb, UID_SZ, uidtb_fail);
FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
pwopn = 0;
_pwcache_setpassent = a_setpassent;
_pwcache_endpwent = a_endpwent;
_pwcache_getpwnam = a_getpwnam;
_pwcache_getpwuid = a_getpwuid;
return (0);
}
#endif
#ifndef HAVE_PWCACHE_GROUPDB
int
pwcache_groupdb(
int (*a_setgroupent)(int),
void (*a_endgrent)(void),
struct group * (*a_getgrnam)(const char *),
struct group * (*a_getgrgid)(gid_t))
{
int i;
/* a_setgroupent and a_endgrent may be NULL */
if (a_getgrnam == NULL || a_getgrgid == NULL)
return (-1);
if (_pwcache_endgrent != NULL)
(*_pwcache_endgrent)();
FLUSHTB(gidtb, GID_SZ, gidtb_fail);
FLUSHTB(grptb, GNM_SZ, grptb_fail);
gropn = 0;
_pwcache_setgroupent = a_setgroupent;
_pwcache_endgrent = a_endgrent;
_pwcache_getgrnam = a_getgrnam;
_pwcache_getgrgid = a_getgrgid;
return (0);
}
#endif
#ifdef TEST_PWCACHE
struct passwd *
test_getpwnam(const char *name)
{
static struct passwd foo;
memset(&foo, 0, sizeof(foo));
if (strcmp(name, "toor") == 0) {
foo.pw_uid = 666;
return &foo;
}
return (getpwnam(name));
}
int
main(int argc, char *argv[])
{
uid_t u;
int r, i;
printf("pass 1 (default userdb)\n");
for (i = 1; i < argc; i++) {
printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
i, pwopn, usrtb_fail, usrtb);
r = uid_from_user(argv[i], &u);
if (r == -1)
printf(" uid_from_user %s: failed\n", argv[i]);
else
printf(" uid_from_user %s: %d\n", argv[i], u);
}
printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
pwopn, usrtb_fail, usrtb);
puts("");
printf("pass 2 (replacement userdb)\n");
printf("pwcache_userdb returned %d\n",
pwcache_userdb(setpassent, test_getpwnam, getpwuid));
printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
for (i = 1; i < argc; i++) {
printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
i, pwopn, usrtb_fail, usrtb);
u = -1;
r = uid_from_user(argv[i], &u);
if (r == -1)
printf(" uid_from_user %s: failed\n", argv[i]);
else
printf(" uid_from_user %s: %d\n", argv[i], u);
}
printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
pwopn, usrtb_fail, usrtb);
puts("");
printf("pass 3 (null pointers)\n");
printf("pwcache_userdb returned %d\n",
pwcache_userdb(NULL, NULL, NULL));
return (0);
}
#endif /* TEST_PWCACHE */

View file

@ -0,0 +1,76 @@
/* $NetBSD: pwcache.h,v 1.1.1.1 2003/03/31 05:02:59 grant Exp $ */
/*-
* Copyright (c) 1992 Keith Muller.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego.
*
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)cache.h 8.1 (Berkeley) 5/31/93
*/
/*
* Constants and data structures used to implement group and password file
* caches. Traditional passwd/group cache routines perform quite poorly with
* archives. The chances of hitting a valid lookup with an archive is quite a
* bit worse than with files already resident on the file system. These misses
* create a MAJOR performance cost. To adress this problem, these routines
* cache both hits and misses.
*
* NOTE: name lengths must be as large as those stored in ANY PROTOCOL and
* as stored in the passwd and group files. CACHE SIZES MUST BE PRIME
*/
#define UNMLEN 32 /* >= user name found in any protocol */
#define GNMLEN 32 /* >= group name found in any protocol */
#define UID_SZ 317 /* size of uid to user_name cache */
#define UNM_SZ 317 /* size of user_name to uid cache */
#define GID_SZ 251 /* size of gid to group_name cache */
#define GNM_SZ 251 /* size of group_name to gid cache */
#define VALID 1 /* entry and name are valid */
#define INVALID 2 /* entry valid, name NOT valid */
/*
* Node structures used in the user, group, uid, and gid caches.
*/
typedef struct uidc {
int valid; /* is this a valid or a miss entry */
char name[UNMLEN]; /* uid name */
uid_t uid; /* cached uid */
} UIDC;
typedef struct gidc {
int valid; /* is this a valid or a miss entry */
char name[GNMLEN]; /* gid name */
gid_t gid; /* cached gid */
} GIDC;

View file

@ -0,0 +1,470 @@
/* $NetBSD: rmd160.c,v 1.1.1.1 2003/03/31 05:02:55 grant Exp $ */
/********************************************************************\
*
* FILE: rmd160.c
*
* CONTENTS: A sample C-implementation of the RIPEMD-160
* hash-function.
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* (Arranged for libc by Todd C. Miller)
* DATE: 1 March 1996
* VERSION: 1.0
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
\********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#ifndef lint
__RCSID("$NetBSD: rmd160.c,v 1.1.1.1 2003/03/31 05:02:55 grant Exp $");
#endif /* not lint */
/* header files */
/* #include "namespace.h" */
#include <assert.h>
#include <rmd160.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
#if defined(HAVE_MEMSET)
#define ZEROIZE(d, l) memset((d), 0, (l))
#else
# if defined(HAVE_BZERO)
#define ZEROIZE(d, l) bzero((d), (l))
# else
#error You need either memset or bzero
# endif
#endif
#if 0
#if !defined(_KERNEL) && defined(__weak_alias)
__weak_alias(RMD160Transform,_RMD160Transform)
__weak_alias(RMD160Init,_RMD160Init)
__weak_alias(RMD160Update,_RMD160Update)
__weak_alias(RMD160Final,_RMD160Final)
#endif
#endif
/********************************************************************/
/* macro definitions */
/* collect four bytes into one word: */
#define BYTES_TO_DWORD(strptr) \
(((u_int32_t) *((strptr)+3) << 24) | \
((u_int32_t) *((strptr)+2) << 16) | \
((u_int32_t) *((strptr)+1) << 8) | \
((u_int32_t) *(strptr)))
/* ROL(x, n) cyclically rotates x over n bits to the left */
/* x must be of an unsigned 32 bits type and 0 <= n < 32. */
#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* the three basic functions F(), G() and H() */
#define F(x, y, z) ((x) ^ (y) ^ (z))
#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define H(x, y, z) (((x) | ~(y)) ^ (z))
#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define J(x, y, z) ((x) ^ ((y) | ~(z)))
/* the eight basic operations FF() through III() */
#define FF(a, b, c, d, e, x, s) { \
(a) += F((b), (c), (d)) + (x); \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define GG(a, b, c, d, e, x, s) { \
(a) += G((b), (c), (d)) + (x) + 0x5a827999U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define HH(a, b, c, d, e, x, s) { \
(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define II(a, b, c, d, e, x, s) { \
(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define JJ(a, b, c, d, e, x, s) { \
(a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define FFF(a, b, c, d, e, x, s) { \
(a) += F((b), (c), (d)) + (x); \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define GGG(a, b, c, d, e, x, s) { \
(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define HHH(a, b, c, d, e, x, s) { \
(a) += H((b), (c), (d)) + (x) + 0x6d703ef3U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define III(a, b, c, d, e, x, s) { \
(a) += I((b), (c), (d)) + (x) + 0x5c4dd124U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define JJJ(a, b, c, d, e, x, s) { \
(a) += J((b), (c), (d)) + (x) + 0x50a28be6U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
/********************************************************************/
void
RMD160Init(RMD160_CTX *context)
{
_DIAGASSERT(context != NULL);
/* ripemd-160 initialization constants */
context->state[0] = 0x67452301U;
context->state[1] = 0xefcdab89U;
context->state[2] = 0x98badcfeU;
context->state[3] = 0x10325476U;
context->state[4] = 0xc3d2e1f0U;
context->length[0] = context->length[1] = 0;
context->buflen = 0;
}
/********************************************************************/
void
RMD160Transform(u_int32_t state[5], const u_int32_t block[16])
{
u_int32_t aa, bb, cc, dd, ee;
u_int32_t aaa, bbb, ccc, ddd, eee;
_DIAGASSERT(state != NULL);
_DIAGASSERT(block != NULL);
aa = aaa = state[0];
bb = bbb = state[1];
cc = ccc = state[2];
dd = ddd = state[3];
ee = eee = state[4];
/* round 1 */
FF(aa, bb, cc, dd, ee, block[ 0], 11);
FF(ee, aa, bb, cc, dd, block[ 1], 14);
FF(dd, ee, aa, bb, cc, block[ 2], 15);
FF(cc, dd, ee, aa, bb, block[ 3], 12);
FF(bb, cc, dd, ee, aa, block[ 4], 5);
FF(aa, bb, cc, dd, ee, block[ 5], 8);
FF(ee, aa, bb, cc, dd, block[ 6], 7);
FF(dd, ee, aa, bb, cc, block[ 7], 9);
FF(cc, dd, ee, aa, bb, block[ 8], 11);
FF(bb, cc, dd, ee, aa, block[ 9], 13);
FF(aa, bb, cc, dd, ee, block[10], 14);
FF(ee, aa, bb, cc, dd, block[11], 15);
FF(dd, ee, aa, bb, cc, block[12], 6);
FF(cc, dd, ee, aa, bb, block[13], 7);
FF(bb, cc, dd, ee, aa, block[14], 9);
FF(aa, bb, cc, dd, ee, block[15], 8);
/* round 2 */
GG(ee, aa, bb, cc, dd, block[ 7], 7);
GG(dd, ee, aa, bb, cc, block[ 4], 6);
GG(cc, dd, ee, aa, bb, block[13], 8);
GG(bb, cc, dd, ee, aa, block[ 1], 13);
GG(aa, bb, cc, dd, ee, block[10], 11);
GG(ee, aa, bb, cc, dd, block[ 6], 9);
GG(dd, ee, aa, bb, cc, block[15], 7);
GG(cc, dd, ee, aa, bb, block[ 3], 15);
GG(bb, cc, dd, ee, aa, block[12], 7);
GG(aa, bb, cc, dd, ee, block[ 0], 12);
GG(ee, aa, bb, cc, dd, block[ 9], 15);
GG(dd, ee, aa, bb, cc, block[ 5], 9);
GG(cc, dd, ee, aa, bb, block[ 2], 11);
GG(bb, cc, dd, ee, aa, block[14], 7);
GG(aa, bb, cc, dd, ee, block[11], 13);
GG(ee, aa, bb, cc, dd, block[ 8], 12);
/* round 3 */
HH(dd, ee, aa, bb, cc, block[ 3], 11);
HH(cc, dd, ee, aa, bb, block[10], 13);
HH(bb, cc, dd, ee, aa, block[14], 6);
HH(aa, bb, cc, dd, ee, block[ 4], 7);
HH(ee, aa, bb, cc, dd, block[ 9], 14);
HH(dd, ee, aa, bb, cc, block[15], 9);
HH(cc, dd, ee, aa, bb, block[ 8], 13);
HH(bb, cc, dd, ee, aa, block[ 1], 15);
HH(aa, bb, cc, dd, ee, block[ 2], 14);
HH(ee, aa, bb, cc, dd, block[ 7], 8);
HH(dd, ee, aa, bb, cc, block[ 0], 13);
HH(cc, dd, ee, aa, bb, block[ 6], 6);
HH(bb, cc, dd, ee, aa, block[13], 5);
HH(aa, bb, cc, dd, ee, block[11], 12);
HH(ee, aa, bb, cc, dd, block[ 5], 7);
HH(dd, ee, aa, bb, cc, block[12], 5);
/* round 4 */
II(cc, dd, ee, aa, bb, block[ 1], 11);
II(bb, cc, dd, ee, aa, block[ 9], 12);
II(aa, bb, cc, dd, ee, block[11], 14);
II(ee, aa, bb, cc, dd, block[10], 15);
II(dd, ee, aa, bb, cc, block[ 0], 14);
II(cc, dd, ee, aa, bb, block[ 8], 15);
II(bb, cc, dd, ee, aa, block[12], 9);
II(aa, bb, cc, dd, ee, block[ 4], 8);
II(ee, aa, bb, cc, dd, block[13], 9);
II(dd, ee, aa, bb, cc, block[ 3], 14);
II(cc, dd, ee, aa, bb, block[ 7], 5);
II(bb, cc, dd, ee, aa, block[15], 6);
II(aa, bb, cc, dd, ee, block[14], 8);
II(ee, aa, bb, cc, dd, block[ 5], 6);
II(dd, ee, aa, bb, cc, block[ 6], 5);
II(cc, dd, ee, aa, bb, block[ 2], 12);
/* round 5 */
JJ(bb, cc, dd, ee, aa, block[ 4], 9);
JJ(aa, bb, cc, dd, ee, block[ 0], 15);
JJ(ee, aa, bb, cc, dd, block[ 5], 5);
JJ(dd, ee, aa, bb, cc, block[ 9], 11);
JJ(cc, dd, ee, aa, bb, block[ 7], 6);
JJ(bb, cc, dd, ee, aa, block[12], 8);
JJ(aa, bb, cc, dd, ee, block[ 2], 13);
JJ(ee, aa, bb, cc, dd, block[10], 12);
JJ(dd, ee, aa, bb, cc, block[14], 5);
JJ(cc, dd, ee, aa, bb, block[ 1], 12);
JJ(bb, cc, dd, ee, aa, block[ 3], 13);
JJ(aa, bb, cc, dd, ee, block[ 8], 14);
JJ(ee, aa, bb, cc, dd, block[11], 11);
JJ(dd, ee, aa, bb, cc, block[ 6], 8);
JJ(cc, dd, ee, aa, bb, block[15], 5);
JJ(bb, cc, dd, ee, aa, block[13], 6);
/* parallel round 1 */
JJJ(aaa, bbb, ccc, ddd, eee, block[ 5], 8);
JJJ(eee, aaa, bbb, ccc, ddd, block[14], 9);
JJJ(ddd, eee, aaa, bbb, ccc, block[ 7], 9);
JJJ(ccc, ddd, eee, aaa, bbb, block[ 0], 11);
JJJ(bbb, ccc, ddd, eee, aaa, block[ 9], 13);
JJJ(aaa, bbb, ccc, ddd, eee, block[ 2], 15);
JJJ(eee, aaa, bbb, ccc, ddd, block[11], 15);
JJJ(ddd, eee, aaa, bbb, ccc, block[ 4], 5);
JJJ(ccc, ddd, eee, aaa, bbb, block[13], 7);
JJJ(bbb, ccc, ddd, eee, aaa, block[ 6], 7);
JJJ(aaa, bbb, ccc, ddd, eee, block[15], 8);
JJJ(eee, aaa, bbb, ccc, ddd, block[ 8], 11);
JJJ(ddd, eee, aaa, bbb, ccc, block[ 1], 14);
JJJ(ccc, ddd, eee, aaa, bbb, block[10], 14);
JJJ(bbb, ccc, ddd, eee, aaa, block[ 3], 12);
JJJ(aaa, bbb, ccc, ddd, eee, block[12], 6);
/* parallel round 2 */
III(eee, aaa, bbb, ccc, ddd, block[ 6], 9);
III(ddd, eee, aaa, bbb, ccc, block[11], 13);
III(ccc, ddd, eee, aaa, bbb, block[ 3], 15);
III(bbb, ccc, ddd, eee, aaa, block[ 7], 7);
III(aaa, bbb, ccc, ddd, eee, block[ 0], 12);
III(eee, aaa, bbb, ccc, ddd, block[13], 8);
III(ddd, eee, aaa, bbb, ccc, block[ 5], 9);
III(ccc, ddd, eee, aaa, bbb, block[10], 11);
III(bbb, ccc, ddd, eee, aaa, block[14], 7);
III(aaa, bbb, ccc, ddd, eee, block[15], 7);
III(eee, aaa, bbb, ccc, ddd, block[ 8], 12);
III(ddd, eee, aaa, bbb, ccc, block[12], 7);
III(ccc, ddd, eee, aaa, bbb, block[ 4], 6);
III(bbb, ccc, ddd, eee, aaa, block[ 9], 15);
III(aaa, bbb, ccc, ddd, eee, block[ 1], 13);
III(eee, aaa, bbb, ccc, ddd, block[ 2], 11);
/* parallel round 3 */
HHH(ddd, eee, aaa, bbb, ccc, block[15], 9);
HHH(ccc, ddd, eee, aaa, bbb, block[ 5], 7);
HHH(bbb, ccc, ddd, eee, aaa, block[ 1], 15);
HHH(aaa, bbb, ccc, ddd, eee, block[ 3], 11);
HHH(eee, aaa, bbb, ccc, ddd, block[ 7], 8);
HHH(ddd, eee, aaa, bbb, ccc, block[14], 6);
HHH(ccc, ddd, eee, aaa, bbb, block[ 6], 6);
HHH(bbb, ccc, ddd, eee, aaa, block[ 9], 14);
HHH(aaa, bbb, ccc, ddd, eee, block[11], 12);
HHH(eee, aaa, bbb, ccc, ddd, block[ 8], 13);
HHH(ddd, eee, aaa, bbb, ccc, block[12], 5);
HHH(ccc, ddd, eee, aaa, bbb, block[ 2], 14);
HHH(bbb, ccc, ddd, eee, aaa, block[10], 13);
HHH(aaa, bbb, ccc, ddd, eee, block[ 0], 13);
HHH(eee, aaa, bbb, ccc, ddd, block[ 4], 7);
HHH(ddd, eee, aaa, bbb, ccc, block[13], 5);
/* parallel round 4 */
GGG(ccc, ddd, eee, aaa, bbb, block[ 8], 15);
GGG(bbb, ccc, ddd, eee, aaa, block[ 6], 5);
GGG(aaa, bbb, ccc, ddd, eee, block[ 4], 8);
GGG(eee, aaa, bbb, ccc, ddd, block[ 1], 11);
GGG(ddd, eee, aaa, bbb, ccc, block[ 3], 14);
GGG(ccc, ddd, eee, aaa, bbb, block[11], 14);
GGG(bbb, ccc, ddd, eee, aaa, block[15], 6);
GGG(aaa, bbb, ccc, ddd, eee, block[ 0], 14);
GGG(eee, aaa, bbb, ccc, ddd, block[ 5], 6);
GGG(ddd, eee, aaa, bbb, ccc, block[12], 9);
GGG(ccc, ddd, eee, aaa, bbb, block[ 2], 12);
GGG(bbb, ccc, ddd, eee, aaa, block[13], 9);
GGG(aaa, bbb, ccc, ddd, eee, block[ 9], 12);
GGG(eee, aaa, bbb, ccc, ddd, block[ 7], 5);
GGG(ddd, eee, aaa, bbb, ccc, block[10], 15);
GGG(ccc, ddd, eee, aaa, bbb, block[14], 8);
/* parallel round 5 */
FFF(bbb, ccc, ddd, eee, aaa, block[12] , 8);
FFF(aaa, bbb, ccc, ddd, eee, block[15] , 5);
FFF(eee, aaa, bbb, ccc, ddd, block[10] , 12);
FFF(ddd, eee, aaa, bbb, ccc, block[ 4] , 9);
FFF(ccc, ddd, eee, aaa, bbb, block[ 1] , 12);
FFF(bbb, ccc, ddd, eee, aaa, block[ 5] , 5);
FFF(aaa, bbb, ccc, ddd, eee, block[ 8] , 14);
FFF(eee, aaa, bbb, ccc, ddd, block[ 7] , 6);
FFF(ddd, eee, aaa, bbb, ccc, block[ 6] , 8);
FFF(ccc, ddd, eee, aaa, bbb, block[ 2] , 13);
FFF(bbb, ccc, ddd, eee, aaa, block[13] , 6);
FFF(aaa, bbb, ccc, ddd, eee, block[14] , 5);
FFF(eee, aaa, bbb, ccc, ddd, block[ 0] , 15);
FFF(ddd, eee, aaa, bbb, ccc, block[ 3] , 13);
FFF(ccc, ddd, eee, aaa, bbb, block[ 9] , 11);
FFF(bbb, ccc, ddd, eee, aaa, block[11] , 11);
/* combine results */
ddd += cc + state[1]; /* final result for state[0] */
state[1] = state[2] + dd + eee;
state[2] = state[3] + ee + aaa;
state[3] = state[4] + aa + bbb;
state[4] = state[0] + bb + ccc;
state[0] = ddd;
}
/********************************************************************/
void
RMD160Update(RMD160_CTX *context, const u_char *data, u_int32_t nbytes)
{
u_int32_t X[16];
u_int32_t ofs = 0;
u_int32_t i;
#if BYTE_ORDER != LITTLE_ENDIAN
u_int32_t j;
#endif
_DIAGASSERT(context != NULL);
_DIAGASSERT(data != NULL);
/* update length[] */
if (context->length[0] + nbytes < context->length[0])
context->length[1]++; /* overflow to msb of length */
context->length[0] += nbytes;
ZEROIZE(X, sizeof(X));
if ( context->buflen + nbytes < 64 )
{
(void)memcpy(context->bbuffer + context->buflen, data, nbytes);
context->buflen += nbytes;
}
else
{
/* process first block */
ofs = 64 - context->buflen;
(void)memcpy(context->bbuffer + context->buflen, data, ofs);
#if BYTE_ORDER == LITTLE_ENDIAN
(void)memcpy(X, context->bbuffer, sizeof(X));
#else
for (j=0; j < 16; j++)
X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
#endif
RMD160Transform(context->state, X);
nbytes -= ofs;
/* process remaining complete blocks */
for (i = 0; i < (nbytes >> 6); i++) {
#if BYTE_ORDER == LITTLE_ENDIAN
(void)memcpy(X, data + (64 * i) + ofs, sizeof(X));
#else
for (j=0; j < 16; j++)
X[j] = BYTES_TO_DWORD(data + (64 * i) + (4 * j) + ofs);
#endif
RMD160Transform(context->state, X);
}
/*
* Put last bytes from data into context's buffer
*/
context->buflen = nbytes & 63;
memcpy(context->bbuffer, data + (64 * i) + ofs, context->buflen);
}
}
/********************************************************************/
void
RMD160Final(u_char digest[20], RMD160_CTX *context)
{
u_int32_t i;
u_int32_t X[16];
#if BYTE_ORDER != LITTLE_ENDIAN
u_int32_t j;
#endif
_DIAGASSERT(digest != NULL);
_DIAGASSERT(context != NULL);
/* append the bit m_n == 1 */
context->bbuffer[context->buflen] = (u_char)'\200';
ZEROIZE(context->bbuffer + context->buflen + 1,
63 - context->buflen);
#if BYTE_ORDER == LITTLE_ENDIAN
(void)memcpy(X, context->bbuffer, sizeof(X));
#else
for (j=0; j < 16; j++)
X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
#endif
if ((context->buflen) > 55) {
/* length goes to next block */
RMD160Transform(context->state, X);
ZEROIZE(X, sizeof(X));
}
/* append length in bits */
X[14] = context->length[0] << 3;
X[15] = (context->length[0] >> 29) |
(context->length[1] << 3);
RMD160Transform(context->state, X);
if (digest != NULL) {
for (i = 0; i < 20; i += 4) {
/* extracts the 8 least significant bits. */
digest[i] = context->state[i>>2];
digest[i + 1] = (context->state[i>>2] >> 8);
digest[i + 2] = (context->state[i>>2] >> 16);
digest[i + 3] = (context->state[i>>2] >> 24);
}
}
}
/************************ end of file rmd160.c **********************/

View file

@ -0,0 +1,46 @@
/* $NetBSD: rmd160.h,v 1.1.1.1 2003/03/31 05:02:59 grant Exp $ */
/********************************************************************\
*
* FILE: rmd160.h
*
* CONTENTS: Header file for a sample C-implementation of the
* RIPEMD-160 hash-function.
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* DATE: 1 March 1996
* VERSION: 1.0
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
\********************************************************************/
/*
* from OpenBSD: rmd160.h,v 1.4 1999/08/16 09:59:04 millert Exp
*/
#ifndef _RMD160_H_
#define _RMD160_H_
typedef struct {
u_int32_t state[5]; /* state (ABCDE) */
u_int32_t length[2]; /* number of bits */
u_char bbuffer[64]; /* overflow buffer */
u_int32_t buflen; /* number of chars in bbuffer */
} RMD160_CTX;
__BEGIN_DECLS
void RMD160Init(RMD160_CTX *);
void RMD160Transform(u_int32_t[5], const u_int32_t[16]);
void RMD160Update(RMD160_CTX *, const u_char *, u_int32_t);
void RMD160Final(u_char[20], RMD160_CTX *);
#ifndef _KERNEL
char *RMD160End(RMD160_CTX *, char *);
char *RMD160File(char *, char *);
char *RMD160Data(const u_char *, size_t, char *);
#endif /* _KERNEL */
__END_DECLS
#endif /* !_RMD160_H_ */

View file

@ -0,0 +1,111 @@
/* $NetBSD: rmd160hl.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $ */
/* rmd160hl.c
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* from OpenBSD: rmd160hl.c,v 1.2 1999/08/17 09:13:12 millert Exp $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#ifndef lint
__RCSID("$NetBSD: rmd160hl.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $");
#endif /* not lint */
/* #include "namespace.h" */
#include <assert.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <rmd160.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
#if 0
#if defined(__weak_alias)
__weak_alias(RMD160End,_RMD160End)
__weak_alias(RMD160File,_RMD160File)
__weak_alias(RMD160Data,_RMD160Data)
#endif
#endif
char *
RMD160End(RMD160_CTX *ctx, char *buf)
{
int i;
char *p = buf;
u_char digest[20];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != NULL);
/* buf may be NULL */
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
RMD160Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[(u_int32_t)digest[i] >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
}
char *
RMD160File(char *filename, char *buf)
{
u_char buffer[BUFSIZ];
RMD160_CTX ctx;
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
RMD160Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return(0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
RMD160Update(&ctx, buffer, (size_t)num);
oerrno = errno;
close(fd);
errno = oerrno;
return(num < 0 ? 0 : RMD160End(&ctx, buf));
}
char *
RMD160Data(const u_char *data, size_t len, char *buf)
{
RMD160_CTX ctx;
_DIAGASSERT(data != NULL);
/* XXX: buf may be NULL ? */
RMD160Init(&ctx);
RMD160Update(&ctx, data, len);
return(RMD160End(&ctx, buf));
}

View file

@ -0,0 +1,148 @@
/*
* Copyright (c) 1987 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)setenv.c 5.6 (Berkeley) 6/4/91";*/
static char *rcsid = "$Id: setenv.c,v 1.1.1.1 2003/03/31 05:03:00 grant Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
/*
* __findenv --
* Returns pointer to value associated with name, if any, else NULL.
* Sets offset to be the offset of the name/value combination in the
* environmental array, for use by setenv(3) and unsetenv(3).
* Explicitly removes '=' in argument name.
*
* This routine *should* be a static; don't use it.
*/
static char *
__findenv(name, offset)
register char *name;
int *offset;
{
extern char **environ;
register int len;
register char **P, *C;
for (C = name, len = 0; *C && *C != '='; ++C, ++len);
for (P = environ; *P; ++P)
if (!strncmp(*P, name, len))
if (*(C = *P + len) == '=') {
*offset = P - environ;
return(++C);
}
return(NULL);
}
/*
* setenv --
* Set the value of the environmental variable "name" to be
* "value". If rewrite is set, replace any current value.
*/
setenv(name, value, rewrite)
register const char *name;
register const char *value;
int rewrite;
{
extern char **environ;
static int alloced; /* if allocated space before */
register char *C;
int l_value, offset;
char *__findenv();
if (*value == '=') /* no `=' in value */
++value;
l_value = strlen(value);
if ((C = __findenv(name, &offset))) { /* find if already exists */
if (!rewrite)
return (0);
if (strlen(C) >= l_value) { /* old larger; copy over */
while (*C++ = *value++);
return (0);
}
} else { /* create new slot */
register int cnt;
register char **P;
for (P = environ, cnt = 0; *P; ++P, ++cnt);
if (alloced) { /* just increase size */
environ = (char **)realloc((char *)environ,
(size_t)(sizeof(char *) * (cnt + 2)));
if (!environ)
return (-1);
}
else { /* get new space */
alloced = 1; /* copy old entries into it */
P = (char **)malloc((size_t)(sizeof(char *) *
(cnt + 2)));
if (!P)
return (-1);
bcopy(environ, P, cnt * sizeof(char *));
environ = P;
}
environ[cnt + 1] = NULL;
offset = cnt;
}
for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */
if (!(environ[offset] = /* name + `=' + value */
malloc((size_t)((int)(C - name) + l_value + 2))))
return (-1);
for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
;
for (*C++ = '='; *C++ = *value++; )
;
return (0);
}
/*
* unsetenv(name) --
* Delete environmental variable "name".
*/
void
unsetenv(name)
const char *name;
{
extern char **environ;
register char **P;
int offset;
char *__findenv();
while (__findenv(name, &offset)) /* if set multiple times */
for (P = &environ[offset];; ++P)
if (!(*P = *(P + 1)))
break;
}

View file

@ -0,0 +1,507 @@
/* $NetBSD: setmode.c,v 1.1.1.1 2003/03/31 05:03:00 grant Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Dave Borman at Cray Research, Inc.
*
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
#else
__RCSID("$NetBSD: setmode.c,v 1.1.1.1 2003/03/31 05:03:00 grant Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef SETMODE_DEBUG
#include <stdio.h>
#endif
#ifndef S_ISTXT
#define S_ISTXT S_ISVTX
#endif
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
#ifndef __ELF__
#define _getmode getmode
#define _setmode setmode
#else
#ifdef __weak_alias
__weak_alias(getmode,_getmode)
__weak_alias(setmode,_setmode)
#endif
#endif
#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
typedef struct bitcmd {
char cmd;
char cmd2;
mode_t bits;
} BITCMD;
#define CMD2_CLR 0x01
#define CMD2_SET 0x02
#define CMD2_GBITS 0x04
#define CMD2_OBITS 0x08
#define CMD2_UBITS 0x10
static BITCMD *addcmd (BITCMD *, int, int, int, u_int);
static void compress_mode (BITCMD *);
#ifdef SETMODE_DEBUG
static void dumpmode (BITCMD *);
#endif
#ifndef HAVE_GETMODE
/*
* Given the old mode and an array of bitcmd structures, apply the operations
* described in the bitcmd structures to the old mode, and return the new mode.
* Note that there is no '=' command; a strict assignment is just a '-' (clear
* bits) followed by a '+' (set bits).
*/
mode_t
getmode(bbox, omode)
const void *bbox;
mode_t omode;
{
const BITCMD *set;
mode_t clrval, newmode, value;
_DIAGASSERT(bbox != NULL);
set = (const BITCMD *)bbox;
newmode = omode;
for (value = 0;; set++)
switch(set->cmd) {
/*
* When copying the user, group or other bits around, we "know"
* where the bits are in the mode so that we can do shifts to
* copy them around. If we don't use shifts, it gets real
* grundgy with lots of single bit checks and bit sets.
*/
case 'u':
value = (newmode & S_IRWXU) >> 6;
goto common;
case 'g':
value = (newmode & S_IRWXG) >> 3;
goto common;
case 'o':
value = newmode & S_IRWXO;
common: if (set->cmd2 & CMD2_CLR) {
clrval =
(set->cmd2 & CMD2_SET) ? S_IRWXO : value;
if (set->cmd2 & CMD2_UBITS)
newmode &= ~((clrval<<6) & set->bits);
if (set->cmd2 & CMD2_GBITS)
newmode &= ~((clrval<<3) & set->bits);
if (set->cmd2 & CMD2_OBITS)
newmode &= ~(clrval & set->bits);
}
if (set->cmd2 & CMD2_SET) {
if (set->cmd2 & CMD2_UBITS)
newmode |= (value<<6) & set->bits;
if (set->cmd2 & CMD2_GBITS)
newmode |= (value<<3) & set->bits;
if (set->cmd2 & CMD2_OBITS)
newmode |= value & set->bits;
}
break;
case '+':
newmode |= set->bits;
break;
case '-':
newmode &= ~set->bits;
break;
case 'X':
if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
newmode |= set->bits;
break;
case '\0':
default:
#ifdef SETMODE_DEBUG
(void)printf("getmode:%04o -> %04o\n", omode, newmode);
#endif
return (newmode);
}
}
#endif /* HAVE_GETMODE */
#ifndef HAVE_SETMODE
#define ADDCMD(a, b, c, d) do { \
if (set >= endset) { \
BITCMD *newset; \
setlen += SET_LEN_INCR; \
newset = realloc(saveset, sizeof(BITCMD) * setlen); \
if (newset == NULL) { \
free(saveset); \
return (NULL); \
} \
set = newset + (set - saveset); \
saveset = newset; \
endset = newset + (setlen - 2); \
} \
set = addcmd(set, (a), (b), (c), (d)); \
} while (/*CONSTCOND*/0)
#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
void *
setmode(p)
const char *p;
{
int perm, who;
char op, *ep;
BITCMD *set, *saveset, *endset;
sigset_t sigset, sigoset;
mode_t mask;
int equalopdone = 0; /* pacify gcc */
int permXbits, setlen;
if (!*p)
return (NULL);
/*
* Get a copy of the mask for the permissions that are mask relative.
* Flip the bits, we want what's not set. Since it's possible that
* the caller is opening files inside a signal handler, protect them
* as best we can.
*/
sigfillset(&sigset);
(void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
(void)umask(mask = umask(0));
mask = ~mask;
(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
setlen = SET_LEN + 2;
if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
return (NULL);
saveset = set;
endset = set + (setlen - 2);
/*
* If an absolute number, get it and return; disallow non-octal digits
* or illegal bits.
*/
if (isdigit((unsigned char)*p)) {
perm = (mode_t)strtol(p, &ep, 8);
if (*ep || perm & ~(STANDARD_BITS|S_ISTXT)) {
free(saveset);
return (NULL);
}
ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
set->cmd = 0;
return (saveset);
}
/*
* Build list of structures to set/clear/copy bits as described by
* each clause of the symbolic mode.
*/
for (;;) {
/* First, find out which bits might be modified. */
for (who = 0;; ++p) {
switch (*p) {
case 'a':
who |= STANDARD_BITS;
break;
case 'u':
who |= S_ISUID|S_IRWXU;
break;
case 'g':
who |= S_ISGID|S_IRWXG;
break;
case 'o':
who |= S_IRWXO;
break;
default:
goto getop;
}
}
getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
free(saveset);
return (NULL);
}
if (op == '=')
equalopdone = 0;
who &= ~S_ISTXT;
for (perm = 0, permXbits = 0;; ++p) {
switch (*p) {
case 'r':
perm |= S_IRUSR|S_IRGRP|S_IROTH;
break;
case 's':
/*
* If specific bits where requested and
* only "other" bits ignore set-id.
*/
if (who == 0 || (who & ~S_IRWXO))
perm |= S_ISUID|S_ISGID;
break;
case 't':
/*
* If specific bits where requested and
* only "other" bits ignore set-id.
*/
if (who == 0 || (who & ~S_IRWXO)) {
who |= S_ISTXT;
perm |= S_ISTXT;
}
break;
case 'w':
perm |= S_IWUSR|S_IWGRP|S_IWOTH;
break;
case 'X':
permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 'x':
perm |= S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 'u':
case 'g':
case 'o':
/*
* When ever we hit 'u', 'g', or 'o', we have
* to flush out any partial mode that we have,
* and then do the copying of the mode bits.
*/
if (perm) {
ADDCMD(op, who, perm, mask);
perm = 0;
}
if (op == '=')
equalopdone = 1;
if (op == '+' && permXbits) {
ADDCMD('X', who, permXbits, mask);
permXbits = 0;
}
ADDCMD(*p, who, op, mask);
break;
default:
/*
* Add any permissions that we haven't already
* done.
*/
if (perm || (op == '=' && !equalopdone)) {
if (op == '=')
equalopdone = 1;
ADDCMD(op, who, perm, mask);
perm = 0;
}
if (permXbits) {
ADDCMD('X', who, permXbits, mask);
permXbits = 0;
}
goto apply;
}
}
apply: if (!*p)
break;
if (*p != ',')
goto getop;
++p;
}
set->cmd = 0;
#ifdef SETMODE_DEBUG
(void)printf("Before compress_mode()\n");
dumpmode(saveset);
#endif
compress_mode(saveset);
#ifdef SETMODE_DEBUG
(void)printf("After compress_mode()\n");
dumpmode(saveset);
#endif
return (saveset);
}
static BITCMD *
addcmd(set, op, who, oparg, mask)
BITCMD *set;
int oparg, who;
int op;
u_int mask;
{
_DIAGASSERT(set != NULL);
switch (op) {
case '=':
set->cmd = '-';
set->bits = who ? who : STANDARD_BITS;
set++;
op = '+';
/* FALLTHROUGH */
case '+':
case '-':
case 'X':
set->cmd = op;
set->bits = (who ? who : mask) & oparg;
break;
case 'u':
case 'g':
case 'o':
set->cmd = op;
if (who) {
set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
((who & S_IRGRP) ? CMD2_GBITS : 0) |
((who & S_IROTH) ? CMD2_OBITS : 0);
set->bits = (mode_t)~0;
} else {
set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
set->bits = mask;
}
if (oparg == '+')
set->cmd2 |= CMD2_SET;
else if (oparg == '-')
set->cmd2 |= CMD2_CLR;
else if (oparg == '=')
set->cmd2 |= CMD2_SET|CMD2_CLR;
break;
}
return (set + 1);
}
#ifdef SETMODE_DEBUG
static void
dumpmode(set)
BITCMD *set;
{
_DIAGASSERT(set != NULL);
for (; set->cmd; ++set)
(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
set->cmd2 & CMD2_CLR ? " CLR" : "",
set->cmd2 & CMD2_SET ? " SET" : "",
set->cmd2 & CMD2_UBITS ? " UBITS" : "",
set->cmd2 & CMD2_GBITS ? " GBITS" : "",
set->cmd2 & CMD2_OBITS ? " OBITS" : "");
}
#endif
/*
* Given an array of bitcmd structures, compress by compacting consecutive
* '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
* 'g' and 'o' commands continue to be separate. They could probably be
* compacted, but it's not worth the effort.
*/
static void
compress_mode(set)
BITCMD *set;
{
BITCMD *nset;
int setbits, clrbits, Xbits, op;
_DIAGASSERT(set != NULL);
for (nset = set;;) {
/* Copy over any 'u', 'g' and 'o' commands. */
while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
*set++ = *nset++;
if (!op)
return;
}
for (setbits = clrbits = Xbits = 0;; nset++) {
if ((op = nset->cmd) == '-') {
clrbits |= nset->bits;
setbits &= ~nset->bits;
Xbits &= ~nset->bits;
} else if (op == '+') {
setbits |= nset->bits;
clrbits &= ~nset->bits;
Xbits &= ~nset->bits;
} else if (op == 'X')
Xbits |= nset->bits & ~setbits;
else
break;
}
if (clrbits) {
set->cmd = '-';
set->cmd2 = 0;
set->bits = clrbits;
set++;
}
if (setbits) {
set->cmd = '+';
set->cmd2 = 0;
set->bits = setbits;
set++;
}
if (Xbits) {
set->cmd = 'X';
set->cmd2 = 0;
set->bits = Xbits;
set++;
}
}
}
#endif /* !HAVE_SETMODE */

View file

@ -0,0 +1,58 @@
/* $Id: setprogname.c,v 1.1.1.1 2003/03/31 05:03:00 grant Exp $ */
/* $NetBSD: setprogname.c,v 1.1.1.1 2003/03/31 05:03:00 grant Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Todd Vierling.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
static const char *__progname = "<unset_progname>";
void
setprogname(const char *progname)
{
__progname = strrchr(progname, '/');
if (__progname == NULL)
__progname = progname;
else
__progname++;
}
const char *
getprogname(void)
{
return __progname;
}

View file

@ -0,0 +1,288 @@
/* $NetBSD: sha1.c,v 1.1.1.1 2003/03/31 05:02:50 grant Exp $ */
/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */
/*
* SHA-1 in C
* By Steve Reid <steve@edmweb.com>
* 100% Public Domain
*
* Test Vectors (from FIPS PUB 180-1)
* "abc"
* A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
* 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
* A million repetitions of "a"
* 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <nbtypes.h>
#define SHA1HANDSOFF /* Copies data before messing with it. */
#if defined(_KERNEL) || defined(_STANDALONE)
#include <sys/param.h>
#include <sys/systm.h>
#define _DIAGASSERT(x) (void)0
#else
/* #include "namespace.h" */
#include <assert.h>
#include <string.h>
#endif
#include <sha1.h>
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
/*
* XXX Kludge until there is resolution regarding mem*() functions
* XXX in the kernel.
*/
#if defined(_KERNEL) || defined(_STANDALONE)
#define memcpy(s, d, l) bcopy((d), (s), (l))
#endif
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
/*
* blk0() and blk() perform the initial expand.
* I got the idea of expanding during the round function from SSLeay
*/
#if BYTE_ORDER == LITTLE_ENDIAN
# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|(rol(block->l[i],8)&0x00FF00FF))
#else
# define blk0(i) block->l[i]
#endif
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
/*
* (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
*/
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
#if 0
__weak_alias(SHA1Transform,_SHA1Transform)
__weak_alias(SHA1Init,_SHA1Init)
__weak_alias(SHA1Update,_SHA1Update)
__weak_alias(SHA1Final,_SHA1Final)
#endif
typedef union {
u_char c[64];
u_int l[16];
} CHAR64LONG16;
#ifdef __sparc_v9__
void do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
void do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
void do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
void do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
void
do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
{
nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3);
nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7);
nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15);
nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
}
void
do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
{
nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23);
nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27);
nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35);
nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
}
void
do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
{
nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43);
nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47);
nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55);
nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
}
void
do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
{
nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63);
nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67);
nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75);
nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
}
#endif
/*
* Hash a single 512-bit block. This is the core of the algorithm.
*/
void SHA1Transform(state, buffer)
u_int32_t state[5];
const u_char buffer[64];
{
u_int32_t a, b, c, d, e;
CHAR64LONG16 *block;
#ifdef SHA1HANDSOFF
CHAR64LONG16 workspace;
#endif
_DIAGASSERT(buffer != 0);
_DIAGASSERT(state != 0);
#ifdef SHA1HANDSOFF
block = &workspace;
(void)memcpy(block, buffer, 64);
#else
block = (CHAR64LONG16 *)(void *)buffer;
#endif
/* Copy context->state[] to working vars */
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
#ifdef __sparc_v9__
do_R01(&a, &b, &c, &d, &e, block);
do_R2(&a, &b, &c, &d, &e, block);
do_R3(&a, &b, &c, &d, &e, block);
do_R4(&a, &b, &c, &d, &e, block);
#else
/* 4 rounds of 20 operations each. Loop unrolled. */
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
#endif
/* Add the working vars back into context.state[] */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
/* Wipe variables */
a = b = c = d = e = 0;
}
/*
* SHA1Init - Initialize new context
*/
void SHA1Init(context)
SHA1_CTX *context;
{
_DIAGASSERT(context != 0);
/* SHA1 initialization constants */
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0xC3D2E1F0;
context->count[0] = context->count[1] = 0;
}
/*
* Run your data through this.
*/
void SHA1Update(context, data, len)
SHA1_CTX *context;
const u_char *data;
u_int len;
{
u_int i, j;
_DIAGASSERT(context != 0);
_DIAGASSERT(data != 0);
j = context->count[0];
if ((context->count[0] += len << 3) < j)
context->count[1] += (len>>29)+1;
j = (j >> 3) & 63;
if ((j + len) > 63) {
(void)memcpy(&context->buffer[j], data, (i = 64-j));
SHA1Transform(context->state, context->buffer);
for ( ; i + 63 < len; i += 64)
SHA1Transform(context->state, &data[i]);
j = 0;
} else {
i = 0;
}
(void)memcpy(&context->buffer[j], &data[i], len - i);
}
/*
* Add padding and return the message digest.
*/
void SHA1Final(digest, context)
u_char digest[20];
SHA1_CTX* context;
{
u_int i;
u_char finalcount[8];
_DIAGASSERT(digest != 0);
_DIAGASSERT(context != 0);
for (i = 0; i < 8; i++) {
finalcount[i] = (u_char)((context->count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
}
SHA1Update(context, (const u_char *)"\200", 1);
while ((context->count[0] & 504) != 448)
SHA1Update(context, (const u_char *)"\0", 1);
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
if (digest) {
for (i = 0; i < 20; i++)
digest[i] = (u_char)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
}

View file

@ -0,0 +1,28 @@
/* $NetBSD: sha1.h,v 1.1.1.1 2003/03/31 05:03:00 grant Exp $ */
/*
* SHA-1 in C
* By Steve Reid <steve@edmweb.com>
* 100% Public Domain
*/
#ifndef _SYS_SHA1_H_
#define _SYS_SHA1_H_
typedef struct {
u_int32_t state[5];
u_int32_t count[2];
u_char buffer[64];
} SHA1_CTX;
void SHA1Transform __P((u_int32_t state[5], const u_char buffer[64]));
void SHA1Init __P((SHA1_CTX *context));
void SHA1Update __P((SHA1_CTX *context, const u_char *data, u_int len));
void SHA1Final __P((u_char digest[20], SHA1_CTX *context));
#ifndef _KERNEL
char *SHA1End __P((SHA1_CTX *, char *));
char *SHA1File __P((char *, char *));
char *SHA1Data __P((const u_char *, size_t, char *));
#endif /* _KERNEL */
#endif /* _SYS_SHA1_H_ */

View file

@ -0,0 +1,117 @@
/* $NetBSD: sha1hl.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $ */
/* sha1hl.c
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*/
/* #include "namespace.h" */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#include <sys/uio.h>
#include <assert.h>
#include <errno.h>
#include <sha1.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: sha1hl.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $");
#endif /* LIBC_SCCS and not lint */
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
#if 0
__weak_alias(SHA1End,_SHA1End)
__weak_alias(SHA1File,_SHA1File)
__weak_alias(SHA1Data,_SHA1Data)
#endif
/* ARGSUSED */
char *
SHA1End(ctx, buf)
SHA1_CTX *ctx;
char *buf;
{
int i;
char *p = buf;
u_char digest[20];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != NULL);
/* buf may be NULL */
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
SHA1Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[((u_int32_t)digest[i]) >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
}
char *
SHA1File (filename, buf)
char *filename;
char *buf;
{
u_char buffer[BUFSIZ];
SHA1_CTX ctx;
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA1Init(&ctx);
if ((fd = open(filename,O_RDONLY)) < 0)
return(0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA1Update(&ctx, buffer, (size_t)num);
oerrno = errno;
close(fd);
errno = oerrno;
return(num < 0 ? 0 : SHA1End(&ctx, buf));
}
char *
SHA1Data (data, len, buf)
const u_char *data;
size_t len;
char *buf;
{
SHA1_CTX ctx;
_DIAGASSERT(data != NULL);
/* XXX: buf may be NULL ? */
SHA1Init(&ctx);
SHA1Update(&ctx, data, len);
return(SHA1End(&ctx, buf));
}

View file

@ -0,0 +1,975 @@
/*
* sha2.c
*
* Version 1.0.0beta1
*
* Written by Aaron D. Gifford <me@aarongifford.com>
*
* Copyright 2000 Aaron D. Gifford. 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
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``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(S) OR CONTRIBUTOR(S) 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.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#include <stdio.h>
#include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
#include <assert.h> /* assert() */
#include "sha2.h"
/*
* ASSERT NOTE:
* Some sanity checking code is included using assert(). On my FreeBSD
* system, this additional code can be removed by compiling with NDEBUG
* defined. Check your own systems manpage on assert() to see how to
* compile WITHOUT the sanity checking code on your system.
*
* UNROLLED TRANSFORM LOOP NOTE:
* You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
* loop version for the hash transform rounds (defined using macros
* later in this file). Either define on the command line, for example:
*
* cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
*
* or define below:
*
* #define SHA2_UNROLL_TRANSFORM
*
*/
/*** SHA-256/384/512 Machine Architecture Definitions *****************/
/*
* BYTE_ORDER NOTE:
*
* Please make sure that your system defines BYTE_ORDER. If your
* architecture is little-endian, make sure it also defines
* LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
* equivilent.
*
* If your system does not define the above, then you can do so by
* hand like this:
*
* #define LITTLE_ENDIAN 1234
* #define BIG_ENDIAN 4321
*
* And for little-endian machines, add:
*
* #define BYTE_ORDER LITTLE_ENDIAN
*
* Or for big-endian machines:
*
* #define BYTE_ORDER BIG_ENDIAN
*
* The FreeBSD machine this was written on defines BYTE_ORDER
* appropriately by including <sys/types.h> (which in turn includes
* <machine/endian.h> where the appropriate definitions are actually
* made).
*/
#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
#endif
/*
* Define the followingsha2_* types to types of the correct length on
* the native archtecture. Most BSD systems and Linux define u_intXX_t
* types. Machines with very recent ANSI C headers, can use the
* uintXX_t definintions from inttypes.h by defining USE_INTTYPES_H
* during compile or in the sha.h header file.
*
* Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
* will need to define these three typedefs below (and the appropriate
* ones in sha.h too) by hand according to their system architecture.
*
* Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
* types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
*/
#ifdef USE_INTTYPES_H
typedef uint8_t sha2_byte; /* Exactly 1 byte */
typedef uint32_t sha2_word32; /* Exactly 4 bytes */
typedef uint64_t sha2_word64; /* Exactly 8 bytes */
#else /* USE_INTTYPES_H */
typedef u_int8_t sha2_byte; /* Exactly 1 byte */
typedef u_int32_t sha2_word32; /* Exactly 4 bytes */
typedef u_int64_t sha2_word64; /* Exactly 8 bytes */
#endif /* USE_INTTYPES_H */
/*** SHA-256/384/512 Various Length Definitions ***********************/
/* NOTE: Most of these are in sha2.h */
#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
/*** ENDIAN REVERSAL MACROS *******************************************/
#if BYTE_ORDER == LITTLE_ENDIAN
#define REVERSE32(w,x) { \
sha2_word32 tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
}
#define REVERSE64(w,x) { \
sha2_word64 tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
((tmp & 0x00ff00ff00ff00ffULL) << 8); \
(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
((tmp & 0x0000ffff0000ffffULL) << 16); \
}
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
/*
* Macro for incrementally adding the unsigned 64-bit integer n to the
* unsigned 128-bit integer (represented using a two-element array of
* 64-bit words):
*/
#define ADDINC128(w,n) { \
(w)[0] += (sha2_word64)(n); \
if ((w)[0] < (n)) { \
(w)[1]++; \
} \
}
#if !defined(MEMSET_BZERO) && !defined(MEMCPY_BCOPY)
/*
* Macros for copying blocks of memory and for zeroing out ranges
* of memory. Using these macros makes it easy to switch from
* using memset()/memcpy() and using bzero()/bcopy().
*
* Please define either SHA2_USE_MEMSET_MEMCPY or define
* SHA2_USE_BZERO_BCOPY depending on which function set you
* choose to use:
*/
#if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
/* Default to memset()/memcpy() if no option is specified */
#define SHA2_USE_MEMSET_MEMCPY 1
#endif
#if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
/* Abort with an error if BOTH options are defined */
#error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
#endif
#ifdef SHA2_USE_MEMSET_MEMCPY
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
#endif
#ifdef SHA2_USE_BZERO_BCOPY
#define MEMSET_BZERO(p,l) bzero((p), (l))
#define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
#endif
#endif /* !defined(MEMSET_BZERO) && !defined(MEMCPY_BCOPY) */
/*** THE SIX LOGICAL FUNCTIONS ****************************************/
/*
* Bit shifting and rotation (used by the six SHA-XYZ logical functions:
*
* NOTE: The naming of R and S appears backwards here (R is a SHIFT and
* S is a ROTATION) because the SHA-256/384/512 description document
* (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
* same "backwards" definition.
*/
/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
#define R(b,x) ((x) >> (b))
/* 32-bit Rotate-right (used in SHA-256): */
#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
/* Four of six logical functions used in SHA-256: */
#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
/* Four of six logical functions used in SHA-384 and SHA-512: */
#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
/*** INTERNAL FUNCTION PROTOTYPES *************************************/
/* NOTE: These should not be accessed directly from outside this
* library -- they are intended for private internal visibility/use
* only.
*/
void SHA512_Last(SHA512_CTX*);
void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
/* Hash constant words K for SHA-256: */
const static sha2_word32 K256[64] = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
};
/* Initial hash value H for SHA-256: */
const static sha2_word32 sha256_initial_hash_value[8] = {
0x6a09e667UL,
0xbb67ae85UL,
0x3c6ef372UL,
0xa54ff53aUL,
0x510e527fUL,
0x9b05688cUL,
0x1f83d9abUL,
0x5be0cd19UL
};
/* Hash constant words K for SHA-384 and SHA-512: */
const static sha2_word64 K512[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
};
/* Initial hash value H for SHA-384 */
const static sha2_word64 sha384_initial_hash_value[8] = {
0xcbbb9d5dc1059ed8ULL,
0x629a292a367cd507ULL,
0x9159015a3070dd17ULL,
0x152fecd8f70e5939ULL,
0x67332667ffc00b31ULL,
0x8eb44a8768581511ULL,
0xdb0c2e0d64f98fa7ULL,
0x47b5481dbefa4fa4ULL
};
/* Initial hash value H for SHA-512 */
const static sha2_word64 sha512_initial_hash_value[8] = {
0x6a09e667f3bcc908ULL,
0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL,
0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL,
0x5be0cd19137e2179ULL
};
/*** SHA-256: *********************************************************/
void SHA256_Init(SHA256_CTX* context) {
if (context == (SHA256_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
context->bitcount = 0;
}
#ifdef SHA2_UNROLL_TRANSFORM
/* Unrolled SHA-256 round macros: */
#if BYTE_ORDER == LITTLE_ENDIAN
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
REVERSE32(*data++, W256[j]); \
T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
K256[j] + W256[j]; \
(d) += T1; \
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
#else /* BYTE_ORDER == LITTLE_ENDIAN */
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
K256[j] + (W256[j] = *data++); \
(d) += T1; \
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
#define ROUND256(a,b,c,d,e,f,g,h) \
s0 = W256[(j+1)&0x0f]; \
s0 = sigma0_256(s0); \
s1 = W256[(j+14)&0x0f]; \
s1 = sigma1_256(s1); \
T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
sha2_word32 T1, *W256;
int j;
W256 = (sha2_word32*)context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0;
do {
/* Rounds 0 to 15 (unrolled): */
ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
} while (j < 16);
/* Now for the remaining rounds to 64: */
do {
ROUND256(a,b,c,d,e,f,g,h);
ROUND256(h,a,b,c,d,e,f,g);
ROUND256(g,h,a,b,c,d,e,f);
ROUND256(f,g,h,a,b,c,d,e);
ROUND256(e,f,g,h,a,b,c,d);
ROUND256(d,e,f,g,h,a,b,c);
ROUND256(c,d,e,f,g,h,a,b);
ROUND256(b,c,d,e,f,g,h,a);
} while (j < 64);
/* Compute the current intermediate hash value */
context->state[0] += a;
context->state[1] += b;
context->state[2] += c;
context->state[3] += d;
context->state[4] += e;
context->state[5] += f;
context->state[6] += g;
context->state[7] += h;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
}
#else /* SHA2_UNROLL_TRANSFORM */
void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
sha2_word32 T1, T2, *W256;
int j;
W256 = (sha2_word32*)context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0;
do {
#if BYTE_ORDER == LITTLE_ENDIAN
/* Copy data while converting to host byte order */
REVERSE32(*data++,W256[j]);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
#else /* BYTE_ORDER == LITTLE_ENDIAN */
/* Apply the SHA-256 compression function to update a..h with copy */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 16);
do {
/* Part of the message block expansion: */
s0 = W256[(j+1)&0x0f];
s0 = sigma0_256(s0);
s1 = W256[(j+14)&0x0f];
s1 = sigma1_256(s1);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 64);
/* Compute the current intermediate hash value */
context->state[0] += a;
context->state[1] += b;
context->state[2] += c;
context->state[3] += d;
context->state[4] += e;
context->state[5] += f;
context->state[6] += g;
context->state[7] += h;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
}
#endif /* SHA2_UNROLL_TRANSFORM */
void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
unsigned int freespace, usedspace;
if (len == 0) {
/* Calling with no data is valid - we do nothing */
return;
}
/* Sanity check: */
assert(context != NULL && data != NULL);
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
if (usedspace > 0) {
/* Calculate how much free space is available in the buffer */
freespace = SHA256_BLOCK_LENGTH - usedspace;
if (len >= freespace) {
/* Fill the buffer completely and process it */
MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
context->bitcount += freespace << 3;
len -= freespace;
data += freespace;
SHA256_Transform(context, (sha2_word32*)context->buffer);
} else {
/* The buffer is not yet full */
MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
context->bitcount += len << 3;
/* Clean up: */
usedspace = freespace = 0;
return;
}
}
while (len >= SHA256_BLOCK_LENGTH) {
/* Process as many complete blocks as we can */
SHA256_Transform(context, (const sha2_word32*)data);
context->bitcount += SHA256_BLOCK_LENGTH << 3;
len -= SHA256_BLOCK_LENGTH;
data += SHA256_BLOCK_LENGTH;
}
if (len > 0) {
/* There's left-overs, so save 'em */
MEMCPY_BCOPY(context->buffer, data, len);
context->bitcount += len << 3;
}
/* Clean up: */
usedspace = freespace = 0;
}
void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
sha2_word32 *d = (sha2_word32*)digest;
unsigned int usedspace;
/* Sanity check: */
assert(context != NULL);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (sha2_byte*)0) {
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
#if BYTE_ORDER == LITTLE_ENDIAN
/* Convert FROM host byte order */
REVERSE64(context->bitcount,context->bitcount);
#endif
if (usedspace > 0) {
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace < SHA256_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */
MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA256_BLOCK_LENGTH) {
MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
SHA256_Transform(context, (sha2_word32*)context->buffer);
/* And set-up for the last transform: */
MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
}
} else {
/* Set-up for the last transform: */
MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
}
/* Set the bit count: */
*(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
/* Final transform: */
SHA256_Transform(context, (sha2_word32*)context->buffer);
#if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE32(context->state[j],context->state[j]);
*d++ = context->state[j];
}
}
#else
MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
#endif
}
/* Clean up state data: */
MEMSET_BZERO(context, sizeof(SHA256_CTX));
usedspace = 0;
}
/*** SHA-512: *********************************************************/
void SHA512_Init(SHA512_CTX* context) {
if (context == (SHA512_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
}
#ifdef SHA2_UNROLL_TRANSFORM
/* Unrolled SHA-512 round macros: */
#if BYTE_ORDER == LITTLE_ENDIAN
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
REVERSE64(*data++, W512[j]); \
T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
K512[j] + W512[j]; \
(d) += T1, \
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
j++
#else /* BYTE_ORDER == LITTLE_ENDIAN */
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
K512[j] + (W512[j] = *data++); \
(d) += T1; \
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
j++
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
#define ROUND512(a,b,c,d,e,f,g,h) \
s0 = W512[(j+1)&0x0f]; \
s0 = sigma0_512(s0); \
s1 = W512[(j+14)&0x0f]; \
s1 = sigma1_512(s1); \
T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
j++
void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
int j;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0;
do {
ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
} while (j < 16);
/* Now for the remaining rounds up to 79: */
do {
ROUND512(a,b,c,d,e,f,g,h);
ROUND512(h,a,b,c,d,e,f,g);
ROUND512(g,h,a,b,c,d,e,f);
ROUND512(f,g,h,a,b,c,d,e);
ROUND512(e,f,g,h,a,b,c,d);
ROUND512(d,e,f,g,h,a,b,c);
ROUND512(c,d,e,f,g,h,a,b);
ROUND512(b,c,d,e,f,g,h,a);
} while (j < 80);
/* Compute the current intermediate hash value */
context->state[0] += a;
context->state[1] += b;
context->state[2] += c;
context->state[3] += d;
context->state[4] += e;
context->state[5] += f;
context->state[6] += g;
context->state[7] += h;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
}
#else /* SHA2_UNROLL_TRANSFORM */
void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
int j;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0;
do {
#if BYTE_ORDER == LITTLE_ENDIAN
/* Convert TO host byte order */
REVERSE64(*data++, W512[j]);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
#else /* BYTE_ORDER == LITTLE_ENDIAN */
/* Apply the SHA-512 compression function to update a..h with copy */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 16);
do {
/* Part of the message block expansion: */
s0 = W512[(j+1)&0x0f];
s0 = sigma0_512(s0);
s1 = W512[(j+14)&0x0f];
s1 = sigma1_512(s1);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 80);
/* Compute the current intermediate hash value */
context->state[0] += a;
context->state[1] += b;
context->state[2] += c;
context->state[3] += d;
context->state[4] += e;
context->state[5] += f;
context->state[6] += g;
context->state[7] += h;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
}
#endif /* SHA2_UNROLL_TRANSFORM */
void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
unsigned int freespace, usedspace;
if (len == 0) {
/* Calling with no data is valid - we do nothing */
return;
}
/* Sanity check: */
assert(context != NULL && data != NULL);
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
if (usedspace > 0) {
/* Calculate how much free space is available in the buffer */
freespace = SHA512_BLOCK_LENGTH - usedspace;
if (len >= freespace) {
/* Fill the buffer completely and process it */
MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
ADDINC128(context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
SHA512_Transform(context, (const sha2_word64*)context->buffer);
} else {
/* The buffer is not yet full */
MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
ADDINC128(context->bitcount, len << 3);
/* Clean up: */
usedspace = freespace = 0;
return;
}
}
while (len >= SHA512_BLOCK_LENGTH) {
/* Process as many complete blocks as we can */
SHA512_Transform(context, (const sha2_word64*)data);
ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
len -= SHA512_BLOCK_LENGTH;
data += SHA512_BLOCK_LENGTH;
}
if (len > 0) {
/* There's left-overs, so save 'em */
MEMCPY_BCOPY(context->buffer, data, len);
ADDINC128(context->bitcount, len << 3);
}
/* Clean up: */
usedspace = freespace = 0;
}
void SHA512_Last(SHA512_CTX* context) {
unsigned int usedspace;
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
#if BYTE_ORDER == LITTLE_ENDIAN
/* Convert FROM host byte order */
REVERSE64(context->bitcount[0],context->bitcount[0]);
REVERSE64(context->bitcount[1],context->bitcount[1]);
#endif
if (usedspace > 0) {
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace < SHA512_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */
MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA512_BLOCK_LENGTH) {
MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
SHA512_Transform(context, (const sha2_word64*)context->buffer);
/* And set-up for the last transform: */
MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
}
} else {
/* Prepare for final transform: */
MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
}
/* Store the length of input data (in bits): */
*(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
*(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
/* Final transform: */
SHA512_Transform(context, (const sha2_word64*)context->buffer);
}
void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
assert(context != NULL);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (sha2_byte*)0) {
SHA512_Last(context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE64(context->state[j],context->state[j]);
*d++ = context->state[j];
}
}
#else
MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
#endif
}
/* Zero out state data */
MEMSET_BZERO(context, sizeof(SHA512_CTX));
}
/*** SHA-384: *********************************************************/
void SHA384_Init(SHA384_CTX* context) {
if (context == (SHA384_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
}
void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
SHA512_Update((SHA512_CTX*)context, data, len);
}
void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
assert(context != NULL);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (sha2_byte*)0) {
SHA512_Last((SHA512_CTX*)context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 6; j++) {
REVERSE64(context->state[j],context->state[j]);
*d++ = context->state[j];
}
}
#else
MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
#endif
}
/* Zero out state data */
MEMSET_BZERO(context, sizeof(SHA384_CTX));
}

View file

@ -0,0 +1,151 @@
/*
* sha2.h
*
* Version 1.0.0beta1
*
* Written by Aaron D. Gifford <me@aarongifford.com>
*
* Copyright 2000 Aaron D. Gifford. 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
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``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(S) OR CONTRIBUTOR(S) 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.
*
*/
#ifndef __SHA2_H__
#define __SHA2_H__
#ifdef __cplusplus
extern "C" {
#endif
/*** SHA-256/384/512 Various Length Definitions ***********************/
#define SHA256_BLOCK_LENGTH 64
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA384_BLOCK_LENGTH 128
#define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_BLOCK_LENGTH 128
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
/*** SHA-256/384/512 Context Structures *******************************/
typedef struct _SHA256_CTX {
uint32_t state[8];
uint64_t bitcount;
uint8_t buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX {
uint64_t state[8];
uint64_t bitcount[2];
uint8_t buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;
typedef SHA512_CTX SHA384_CTX;
/*** SHA-256/384/512 Function Prototypes ******************************/
#ifndef NOPROTO
#ifdef SHA2_USE_INTTYPES_H
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const uint8_t*, size_t, uint8_t *);
char *SHA256_File(char *, char *);
void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
char *SHA384_File(char *, char *);
void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
char *SHA512_File(char *, char *);
#else /* SHA2_USE_INTTYPES_H */
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, u_char *);
char* SHA256_Data(const u_int8_t*, size_t, u_char *);
char *SHA256_File(char *, char *);
void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
char *SHA384_File(char *, char *);
void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
char *SHA512_File(char *, char *);
#endif /* SHA2_USE_INTTYPES_H */
#else /* NOPROTO */
void SHA256_Init();
void SHA256_Update();
void SHA256_Final();
char* SHA256_End();
char* SHA256_Data();
char *SHA256_File();
void SHA384_Init();
void SHA384_Update();
void SHA384_Final();
char* SHA384_End();
char* SHA384_Data();
char *SHA384_File();
void SHA512_Init();
void SHA512_Update();
void SHA512_Final();
char* SHA512_End();
char* SHA512_Data();
char *SHA512_File();
#endif /* NOPROTO */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __SHA2_H__ */

View file

@ -0,0 +1,262 @@
/* $NetBSD: sha2hl.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $ */
/*
* sha2hl.c
* This code includes some functions taken from sha2.c, hence the
* following licence reproduction.
*
* This code is not a verbatim copy, since some routines have been added,
* and some bugs have been fixed.
*
* Version 1.0.0beta1
*
* Written by Aaron D. Gifford <me@aarongifford.com>
*
* Copyright 2000 Aaron D. Gifford. 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
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``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(S) OR CONTRIBUTOR(S) 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.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <nbtypes.h>
#ifndef lint
__RCSID("$NetBSD: sha2hl.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $");
#endif /* not lint */
/* #include "namespace.h" */
#include <assert.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <sha2.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
#ifndef MEMSET_BZERO
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
#endif
/*
* Constant used by SHA256/384/512_End() functions for converting the
* digest to a readable hexadecimal character string:
*/
static const char sha2_hex_digits[] = "0123456789abcdef";
char *
SHA256_File(char *filename, char *buf)
{
u_char buffer[BUFSIZ * 20];
SHA256_CTX ctx;
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA256_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return (0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA256_Update(&ctx, buffer, (size_t) num);
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0 ? 0 : SHA256_End(&ctx, buf));
}
char *
SHA256_End(SHA256_CTX *ctx, u_char *buffer)
{
u_char digest[SHA256_DIGEST_LENGTH], *d = digest;
u_char *ret;
int i;
/* Sanity check: */
assert(ctx != NULL);
if ((ret = buffer) != NULL) {
SHA256_Final(digest, ctx);
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
*buffer++ = sha2_hex_digits[*d & 0x0f];
d++;
}
*buffer = (char) 0;
} else {
(void) MEMSET_BZERO(ctx, sizeof(SHA256_CTX));
}
(void) MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
return ret;
}
char *
SHA256_Data(const u_char * data, size_t len, u_char *digest)
{
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, data, len);
return SHA256_End(&ctx, digest);
}
char *
SHA384_File(char *filename, char *buf)
{
SHA384_CTX ctx;
u_char buffer[BUFSIZ * 20];
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA384_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return (0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA384_Update(&ctx, buffer, (size_t) num);
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0 ? 0 : SHA384_End(&ctx, buf));
}
char *
SHA384_End(SHA384_CTX * ctx, char buffer[])
{
u_char digest[SHA384_DIGEST_LENGTH], *d = digest;
u_char *ret;
int i;
/* Sanity check: */
assert(ctx != NULL);
if ((ret = buffer) != NULL) {
SHA384_Final(digest, ctx);
for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
*buffer++ = sha2_hex_digits[*d & 0x0f];
d++;
}
*buffer = (char) 0;
} else {
(void) MEMSET_BZERO(ctx, sizeof(SHA384_CTX));
}
(void) MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
return ret;
}
char *
SHA384_Data(const u_char * data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH])
{
SHA384_CTX ctx;
SHA384_Init(&ctx);
SHA384_Update(&ctx, data, len);
return SHA384_End(&ctx, digest);
}
char *
SHA512_File(char *filename, char *buf)
{
SHA512_CTX ctx;
u_char buffer[BUFSIZ * 20];
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA512_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return (0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA512_Update(&ctx, buffer, (size_t) num);
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0 ? 0 : SHA512_End(&ctx, buf));
}
char *
SHA512_End(SHA512_CTX * ctx, char buffer[])
{
u_char digest[SHA512_DIGEST_LENGTH], *d = digest;
u_char *ret;
int i;
/* Sanity check: */
assert(ctx != NULL);
if ((ret = buffer) != NULL) {
SHA512_Final(digest, ctx);
for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
*buffer++ = sha2_hex_digits[*d & 0x0f];
d++;
}
*buffer = (char) 0;
} else {
(void) MEMSET_BZERO(ctx, sizeof(SHA512_CTX));
}
(void) MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
return ret;
}
char *
SHA512_Data(const u_char * data, size_t len, char *digest)
{
SHA512_CTX ctx;
SHA512_Init(&ctx);
SHA512_Update(&ctx, data, len);
return SHA512_End(&ctx, digest);
}

View file

@ -0,0 +1,120 @@
/* $Id: sl_init.c,v 1.1.1.1 2003/03/31 05:03:02 grant Exp $ */
/* $NetBSD: sl_init.c,v 1.1.1.1 2003/03/31 05:03:02 grant Exp $ */
/*-
* Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
#define _SL_CHUNKSIZE 20
/*
* sl_init(): Initialize a string list
*/
StringList *
sl_init(void)
{
StringList *sl;
sl = malloc(sizeof(StringList));
if (sl == NULL)
return (NULL);
sl->sl_cur = 0;
sl->sl_max = _SL_CHUNKSIZE;
sl->sl_str = malloc(sl->sl_max * sizeof(char *));
if (sl->sl_str == NULL) {
free(sl);
sl = NULL;
}
return (sl);
}
/*
* sl_add(): Add an item to the string list
*/
int
sl_add(StringList *sl, char *name)
{
if (sl->sl_cur == sl->sl_max - 1) {
char **new;
sl->sl_max += _SL_CHUNKSIZE;
new = (char **)realloc(sl->sl_str, sl->sl_max * sizeof(char *));
if (new == NULL)
return (-1);
sl->sl_str = new;
}
sl->sl_str[sl->sl_cur++] = name;
return (0);
}
/*
* sl_free(): Free a stringlist
*/
void
sl_free(StringList *sl, int all)
{
size_t i;
if (sl == NULL)
return;
if (sl->sl_str) {
if (all)
for (i = 0; i < sl->sl_cur; i++)
free(sl->sl_str[i]);
free(sl->sl_str);
}
free(sl);
}
/*
* sl_find(): Find a name in the string list
*/
char *
sl_find(StringList *sl, char *name)
{
size_t i;
for (i = 0; i < sl->sl_cur; i++)
if (strcmp(sl->sl_str[i], name) == 0)
return sl->sl_str[i];
return (NULL);
}

View file

@ -0,0 +1,788 @@
/* $Id: snprintf.c,v 1.1.1.1 2003/03/31 05:03:03 grant Exp $ */
/*
* 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
*/
/**************************************************************
* Original:
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
* A bombproof version of doprnt (dopr) included.
* Sigh. This sort of thing is always nasty do deal with. Note that
* the version here does not include floating point...
*
* snprintf() is used instead of sprintf() as it does limit checks
* for string length. This covers a nasty loophole.
*
* The other functions are there to prevent NULL pointers from
* causing nast effects.
*
* More Recently:
* Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
* This was ugly. It is still ugly. I opted out of floating point
* numbers, but the formatter understands just about everything
* from the normal C string format, at least as far as I can tell from
* the Solaris 2.5 printf(3S) man page.
*
* Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
* Ok, added some minimal floating point support, which means this
* probably requires libm on most operating systems. Don't yet
* support the exponent (e,E) and sigfig (g,G). Also, fmtint()
* was pretty badly broken, it just wasn't being exercised in ways
* which showed it, so that's been fixed. Also, formated the code
* to mutt conventions, and removed dead code left over from the
* original. Also, there is now a builtin-test, just compile with:
* gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
* and run snprintf for results.
*
* Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
* The PGP code was using unsigned hexadecimal formats.
* Unfortunately, unsigned formats simply didn't work.
*
* Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
* The original code assumed that both snprintf() and vsnprintf() were
* missing. Some systems only have snprintf() but not vsnprintf(), so
* the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
*
* Andrew Tridgell (tridge@samba.org) Oct 1998
* fixed handling of %.0f
* added test for HAVE_LONG_DOUBLE
*
* Luke Mewburn <lukem@netbsd.org>, Thu Sep 30 23:28:21 EST 1999
* cleaned up formatting, autoconf tests
* added long long support
*
**************************************************************/
#include "nbcompat.h"
#if HAVE_LONG_DOUBLE
#define LDOUBLE long double
#else
#define LDOUBLE double
#endif
#if HAVE_LONG_LONG
#define LLONG long long
#else
#define LLONG long
#endif
static void dopr(char *buffer, size_t maxlen, size_t *retlen,
const char *format, va_list args);
static void fmtstr(char *buffer, size_t * currlen, size_t maxlen,
char *value, int min, int max, int flags);
static void fmtint(char *buffer, size_t * currlen, size_t maxlen,
LLONG value, int base, int min, int max, int flags);
static void fmtfp(char *buffer, size_t * currlen, size_t maxlen,
LDOUBLE fvalue, int min, int max, int flags);
static void dopr_outch(char *buffer, size_t * currlen, size_t maxlen, int c);
/*
* dopr(): poor man's version of doprintf
*/
/* format read states */
#define DP_S_DEFAULT 0
#define DP_S_FLAGS 1
#define DP_S_MIN 2
#define DP_S_DOT 3
#define DP_S_MAX 4
#define DP_S_MOD 5
#define DP_S_CONV 6
#define DP_S_DONE 7
/* format flags - Bits */
#define DP_F_MINUS (1 << 0)
#define DP_F_PLUS (1 << 1)
#define DP_F_SPACE (1 << 2)
#define DP_F_NUM (1 << 3)
#define DP_F_ZERO (1 << 4)
#define DP_F_UP (1 << 5)
#define DP_F_UNSIGNED (1 << 6)
/* Conversion Flags */
#define DP_C_SHORT 1
#define DP_C_LONG 2
#define DP_C_LDOUBLE 3
#define DP_C_LLONG 4
#define char_to_int(p) (p - '0')
static void
dopr(char *buffer, size_t maxlen, size_t *retlen, const char *format,
va_list args)
{
char ch;
LLONG value;
LDOUBLE fvalue;
char *strvalue;
int min;
int max;
int state;
int flags;
int cflags;
size_t currlen;
state = DP_S_DEFAULT;
flags = currlen = cflags = min = 0;
max = -1;
ch = *format++;
while (state != DP_S_DONE) {
if ((ch == '\0') || (currlen >= maxlen))
state = DP_S_DONE;
switch (state) {
case DP_S_DEFAULT:
if (ch == '%')
state = DP_S_FLAGS;
else
dopr_outch(buffer, &currlen, maxlen, ch);
ch = *format++;
break;
case DP_S_FLAGS:
switch (ch) {
case '-':
flags |= DP_F_MINUS;
ch = *format++;
break;
case '+':
flags |= DP_F_PLUS;
ch = *format++;
break;
case ' ':
flags |= DP_F_SPACE;
ch = *format++;
break;
case '#':
flags |= DP_F_NUM;
ch = *format++;
break;
case '0':
flags |= DP_F_ZERO;
ch = *format++;
break;
default:
state = DP_S_MIN;
break;
}
break;
case DP_S_MIN:
if (isdigit((unsigned char) ch)) {
min = 10 * min + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
min = va_arg(args, int);
ch = *format++;
state = DP_S_DOT;
} else
state = DP_S_DOT;
break;
case DP_S_DOT:
if (ch == '.') {
state = DP_S_MAX;
ch = *format++;
} else
state = DP_S_MOD;
break;
case DP_S_MAX:
if (isdigit((unsigned char) ch)) {
if (max < 0)
max = 0;
max = 10 * max + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
max = va_arg(args, int);
ch = *format++;
state = DP_S_MOD;
} else
state = DP_S_MOD;
break;
case DP_S_MOD:
switch (ch) {
case 'h':
cflags = DP_C_SHORT;
ch = *format++;
break;
case 'l':
if (*format == 'l') {
cflags = DP_C_LLONG;
format++;
} else
cflags = DP_C_LONG;
ch = *format++;
break;
case 'q':
cflags = DP_C_LLONG;
ch = *format++;
break;
case 'L':
cflags = DP_C_LDOUBLE;
ch = *format++;
break;
default:
break;
}
state = DP_S_CONV;
break;
case DP_S_CONV:
switch (ch) {
case 'd':
case 'i':
switch (cflags) {
case DP_C_SHORT:
value = va_arg(args, int);
break;
case DP_C_LONG:
value = va_arg(args, long int);
break;
case DP_C_LLONG:
value = va_arg(args, LLONG);
break;
default:
value = va_arg(args, int);
break;
}
fmtint(buffer, &currlen, maxlen, value, 10,
min, max, flags);
break;
case 'X':
flags |= DP_F_UP;
/* FALLTHROUGH */
case 'x':
case 'o':
case 'u':
flags |= DP_F_UNSIGNED;
switch (cflags) {
case DP_C_SHORT:
value = va_arg(args, unsigned int);
break;
case DP_C_LONG:
value = (LLONG) va_arg(args,
unsigned long int);
break;
case DP_C_LLONG:
value = va_arg(args, unsigned LLONG);
break;
default:
value = (LLONG) va_arg(args,
unsigned int);
break;
}
fmtint(buffer, &currlen, maxlen, value,
ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
min, max, flags);
break;
case 'f':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
/* um, floating point? */
fmtfp(buffer, &currlen, maxlen, fvalue, min,
max, flags);
break;
case 'E':
flags |= DP_F_UP;
case 'e':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
break;
case 'G':
flags |= DP_F_UP;
case 'g':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
break;
case 'c':
dopr_outch(buffer, &currlen, maxlen,
va_arg(args, int));
break;
case 's':
strvalue = va_arg(args, char *);
if (max < 0)
max = maxlen; /* ie, no max */
fmtstr(buffer, &currlen, maxlen, strvalue,
min, max, flags);
break;
case 'p':
value = (long)va_arg(args, void *);
fmtint(buffer, &currlen, maxlen,
value, 16, min, max, flags);
break;
case 'n':
/* XXX */
if (cflags == DP_C_SHORT) {
short int *num;
num = va_arg(args, short int *);
*num = currlen;
} else if (cflags == DP_C_LONG) { /* XXX */
long int *num;
num = va_arg(args, long int *);
*num = (long int) currlen;
} else if (cflags == DP_C_LLONG) { /* XXX */
LLONG *num;
num = va_arg(args, LLONG *);
*num = (LLONG) currlen;
} else {
int *num;
num = va_arg(args, int *);
*num = currlen;
}
break;
case '%':
dopr_outch(buffer, &currlen, maxlen, ch);
break;
case 'w':
/* not supported yet, treat as next char */
ch = *format++;
break;
default:
/* Unknown, skip */
break;
}
ch = *format++;
state = DP_S_DEFAULT;
flags = cflags = min = 0;
max = -1;
break;
case DP_S_DONE:
break;
default:
/* hmm? */
break; /* some picky compilers need this */
}
}
if (currlen >= maxlen - 1)
currlen = maxlen - 1;
buffer[currlen] = '\0';
*retlen = currlen;
}
static void
fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value,
int min, int max, int flags)
{
int padlen, strln; /* amount to pad */
int cnt = 0;
if (value == 0) {
value = "<NULL>";
}
for (strln = 0; value[strln]; ++strln)
; /* strlen */
padlen = min - strln;
if (padlen < 0)
padlen = 0;
if (flags & DP_F_MINUS)
padlen = -padlen; /* Left Justify */
while ((padlen > 0) && (cnt < max)) {
dopr_outch(buffer, currlen, maxlen, ' ');
--padlen;
++cnt;
}
while (*value && (cnt < max)) {
dopr_outch(buffer, currlen, maxlen, *value++);
++cnt;
}
while ((padlen < 0) && (cnt < max)) {
dopr_outch(buffer, currlen, maxlen, ' ');
++padlen;
++cnt;
}
}
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
static void
fmtint(char *buffer, size_t *currlen, size_t maxlen, LLONG value, int base,
int min, int max, int flags)
{
int signvalue = 0;
unsigned LLONG uvalue;
char convert[20];
int place = 0;
int spadlen = 0; /* amount to space pad */
int zpadlen = 0; /* amount to zero pad */
int caps = 0;
if (max < 0)
max = 0;
uvalue = value;
if (!(flags & DP_F_UNSIGNED)) {
if (value < 0) {
signvalue = '-';
uvalue = -value;
} else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
signvalue = '+';
else if (flags & DP_F_SPACE)
signvalue = ' ';
}
if (flags & DP_F_UP)
caps = 1; /* Should characters be upper case? */
do {
convert[place++] =
(caps ? "0123456789ABCDEF" : "0123456789abcdef")
[uvalue % (unsigned) base];
uvalue = (uvalue / (unsigned) base);
} while (uvalue && (place < 20));
if (place == 20)
place--;
convert[place] = 0;
zpadlen = max - place;
spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
if (zpadlen < 0)
zpadlen = 0;
if (spadlen < 0)
spadlen = 0;
if (flags & DP_F_ZERO) {
zpadlen = MAX(zpadlen, spadlen);
spadlen = 0;
}
if (flags & DP_F_MINUS)
spadlen = -spadlen; /* Left Justifty */
#ifdef DEBUG_SNPRINTF
printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
zpadlen, spadlen, min, max, place);
#endif
/* Spaces */
while (spadlen > 0) {
dopr_outch(buffer, currlen, maxlen, ' ');
--spadlen;
}
/* Sign */
if (signvalue)
dopr_outch(buffer, currlen, maxlen, signvalue);
/* Zeros */
if (zpadlen > 0) {
while (zpadlen > 0) {
dopr_outch(buffer, currlen, maxlen, '0');
--zpadlen;
}
}
/* Digits */
while (place > 0)
dopr_outch(buffer, currlen, maxlen, convert[--place]);
/* Left Justified spaces */
while (spadlen < 0) {
dopr_outch(buffer, currlen, maxlen, ' ');
++spadlen;
}
}
static LDOUBLE
abs_val(LDOUBLE value)
{
LDOUBLE result = value;
if (value < 0)
result = -value;
return result;
}
static LDOUBLE
pow10(int exp)
{
LDOUBLE result = 1;
while (exp) {
result *= 10;
exp--;
}
return result;
}
static long
round(LDOUBLE value)
{
long intpart;
intpart = (long) value;
value = value - intpart;
if (value >= 0.5)
intpart++;
return intpart;
}
static void
fmtfp(char *buffer, size_t *currlen, size_t maxlen, LDOUBLE fvalue,
int min, int max, int flags)
{
int signvalue = 0;
LDOUBLE ufvalue;
char iconvert[20];
char fconvert[20];
int iplace = 0;
int fplace = 0;
int padlen = 0; /* amount to pad */
int zpadlen = 0;
int caps = 0;
long intpart;
long fracpart;
/* 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;
ufvalue = abs_val(fvalue);
if (fvalue < 0)
signvalue = '-';
else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
signvalue = '+';
else if (flags & DP_F_SPACE)
signvalue = ' ';
#if 0
if (flags & DP_F_UP)
caps = 1; /* Should characters be upper case? */
#endif
intpart = (long) ufvalue;
/* 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 */
fracpart = round((pow10(max)) * (ufvalue - intpart));
if (fracpart >= pow10(max)) {
intpart++;
fracpart -= pow10(max);
}
#ifdef DEBUG_SNPRINTF
printf("fmtfp: %g %d.%d min=%d max=%d\n",
(double) fvalue, intpart, fracpart, min, max);
#endif
/* Convert integer part */
do {
iconvert[iplace++] =
(caps ? "0123456789ABCDEF"
: "0123456789abcdef")[intpart % 10];
intpart = (intpart / 10);
} while (intpart && (iplace < 20));
if (iplace == 20)
iplace--;
iconvert[iplace] = 0;
/* Convert fractional part */
do {
fconvert[fplace++] =
(caps ? "0123456789ABCDEF"
: "0123456789abcdef")[fracpart % 10];
fracpart = (fracpart / 10);
} while (fracpart && (fplace < 20));
if (fplace == 20)
fplace--;
fconvert[fplace] = 0;
/* -1 for decimal point, another -1 if we are printing a sign */
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
zpadlen = max - fplace;
if (zpadlen < 0)
zpadlen = 0;
if (padlen < 0)
padlen = 0;
if (flags & DP_F_MINUS)
padlen = -padlen; /* Left Justifty */
if ((flags & DP_F_ZERO) && (padlen > 0)) {
if (signvalue) {
dopr_outch(buffer, currlen, maxlen, signvalue);
--padlen;
signvalue = 0;
}
while (padlen > 0) {
dopr_outch(buffer, currlen, maxlen, '0');
--padlen;
}
}
while (padlen > 0) {
dopr_outch(buffer, currlen, maxlen, ' ');
--padlen;
}
if (signvalue)
dopr_outch(buffer, currlen, maxlen, signvalue);
while (iplace > 0)
dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
#ifdef DEBUG_SNPRINTF
printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
#endif
/*
* Decimal point. This should probably use locale to find the correct
* char to print out.
*/
if (max > 0) {
dopr_outch(buffer, currlen, maxlen, '.');
while (fplace > 0)
dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
}
while (zpadlen > 0) {
dopr_outch(buffer, currlen, maxlen, '0');
--zpadlen;
}
while (padlen < 0) {
dopr_outch(buffer, currlen, maxlen, ' ');
++padlen;
}
}
static void
dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c)
{
if (*currlen < maxlen)
buffer[(*currlen)++] = (char)c;
}
int
vsnprintf(char *str, size_t count, const char *fmt, va_list args)
{
size_t retlen;
str[0] = 0;
dopr(str, count, &retlen, fmt, args);
return (retlen);
}
/* VARARGS3 */
int
snprintf(char *str, size_t count, const char *fmt, ...)
{
va_list ap;
int rv;
va_start(ap, fmt);
rv = vsnprintf(str, count, fmt, ap);
va_end(ap);
return (rv);
}
#ifdef TEST_SNPRINTF
#ifndef LONG_STRING
#define LONG_STRING 1024
#endif
int
main(int argc, char *argv[])
{
char buf1[LONG_STRING];
char buf2[LONG_STRING];
char *fp_fmt[] = {
"%-1.5f",
"%1.5f",
"%123.9f",
"%10.5f",
"% 10.5f",
"%+22.9f",
"%+4.9f",
"%01.3f",
"%4f",
"%3.1f",
"%3.2f",
"%.0f",
"%.1f",
NULL
};
double fp_nums[] = {
-1.5, 134.21, 91340.2, 341.1234, 0203.9,
0.96, 0.996, 0.9996, 1.996, 4.136,
0
};
char *int_fmt[] = {
"%-1.5d",
"%1.5d",
"%123.9d",
"%5.5d",
"%10.5d",
"% 10.5d",
"%+22.33d",
"%01.3d",
"%4d",
#if HAVE_LONG_LONG
"%12lld",
#endif
NULL
};
LLONG int_nums[] = {
-1, 134, 91340, 341, 0203,
4294967290,
4294967297,
0
};
int x, y;
int fail = 0;
int num = 0;
int len;
printf("Testing snprintf format codes against system sprintf...\n");
for (x = 0; fp_fmt[x] != NULL; x++) {
printf("testing %s\n", fp_fmt[x]);
for (y = 0; fp_nums[y] != 0; y++) {
snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
sprintf(buf2, fp_fmt[x], fp_nums[y]);
if (strcmp(buf1, buf2)) {
printf("snprintf doesn't match Format: %s\n",
fp_fmt[x]);
printf("\tsnprintf = %s\n\tsprintf = %s\n",
buf1, buf2);
fail++;
}
num++;
}
}
for (x = 0; int_fmt[x] != NULL; x++) {
printf("testing %s\n", int_fmt[x]);
for (y = 0; int_nums[y] != 0; y++) {
len = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
printf("got %d >%s< (%d)\n", len, buf1, (int)strlen(buf1));
sprintf(buf2, int_fmt[x], int_nums[y]);
if (strcmp(buf1, buf2)) {
printf("snprintf doesn't match Format: %s\n",
int_fmt[x]);
printf("\tsnprintf = %s\n\tsprintf = %s\n",
buf1, buf2);
fail++;
}
num++;
}
}
printf("%d tests failed out of %d.\n", fail, num);
exit(0);
}
#endif /* TEST_SNPRINTF */

View file

@ -0,0 +1,593 @@
/* $NetBSD: spec.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/*-
* Copyright (c) 1989, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
/*-
* Copyright (c) 2001-2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn of Wasabi Systems.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(__RCSID) && !defined(lint)
#if 0
static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: spec.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include "extern.h"
#include "pack_dev.h"
size_t mtree_lineno; /* Current spec line number */
int Wflag; /* Don't "whack" permissions */
static dev_t parsedev(char *);
static void replacenode(NODE *, NODE *);
static void set(char *, NODE *);
static void unset(char *, NODE *);
NODE *
spec(FILE *fp)
{
NODE *centry, *last, *pathparent, *cur;
char *p, *e, *next;
NODE ginfo, *root;
char *buf, *tname;
size_t tnamelen, plen;
root = NULL;
centry = last = NULL;
tname = NULL;
tnamelen = 0;
memset(&ginfo, 0, sizeof(ginfo));
for (mtree_lineno = 0;
(buf = fparseln(fp, NULL, &mtree_lineno, NULL,
FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
free(buf)) {
/* Skip leading whitespace. */
for (p = buf; *p && isspace((unsigned char)*p); ++p)
continue;
/* If nothing but whitespace, continue. */
if (!*p)
continue;
#ifdef DEBUG
fprintf(stderr, "line %lu: {%s}\n",
(u_long)mtree_lineno, p);
#endif
/* Grab file name, "$", "set", or "unset". */
next = buf;
while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
continue;
if (p == NULL)
mtree_err("missing field");
if (p[0] == '/') {
if (strcmp(p + 1, "set") == 0)
set(next, &ginfo);
else if (strcmp(p + 1, "unset") == 0)
unset(next, &ginfo);
else
mtree_err("invalid specification `%s'", p);
continue;
}
if (strcmp(p, "..") == 0) {
/* Don't go up, if haven't gone down. */
if (root == NULL)
goto noparent;
if (last->type != F_DIR || last->flags & F_DONE) {
if (last == root)
goto noparent;
last = last->parent;
}
last->flags |= F_DONE;
continue;
noparent: mtree_err("no parent node");
}
plen = strlen(p) + 1;
if (plen > tnamelen) {
tnamelen = plen;
if ((tname = realloc(tname, tnamelen)) == NULL)
mtree_err("realloc: %s", strerror(errno));
}
if (strunvis(tname, p) == -1)
mtree_err("strunvis failed on `%s'", p);
p = tname;
pathparent = NULL;
if (strchr(p, '/') != NULL) {
cur = root;
for (; (e = strchr(p, '/')) != NULL; p = e+1) {
if (p == e)
continue; /* handle // */
*e = '\0';
if (strcmp(p, ".") != 0) {
while (cur &&
strcmp(cur->name, p) != 0) {
cur = cur->next;
}
}
if (cur == NULL || cur->type != F_DIR) {
mtree_err("%s: %s", tname,
strerror(ENOENT));
}
*e = '/';
pathparent = cur;
cur = cur->child;
}
if (*p == '\0')
mtree_err("%s: empty leaf element", tname);
}
if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
mtree_err("%s", strerror(errno));
*centry = ginfo;
centry->lineno = mtree_lineno;
strcpy(centry->name, p);
#define MAGIC "?*["
if (strpbrk(p, MAGIC))
centry->flags |= F_MAGIC;
set(next, centry);
if (root == NULL) {
/*
* empty tree
*/
if (strcmp(centry->name, ".") != 0 ||
centry->type != F_DIR)
mtree_err(
"root node must be the directory `.'");
last = root = centry;
root->parent = root;
} else if (pathparent != NULL) {
/*
* full path entry
*/
centry->parent = pathparent;
cur = pathparent->child;
if (cur == NULL) {
pathparent->child = centry;
last = centry;
} else {
for (; cur != NULL; cur = cur->next) {
if (strcmp(cur->name, centry->name)
== 0) {
/* existing entry; replace */
replacenode(cur, centry);
break;
}
if (cur->next == NULL) {
/* last entry; add new */
cur->next = centry;
centry->prev = cur;
break;
}
}
last = cur;
while (last->next != NULL)
last = last->next;
}
} else if (strcmp(centry->name, ".") == 0) {
/*
* duplicate "." entry; always replace
*/
replacenode(root, centry);
} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
/*
* new relative child
* (no duplicate check)
*/
centry->parent = last;
last = last->child = centry;
} else {
/*
* relative entry, up one directory
* (no duplicate check)
*/
centry->parent = last->parent;
centry->prev = last;
last = last->next = centry;
}
}
return (root);
}
/*
* dump_nodes --
* dump the NODEs from `cur', based in the directory `dir'.
* if pathlast is none zero, print the path last, otherwise print
* it first.
*/
void
dump_nodes(const char *dir, NODE *root, int pathlast)
{
NODE *cur;
char path[MAXPATHLEN];
const char *name;
for (cur = root; cur != NULL; cur = cur->next) {
if (cur->type != F_DIR && !matchtags(cur))
continue;
if (snprintf(path, sizeof(path), "%s%s%s",
dir, *dir ? "/" : "", cur->name)
>= sizeof(path))
mtree_err("Pathname too long.");
if (!pathlast)
printf("%s ", vispath(path));
#define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
if (MATCHFLAG(F_TYPE))
printf("type=%s ", nodetype(cur->type));
if (MATCHFLAG(F_UID | F_UNAME)) {
if (keys & F_UNAME &&
(name = user_from_uid(cur->st_uid, 1)) != NULL)
printf("uname=%s ", name);
else
printf("uid=%u ", cur->st_uid);
}
if (MATCHFLAG(F_GID | F_GNAME)) {
if (keys & F_GNAME &&
(name = group_from_gid(cur->st_gid, 1)) != NULL)
printf("gname=%s ", name);
else
printf("gid=%u ", cur->st_gid);
}
if (MATCHFLAG(F_MODE))
printf("mode=%#o ", cur->st_mode);
if (MATCHFLAG(F_DEV) &&
(cur->type == F_BLOCK || cur->type == F_CHAR))
printf("device=%#x ", cur->st_rdev);
if (MATCHFLAG(F_NLINK))
printf("nlink=%d ", cur->st_nlink);
if (MATCHFLAG(F_SLINK))
printf("link=%s ", cur->slink);
if (MATCHFLAG(F_SIZE))
printf("size=%lld ", (long long)cur->st_size);
if (MATCHFLAG(F_TIME))
printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
cur->st_mtimespec.tv_nsec);
if (MATCHFLAG(F_CKSUM))
printf("cksum=%lu ", cur->cksum);
if (MATCHFLAG(F_MD5))
printf("md5=%s ", cur->md5digest);
if (MATCHFLAG(F_RMD160))
printf("rmd160=%s ", cur->rmd160digest);
if (MATCHFLAG(F_SHA1))
printf("sha1=%s ", cur->sha1digest);
if (MATCHFLAG(F_FLAGS))
printf("flags=%s ",
flags_to_string(cur->st_flags, "none"));
if (MATCHFLAG(F_IGN))
printf("ignore ");
if (MATCHFLAG(F_OPT))
printf("optional ");
if (MATCHFLAG(F_TAGS))
printf("tags=%s ", cur->tags);
puts(pathlast ? vispath(path) : "");
if (cur->child)
dump_nodes(path, cur->child, pathlast);
}
}
/*
* vispath --
* strsvis(3) encodes path, which must not be longer than MAXPATHLEN
* characters long, and returns a pointer to a static buffer containing
* the result.
*/
char *
vispath(const char *path)
{
const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
static char pathbuf[4*MAXPATHLEN + 1];
strsvis(pathbuf, path, VIS_CSTYLE, extra);
return(pathbuf);
}
static dev_t
parsedev(char *arg)
{
#define MAX_PACK_ARGS 3
u_long numbers[MAX_PACK_ARGS];
char *p, *ep, *dev;
int argc;
pack_t *pack;
dev_t result;
if ((dev = strchr(arg, ',')) != NULL) {
*dev++='\0';
if ((pack = pack_find(arg)) == NULL)
mtree_err("unknown format `%s'", arg);
argc = 0;
while ((p = strsep(&dev, ",")) != NULL) {
if (*p == '\0')
mtree_err("missing number");
numbers[argc++] = strtoul(p, &ep, 0);
if (*ep != '\0')
mtree_err("invalid number `%s'",
p);
if (argc > MAX_PACK_ARGS)
mtree_err("too many arguments");
}
if (argc < 2)
mtree_err("not enough arguments");
result = (*pack)(argc, numbers);
} else {
result = (dev_t)strtoul(arg, &ep, 0);
if (*ep != '\0')
mtree_err("invalid device `%s'", arg);
}
return (result);
}
static void
replacenode(NODE *cur, NODE *new)
{
if (cur->type != new->type)
mtree_err("existing entry type `%s' does not match type `%s'",
nodetype(cur->type), nodetype(new->type));
#define REPLACE(x) cur->x = new->x
#define REPLACESTR(x) if (cur->x) free(cur->x); cur->x = new->x
REPLACE(st_size);
REPLACE(st_mtimespec);
REPLACESTR(slink);
REPLACE(st_uid);
REPLACE(st_gid);
REPLACE(st_mode);
REPLACE(st_rdev);
REPLACE(st_flags);
REPLACE(st_nlink);
REPLACE(cksum);
REPLACESTR(md5digest);
REPLACESTR(rmd160digest);
REPLACESTR(sha1digest);
REPLACESTR(tags);
REPLACE(lineno);
REPLACE(flags);
free(new);
}
static void
set(char *t, NODE *ip)
{
int type, value, len;
gid_t gid;
uid_t uid;
char *kw, *val, *md, *ep;
void *m;
val = NULL;
while ((kw = strsep(&t, "= \t")) != NULL) {
if (*kw == '\0')
continue;
if (strcmp(kw, "all") == 0)
mtree_err("invalid keyword `all'");
ip->flags |= type = parsekey(kw, &value);
if (value) {
while ((val = strsep(&t, " \t")) != NULL &&
*val == '\0')
continue;
if (val == NULL)
mtree_err("missing value");
}
switch(type) {
case F_CKSUM:
ip->cksum = strtoul(val, &ep, 10);
if (*ep)
mtree_err("invalid checksum `%s'", val);
break;
case F_DEV:
ip->st_rdev = parsedev(val);
break;
case F_FLAGS:
if (strcmp("none", val) == 0)
ip->st_flags = 0;
else if (string_to_flags(&val, &ip->st_flags, NULL)
!= 0)
mtree_err("invalid flag `%s'", val);
break;
case F_GID:
ip->st_gid = (gid_t)strtoul(val, &ep, 10);
if (*ep)
mtree_err("invalid gid `%s'", val);
break;
case F_GNAME:
if (Wflag) /* don't parse if whacking */
break;
if (gid_from_group(val, &gid) == -1)
mtree_err("unknown group `%s'", val);
ip->st_gid = gid;
break;
case F_IGN:
/* just set flag bit */
break;
case F_MD5:
if (val[0]=='0' && val[1]=='x')
md=&val[2];
else
md=val;
if ((ip->md5digest = strdup(md)) == NULL)
mtree_err("memory allocation error");
break;
case F_MODE:
if ((m = setmode(val)) == NULL)
mtree_err("invalid file mode `%s'", val);
ip->st_mode = getmode(m, 0);
free(m);
break;
case F_NLINK:
ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
if (*ep)
mtree_err("invalid link count `%s'", val);
break;
case F_OPT:
/* just set flag bit */
break;
case F_RMD160:
if (val[0]=='0' && val[1]=='x')
md=&val[2];
else
md=val;
if ((ip->rmd160digest = strdup(md)) == NULL)
mtree_err("memory allocation error");
break;
case F_SHA1:
if (val[0]=='0' && val[1]=='x')
md=&val[2];
else
md=val;
if ((ip->sha1digest = strdup(md)) == NULL)
mtree_err("memory allocation error");
break;
case F_SIZE:
ip->st_size = (off_t)strtoll(val, &ep, 10);
if (*ep)
mtree_err("invalid size `%s'", val);
break;
case F_SLINK:
if ((ip->slink = strdup(val)) == NULL)
mtree_err("memory allocation error");
break;
case F_TAGS:
len = strlen(val) + 3; /* "," + str + ",\0" */
if ((ip->tags = malloc(len)) == NULL)
mtree_err("memory allocation error");
snprintf(ip->tags, len, ",%s,", val);
break;
case F_TIME:
ip->st_mtimespec.tv_sec =
(time_t)strtoul(val, &ep, 10);
if (*ep != '.')
mtree_err("invalid time `%s'", val);
val = ep + 1;
ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
if (*ep)
mtree_err("invalid time `%s'", val);
break;
case F_TYPE:
ip->type = parsetype(val);
break;
case F_UID:
ip->st_uid = (uid_t)strtoul(val, &ep, 10);
if (*ep)
mtree_err("invalid uid `%s'", val);
break;
case F_UNAME:
if (Wflag) /* don't parse if whacking */
break;
if (uid_from_user(val, &uid) == -1)
mtree_err("unknown user `%s'", val);
ip->st_uid = uid;
break;
default:
mtree_err(
"set(): unsupported key type 0x%x (INTERNAL ERROR)",
type);
/* NOTREACHED */
}
}
}
static void
unset(char *t, NODE *ip)
{
char *p;
while ((p = strsep(&t, " \t")) != NULL) {
if (*p == '\0')
continue;
ip->flags &= ~parsekey(p, NULL);
}
}

View file

@ -0,0 +1,200 @@
/* $NetBSD: stat_flags.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/*-
* Copyright (c) 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(__RCSID) && !defined(lint)
#if 0
static char sccsid[] = "@(#)stat_flags.c 8.2 (Berkeley) 7/28/94";
#else
__RCSID("$NetBSD: stat_flags.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $");
#endif
#endif /* not lint */
#if HAVE_CONFIG_H
#include "config.h"
#else
#ifdef netbsd
#define HAVE_STRUCT_STAT_ST_FLAGS 1
#endif
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <stddef.h>
#include <string.h>
#include "stat_flags.h"
#define SAPPEND(s) { \
if (prefix != NULL) \
(void)strcat(string, prefix); \
(void)strcat(string, s); \
prefix = ","; \
}
/*
* flags_to_string --
* Convert stat flags to a comma-separated string. If no flags
* are set, return the default string.
*/
char *
flags_to_string(u_long flags, const char *def)
{
static char string[128];
const char *prefix;
string[0] = '\0';
prefix = NULL;
#if HAVE_STRUCT_STAT_ST_FLAGS
if (flags & UF_APPEND)
SAPPEND("uappnd");
if (flags & UF_IMMUTABLE)
SAPPEND("uchg");
if (flags & UF_NODUMP)
SAPPEND("nodump");
if (flags & UF_OPAQUE)
SAPPEND("opaque");
if (flags & SF_APPEND)
SAPPEND("sappnd");
if (flags & SF_ARCHIVED)
SAPPEND("arch");
if (flags & SF_IMMUTABLE)
SAPPEND("schg");
#endif
if (prefix == NULL)
#if defined(netbsd) || defined(openbsd) || defined(freebsd)
strlcpy(string, def, sizeof(string));
#else
strncpy(string, def, sizeof(string));
#endif
return (string);
}
#define TEST(a, b, f) { \
if (!strcmp(a, b)) { \
if (clear) { \
if (clrp) \
*clrp |= (f); \
if (setp) \
*setp &= ~(f); \
} else { \
if (setp) \
*setp |= (f); \
if (clrp) \
*clrp &= ~(f); \
} \
break; \
} \
}
/*
* string_to_flags --
* Take string of arguments and return stat flags. Return 0 on
* success, 1 on failure. On failure, stringp is set to point
* to the offending token.
*/
int
string_to_flags(char **stringp, u_long *setp, u_long *clrp)
{
#if HAVE_STRUCT_STAT_ST_FLAGS
int clear;
char *string, *p;
#endif
if (setp)
*setp = 0;
if (clrp)
*clrp = 0;
#if HAVE_STRUCT_STAT_ST_FLAGS
string = *stringp;
while ((p = strsep(&string, "\t ,")) != NULL) {
clear = 0;
*stringp = p;
if (*p == '\0')
continue;
if (p[0] == 'n' && p[1] == 'o') {
clear = 1;
p += 2;
}
switch (p[0]) {
case 'a':
TEST(p, "arch", SF_ARCHIVED);
TEST(p, "archived", SF_ARCHIVED);
return (1);
case 'd':
clear = !clear;
TEST(p, "dump", UF_NODUMP);
return (1);
case 'n':
/*
* Support `nonodump'. Note that
* the state of clear is not changed.
*/
TEST(p, "nodump", UF_NODUMP);
return (1);
case 'o':
TEST(p, "opaque", UF_OPAQUE);
return (1);
case 's':
TEST(p, "sappnd", SF_APPEND);
TEST(p, "sappend", SF_APPEND);
TEST(p, "schg", SF_IMMUTABLE);
TEST(p, "schange", SF_IMMUTABLE);
TEST(p, "simmutable", SF_IMMUTABLE);
return (1);
case 'u':
TEST(p, "uappnd", UF_APPEND);
TEST(p, "uappend", UF_APPEND);
TEST(p, "uchg", UF_IMMUTABLE);
TEST(p, "uchange", UF_IMMUTABLE);
TEST(p, "uimmutable", UF_IMMUTABLE);
return (1);
default:
return (1);
}
}
#endif
return (0);
}

View file

@ -0,0 +1,39 @@
/* $NetBSD: stat_flags.h,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/*-
* Copyright (c) 1991, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)extern.h 8.1 (Berkeley) 5/31/93
*/
char *flags_to_string(u_long, const char *);
int string_to_flags(char **, u_long *, u_long *);

View file

@ -0,0 +1,50 @@
/* $Id: strdup.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/* $NetBSD: strdup.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/*
* Copyright (c) 1988, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#include "nbcompat.h"
char *
strdup(const char *str)
{
size_t len;
char *copy;
len = strlen(str) + 1;
if (!(copy = malloc(len)))
return (NULL);
memcpy(copy, str, len);
return (copy);
}

View file

@ -0,0 +1,19 @@
/* $NetBSD: strerror.c,v 1.1.1.1 2003/03/31 05:02:57 grant Exp $ */
#include "nbcompat.h"
char *
strerror(int n)
{
static char msg[] = "Unknown error (1234567890)";
extern int sys_nerr;
extern char *sys_errlist[];
if (n >= sys_nerr) {
snprintf(msg, sizeof(msg), "Unknown error (%d)", n);
return(msg);
} else {
return(sys_errlist[n]);
}
}

View file

@ -0,0 +1,66 @@
/* $Id: strlcat.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/* $NetBSD: strlcat.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/* from OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp */
/*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (*d != '\0' && n-- != 0)
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}

View file

@ -0,0 +1,63 @@
/* $Id: strlcpy.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/* $NetBSD: strlcpy.c,v 1.1.1.1 2003/03/31 05:03:04 grant Exp $ */
/* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */
/*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}

View file

@ -0,0 +1,392 @@
/* $Id: strptime.c,v 1.1.1.1 2003/03/31 05:03:05 grant Exp $ */
/* $NetBSD: strptime.c,v 1.1.1.1 2003/03/31 05:03:05 grant Exp $ */
/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code was contributed to The NetBSD Foundation by Klaus Klein.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
/*
* We do not implement alternate representations. However, we always
* check whether a given modifier is allowed for a certain conversion.
*/
#define ALT_E 0x01
#define ALT_O 0x02
#define LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); }
static int conv_num(const char **, int *, int, int);
static const char *day[7] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"
};
static const char *abday[7] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
static const char *mon[12] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
static const char *abmon[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static const char *am_pm[2] = {
"AM", "PM"
};
char *
strptime(const char *buf, const char *fmt, struct tm *tm)
{
char c;
const char *bp;
size_t len = 0;
int alt_format, i, split_year = 0;
bp = buf;
while ((c = *fmt) != '\0') {
/* Clear `alternate' modifier prior to new conversion. */
alt_format = 0;
/* Eat up white-space. */
if (isspace(c)) {
while (isspace(*bp))
bp++;
fmt++;
continue;
}
if ((c = *fmt++) != '%')
goto literal;
again: switch (c = *fmt++) {
case '%': /* "%%" is converted to "%". */
literal:
if (c != *bp++)
return (0);
break;
/*
* "Alternative" modifiers. Just set the appropriate flag
* and start over again.
*/
case 'E': /* "%E?" alternative conversion modifier. */
LEGAL_ALT(0);
alt_format |= ALT_E;
goto again;
case 'O': /* "%O?" alternative conversion modifier. */
LEGAL_ALT(0);
alt_format |= ALT_O;
goto again;
/*
* "Complex" conversion rules, implemented through recursion.
*/
case 'c': /* Date and time, using the locale's format. */
LEGAL_ALT(ALT_E);
if (!(bp = strptime(bp, "%x %X", tm)))
return (0);
break;
case 'D': /* The date as "%m/%d/%y". */
LEGAL_ALT(0);
if (!(bp = strptime(bp, "%m/%d/%y", tm)))
return (0);
break;
case 'R': /* The time as "%H:%M". */
LEGAL_ALT(0);
if (!(bp = strptime(bp, "%H:%M", tm)))
return (0);
break;
case 'r': /* The time in 12-hour clock representation. */
LEGAL_ALT(0);
if (!(bp = strptime(bp, "%I:%M:%S %p", tm)))
return (0);
break;
case 'T': /* The time as "%H:%M:%S". */
LEGAL_ALT(0);
if (!(bp = strptime(bp, "%H:%M:%S", tm)))
return (0);
break;
case 'X': /* The time, using the locale's format. */
LEGAL_ALT(ALT_E);
if (!(bp = strptime(bp, "%H:%M:%S", tm)))
return (0);
break;
case 'x': /* The date, using the locale's format. */
LEGAL_ALT(ALT_E);
if (!(bp = strptime(bp, "%m/%d/%y", tm)))
return (0);
break;
/*
* "Elementary" conversion rules.
*/
case 'A': /* The day of week, using the locale's form. */
case 'a':
LEGAL_ALT(0);
for (i = 0; i < 7; i++) {
/* Full name. */
len = strlen(day[i]);
if (strncasecmp(day[i], bp, len) == 0)
break;
/* Abbreviated name. */
len = strlen(abday[i]);
if (strncasecmp(abday[i], bp, len) == 0)
break;
}
/* Nothing matched. */
if (i == 7)
return (0);
tm->tm_wday = i;
bp += len;
break;
case 'B': /* The month, using the locale's form. */
case 'b':
case 'h':
LEGAL_ALT(0);
for (i = 0; i < 12; i++) {
/* Full name. */
len = strlen(mon[i]);
if (strncasecmp(mon[i], bp, len) == 0)
break;
/* Abbreviated name. */
len = strlen(abmon[i]);
if (strncasecmp(abmon[i], bp, len) == 0)
break;
}
/* Nothing matched. */
if (i == 12)
return (0);
tm->tm_mon = i;
bp += len;
break;
case 'C': /* The century number. */
LEGAL_ALT(ALT_E);
if (!(conv_num(&bp, &i, 0, 99)))
return (0);
if (split_year) {
tm->tm_year = (tm->tm_year % 100) + (i * 100);
} else {
tm->tm_year = i * 100;
split_year = 1;
}
break;
case 'd': /* The day of month. */
case 'e':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
return (0);
break;
case 'k': /* The hour (24-hour clock representation). */
LEGAL_ALT(0);
/* FALLTHROUGH */
case 'H':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
return (0);
break;
case 'l': /* The hour (12-hour clock representation). */
LEGAL_ALT(0);
/* FALLTHROUGH */
case 'I':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
return (0);
if (tm->tm_hour == 12)
tm->tm_hour = 0;
break;
case 'j': /* The day of year. */
LEGAL_ALT(0);
if (!(conv_num(&bp, &i, 1, 366)))
return (0);
tm->tm_yday = i - 1;
break;
case 'M': /* The minute. */
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
return (0);
break;
case 'm': /* The month. */
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &i, 1, 12)))
return (0);
tm->tm_mon = i - 1;
break;
case 'p': /* The locale's equivalent of AM/PM. */
LEGAL_ALT(0);
/* AM? */
if (strcasecmp(am_pm[0], bp) == 0) {
if (tm->tm_hour > 11)
return (0);
bp += strlen(am_pm[0]);
break;
}
/* PM? */
else if (strcasecmp(am_pm[1], bp) == 0) {
if (tm->tm_hour > 11)
return (0);
tm->tm_hour += 12;
bp += strlen(am_pm[1]);
break;
}
/* Nothing matched. */
return (0);
case 'S': /* The seconds. */
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
return (0);
break;
case 'U': /* The week of year, beginning on sunday. */
case 'W': /* The week of year, beginning on monday. */
LEGAL_ALT(ALT_O);
/*
* XXX This is bogus, as we can not assume any valid
* information present in the tm structure at this
* point to calculate a real value, so just check the
* range for now.
*/
if (!(conv_num(&bp, &i, 0, 53)))
return (0);
break;
case 'w': /* The day of week, beginning on sunday. */
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
return (0);
break;
case 'Y': /* The year. */
LEGAL_ALT(ALT_E);
if (!(conv_num(&bp, &i, 0, 9999)))
return (0);
tm->tm_year = i - TM_YEAR_BASE;
break;
case 'y': /* The year within 100 years of the epoch. */
LEGAL_ALT(ALT_E | ALT_O);
if (!(conv_num(&bp, &i, 0, 99)))
return (0);
if (split_year) {
tm->tm_year = ((tm->tm_year / 100) * 100) + i;
break;
}
split_year = 1;
if (i <= 68)
tm->tm_year = i + 2000 - TM_YEAR_BASE;
else
tm->tm_year = i + 1900 - TM_YEAR_BASE;
break;
/*
* Miscellaneous conversions.
*/
case 'n': /* Any kind of white-space. */
case 't':
LEGAL_ALT(0);
while (isspace(*bp))
bp++;
break;
default: /* Unknown/unsupported conversion. */
return (0);
}
}
/* LINTED functional specification */
return ((char *)bp);
}
static int
conv_num(const char **buf, int *dest, int llim, int ulim)
{
int result = 0;
/* The limit also determines the number of valid digits. */
int rulim = ulim;
if (**buf < '0' || **buf > '9')
return (0);
do {
result *= 10;
result += *(*buf)++ - '0';
rulim /= 10;
} while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
if (result < llim || result > ulim)
return (0);
*dest = result;
return (1);
}

View file

@ -0,0 +1,75 @@
/* $Id: strsep.c,v 1.1.1.1 2003/03/31 05:03:05 grant Exp $ */
/* $NetBSD: strsep.c,v 1.1.1.1 2003/03/31 05:03:05 grant Exp $ */
/*-
* Copyright (c) 1990, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#include "nbcompat.h"
/*
* Get next token from string *stringp, where tokens are possibly-empty
* strings separated by characters from delim.
*
* Writes NULs into the string at *stringp to end tokens.
* delim need not remain constant from call to call.
* On return, *stringp points past the last NUL written (if there might
* be further tokens), or is NULL (if there are definitely no more tokens).
*
* If *stringp is NULL, strsep returns NULL.
*/
char *
strsep(char **stringp, const char *delim)
{
char *s;
const char *spanp;
int c, sc;
char *tok;
if ((s = *stringp) == NULL)
return (NULL);
for (tok = s;;) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}

View file

@ -0,0 +1,151 @@
/* $Id: strtoll.c,v 1.1.1.1 2003/03/31 05:03:05 grant Exp $ */
/* $NetBSD: strtoll.c,v 1.1.1.1 2003/03/31 05:03:05 grant Exp $ */
/*-
* Copyright (c) 1992, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#include "nbcompat.h"
/*
* Convert a string to a quad integer.
*
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
*/
long long
strtoll(const char *nptr, char **endptr, int base)
{
const char *s;
long long acc, cutoff;
int c;
int neg, any, cutlim;
/* endptr may be NULL */
#ifdef __GNUC__
/* This outrageous construct just to shut up a GCC warning. */
(void) &acc; (void) &cutoff;
#endif
/*
* Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x.
*/
s = nptr;
do {
c = (unsigned char) *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
/*
* Compute the cutoff value between legal numbers and illegal
* numbers. That is the largest legal value, divided by the
* base. An input number that is greater than this value, if
* followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit. For instance, if the range for quads is
* [-9223372036854775808..9223372036854775807] and the input base
* is 10, cutoff will be set to 922337203685477580 and cutlim to
* either 7 (neg==0) or 8 (neg==1), meaning that if we have
* accumulated a value > 922337203685477580, or equal but the
* next digit is > 7 (or 8), the number is too big, and we will
* return a range error.
*
* Set any if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = neg ? QUAD_MIN : QUAD_MAX;
cutlim = (int)(cutoff % base);
cutoff /= base;
if (neg) {
if (cutlim > 0) {
cutlim -= base;
cutoff += 1;
}
cutlim = -cutlim;
}
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0)
continue;
if (neg) {
if (acc < cutoff || (acc == cutoff && c > cutlim)) {
any = -1;
acc = QUAD_MIN;
errno = ERANGE;
} else {
any = 1;
acc *= base;
acc -= c;
}
} else {
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1;
acc = QUAD_MAX;
errno = ERANGE;
} else {
any = 1;
acc *= base;
acc += c;
}
}
}
if (endptr != 0)
/* LINTED interface specification */
*endptr = (char *)(any ? s - 1 : nptr);
return (acc);
}

View file

@ -0,0 +1,119 @@
/* $Id: timegm.c,v 1.1.1.1 2003/03/31 05:03:06 grant Exp $ */
#include "nbcompat.h"
/*
* UTC version of mktime(3)
*/
/*
* This code is not portable, but works on most Unix-like systems.
* If the local timezone has no summer time, using mktime(3) function
* and adjusting offset would be usable (adjusting leap seconds
* is still required, though), but the assumption is not always true.
*
* Anyway, no portable and correct implementation of UTC to time_t
* conversion exists....
*/
static time_t
sub_mkgmt(struct tm *tm)
{
int y, nleapdays;
time_t t;
/* days before the month */
static const unsigned short moff[12] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
/*
* XXX: This code assumes the given time to be normalized.
* Normalizing here is impossible in case the given time is a leap
* second but the local time library is ignorant of leap seconds.
*/
/* minimal sanity checking not to access outside of the array */
if ((unsigned) tm->tm_mon >= 12)
return (time_t) -1;
if (tm->tm_year < EPOCH_YEAR - TM_YEAR_BASE)
return (time_t) -1;
y = tm->tm_year + TM_YEAR_BASE - (tm->tm_mon < 2);
nleapdays = y / 4 - y / 100 + y / 400 -
((EPOCH_YEAR-1) / 4 - (EPOCH_YEAR-1) / 100 + (EPOCH_YEAR-1) / 400);
t = ((((time_t) (tm->tm_year - (EPOCH_YEAR - TM_YEAR_BASE)) * 365 +
moff[tm->tm_mon] + tm->tm_mday - 1 + nleapdays) * 24 +
tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
return (t < 0 ? (time_t) -1 : t);
}
time_t
timegm(struct tm *tm)
{
time_t t, t2;
struct tm *tm2;
int sec;
/* Do the first guess. */
if ((t = sub_mkgmt(tm)) == (time_t) -1)
return (time_t) -1;
/* save value in case *tm is overwritten by gmtime() */
sec = tm->tm_sec;
tm2 = gmtime(&t);
if ((t2 = sub_mkgmt(tm2)) == (time_t) -1)
return (time_t) -1;
if (t2 < t || tm2->tm_sec != sec) {
/*
* Adjust for leap seconds.
*
* real time_t time
* |
* tm
* / ... (a) first sub_mkgmt() conversion
* t
* |
* tm2
* / ... (b) second sub_mkgmt() conversion
* t2
* --->time
*/
/*
* Do the second guess, assuming (a) and (b) are almost equal.
*/
t += t - t2;
tm2 = gmtime(&t);
/*
* Either (a) or (b), may include one or two extra
* leap seconds. Try t, t + 2, t - 2, t + 1, and t - 1.
*/
if (tm2->tm_sec == sec
|| (t += 2, tm2 = gmtime(&t), tm2->tm_sec == sec)
|| (t -= 4, tm2 = gmtime(&t), tm2->tm_sec == sec)
|| (t += 3, tm2 = gmtime(&t), tm2->tm_sec == sec)
|| (t -= 2, tm2 = gmtime(&t), tm2->tm_sec == sec))
; /* found */
else {
/*
* Not found.
*/
if (sec >= 60)
/*
* The given time is a leap second
* (sec 60 or 61), but the time library
* is ignorant of the leap second.
*/
; /* treat sec 60 as 59,
sec 61 as 0 of the next minute */
else
/* The given time may not be normalized. */
t++; /* restore t */
}
}
return (t < 0 ? (time_t) -1 : t);
}

View file

@ -0,0 +1,320 @@
/* $NetBSD: unvis.c,v 1.1.1.1 2003/03/31 05:03:06 grant Exp $ */
/*-
* Copyright (c) 1989, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: unvis.c,v 1.1.1.1 2003/03/31 05:03:06 grant Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#define __LIBC12_SOURCE__
#include "namespace.h"
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <vis.h>
#if !HAVE_VIS_H
/*
* decode driven by state machine
*/
#define S_GROUND 0 /* haven't seen escape char */
#define S_START 1 /* start decoding special sequence */
#define S_META 2 /* metachar started (M) */
#define S_META1 3 /* metachar more, regular char (-) */
#define S_CTRL 4 /* control char started (^) */
#define S_OCTAL2 5 /* octal digit 2 */
#define S_OCTAL3 6 /* octal digit 3 */
#define S_HEX1 7 /* hex digit */
#define S_HEX2 8 /* hex digit 2 */
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
#define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
int
unvis(cp, c, astate, flag)
char *cp;
int c;
int *astate, flag;
{
return __unvis13(cp, (int)c, astate, flag);
}
/*
* unvis - decode characters previously encoded by vis
*/
int
__unvis13(cp, c, astate, flag)
char *cp;
int c;
int *astate, flag;
{
_DIAGASSERT(cp != NULL);
_DIAGASSERT(astate != NULL);
if (flag & UNVIS_END) {
if (*astate == S_OCTAL2 || *astate == S_OCTAL3
|| *astate == S_HEX2) {
*astate = S_GROUND;
return (UNVIS_VALID);
}
return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
}
switch (*astate) {
case S_GROUND:
*cp = 0;
if (c == '\\') {
*astate = S_START;
return (0);
}
if ((flag & VIS_HTTPSTYLE) && c == '%') {
*astate = S_HEX1;
return (0);
}
*cp = c;
return (UNVIS_VALID);
case S_START:
switch(c) {
case '\\':
*cp = c;
*astate = S_GROUND;
return (UNVIS_VALID);
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
*cp = (c - '0');
*astate = S_OCTAL2;
return (0);
case 'M':
*cp = (char)0200;
*astate = S_META;
return (0);
case '^':
*astate = S_CTRL;
return (0);
case 'n':
*cp = '\n';
*astate = S_GROUND;
return (UNVIS_VALID);
case 'r':
*cp = '\r';
*astate = S_GROUND;
return (UNVIS_VALID);
case 'b':
*cp = '\b';
*astate = S_GROUND;
return (UNVIS_VALID);
case 'a':
*cp = '\007';
*astate = S_GROUND;
return (UNVIS_VALID);
case 'v':
*cp = '\v';
*astate = S_GROUND;
return (UNVIS_VALID);
case 't':
*cp = '\t';
*astate = S_GROUND;
return (UNVIS_VALID);
case 'f':
*cp = '\f';
*astate = S_GROUND;
return (UNVIS_VALID);
case 's':
*cp = ' ';
*astate = S_GROUND;
return (UNVIS_VALID);
case 'E':
*cp = '\033';
*astate = S_GROUND;
return (UNVIS_VALID);
case '\n':
/*
* hidden newline
*/
*astate = S_GROUND;
return (UNVIS_NOCHAR);
case '$':
/*
* hidden marker
*/
*astate = S_GROUND;
return (UNVIS_NOCHAR);
}
*astate = S_GROUND;
return (UNVIS_SYNBAD);
case S_META:
if (c == '-')
*astate = S_META1;
else if (c == '^')
*astate = S_CTRL;
else {
*astate = S_GROUND;
return (UNVIS_SYNBAD);
}
return (0);
case S_META1:
*astate = S_GROUND;
*cp |= c;
return (UNVIS_VALID);
case S_CTRL:
if (c == '?')
*cp |= 0177;
else
*cp |= c & 037;
*astate = S_GROUND;
return (UNVIS_VALID);
case S_OCTAL2: /* second possible octal digit */
if (isoctal(c)) {
/*
* yes - and maybe a third
*/
*cp = (*cp << 3) + (c - '0');
*astate = S_OCTAL3;
return (0);
}
/*
* no - done with current sequence, push back passed char
*/
*astate = S_GROUND;
return (UNVIS_VALIDPUSH);
case S_OCTAL3: /* third possible octal digit */
*astate = S_GROUND;
if (isoctal(c)) {
*cp = (*cp << 3) + (c - '0');
return (UNVIS_VALID);
}
/*
* we were done, push back passed char
*/
return (UNVIS_VALIDPUSH);
case S_HEX1:
if (isxdigit(c)) {
*cp = xtod(c);
*astate = S_HEX2;
return (0);
}
/*
* no - done with current sequence, push back passed char
*/
*astate = S_GROUND;
return (UNVIS_VALIDPUSH);
case S_HEX2:
*astate = S_GROUND;
if (isxdigit(c)) {
*cp = xtod(c) | (*cp << 4);
return (UNVIS_VALID);
}
return (UNVIS_VALIDPUSH);
default:
/*
* decoder in unknown state - (probably uninitialized)
*/
*astate = S_GROUND;
return (UNVIS_SYNBAD);
}
}
/*
* strunvis - decode src into dst
*
* Number of chars decoded into dst is returned, -1 on error.
* Dst is null terminated.
*/
int
strunvisx(dst, src, flag)
char *dst;
const char *src;
int flag;
{
char c;
char *start = dst;
int state = 0;
_DIAGASSERT(src != NULL);
_DIAGASSERT(dst != NULL);
while ((c = *src++) != '\0') {
again:
switch (__unvis13(dst, c, &state, flag)) {
case UNVIS_VALID:
dst++;
break;
case UNVIS_VALIDPUSH:
dst++;
goto again;
case 0:
case UNVIS_NOCHAR:
break;
default:
return (-1);
}
}
if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID)
dst++;
*dst = '\0';
return (dst - start);
}
int
strunvis(dst, src)
char *dst;
const char *src;
{
return strunvisx(dst, src, 0);
}
#endif

View file

@ -0,0 +1,54 @@
/* $Id: usleep.c,v 1.1.1.1 2003/03/31 05:03:06 grant Exp $ */
/*-
* Copyright (c) 1999-2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn.
*
* 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
#include "nbcompat.h"
int
usleep(unsigned int usec)
{
#if HAVE_SELECT
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = usec;
return (select(1, NULL, NULL, NULL, &tv));
#elif HAVE_POLL
return (poll(NULL, 0, usec / 1000);
#else
# error no way to implement usleep
#endif
}

View file

@ -0,0 +1,126 @@
/* $NetBSD: util.h,v 1.1.1.1 2003/03/31 05:03:06 grant Exp $ */
/*-
* Copyright (c) 1995
* 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifndef _UTIL_H_
#define _UTIL_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#ifdef HAVE_SYS_TTYCOM_H
#include <sys/ttycom.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_UTMP_H
#include <utmp.h>
#endif
#define PIDLOCK_NONBLOCK 1
#define PIDLOCK_USEHOSTNAME 2
#define FPARSELN_UNESCESC 0x01
#define FPARSELN_UNESCCONT 0x02
#define FPARSELN_UNESCCOMM 0x04
#define FPARSELN_UNESCREST 0x08
#define FPARSELN_UNESCALL 0x0f
__BEGIN_DECLS
struct iovec;
struct passwd;
struct termios;
struct utmp;
struct winsize;
pid_t forkpty(int *, char *, struct termios *, struct winsize *);
const char *getbootfile(void);
int getmaxpartitions(void);
int getrawpartition(void);
void login(const struct utmp *);
int login_tty(int);
int logout(const char *);
void logwtmp(const char *, const char *, const char *);
int opendisk(const char *, int, char *, size_t, int);
int openpty(int *, int *, char *, struct termios *,
struct winsize *);
void pidfile(const char *);
int pidlock(const char *, int, pid_t *, const char *);
int pw_abort(void);
void pw_copy(int, int, struct passwd *, struct passwd *);
void pw_edit(int, const char *);
void pw_error(const char *, int, int);
void pw_getconf(char *, size_t, const char *, const char *);
const char *pw_getprefix(void);
void pw_init(void);
int pw_lock(int);
int pw_mkdb(const char *, int);
void pw_prompt(void);
int pw_setprefix(const char *);
int secure_path(const char *);
int ttyaction(const char *, const char *, const char *);
int ttylock(const char *, int, pid_t *);
char *ttymsg(struct iovec *, int, const char *, int);
int ttyunlock(const char *);
#ifndef HAVE_STRSEP
char *strsep(char **, const char *);
#endif
#ifndef HAVE_FGETLN
char *fgetln(FILE *, size_t *);
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
#define LIST_FIRST(head) ((head)->lh_first)
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#endif
__END_DECLS
static int isescaped(const char *, const char *, int);
#endif /* !_UTIL_H_ */

View file

@ -0,0 +1,375 @@
/* $NetBSD: vis.c,v 1.1.1.1 2003/03/31 05:02:59 grant Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* Copyright (c) 1989, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: vis.c,v 1.1.1.1 2003/03/31 05:02:59 grant Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <sys/types.h>
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include "vis.h"
#include <stdlib.h>
#include <nbtypes.h>
#ifndef __ELF__
#define _strsvis strsvis
#define _strsvisx strsvisx
#define _strvis strvis
#define _strvisx strvisx
#define _svis svis
#define _vis vis
#else
#ifdef __weak_alias
__weak_alias(strsvis,_strsvis)
__weak_alias(strsvisx,_strsvisx)
__weak_alias(strvis,_strvis)
__weak_alias(strvisx,_strvisx)
__weak_alias(svis,_svis)
__weak_alias(vis,_vis)
#endif
#endif
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#undef BELL
#if defined(__STDC__)
#define BELL '\a'
#else
#define BELL '\007'
#endif
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
#define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
#define issafe(c) (c == '\b' || c == BELL || c == '\r')
#define xtoa(c) "0123456789abcdef"[c]
#define MAXEXTRAS 5
#define MAKEEXTRALIST(flag, extra, orig) \
do { \
const char *o = orig; \
char *e; \
while (*o++) \
continue; \
extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
continue; \
e--; \
if (flag & VIS_SP) *e++ = ' '; \
if (flag & VIS_TAB) *e++ = '\t'; \
if (flag & VIS_NL) *e++ = '\n'; \
if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
*e = '\0'; \
} while (/*CONSTCOND*/0)
/*
* This is HVIS, the macro of vis used to HTTP style (RFC 1808)
*/
#define HVIS(dst, c, flag, nextc, extra) \
do \
if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
*dst++ = '%'; \
*dst++ = xtoa(((unsigned int)c >> 4) & 0xf); \
*dst++ = xtoa((unsigned int)c & 0xf); \
} else { \
SVIS(dst, c, flag, nextc, extra); \
} \
while (/*CONSTCOND*/0)
/*
* This is SVIS, the central macro of vis.
* dst: Pointer to the destination buffer
* c: Character to encode
* flag: Flag word
* nextc: The character following 'c'
* extra: Pointer to the list of extra characters to be
* backslash-protected.
*/
#define SVIS(dst, c, flag, nextc, extra) \
do { \
int isextra, isc; \
isextra = strchr(extra, c) != NULL; \
if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || \
((flag & VIS_SAFE) && issafe(c)))) { \
*dst++ = c; \
break; \
} \
isc = 0; \
if (flag & VIS_CSTYLE) { \
switch (c) { \
case '\n': \
isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
break; \
case '\r': \
isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
break; \
case '\b': \
isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
break; \
case BELL: \
isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
break; \
case '\v': \
isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
break; \
case '\t': \
isc = 1; *dst++ = '\\'; *dst++ = 't'; \
break; \
case '\f': \
isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
break; \
case ' ': \
isc = 1; *dst++ = '\\'; *dst++ = 's'; \
break; \
case '\0': \
isc = 1; *dst++ = '\\'; *dst++ = '0'; \
if (isoctal(nextc)) { \
*dst++ = '0'; \
*dst++ = '0'; \
} \
} \
} \
if (isc) break; \
if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
*dst++ = '\\'; \
*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \
*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \
*dst++ = (c & 07) + '0'; \
} else { \
if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
if (c & 0200) { \
c &= 0177; *dst++ = 'M'; \
} \
if (iscntrl(c)) { \
*dst++ = '^'; \
if (c == 0177) \
*dst++ = '?'; \
else \
*dst++ = c + '@'; \
} else { \
*dst++ = '-'; *dst++ = c; \
} \
} \
} while (/*CONSTCOND*/0)
/*
* svis - visually encode characters, also encoding the characters
* pointed to by `extra'
*/
char *
svis(dst, c, flag, nextc, extra)
char *dst;
int c, flag, nextc;
const char *extra;
{
char *nextra;
_DIAGASSERT(dst != NULL);
_DIAGASSERT(extra != NULL);
MAKEEXTRALIST(flag, nextra, extra);
if (flag & VIS_HTTPSTYLE)
HVIS(dst, c, flag, nextc, nextra);
else
SVIS(dst, c, flag, nextc, nextra);
*dst = '\0';
free(nextra);
return(dst);
}
#ifndef HAVE_STRSVIS
/*
* strsvis, strsvisx - visually encode characters from src into dst
*
* Extra is a pointer to a \0-terminated list of characters to
* be encoded, too. These functions are useful e. g. to
* encode strings in such a way so that they are not interpreted
* by a shell.
*
* Dst must be 4 times the size of src to account for possible
* expansion. The length of dst, not including the trailing NULL,
* is returned.
*
* Strsvisx encodes exactly len bytes from src into dst.
* This is useful for encoding a block of data.
*/
int
strsvis(dst, src, flag, extra)
char *dst;
const char *src;
int flag;
const char *extra;
{
char c;
char *start;
char *nextra;
_DIAGASSERT(dst != NULL);
_DIAGASSERT(src != NULL);
_DIAGASSERT(extra != NULL);
MAKEEXTRALIST(flag, nextra, extra);
if (flag & VIS_HTTPSTYLE) {
for (start = dst; (c = *src++) != '\0'; /* empty */)
HVIS(dst, c, flag, *src, nextra);
} else {
for (start = dst; (c = *src++) != '\0'; /* empty */)
SVIS(dst, c, flag, *src, nextra);
}
*dst = '\0';
free(nextra);
return (dst - start);
}
int
strsvisx(dst, src, len, flag, extra)
char *dst;
const char *src;
size_t len;
int flag;
const char *extra;
{
char c;
char *start;
char *nextra;
_DIAGASSERT(dst != NULL);
_DIAGASSERT(src != NULL);
_DIAGASSERT(extra != NULL);
MAKEEXTRALIST(flag, nextra, extra);
if (flag & VIS_HTTPSTYLE) {
for (start = dst; len > 0; len--) {
c = *src++;
HVIS(dst, c, flag, len ? *src : '\0', nextra);
}
} else {
for (start = dst; len > 0; len--) {
c = *src++;
SVIS(dst, c, flag, len ? *src : '\0', nextra);
}
}
*dst = '\0';
free(nextra);
return (dst - start);
}
#endif
/*
* vis - visually encode characters
*/
char *
vis(dst, c, flag, nextc)
char *dst;
int c, flag, nextc;
{
char *extra;
_DIAGASSERT(dst != NULL);
MAKEEXTRALIST(flag, extra, "");
if (flag & VIS_HTTPSTYLE)
HVIS(dst, c, flag, nextc, extra);
else
SVIS(dst, c, flag, nextc, extra);
*dst = '\0';
free(extra);
return (dst);
}
#ifdef HAVE_STRVIS
/*
* strvis, strvisx - visually encode characters from src into dst
*
* Dst must be 4 times the size of src to account for possible
* expansion. The length of dst, not including the trailing NULL,
* is returned.
*
* Strvisx encodes exactly len bytes from src into dst.
* This is useful for encoding a block of data.
*/
int
strvis(dst, src, flag)
char *dst;
const char *src;
int flag;
{
char *extra;
int ret;
MAKEEXTRALIST(flag, extra, "");
ret = strsvis(dst, src, flag, extra);
free(extra);
return(ret);
}
int
strvisx(dst, src, len, flag)
char *dst;
const char *src;
size_t len;
int flag;
{
char *extra;
int ret;
MAKEEXTRALIST(flag, extra, "");
ret = strsvisx(dst, src, len, flag, extra);
free(extra);
return(ret);
}
#endif

View file

@ -0,0 +1,107 @@
/* $NetBSD: vis.h,v 1.1.1.1 2003/03/31 05:03:07 grant Exp $ */
/*-
* Copyright (c) 1990, 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
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)vis.h 8.1 (Berkeley) 6/2/93
*/
#ifndef _VIS_H_
#define _VIS_H_
/*
* to select alternate encoding format
*/
#define VIS_OCTAL 0x01 /* use octal \ddd format */
#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropiate */
/*
* to alter set of characters encoded (default is to encode all
* non-graphic except space, tab, and newline).
*/
#define VIS_SP 0x04 /* also encode space */
#define VIS_TAB 0x08 /* also encode tab */
#define VIS_NL 0x10 /* also encode newline */
#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
#define VIS_SAFE 0x20 /* only encode "unsafe" characters */
/*
* other
*/
#define VIS_NOSLASH 0x40 /* inhibit printing '\' */
#define VIS_HTTPSTYLE 0x80 /* http-style escape % HEX HEX */
/*
* unvis return codes
*/
#define UNVIS_VALID 1 /* character valid */
#define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */
#define UNVIS_NOCHAR 3 /* valid sequence, no character produced */
#define UNVIS_SYNBAD -1 /* unrecognized escape sequence */
#define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */
/*
* unvis flags
*/
#define UNVIS_END 1 /* no more characters */
#ifndef _DIAGASSERT
#define _DIAGASSERT(e)
#endif
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#ifndef __BEGIN_DECLS
#define __BEGIN_DECLS
#define __END_DECLS
#endif
__BEGIN_DECLS
char *vis (char *, int, int, int);
char *svis (char *, int, int, int, const char *);
int strvis (char *, const char *, int);
int strsvis (char *, const char *, int, const char *);
int strvisx (char *, const char *, size_t, int);
int strsvisx (char *, const char *, size_t, int, const char *);
int strunvis (char *, const char *);
int strunvisx (char *, const char *, int);
#if defined(__LIBC12_SOURCE__) || !defined(__NetBSD__)
int unvis (char *, int, int *, int);
int __unvis13 (char *, int, int *, int);
#else
int unvis (char *, int, int *, int) __RENAME(__unvis13);
#endif
__END_DECLS
#endif /* !_VIS_H_ */