[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: printfs in str.c
From: |
Ben Pfaff |
Subject: |
Re: printfs in str.c |
Date: |
Mon, 27 Feb 2006 13:02:33 -0800 |
User-agent: |
Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) |
Ben Pfaff <address@hidden> writes:
> Arguably, we could just assume the proper return values these
> days. The C standard came out in 1989, so vendors have had about
> 17 years to fix their libraries.
I believe that the following patches would successfully implement
this policy. I've only tested that it compiles properly.
It'd be better to actually search-and-replace nsprintf() by
sprintf(), instead of using a #define, and similarly for
vsprintf().
cd /home/blp/pspp/feb17-1/src/libpspp/
diff -up /home/blp/pspp/feb17-1/src/libpspp/str.h\~
/home/blp/pspp/feb17-1/src/libpspp/str.h
--- /home/blp/pspp/feb17-1/src/libpspp/str.h~ 2006-02-26 15:43:55.000000000
-0800
+++ /home/blp/pspp/feb17-1/src/libpspp/str.h 2006-02-27 12:59:09.000000000
-0800
@@ -37,57 +37,6 @@
#include "vsnprintf.h"
#include "xvasprintf.h"
-/* sprintf() wrapper functions for convenience. */
-
-/* spprintf() calls sprintf() and returns the address of the null
- terminator in the resulting string. It should be portable the way
- it's been implemented. */
-#if __GNUC__
- #if HAVE_GOOD_SPRINTF
- #define spprintf(BUF, FORMAT, ARGS...) \
- ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
- #else
- #define spprintf(BUF, FORMAT, ARGS...) \
- ({ sprintf ((BUF), (FORMAT) , ## ARGS); \
- strchr ((BUF), 0); })
- #endif
-#else /* Not GNU C. */
- char *spprintf (char *buf, const char *format, ...);
-#endif /* Not GNU C. */
-
-/* nsprintf() calls sprintf() and returns the strlen() of the
- resulting string. It should be portable the way it's been
- implemented. */
-#if __GNUC__
- #if HAVE_GOOD_SPRINTF
- #define nsprintf(BUF, FORMAT, ARGS...) \
- (sprintf ((BUF), (FORMAT) , ## ARGS))
- #define nvsprintf(BUF, FORMAT, ARGS) \
- (vsprintf ((BUF), (FORMAT), (ARGS)))
- #else /* Not good sprintf(). */
- #define nsprintf(BUF, FORMAT, ARGS...) \
- ({ \
- char *pbuf = BUF; \
- sprintf ((pbuf), (FORMAT) , ## ARGS); \
- strlen (pbuf); \
- })
- #define nvsprintf(BUF, FORMAT, ARGS) \
- ({ \
- char *pbuf = BUF; \
- vsprintf ((pbuf), (FORMAT), (ARGS)); \
- strlen (pbuf); \
- })
- #endif /* Not good sprintf(). */
-#else /* Not GNU C. */
- #if HAVE_GOOD_SPRINTF
- #define nsprintf sprintf
- #define nvsprintf vsprintf
- #else /* Not good sprintf(). */
- int nsprintf (char *buf, const char *format, ...);
- int nvsprintf (char *buf, const char *format, va_list args);
- #endif /* Not good sprintf(). */
-#endif /* Not GNU C. */
-
/* Miscellaneous. */
void buf_reverse (char *, size_t);
@@ -232,4 +182,20 @@ ds_end (const struct string *st)
}
#endif
+#define nsprintf sprintf
+#define nvsprintf vsprintf
+
+static inline char *
+spprintf (char *dst, const char *format, ...)
+{
+ va_list args;
+ int count;
+
+ va_start (args, format);
+ count = nvsprintf (dst, format, args);
+ va_end (args);
+
+ return dst + count;
+}
+
#endif /* str_h */
diff -up /home/blp/pspp/feb17-1/src/libpspp/str.c\~
/home/blp/pspp/feb17-1/src/libpspp/str.c
--- /home/blp/pspp/feb17-1/src/libpspp/str.c~ 2006-02-20 16:10:47.000000000
-0800
+++ /home/blp/pspp/feb17-1/src/libpspp/str.c 2006-02-27 12:55:07.000000000
-0800
@@ -26,53 +26,6 @@
#include "alloc.h"
#include "pspp-error.h"
-/* sprintf() wrapper functions for convenience. */
-
-#if !__GNUC__
-char *
-spprintf (char *buf, const char *format,...)
-{
-#if HAVE_GOOD_SPRINTF
- int count;
-#endif
- va_list args;
-
- va_start (args, format);
-#if HAVE_GOOD_SPRINTF
- count =
-#endif
- vsprintf (buf, format, args);
- va_end (args);
-
-#if HAVE_GOOD_SPRINTF
- return &buf[count];
-#else
- return strchr (buf, 0);
-#endif
-}
-#endif /* !__GNUC__ */
-
-#if !__GNUC__ && !HAVE_GOOD_SPRINTF
-int
-nsprintf (char *buf, const char *format,...)
-{
- va_list args;
-
- va_start (args, format);
- vsprintf (buf, format, args);
- va_end (args);
-
- return strlen (buf);
-}
-
-int
-nvsprintf (char *buf, const char *format, va_list args)
-{
- vsprintf (buf, format, args);
- return strlen (buf);
-}
-#endif /* Not GNU C and not good sprintf(). */
-
/* Reverses the order of NBYTES bytes at address P, thus converting
between little- and big-endian byte orders. */
void
Diff finished at Mon Feb 27 13:00:23
--
"Unix... is not so much a product
as it is a painstakingly compiled oral history
of the hacker subculture."
--Neal Stephenson