]> thyrsus.com Git - intercal.git/commitdiff
Add ick_snprintf_or_die
authorArvid Norlander <anmaster@tele2.se>
Wed, 19 Nov 2008 22:40:54 +0000 (17:40 -0500)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 26 Aug 2010 00:35:23 +0000 (20:35 -0400)
src/uncommon.c
src/uncommon.h

index 2ef57c224d62a471f548a0762d5e864377081f43..0dc93e303dd0500ea4bd4828f59e1d2e749c6d33 100644 (file)
@@ -21,6 +21,8 @@ LICENSE TERMS
 
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
+#include <ick_lose.h>
 #include "uncommon.h"
 
 /* Options that might affect this */
@@ -161,3 +163,24 @@ int ick_printfopens=0;
   else
     return NULL;
 }
+
+/**
+ * Invoke snprintf(), if supported. Otherwise invoke sprintf() and abort
+ * if we did overflow.
+ */
+int ick_snprintf_or_die(char *str, size_t size, const char *format, ...)
+{
+  va_list ap;
+  int retval;
+  va_start(ap, format);
+#ifdef HAVE_SNPRINTF
+  retval = vsnprintf(str, size, format, ap);
+#else
+  retval = vsprintf(str, format, ap);
+  /* TODO: Replace with some nice ick_lose(). */
+  if (retval >= size)
+    abort();
+#endif
+  va_end(ap);
+  return retval;
+}
index 2d879cdebc029ccf68c24d972575c8ce8d8dd5a9..3144680ffe03c711a8d5ca7b0841d45b8396c48d 100644 (file)
@@ -6,3 +6,8 @@
 /*@null@*/ /*@dependent@*/ extern FILE* ick_findandfopen(/*@observer@*/ const char*, /*@observer@*/ const char*, /*@observer@*/ const char*, /*@observer@*/ const char*);
 /*@observer@*/ /*@null@*/ extern const char* ick_findandtestopen(/*@observer@*/ const char*, /*@observer@*/ const char*, /*@observer@*/ const char*, /*@observer@*/ const char*);
 /*@null@*/ /*@dependent@*/ extern FILE* ick_findandfreopen(/*@observer@*/ const char*, /*@observer@*/ const char*, /*@observer@*/ const char*, /*@observer@*/ const char*, FILE*);
+extern int ick_snprintf_or_die(char *str, size_t size, const char *format, ...)
+#ifdef __GNUC__
+  __attribute__ ((format(printf, 3, 4)))
+#endif
+;