*/
#include <stdlib.h>
+#include <stdio.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
buf->s = n;
}
+char *decstr(char **p, size_t *len)
+{
+ char *p2, *ret;
+
+ for(p2 = *p; (p2 - *p) < *len; p2++) {
+ if(*p2 == 0)
+ break;
+ }
+ if((p2 - *p) == *len)
+ return(NULL);
+ p2++;
+ ret = *p;
+ *len -= p2 - *p;
+ *p = p2;
+ return(ret);
+}
+
+char *vsprintf2(char *format, va_list al)
+{
+ int ret;
+ char *buf;
+ va_list al2;
+
+ va_copy(al2, al);
+ ret = vsnprintf(NULL, 0, format, al2);
+ va_end(al2);
+ buf = smalloc(ret + 1);
+ va_copy(al2, al);
+ vsnprintf(buf, ret + 1, format, al2);
+ va_end(al2);
+ return(buf);
+}
+
+char *sprintf2(char *format, ...)
+{
+ va_list args;
+ char *buf;
+
+ va_start(args, format);
+ buf = vsprintf2(format, args);
+ va_end(args);
+ return(buf);
+}
+
+char *sprintf3(char *format, ...)
+{
+ static char *buf = NULL;
+ va_list args;
+
+ if(buf != NULL)
+ free(buf);
+ va_start(args, format);
+ buf = vsprintf2(format, args);
+ va_end(args);
+ return(buf);
+}
#ifndef _UTILS_H
#define _UTILS_H
+#include <stdarg.h>
+
#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
#define szmalloc(size) memset(smalloc(size), 0, size)
-#define sstrdup(str) ({char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
+#define sstrdup(str) ({const char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
#define omalloc(o) ((o) = szmalloc(sizeof(*(o))))
#define bufinit(buf) memset(&(buf), 0, sizeof(buf))
memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
(buf).d += __bufcat_size__; \
} while(0)
-#define bufcatstr2(buf, str) \
+#define bufcatstr(buf, str) \
do { \
char *__buf__; \
__buf__ = (str); \
bufcat((buf), __buf__, strlen(__buf__)); \
} while(0)
+#define bufcatstr2(buf, str) \
+do { \
+ char *__buf__; \
+ __buf__ = (str); \
+ bufcat((buf), __buf__, strlen(__buf__) + 1); \
+} while(0)
+#define bufeat(buf, len) memmove((buf).b, (buf).b + (len), (buf).d -= (len))
struct buffer {
void *b;
};
void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
+char *decstr(char **p, size_t *len);
+char *vsprintf2(char *format, va_list al);
+char *sprintf2(char *format, ...);
+char *sprintf3(char *format, ...);
#endif