use fmt_*() functions instead of *int2str()
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 20 Nov 2003 15:37:03 +0000 (15:37 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 20 Nov 2003 15:37:03 +0000 (15:37 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@420 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/lib/Makefile-files
util-vserver/lib/fmt-32.c [moved from util-vserver/lib/int2str.c with 70% similarity]
util-vserver/lib/fmt-64.c [moved from util-vserver/lib/uint2str.c with 56% similarity]
util-vserver/lib/fmt.h [new file with mode: 0644]
util-vserver/lib/fmt.hc [new file with mode: 0644]
util-vserver/lib/getprocentry-legacy.c
util-vserver/lib/internal.h
util-vserver/src/save_ctxinfo.c
util-vserver/src/vlimit.c
util-vserver/tests/getctx.c
util-vserver/tests/getinitpid.c

index aad5979..7513b35 100644 (file)
@@ -30,8 +30,8 @@ lib_SRCS                      =  lib/syscall.c \
                                   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) \
@@ -48,6 +48,8 @@ lib_XHDRS                       =  lib/syscall-compat.hc \
                                   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 \
similarity index 70%
rename from util-vserver/lib/int2str.c
rename to util-vserver/lib/fmt-32.c
index 701ece4..0608a61 100644 (file)
@@ -1,4 +1,4 @@
-// $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"
similarity index 56%
rename from util-vserver/lib/uint2str.c
rename to util-vserver/lib/fmt-64.c
index 2ba5bf1..84b7376 100644 (file)
@@ -1,4 +1,4 @@
-// $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"
diff --git a/util-vserver/lib/fmt.h b/util-vserver/lib/fmt.h
new file mode 100644 (file)
index 0000000..e869829
--- /dev/null
@@ -0,0 +1,97 @@
+// $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
diff --git a/util-vserver/lib/fmt.hc b/util-vserver/lib/fmt.hc
new file mode 100644 (file)
index 0000000..25273f4
--- /dev/null
@@ -0,0 +1,86 @@
+// $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;
+}
index 32e128b..16d8cd8 100644 (file)
@@ -43,7 +43,7 @@ utilvserver_getProcEntry(pid_t pid,
                         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;
@@ -56,9 +56,7 @@ utilvserver_getProcEntry(pid_t pid,
   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");
   }
 
index 35c3aed..5891274 100644 (file)
 #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
index 3163596..24d77f8 100644 (file)
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
   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]);
@@ -71,7 +71,7 @@ int main(int argc, char *argv[])
   }
 
   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  ||
index df73ccb..cfed32a 100644 (file)
@@ -102,11 +102,9 @@ appendLimit(char *ptr, bool do_it, vc_limit_t lim)
       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 = ' ';
     }
   }
@@ -140,7 +138,7 @@ showAll(int ctx)
     }
 
     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);
index 93909b2..4033a83 100644 (file)
 
 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");
index 1de298b..15ecf66 100644 (file)
 
 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");