/* __LINE__ */
char *ftepp_predef_line(lex_file *context) {
- char *value = (char*)mem_a(128);
- sprintf(value, "%d", (int)context->line);
+ char *value;
+ util_asprintf(&value, "%d", (int)context->line);
return value;
}
/* __FILE__ */
}
/* __COUNTER_LAST__ */
char *ftepp_predef_counterlast(lex_file *context) {
- char *value = (char*)mem_a(128);
- sprintf(value, "%u", ftepp_predef_countval);
+ char *value;
+ util_asprintf(&value, "%u", ftepp_predef_countval);
(void)context;
return value;
}
/* __COUNTER__ */
char *ftepp_predef_counter(lex_file *context) {
- char *value = (char*)mem_a(128);
+ char *value;
ftepp_predef_countval ++;
- sprintf(value, "%u", ftepp_predef_countval);
+ util_asprintf(&value, "%u", ftepp_predef_countval);
(void)context;
return value;
}
/* __RANDOM__ */
char *ftepp_predef_random(lex_file *context) {
- char *value = (char*)mem_a(128);
+ char *value;
ftepp_predef_randval = (util_rand() % 0xFF) + 1;
- sprintf(value, "%u", ftepp_predef_randval);
+ util_asprintf(&value, "%u", ftepp_predef_randval);
(void)context;
return value;
}
/* __RANDOM_LAST__ */
char *ftepp_predef_randomlast(lex_file *context) {
- char *value = (char*)mem_a(128);
- sprintf(value, "%u", ftepp_predef_randval);
+ char *value;
+ util_asprintf(&value, "%u", ftepp_predef_randval);
(void)context;
return value;
void util_seed(uint32_t);
uint32_t util_rand();
+int util_vasprintf(char **ret, const char *fmt, va_list);
+int util_asprintf (char **ret, const char *fmt, ...);
+
+
#ifdef NOTRACK
# define mem_a(x) malloc (x)
# define mem_d(x) free ((void*)x)
mem_d(ht);
}
+/*
+ * Portable implementation of vasprintf/asprintf. Assumes vsnprintf
+ * exists, otherwise compiler error.
+ */
+int util_vasprintf(char **ret, const char *fmt, va_list args) {
+ int read;
+ va_list copy;
+ va_copy(copy, args);
+
+ *ret = 0;
+ if ((read = vsnprintf(NULL, 0, fmt, args)) >= 0) {
+ char *buffer;
+ if ((buffer = (char*)mem_a(read + 1))) {
+ if ((read = vsnprintf(buffer, read + 1, fmt, copy)) < 0)
+ mem_d(buffer);
+ else
+ *ret = buffer;
+ }
+ }
+ va_end(copy);
+ return read;
+}
+int util_asprintf(char **ret, const char *fmt, ...) {
+ va_list args;
+ int read;
+ va_start(args, fmt);
+ read = util_vasprintf(ret, fmt, args);
+ va_end (args);
+
+ return read;
+}
+
/*
* Implementation of the Mersenne twister PRNG (pseudo random numer
* generator). Implementation of MT19937. Has a period of 2^19937-1