lib/getctx.c \
lib/getinitpid.c \
lib/getversion.c \
- lib/uint2str.c \
- lib/int2str.c \
+ lib/fmt-32.c \
+ lib/fmt-64.c \
lib/capabilities.c \
$(lib_legacy_SRCS) \
$(lib_management_SRCS) \
lib/getinitpid-legacy.hc \
lib/getversion-internal.hc \
lib/safechroot-internal.hc \
+ lib/fmt.hc \
+ lib/fmt.h \
lib/virtual.h \
lib/internal.h \
lib/utils-legacy.h \
-// $Id$ --*- c++ -*--
+// $Id$ --*- c -*--
// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
//
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
-#include "compat.h"
-#include "internal.h"
-
-size_t
-utilvserver_int2str(char *buf, size_t len, signed int val, unsigned char base)
-{
- int offset = 0;
-
- if (val<0 && len>0) {
- *buf++ = '-';
- --len;
- offset = 1;
- val = -val;
- }
-
- return utilvserver_uint2str(buf, len, val, base)+offset;
-}
+#define FMT_BITSIZE 32
+#include "fmt.hc"
-// $Id$ --*- c++ -*--
+// $Id$ --*- c -*--
// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
//
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
-#include "compat.h"
-#include <assert.h>
-#include <stdbool.h>
-#include <string.h>
-
-size_t
-utilvserver_uint2str(char *buf, size_t len, unsigned int val, unsigned char base)
-{
- char *ptr = buf+len-1;
- register size_t res;
- if (base>=36 || len==0) return 0;
-
- *ptr = '\0';
- while (ptr>buf) {
- unsigned char digit = val%base;
-
- --ptr;
- *ptr = (digit<10 ? '0'+digit :
- digit<36 ? 'a'+digit-10 :
- (assert(false),'?'));
-
- val /= base;
- if (val==0) break;
- }
-
- assert(ptr>=buf && ptr<=buf+len-1);
-
- res = buf+len-ptr;
- memmove(buf, ptr, res);
-
- return res-1;
-}
+#define FMT_BITSIZE 64
+#include "fmt.hc"
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifndef H_VSERVER_DJINNI_SRC_FMT_H
+#define H_VSERVER_DJINNI_SRC_FMT_H
+
+#include <stdlib.h>
+#include <stdint.h>
+
+#ifndef FMT_PREFIX
+# define FMT_PREFIX fmt_
+#endif
+
+#define FMT_P__(X,Y) X ## Y
+#define FMT_P_(X,Y) FMT_P__(X,Y)
+#define FMT_P(X) FMT_P_(FMT_PREFIX, X)
+
+#define STRINGIFY_(X) #X
+#define STRINGIFY(X) STRINGIFY_(X)
+#define WEAKFUNC(X) __attribute__((__weak__,__alias__(STRINGIFY(FMT_P(X)))))
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+size_t FMT_P(uint64_base)(char *ptr, uint_least64_t val, char base);
+size_t FMT_P( int64_base)(char *ptr, int_least64_t val, char base);
+
+size_t FMT_P(uint32_base)(char *ptr, uint_least32_t val, char base);
+size_t FMT_P( int32_base)(char *ptr, int_least32_t val, char base);
+
+#if __WORDSIZE == 64
+size_t FMT_P(ulong_base)(char *ptr, unsigned long val, char base) WEAKFUNC(uint64_base);
+size_t FMT_P( long_base)(char *ptr, long val, char base) WEAKFUNC( int64_base);
+#else
+size_t FMT_P(ulong_base)(char *ptr, unsigned long val, char base) WEAKFUNC(uint32_base);
+size_t FMT_P( long_base)(char *ptr, long val, char base) WEAKFUNC( int32_base);
+#endif
+
+size_t FMT_P(uint_base)(char *ptr, unsigned int val, char base) WEAKFUNC(uint32_base);
+size_t FMT_P( int_base)(char *ptr, int val, char base) WEAKFUNC( int32_base);
+
+
+inline static size_t
+FMT_P(ulong)(char *ptr, unsigned long val)
+{
+ return FMT_P(ulong_base)(ptr, val, 10);
+}
+
+inline static size_t
+FMT_P(long)(char *ptr, long val)
+{
+ return FMT_P(long_base)(ptr, val, 10);
+}
+
+
+inline static size_t
+FMT_P(uint)(char *ptr, unsigned int val)
+{
+ return FMT_P(uint_base)(ptr, val, 10);
+}
+
+inline static size_t
+FMT_P(int)(char *ptr, int val)
+{
+ return FMT_P(int_base)(ptr, val, 10);
+}
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#undef WEAKFUNC
+#undef STRINGIFY
+#undef STRINGIFY_
+#undef FMT_P
+#undef FMT_P_
+#undef FMT_P__
+
+#endif // H_VSERVER_DJINNI_SRC_FMT_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "fmt.h"
+#include <string.h>
+
+static char const DIGITS[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+
+#define FMT_P__(X,Y) X ## Y
+#define FMT_P_(X,Y) FMT_P__(X,Y)
+#define FMT_P(X) FMT_P_(FMT_PREFIX, X)
+
+#define CONCAT__(x,y,z) x ## y ## z
+#define CONCAT_(x,y,z) CONCAT__(x,y,z)
+#define CONCAT(x,z) CONCAT_(x, FMT_BITSIZE, z)
+
+#define FMT_FN(BASE,SZ) \
+ do { \
+ register __typeof__(val) v = val; \
+ register size_t l = 0; \
+ \
+ if (ptr==0) { \
+ do { \
+ ++l; \
+ v /= BASE; \
+ } while (v!=0); \
+ } \
+ else { \
+ char buf[sizeof(val)*SZ]; \
+ \
+ do { \
+ register unsigned int d = v%BASE; \
+ v /= BASE; \
+ ++l; \
+ buf[sizeof(buf)-l] = DIGITS[d]; \
+ } while (v!=0); \
+ \
+ memcpy(ptr, buf+sizeof(buf)-l, l); \
+ } \
+ \
+ return l; \
+ } while (0)
+
+size_t
+CONCAT(FMT_P(uint),_base)(char *ptr, CONCAT(uint_least,_t) val, char base)
+{
+ //if (base==10) FMT_FN(10,3);
+ if (base==16) FMT_FN(16,2);
+
+ FMT_FN(base,8);
+}
+
+size_t
+CONCAT(FMT_P(int),_base)(char *ptr,
+ CONCAT(int_least,_t) val, char base)
+{
+ size_t offset=0;
+ if (val<0) {
+ val = -val;
+ offset = 1;
+
+ if (ptr!=0)
+ *ptr++ = '-';
+ }
+
+ return CONCAT(FMT_P(uint),_base)(ptr, val, base) + offset;
+}
char *str,
char *buf, size_t bufsize)
{
- char status_name[ sizeof("/proc/01234/status") ];
+ char status_name[ sizeof("/proc//status") + sizeof(unsigned int)*3 + 1 ];
int fd;
size_t len;
char * res = 0;
if (pid==0) strcpy(status_name, "/proc/self/status");
else {
strcpy(status_name, "/proc/");
- len = utilvserver_uint2str(status_name+sizeof("/proc/")-1,
- sizeof(status_name)-sizeof("/proc//status")+1,
- pid, 10);
+ len = utilvserver_fmt_uint(status_name+sizeof("/proc/")-1, pid);
strcpy(status_name+sizeof("/proc/")+len-1, "/status");
}
#ifndef H_UTIL_VSERVER_LIB_INTERNAL_H
#define H_UTIL_VSERVER_LIB_INTERNAL_H
+#include "fmt.h"
#include <stdlib.h>
+
#ifdef __cplusplus
extern "C" {
#endif
-size_t utilvserver_uint2str(char *buf, size_t len,
- unsigned int val, unsigned char base);
-size_t utilvserver_int2str(char *buf, size_t len,
- signed int val, unsigned char base);
int utilvserver_checkCompatVersion();
#ifdef __cplusplus
char runfile[(checkParams(argc,argv),strlen(argv[1])) + sizeof("/run.rev/99999")];
char dstfile[PATH_MAX];
int fd;
- char buf[6];
+ char buf[sizeof(int)*3+2];
ctx_t ctx;
ssize_t len;
ssize_t len1 = strlen(argv[1]);
}
Ereadlink(runfile, dstfile, sizeof(dstfile));
- len = utilvserver_uint2str(buf, sizeof(buf), ctx, 10);
+ len = utilvserver_fmt_uint(buf, ctx);
fd = Eopen(dstfile, O_EXCL|O_CREAT|O_WRONLY, 0644);
if (write(fd, buf, len) !=len ||
ptr += 3;
}
else {
- memcpy(ptr, "0x", 2);
- ptr += 2;
-
- ptr += utilvserver_uint2str(ptr, 20, (lim>>32), 16);
- ptr += utilvserver_uint2str(ptr, 20, lim&0xffffffff, 16);
+ memcpy(ptr, "0x", 2); ptr += 2;
+
+ ptr += utilvserver_fmt_uint64_base(ptr, lim, 16);
*ptr = ' ';
}
}
}
memset(buf, ' ', sizeof buf);
- ptr += utilvserver_uint2str(ptr, 100, i, 10);
+ ptr += utilvserver_fmt_uint(ptr, i);
*ptr = ' ';
ptr = appendLimit(buf+10, mask.min &bitmask, limit.min);
int main(int argc, char *argv[])
{
- char buf[32];
+ char buf[sizeof(int)*3+2];
ctx_t ctx;
if (argc==1) ctx = vc_X_getctx(0);
else ctx = vc_X_getctx(atoi(argv[1]));
- utilvserver_int2str(buf, sizeof buf, ctx, 10);
+ utilvserver_fmt_int(buf, ctx);
WRITE_STR(1, buf);
WRITE_MSG(1, "\n");
int main(int argc, char *argv[])
{
- char buf[32];
+ char buf[sizeof(int)*3+2];
pid_t pid;
if (argc==1) pid = vc_X_getinitpid(0);
else pid = vc_X_getinitpid(atoi(argv[1]));
- utilvserver_int2str(buf, sizeof buf, pid, 10);
+ utilvserver_fmt_int(buf, pid);
WRITE_STR(1, buf);
WRITE_MSG(1, "\n");