summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnas Nashif <anas.nashif@intel.com>2012-11-05 12:56:51 -0800
committerAnas Nashif <anas.nashif@intel.com>2012-11-05 12:56:51 -0800
commit300d4816804c8ceb4a4601a49ec3ec479c1951b5 (patch)
tree7164fe3883806d0d464103fd37dcebbdd2855a1e /lib
downloadnasm-300d4816804c8ceb4a4601a49ec3ec479c1951b5.tar.gz
nasm-300d4816804c8ceb4a4601a49ec3ec479c1951b5.tar.bz2
nasm-300d4816804c8ceb4a4601a49ec3ec479c1951b5.zip
Imported Upstream version 2.09.08upstream/2.09.08
Diffstat (limited to 'lib')
-rw-r--r--lib/snprintf.c25
-rw-r--r--lib/strlcpy.c53
-rw-r--r--lib/vsnprintf.c46
3 files changed, 124 insertions, 0 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c
new file mode 100644
index 0000000..01734dc
--- /dev/null
+++ b/lib/snprintf.c
@@ -0,0 +1,25 @@
+/*
+ * snprintf()
+ *
+ * Implement snprintf() in terms of vsnprintf()
+ */
+
+#include "compiler.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#include "nasmlib.h"
+
+int snprintf(char *str, size_t size, const char *format, ...)
+{
+ va_list ap;
+ int rv;
+
+ va_start(ap, format);
+ rv = vsnprintf(str, size, format, ap);
+ va_end(ap);
+
+ return rv;
+}
diff --git a/lib/strlcpy.c b/lib/strlcpy.c
new file mode 100644
index 0000000..4335359
--- /dev/null
+++ b/lib/strlcpy.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include "compiler.h"
+
+/*
+ * Copy src to string dst of size siz. At most siz-1 characters
+ * will be copied. Always NUL terminates (unless siz == 0).
+ * Returns strlen(src); if retval >= siz, truncation occurred.
+ */
+#ifndef HAVE_STRLCPY
+
+size_t strlcpy(char *dst, const char *src, size_t siz)
+{
+ char *d = dst;
+ const char *s = src;
+ size_t n = siz;
+
+ /* Copy as many bytes as will fit */
+ if (n != 0) {
+ while (--n != 0) {
+ if ((*d++ = *s++) == '\0')
+ break;
+ }
+ }
+
+ /* Not enough room in dst, add NUL and traverse rest of src */
+ if (n == 0) {
+ if (siz != 0)
+ *d = '\0'; /* NUL-terminate dst */
+ while (*s++)
+ ;
+ }
+
+ return(s - src - 1); /* count does not include NUL */
+}
+
+#endif
diff --git a/lib/vsnprintf.c b/lib/vsnprintf.c
new file mode 100644
index 0000000..bc54f94
--- /dev/null
+++ b/lib/vsnprintf.c
@@ -0,0 +1,46 @@
+/*
+ * vsnprintf()
+ *
+ * Poor substitute for a real vsnprintf() function for systems
+ * that don't have them...
+ */
+
+#include "compiler.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include "nasmlib.h"
+
+#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
+
+static char snprintf_buffer[BUFFER_SIZE];
+
+int vsnprintf(char *str, size_t size, const char *format, va_list ap)
+{
+ int rv, bytes;
+
+ if (size > BUFFER_SIZE) {
+ nasm_error(ERR_PANIC|ERR_NOFILE,
+ "vsnprintf: size (%d) > BUFFER_SIZE (%d)",
+ size, BUFFER_SIZE);
+ size = BUFFER_SIZE;
+ }
+
+ rv = vsprintf(snprintf_buffer, format, ap);
+ if (rv >= BUFFER_SIZE)
+ nasm_error(ERR_PANIC|ERR_NOFILE, "vsnprintf buffer overflow");
+
+ if (size > 0) {
+ if ((size_t)rv < size-1)
+ bytes = rv;
+ else
+ bytes = size-1;
+ memcpy(str, snprintf_buffer, bytes);
+ str[bytes] = '\0';
+ }
+
+ return rv;
+}