diff --git a/lib/roken/Makefile.in b/lib/roken/Makefile.in new file mode 100644 index 000000000..9ae1096c9 --- /dev/null +++ b/lib/roken/Makefile.in @@ -0,0 +1,78 @@ +# +# $Id$ +# + +SHELL = /bin/sh + +srcdir = @srcdir@ +VPATH = @srcdir@ + +CC = @CC@ +AR = ar +RANLIB = @RANLIB@ +DEFS = @DEFS@ +CFLAGS = @CFLAGS@ + +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +MKDIRHIER = @top_srcdir@/mkdirhier-sh + +prefix = @prefix@ +exec_prefix = $(prefix) +libdir = $(exec_prefix)/lib + +PICFLAGS = @PICFLAGS@ + +LIBNAME = libbroken +LIBEXT = @LIBEXT@ +SHLIBEXT = @SHLIBEXT@ +LIB = $(LIBNAME).$(LIBEXT) + + +OBJECTS = @LIBOBJS@ + +all: $(LIB) + +Wall: + make CFLAGS="-g -Wall -Wmissing-prototypes -Wmissing-declarations -D__USE_FIXED_PROTOTYPES__" + +.c.o: + $(CC) -c $(CPPFLAGS) $(DEFS) -I../.. -I../../include -I$(srcdir) -I$(srcdir)/../../include $(CFLAGS) $(PICFLAGS) $< + +install: all + +uninstall: + +TAGS: $(SOURCES) + etags $(SOURCES) + +check: + +clean: + rm -f $(LIB) *.o *.a + +mostlyclean: clean + +distclean: clean + rm -f Makefile *.tab.c *~ + rm -rf CVS + +realclean: distclean + rm -f TAGS + +dist: $(DISTFILES) + for file in $(DISTFILES); do \ + ln $$file ../`cat ../.fname`/lib \ + || cp -p $$file ../`cat ../.fname`/lib; \ + done + +$(LIBNAME).a: $(OBJECTS) + rm -f $@ + $(AR) cr $@ $(OBJECTS) + -$(RANLIB) $@ + +$(LIBNAME).$(SHLIBEXT): $(OBJECTS) + rm -f $@ + $(CC) $(CFLAGS) $(PICFLAGS) -shared -o $@ $(OBJECTS) -L../../util/et -lcom_err + +$(OBJECTS): ../../config.h diff --git a/lib/roken/err.c b/lib/roken/err.c new file mode 100644 index 000000000..0f567f5bc --- /dev/null +++ b/lib/roken/err.c @@ -0,0 +1,16 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +err(int eval, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verr(eval, fmt, ap); + va_end(ap); +} diff --git a/lib/roken/err.h b/lib/roken/err.h new file mode 100644 index 000000000..40c08de8e --- /dev/null +++ b/lib/roken/err.h @@ -0,0 +1,21 @@ +#ifndef __ERR_H__ +#define __ERR_H__ + +#include +#include +#include +#include +#include + +extern char *__progname; + +void verr(int eval, const char *fmt, va_list ap); +void err(int eval, const char *fmt, ...); +void verrx(int eval, const char *fmt, va_list ap); +void errx(int eval, const char *fmt, ...); +void vwarn(const char *fmt, va_list ap); +void warn(const char *fmt, ...); +void vwarnx(const char *fmt, va_list ap); +void warnx(const char *fmt, ...); + +#endif /* __ERR_H__ */ diff --git a/lib/roken/err.hin b/lib/roken/err.hin new file mode 100644 index 000000000..40c08de8e --- /dev/null +++ b/lib/roken/err.hin @@ -0,0 +1,21 @@ +#ifndef __ERR_H__ +#define __ERR_H__ + +#include +#include +#include +#include +#include + +extern char *__progname; + +void verr(int eval, const char *fmt, va_list ap); +void err(int eval, const char *fmt, ...); +void verrx(int eval, const char *fmt, va_list ap); +void errx(int eval, const char *fmt, ...); +void vwarn(const char *fmt, va_list ap); +void warn(const char *fmt, ...); +void vwarnx(const char *fmt, va_list ap); +void warnx(const char *fmt, ...); + +#endif /* __ERR_H__ */ diff --git a/lib/roken/errx.c b/lib/roken/errx.c new file mode 100644 index 000000000..6b3b5a8a1 --- /dev/null +++ b/lib/roken/errx.c @@ -0,0 +1,16 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +errx(int eval, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrx(eval, fmt, ap); + va_end(ap); +} diff --git a/lib/roken/getdtablesize.c b/lib/roken/getdtablesize.c new file mode 100644 index 000000000..27b92e4f1 --- /dev/null +++ b/lib/roken/getdtablesize.c @@ -0,0 +1,50 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#ifdef HAVE_SYS_RESOURCE_H +#include +#endif + +#ifdef HAVE_SYS_SYSCTL_H +#include +#endif + +int getdtablesize(void) +{ + int files = -1; +#if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX) + files = sysconf(_SC_OPEN_MAX); + +#elif definded(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) + struct rlimit res; + if(getrlimit(RLIMIT_NOFILE, &res) == 0) + files = res.rlim_cur; + +#elif defined(HAVE_SYSCTL) && defined(CTL_KERN) && defined(KERN_MAXFILES) + int mib[2]; + size_t len; + + mib[0] = CTL_KERN; + mib[1] = KERN_MAXFILES; + len = sizeof(files); + sysctl(&mib, 2, &files, sizeof(nfil), NULL, 0); +#endif + +#ifdef OPEN_MAX + if(files < 0) + files = OPEN_MAX; +#endif + +#ifdef NOFILE + if(files < 0) + files = NOFILE; +#endif + + return files; +} diff --git a/lib/roken/getusershell.c b/lib/roken/getusershell.c new file mode 100644 index 000000000..ff696a03d --- /dev/null +++ b/lib/roken/getusershell.c @@ -0,0 +1,149 @@ +/* $NetBSD: getusershell.c,v 1.5 1995/02/27 04:13:27 cgd Exp $ */ + +/* + * Copyright (c) 1985, 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 +#endif + +RCSID("$Id$"); + +#ifndef HAVE_GETUSERSHELL + +#include +#include +#include +#include +#include + +#ifndef __P +#define __P(X) X +#endif + +#ifndef _PATH_SHELLS +#define _PATH_SHELLS "/etc/shells" +#endif + +/* + * Local shells should NOT be added here. They should be added in + * /etc/shells. + */ + +static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; +static char **curshell, **shells, *strings; +static char **initshells __P((void)); + +/* + * Get a list of shells from _PATH_SHELLS, if it exists. + */ +char * +getusershell() +{ + char *ret; + + if (curshell == NULL) + curshell = initshells(); + ret = *curshell; + if (ret != NULL) + curshell++; + return (ret); +} + +void +endusershell() +{ + + if (shells != NULL) + free(shells); + shells = NULL; + if (strings != NULL) + free(strings); + strings = NULL; + curshell = NULL; +} + +void +setusershell() +{ + + curshell = initshells(); +} + +static char ** +initshells() +{ + register char **sp, *cp; + register FILE *fp; + struct stat statb; + + if (shells != NULL) + free(shells); + shells = NULL; + if (strings != NULL) + free(strings); + strings = NULL; + if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) + return (okshells); + if (fstat(fileno(fp), &statb) == -1) { + (void)fclose(fp); + return (okshells); + } + if ((strings = malloc((u_int)statb.st_size)) == NULL) { + (void)fclose(fp); + return (okshells); + } + shells = calloc((unsigned)statb.st_size / 3, sizeof (char *)); + if (shells == NULL) { + (void)fclose(fp); + free(strings); + strings = NULL; + return (okshells); + } + sp = shells; + cp = strings; + while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { + while (*cp != '#' && *cp != '/' && *cp != '\0') + cp++; + if (*cp == '#' || *cp == '\0') + continue; + *sp++ = cp; + while (!isspace(*cp) && *cp != '#' && *cp != '\0') + cp++; + *cp++ = '\0'; + } + *sp = NULL; + (void)fclose(fp); + return (shells); +} +#endif /* HAVE_GETUSERSHELL */ diff --git a/lib/roken/hstrerror.c b/lib/roken/hstrerror.c new file mode 100644 index 000000000..42e5975c1 --- /dev/null +++ b/lib/roken/hstrerror.c @@ -0,0 +1,26 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#ifndef HAVE_HSTRERROR + +#include +#include + +static char *msg[] = { + "No error", + "Authoritative Answer Host not found", + "Non-Authoritive Host not found, or SERVERFAIL", + "Non recoverable errors, FORMERR, REFUSED, NOTIMP", + "Valid name, no data record of requested type" +}; + +char *hstrerror(int herr) +{ + if(herr >= 0 && herr <= 4) + return msg[herr]; + return "Error number out of range (hstrerror)"; +} + +#endif + diff --git a/lib/roken/inet_aton.c b/lib/roken/inet_aton.c new file mode 100644 index 000000000..b4b8bd56f --- /dev/null +++ b/lib/roken/inet_aton.c @@ -0,0 +1,38 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +RCSID("$Id$"); + + +/* Minimal implementation of inet_aton. Doesn't handle hex numbers. */ + +int inet_aton(char *cp, struct in_addr *adr) +{ + unsigned int a, b, c, d; + + int num; + + num = sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d); + + if(num < 2) + return 0; + + if(num == 2){ + c = b & 0xffff; + b = b >> 16; + } + if(num < 4){ + d = c & 0xff; + c = c >> 8; + } + + if(a > 255 || b > 255 || c > 255 || d > 255) + return 0; + adr->s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d); + return 1; +} diff --git a/lib/roken/memmove.c b/lib/roken/memmove.c new file mode 100644 index 000000000..8604af68a --- /dev/null +++ b/lib/roken/memmove.c @@ -0,0 +1,33 @@ +/* + * memmove for systems that doesn't have it + * + * $Id$ + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +RCSID("$Id$"); + +void* memmove(void *s1, const void *s2, size_t n) +{ + char *s=(char*)s2, *d=(char*)s1; + + if(d > s){ + s+=n-1; + d+=n-1; + while(n){ + *d--=*s--; + n--; + } + }else if(d < s) + while(n){ + *d++=*s++; + n--; + } + return s1; +} diff --git a/lib/roken/putenv.c b/lib/roken/putenv.c new file mode 100644 index 000000000..836d7dd96 --- /dev/null +++ b/lib/roken/putenv.c @@ -0,0 +1,44 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +RCSID("$Id$"); + +extern char **environ; + +/* + * putenv -- + * String points to a string of the form name=value. + * + * Makes the value of the environment variable name equal to + * value by altering an existing variable or creating a new one. + */ +int putenv(const char *string) +{ + int i; + int len; + + len = string - strchr(string, '=') + 1; + + if(environ == NULL){ + environ = malloc(sizeof(char*)); + if(environ == NULL) + return 1; + environ[0] = NULL; + } + + for(i = 0; environ[i]; i++) + if(strncmp(string, environ[i], len)){ + environ[len] = string; + return 0; + } + environ = realloc(environ, sizeof(char*) * (i + 1)); + if(environ == NULL) + return 1; + environ[i] = string; + environ[i+1] = NULL; + return 0; +} + diff --git a/lib/roken/setenv.c b/lib/roken/setenv.c new file mode 100644 index 000000000..ee270e3a8 --- /dev/null +++ b/lib/roken/setenv.c @@ -0,0 +1,31 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +RCSID("$Id$"); + +/* + * This is the easy way out, use putenv to implement setenv. We might + * leak some memory but that is ok since we are usally about to exec + * anyway. + */ + +int +setenv(const char *var, const char *val, int rewrite) +{ + char *t; + + if (!rewrite && getenv(var) != 0) + return 0; + + if ((t = malloc(strlen(var) + strlen(val) + 2)) == 0) + return -1; + + strcpy(t, var); + strcat(t, "="); + strcat(t, val); + if (putenv(t) == 0) + return 0; + else + return -1; +} diff --git a/lib/roken/snprintf.c b/lib/roken/snprintf.c new file mode 100644 index 000000000..9d35de6b4 --- /dev/null +++ b/lib/roken/snprintf.c @@ -0,0 +1,18 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +RCSID("$Id$"); + +int snprintf(char *s, int n, char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsprintf(s, fmt, ap); + va_end(ap); + return ret; +} diff --git a/lib/roken/strerror.c b/lib/roken/strerror.c new file mode 100644 index 000000000..517ac580c --- /dev/null +++ b/lib/roken/strerror.c @@ -0,0 +1,21 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +RCSID("$Id$"); + +static char emsg[1024]; + +char* +strerror(int eno) +{ + if(eno < 0 || eno >= sys_nerr) + sprintf(emsg, "Error %d occurred.", eno); + else + strcpy(emsg, sys_errlist[eno]); + + return emsg; +} diff --git a/lib/roken/verr.c b/lib/roken/verr.c new file mode 100644 index 000000000..04fa0a37a --- /dev/null +++ b/lib/roken/verr.c @@ -0,0 +1,22 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +verr(int eval, const char *fmt, va_list ap) +{ + int sverrno; + + sverrno = errno; + fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + fprintf(stderr, "%s\n", strerror(sverrno)); + exit(eval); +} diff --git a/lib/roken/verrx.c b/lib/roken/verrx.c new file mode 100644 index 000000000..001d25336 --- /dev/null +++ b/lib/roken/verrx.c @@ -0,0 +1,17 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +verrx(int eval, const char *fmt, va_list ap) +{ + fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + exit(eval); +} diff --git a/lib/roken/vwarn.c b/lib/roken/vwarn.c new file mode 100644 index 000000000..f2effc3ee --- /dev/null +++ b/lib/roken/vwarn.c @@ -0,0 +1,21 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +vwarn(const char *fmt, va_list ap) +{ + int sverrno; + + sverrno = errno; + fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + fprintf(stderr, "%s\n", strerror(sverrno)); +} diff --git a/lib/roken/vwarnx.c b/lib/roken/vwarnx.c new file mode 100644 index 000000000..9e58676de --- /dev/null +++ b/lib/roken/vwarnx.c @@ -0,0 +1,16 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +vwarnx(const char *fmt, va_list ap) +{ + fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); +} diff --git a/lib/roken/warn.c b/lib/roken/warn.c new file mode 100644 index 000000000..dec5abf48 --- /dev/null +++ b/lib/roken/warn.c @@ -0,0 +1,16 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +warn(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); +} diff --git a/lib/roken/warnx.c b/lib/roken/warnx.c new file mode 100644 index 000000000..ef8bfec2d --- /dev/null +++ b/lib/roken/warnx.c @@ -0,0 +1,16 @@ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "err.h" + +RCSID("$Id$"); + +void +warnx(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +}