summaryrefslogtreecommitdiffstats
path: root/numlib
diff options
context:
space:
mode:
authorSam Varshavchik2013-08-19 16:39:41 -0400
committerSam Varshavchik2013-08-25 14:43:51 -0400
commit9c45d9ad13fdf439d44d7443ae75da15ea0223ed (patch)
tree7a81a04cb51efb078ee350859a64be2ebc6b8813 /numlib
parenta9520698b770168d1f33d6301463bb70a19655ec (diff)
downloadcourier-libs-9c45d9ad13fdf439d44d7443ae75da15ea0223ed.tar.bz2
Initial checkin
Imported from subversion report, converted to git. Updated all paths in scripts and makefiles, reflecting the new directory hierarchy.
Diffstat (limited to 'numlib')
-rw-r--r--numlib/Makefile.am26
-rw-r--r--numlib/atotimet.c13
-rw-r--r--numlib/atouidt.c14
-rw-r--r--numlib/changeuidgid.c110
-rw-r--r--numlib/configure.in49
-rw-r--r--numlib/numlib.h101
-rw-r--r--numlib/strdevt.c25
-rw-r--r--numlib/strgidt.c25
-rw-r--r--numlib/strhdevt.c28
-rw-r--r--numlib/strhinot.c28
-rw-r--r--numlib/strhpidt.c28
-rw-r--r--numlib/strhtimet.c28
-rw-r--r--numlib/strinot.c25
-rw-r--r--numlib/strofft.c64
-rw-r--r--numlib/strpidt.c25
-rw-r--r--numlib/strsize.c61
-rw-r--r--numlib/strsizet.c25
-rw-r--r--numlib/strtimet.c25
-rw-r--r--numlib/struidt.c25
19 files changed, 725 insertions, 0 deletions
diff --git a/numlib/Makefile.am b/numlib/Makefile.am
new file mode 100644
index 0000000..0a5f103
--- /dev/null
+++ b/numlib/Makefile.am
@@ -0,0 +1,26 @@
+#
+# Copyright 1998 - 2004 Double Precision, Inc. See COPYING for
+# distribution information.
+
+
+CLEANFILES=$(noinst_DATA)
+noinst_LTLIBRARIES=libnumlib.la
+
+libnumlib_la_SOURCES=\
+ atotimet.c \
+ atouidt.c \
+ changeuidgid.c \
+ numlib.h \
+ strdevt.c \
+ strgidt.c \
+ strhdevt.c \
+ strhinot.c \
+ strhpidt.c \
+ strhtimet.c \
+ strinot.c \
+ strofft.c \
+ strpidt.c \
+ strsize.c \
+ strsizet.c \
+ strtimet.c \
+ struidt.c
diff --git a/numlib/atotimet.c b/numlib/atotimet.c
new file mode 100644
index 0000000..0360b5b
--- /dev/null
+++ b/numlib/atotimet.c
@@ -0,0 +1,13 @@
+/*
+** Copyright 2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+LIBMAIL_STRIMPL(time_t, libmail_strtotime_t, libmail_atotime_t)
diff --git a/numlib/atouidt.c b/numlib/atouidt.c
new file mode 100644
index 0000000..6f869d5
--- /dev/null
+++ b/numlib/atouidt.c
@@ -0,0 +1,14 @@
+/*
+** Copyright 2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+LIBMAIL_STRIMPL(uid_t, libmail_strtouid_t, libmail_atouid_t)
+LIBMAIL_STRIMPL(gid_t, libmail_strtogid_t, libmail_atogid_t)
diff --git a/numlib/changeuidgid.c b/numlib/changeuidgid.c
new file mode 100644
index 0000000..adaee40
--- /dev/null
+++ b/numlib/changeuidgid.c
@@ -0,0 +1,110 @@
+/*
+** Copyright 1998 - 2002 Double Precision, Inc. See COPYING for
+** distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <sys/types.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grp.h>
+#include <pwd.h>
+#include <errno.h>
+
+#include "numlib.h"
+
+
+void libmail_changegroup(gid_t gid)
+{
+ if ( setgid(gid))
+ {
+ perror("setgid");
+ exit(1);
+ }
+
+#if HAVE_SETGROUPS
+ if ( getuid() == 0 && setgroups(1, &gid) )
+ {
+ perror("setgroups");
+ exit(1);
+ }
+#endif
+}
+
+void libmail_changeuidgid(uid_t uid, gid_t gid)
+{
+ libmail_changegroup(gid);
+ if ( setuid(uid))
+ {
+ perror("setuid");
+ exit(1);
+ }
+}
+
+void libmail_changeusername(const char *uname, const gid_t *forcegrp)
+{
+struct passwd *pw;
+uid_t changeuid;
+gid_t changegid;
+
+/* uname might be a pointer returned from a previous called to getpw(),
+** and libc has a problem getting it back.
+*/
+char *p=malloc(strlen(uname)+1);
+
+ if (!p)
+ {
+ perror("malloc");
+ exit(1);
+ }
+ strcpy(p, uname);
+
+ errno=ENOENT;
+ if ((pw=getpwnam(p)) == 0)
+ {
+ free(p);
+ perror("getpwnam");
+ exit(1);
+ }
+ free(p);
+
+ changeuid=pw->pw_uid;
+
+ if ( !forcegrp ) forcegrp= &pw->pw_gid;
+
+ changegid= *forcegrp;
+
+ if ( setgid( changegid ))
+ {
+ perror("setgid");
+ exit(1);
+ }
+
+#if HAVE_INITGROUPS
+ if ( getuid() == 0 && initgroups(pw->pw_name, changegid) )
+ {
+ perror("initgroups");
+ exit(1);
+ }
+#else
+#if HAVE_SETGROUPS
+ if ( getuid() == 0 && setgroups(1, &changegid) )
+ {
+ perror("setgroups");
+ exit(1);
+ }
+#endif
+#endif
+
+ if (setuid(changeuid))
+ {
+ perror("setuid");
+ exit(1);
+ }
+}
diff --git a/numlib/configure.in b/numlib/configure.in
new file mode 100644
index 0000000..5291521
--- /dev/null
+++ b/numlib/configure.in
@@ -0,0 +1,49 @@
+dnl Process this file with autoconf to produce a configure script.
+dnl
+dnl Copyright 1998 - 2010 Double Precision, Inc. See COPYING for
+dnl distribution information.
+
+AC_PREREQ(2.59)
+AC_INIT(numlib, 0.10, courier-users@lists.sourceforge.net)
+
+>confdefs.h # Kill PACKAGE_ macros
+
+AC_CONFIG_SRCDIR([atotimet.c])
+AC_CONFIG_AUX_DIR(../..)
+AM_CONFIG_HEADER([config.h])
+AM_INIT_AUTOMAKE([foreign no-define])
+
+dnl Checks for programs.
+AC_PROG_AWK
+AC_PROG_CC
+AC_PROG_INSTALL
+AC_PROG_LN_S
+AC_PROG_LIBTOOL
+
+if test "$GCC" = "yes"
+then
+ CFLAGS="$CFLAGS -Wall"
+fi
+
+CFLAGS="-I.. -I${srcdir}/.. $CFLAGS"
+
+dnl Checks for libraries.
+
+dnl Checks for header files.
+
+AC_CHECK_HEADERS(unistd.h stdint.h)
+
+AC_CHECK_TYPE(int64_t, [ : ],
+ [
+ AC_DEFINE_UNQUOTED(int64_t,long long,[default definition of int64_t])
+ ])
+
+dnl Checks for typedefs, structures, and compiler characteristics.
+AC_TYPE_UID_T
+AC_TYPE_PID_T
+AC_SYS_LARGEFILE
+
+dnl Checks for library functions.
+
+AC_CHECK_FUNCS(setgroups initgroups)
+AC_OUTPUT(Makefile)
diff --git a/numlib/numlib.h b/numlib/numlib.h
new file mode 100644
index 0000000..1aa3386
--- /dev/null
+++ b/numlib/numlib.h
@@ -0,0 +1,101 @@
+#ifndef numlib_h
+#define numlib_h
+
+/*
+** Copyright 1998 - 2010 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#if HAVE_CONFIG_H
+#include "numlib/config.h"
+#endif
+
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
+
+#include <sys/types.h>
+#include <time.h>
+
+#define NUMBUFSIZE 60
+
+/* Convert various system types to decimal */
+
+char *libmail_str_time_t(time_t, char *);
+char *libmail_str_off_t(off_t, char *);
+char *libmail_str_int64_t(int64_t, char *);
+char *libmail_str_pid_t(pid_t, char *);
+char *libmail_str_dev_t(dev_t, char *);
+char *libmail_str_ino_t(ino_t, char *);
+char *libmail_str_uid_t(uid_t, char *);
+char *libmail_str_gid_t(gid_t, char *);
+char *libmail_str_size_t(size_t, char *);
+
+char *libmail_str_sizekb(unsigned long, char *); /* X Kb or X Mb */
+
+/* Convert selected system types to hex */
+
+char *libmail_strh_time_t(time_t, char *);
+char *libmail_strh_pid_t(pid_t, char *);
+char *libmail_strh_ino_t(ino_t, char *);
+char *libmail_strh_dev_t(dev_t, char *);
+
+/* And, now let's do the reverse */
+
+time_t libmail_strtotime_t(const char **);
+time_t libmail_atotime_t(const char *);
+
+uid_t libmail_strtouid_t(const char **);
+uid_t libmail_atouid_t(const char *);
+
+gid_t libmail_strtogid_t(const char **);
+gid_t libmail_atogid_t(const char *);
+
+ /* Common macros: */
+
+#define LIBMAIL_STRIMPL(type, f1, f2) \
+\
+type f1(const char **p)\
+{\
+ type n=0;\
+ while ( **p >= '0' && **p <= '9') n=n*10 + (char)(*(*p)++ - '0');\
+ return n;\
+}\
+\
+type f2(const char *p)\
+{\
+ return f1(&p);\
+}
+
+
+/*
+** The following functions are used by root to reset its user and group id
+** to the authenticated user's. Various functions are provided to handle
+** various situations.
+*/
+
+void libmail_changegroup(gid_t); /* Set the group id only. Also clear any
+ ** auxiliary group ids */
+
+void libmail_changeuidgid(uid_t, gid_t);
+ /* Set both user id and group id. Also clear
+ ** aux group ids */
+
+void libmail_changeusername(const char *, const gid_t *);
+ /*
+ ** Set the userid to the indicate user's. If second argument is
+ ** not null, it points to the groupid to set. If it's null, the
+ ** group id is taken from the passwd file. Auxiliary IDs are set
+ ** to any aux IDs set for the user in the group file. If there are
+ ** no aux group IDs for the user, any AUX ids are cleared.
+ */
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/numlib/strdevt.c b/numlib/strdevt.c
new file mode 100644
index 0000000..aa8b028
--- /dev/null
+++ b/numlib/strdevt.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_dev_t(dev_t t, char *arg)
+{
+ char buf[NUMBUFSIZE];
+ char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strgidt.c b/numlib/strgidt.c
new file mode 100644
index 0000000..828a455
--- /dev/null
+++ b/numlib/strgidt.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_gid_t(gid_t t, char *arg)
+{
+char buf[NUMBUFSIZE];
+char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strhdevt.c b/numlib/strhdevt.c
new file mode 100644
index 0000000..9ff45f2
--- /dev/null
+++ b/numlib/strhdevt.c
@@ -0,0 +1,28 @@
+/*
+** Copyright 1998 - 2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+static const char xdigit[]="0123456789ABCDEF";
+
+char *libmail_strh_dev_t(dev_t t, char *arg)
+{
+char buf[sizeof(t)*2+1];
+char *p=buf+sizeof(buf)-1;
+unsigned i;
+
+ *p=0;
+ for (i=0; i<sizeof(t)*2; i++)
+ {
+ *--p= xdigit[t & 15];
+ t=t / 16;
+ }
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strhinot.c b/numlib/strhinot.c
new file mode 100644
index 0000000..c3cdc10
--- /dev/null
+++ b/numlib/strhinot.c
@@ -0,0 +1,28 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+static const char xdigit[]="0123456789ABCDEF";
+
+char *libmail_strh_ino_t(ino_t t, char *arg)
+{
+char buf[sizeof(t)*2+1];
+char *p=buf+sizeof(buf)-1;
+unsigned i;
+
+ *p=0;
+ for (i=0; i<sizeof(t)*2; i++)
+ {
+ *--p= xdigit[t & 15];
+ t=t / 16;
+ }
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strhpidt.c b/numlib/strhpidt.c
new file mode 100644
index 0000000..5a440f5
--- /dev/null
+++ b/numlib/strhpidt.c
@@ -0,0 +1,28 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+static const char xdigit[]="0123456789ABCDEF";
+
+char *libmail_strh_pid_t(pid_t t, char *arg)
+{
+char buf[sizeof(t)*2+1];
+char *p=buf+sizeof(buf)-1;
+unsigned i;
+
+ *p=0;
+ for (i=0; i<sizeof(t)*2; i++)
+ {
+ *--p= xdigit[t & 15];
+ t=t / 16;
+ }
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strhtimet.c b/numlib/strhtimet.c
new file mode 100644
index 0000000..cb9e6a9
--- /dev/null
+++ b/numlib/strhtimet.c
@@ -0,0 +1,28 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+static const char xdigit[]="0123456789ABCDEF";
+
+char *libmail_strh_time_t(time_t t, char *arg)
+{
+char buf[sizeof(t)*2+1];
+char *p=buf+sizeof(buf)-1;
+unsigned i;
+
+ *p=0;
+ for (i=0; i<sizeof(t)*2; i++)
+ {
+ *--p= xdigit[t & 15];
+ t=t / 16;
+ }
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strinot.c b/numlib/strinot.c
new file mode 100644
index 0000000..314b2f3
--- /dev/null
+++ b/numlib/strinot.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_ino_t(ino_t t, char *arg)
+{
+char buf[NUMBUFSIZE];
+char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strofft.c b/numlib/strofft.c
new file mode 100644
index 0000000..567f912
--- /dev/null
+++ b/numlib/strofft.c
@@ -0,0 +1,64 @@
+/*
+** Copyright 1998 - 2010 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_off_t(off_t t, char *arg)
+{
+ char buf[NUMBUFSIZE];
+ char *p=buf+sizeof(buf)-1;
+ int isneg=0;
+
+ if (t < 0)
+ {
+ t= -t;
+ isneg=1;
+ }
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+
+ if (isneg)
+ *--p='-';
+
+ return (strcpy(arg, p));
+}
+
+char *libmail_str_int64_t(int64_t t, char *arg)
+{
+ char buf[NUMBUFSIZE];
+ char *p=buf+sizeof(buf)-1;
+ int isneg=0;
+
+ if (t < 0)
+ {
+ t= -t;
+ isneg=1;
+ }
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+
+ if (isneg)
+ *--p='-';
+
+ return (strcpy(arg, p));
+}
+
+
+
diff --git a/numlib/strpidt.c b/numlib/strpidt.c
new file mode 100644
index 0000000..0c29b07
--- /dev/null
+++ b/numlib/strpidt.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_pid_t(pid_t t, char *arg)
+{
+char buf[NUMBUFSIZE];
+char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strsize.c b/numlib/strsize.c
new file mode 100644
index 0000000..1c90312
--- /dev/null
+++ b/numlib/strsize.c
@@ -0,0 +1,61 @@
+/*
+** Copyright 2001 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+static void cat_n(char *buf, unsigned long n)
+{
+char bb[NUMBUFSIZE+1];
+char *p=bb+sizeof(bb)-1;
+
+ *p=0;
+ do
+ {
+ *--p = "0123456789"[n % 10];
+ n=n/10;
+ } while (n);
+ strcat(buf, p);
+}
+
+char *libmail_str_sizekb(unsigned long n, char *sizebuf)
+{
+ /* If size is less than 1K bytes, display it as 0.xK */
+
+ if (n < 1024)
+ {
+ strcpy(sizebuf, "0.");
+ cat_n(sizebuf, (int)(10 * n / 1024 ));
+ strcat(sizebuf, "K");
+ }
+ /* If size is less than 1 meg, display is as xK */
+
+ else if (n < 1024 * 1024)
+ {
+ *sizebuf=0;
+ cat_n(sizebuf, (unsigned long)(n+512)/1024);
+ strcat(sizebuf, "K");
+ }
+
+ /* Otherwise, display in megabytes */
+
+ else
+ {
+ unsigned long nm=(double)n / (1024.0 * 1024.0) * 10;
+
+ *sizebuf=0;
+ cat_n( sizebuf, nm / 10);
+ strcat(sizebuf, ".");
+ cat_n( sizebuf, nm % 10);
+ strcat(sizebuf, "M");
+ }
+
+ return (sizebuf);
+}
+
diff --git a/numlib/strsizet.c b/numlib/strsizet.c
new file mode 100644
index 0000000..fd9d1d1
--- /dev/null
+++ b/numlib/strsizet.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_size_t(size_t t, char *arg)
+{
+char buf[NUMBUFSIZE];
+char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}
diff --git a/numlib/strtimet.c b/numlib/strtimet.c
new file mode 100644
index 0000000..63307f2
--- /dev/null
+++ b/numlib/strtimet.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_time_t(time_t t, char *arg)
+{
+char buf[NUMBUFSIZE];
+char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}
diff --git a/numlib/struidt.c b/numlib/struidt.c
new file mode 100644
index 0000000..5843ed2
--- /dev/null
+++ b/numlib/struidt.c
@@ -0,0 +1,25 @@
+/*
+** Copyright 1998 - 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "numlib.h"
+#include <string.h>
+
+
+char *libmail_str_uid_t(uid_t t, char *arg)
+{
+char buf[NUMBUFSIZE];
+char *p=buf+sizeof(buf)-1;
+
+ *p=0;
+ do
+ {
+ *--p= '0' + (t % 10);
+ t=t / 10;
+ } while(t);
+ return (strcpy(arg, p));
+}