changeset 179:0ffecd4e7e1a HEAD

added dec2str() function and largest_t type, removed itoa() and ltoa() macros and did some other cleanups.
author Timo Sirainen <tss@iki.fi>
date Sun, 08 Sep 2002 14:36:03 +0300
parents f012779ee5ef
children 38341ad6a9db
files acconfig.h configure.in src/lib/compat.h src/lib/hostpid.c src/lib/lib.h src/lib/strfuncs.c src/lib/strfuncs.h
diffstat 7 files changed, 63 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/acconfig.h	Sun Sep 08 13:25:02 2002 +0300
+++ b/acconfig.h	Sun Sep 08 14:36:03 2002 +0300
@@ -35,3 +35,7 @@
 #undef UOFF_T_INT
 #undef UOFF_T_LONG
 #undef UOFF_T_LONG_LONG
+
+/* What type should be used for largest_t */
+#undef LARGEST_T_LONG
+#undef LARGEST_T_LONG_LONG
--- a/configure.in	Sun Sep 08 13:25:02 2002 +0300
+++ b/configure.in	Sun Sep 08 14:36:03 2002 +0300
@@ -153,6 +153,12 @@
   AC_ERROR([Couldn't find integer type for off_t])
 fi
 
+if test x$ac_cv_sizeof_long_long != x0; then
+  AC_DEFINE(LARGEST_T_LONG_LONG)
+else
+  AC_DEFINE(LARGEST_T_LONG)
+fi
+
 dnl * memory alignment, needed with non-x86 systems and should speed up
 dnl * x86 systems too. Use 8 with everyone to make sure 64bit lookups
 dnl * work. Currently it should also be safe to set to 4 if off_t == 32bit.
--- a/src/lib/compat.h	Sun Sep 08 13:25:02 2002 +0300
+++ b/src/lib/compat.h	Sun Sep 08 14:36:03 2002 +0300
@@ -7,6 +7,24 @@
 #  define LLONG_MAX 9223372036854775807LL
 #endif
 
+#if defined (UOFF_T_INT)
+typedef unsigned int uoff_t;
+#elif defined (UOFF_T_LONG)
+typedef unsigned long uoff_t;
+#elif defined (UOFF_T_LONG_LONG)
+typedef unsigned long long uoff_t;
+#else
+#  error uoff_t size not set
+#endif
+
+#if defined (LARGEST_T_LONG)
+typedef unsigned long largest_t;
+#elif defined (LARGEST_T_LONG_LONG)
+typedef unsigned long long largest_t;
+#else
+#  error largest_t size not set
+#endif
+
 /* memmove() */
 #ifndef HAVE_MEMMOVE
 #  define memmove my_memmove
--- a/src/lib/hostpid.c	Sun Sep 08 13:25:02 2002 +0300
+++ b/src/lib/hostpid.c	Sun Sep 08 14:36:03 2002 +0300
@@ -33,7 +33,7 @@
 
 void hostpid_init(void)
 {
-	static char hostname[256], pid[100];
+	static char hostname[256], pid[MAX_LARGEST_T_STRLEN];
 
 	if (my_hostname == NULL) {
 		hostname[sizeof(hostname)-1] = '\0';
@@ -44,7 +44,7 @@
 	}
 
 	if (my_pid == NULL) {
-		i_snprintf(pid, sizeof(pid), "%lu", (unsigned long) getpid());
+		dec2str(pid, sizeof(pid), getpid());
 		my_pid = pid;
 	}
 }
--- a/src/lib/lib.h	Sun Sep 08 13:25:02 2002 +0300
+++ b/src/lib/lib.h	Sun Sep 08 14:36:03 2002 +0300
@@ -21,16 +21,6 @@
 typedef struct _IOBuffer IOBuffer;
 typedef struct _TempString TempString;
 
-#if defined (UOFF_T_INT)
-typedef unsigned int uoff_t;
-#elif defined (UOFF_T_LONG)
-typedef unsigned long uoff_t;
-#elif defined (UOFF_T_LONG_LONG)
-typedef unsigned long long uoff_t;
-#else
-#  error uoff_t size not set
-#endif
-
 #include "compat.h"
 #include "macros.h"
 #include "failures.h"
--- a/src/lib/strfuncs.c	Sun Sep 08 13:25:02 2002 +0300
+++ b/src/lib/strfuncs.c	Sun Sep 08 14:36:03 2002 +0300
@@ -923,3 +923,33 @@
         t_buffer_alloc(full_len);
         return data;
 }
+
+static size_t dec2str_recurse(char *buffer, size_t pos, size_t size,
+			      largest_t number)
+{
+	if (number == 0)
+		return 0;
+
+	pos = dec2str_recurse(buffer, pos, size-1, number / 10);
+	if (pos < size)
+		buffer[pos] = '0' + (number % 10);
+	return pos + 1;
+}
+
+void dec2str(char *buffer, size_t size, largest_t number)
+{
+	size_t pos;
+
+	if (size == 0)
+		return;
+
+	pos = dec2str_recurse(buffer, 0, size, number);
+
+	if (pos == 0 && size > 1) {
+		/* we wrote nothing, because number is 0 */
+		buffer[0] = '0';
+		pos++;
+	}
+
+	buffer[pos < size ? pos : size-1] = '\0';
+}
--- a/src/lib/strfuncs.h	Sun Sep 08 13:25:02 2002 +0300
+++ b/src/lib/strfuncs.h	Sun Sep 08 14:36:03 2002 +0300
@@ -1,16 +1,6 @@
 #ifndef __STRFUNC_H
 #define __STRFUNC_H
 
-/* max. size for %d and %ld */
-#define MAX_INT_STRLEN ((sizeof(int) * CHAR_BIT + 2) / 3 + 1)
-#define MAX_LONG_STRLEN ((sizeof(long) * CHAR_BIT + 2) / 3 + 1)
-
-/* `str' should be type char[MAX_INT_STRLEN] or char[MAX_LONG_STRLEN] */
-#define itoa(str, num) \
-	i_snprintf(str, sizeof(str), "%d", num)
-#define ltoa(str, num) \
-	i_snprintf(str, sizeof(str), "%ld", num)
-
 #define is_empty_str(str) \
         ((str) == NULL || (str)[0] == '\0')
 
@@ -64,6 +54,9 @@
 const char *t_strjoin_replace(char *const args[], char separator,
 			      int replacearg, const char *replacedata);
 
+#define MAX_LARGEST_T_STRLEN ((sizeof(largest_t) * CHAR_BIT + 2) / 3 + 1)
+void dec2str(char *buffer, size_t size, largest_t number);
+
 /* INTERNAL */
 const char *temp_strconcat(const char *str1, va_list args,
 			   unsigned int *ret_len);