/* Write a line to the io write buffer from a va_list. */ void io_vwriteline(struct io *io, const char *fmt, va_list ap) { int n; va_list aq; if (io->error != NULL) return; IO_DEBUG(io, "in: wr: used=%zu, free=%zu", BUFFER_USED(io->wr), BUFFER_FREE(io->wr)); if (fmt != NULL) { va_copy(aq, ap); n = xvsnprintf(NULL, 0, fmt, aq); va_end(aq); buffer_ensure(io->wr, n + 1); xvsnprintf(BUFFER_IN(io->wr), n + 1, fmt, ap); buffer_add(io->wr, n); } else n = 0; io_write(io, io->eol, strlen(io->eol)); IO_DEBUG(io, "out: %zu bytes, wr: used=%zu, free=%zu", n + strlen(io->eol), BUFFER_USED(io->wr), BUFFER_FREE(io->wr)); }
void strb_vadd(struct strb **sbp, const char *key, const char *value, va_list ap) { struct strb *sb = *sbp; size_t size, keylen, valuelen; u_int n; struct strbent sbe, *sbep; va_list aq; keylen = strlen(key) + 1; va_copy(aq, ap); valuelen = xvsnprintf(NULL, 0, value, aq) + 1; va_end(aq); size = sb->str_size; while (sb->str_size - sb->str_used < keylen + valuelen) { if (STRB_SIZE(sb) > SIZE_MAX / 2) fatalx("size too large"); sb->str_size *= 2; } if (size != sb->str_size) { sb = *sbp = xrealloc(sb, 1, STRB_SIZE(sb)); memmove( STRB_ENTBASE(sb), STRB_BASE(sb) + size, STRB_ENTSIZE(sb)); memset(STRB_BASE(sb) + size, 0, sb->str_size - size); } sbep = strb_address(sb, key); if (sbep == NULL) { if (sb->ent_used > sb->ent_max) { /* Allocate some more entries. */ n = sb->ent_max; size = STRB_SIZE(sb); if (sb->ent_max > UINT_MAX / 2) fatalx("ent_max too large"); sb->ent_max *= 2; if (STRB_SIZE(sb) < size) fatalx("size too large"); sb = *sbp = xrealloc(sb, 1, STRB_SIZE(sb)); memset(STRB_ENTRY(sb, n), 0, STRB_ENTSIZE(sb) / 2); } sbep = STRB_ENTRY(sb, sb->ent_used); sb->ent_used++; sbe.key = sb->str_used; memcpy(STRB_KEY(sb, &sbe), key, keylen); sb->str_used += keylen; } else memcpy(&sbe, sbep, sizeof sbe); sbe.value = sb->str_used; xvsnprintf(STRB_VALUE(sb, &sbe), valuelen, value, ap); sb->str_used += valuelen; memcpy(sbep, &sbe, sizeof sbe); }
/* Make path into buffer. */ int vppath(char *buf, size_t len, const char *fmt, va_list ap) { if ((size_t) xvsnprintf(buf, len, fmt, ap) >= len) { errno = ENAMETOOLONG; return (-1); } return (0); }
/* * Error reporting function. Use it like printf. If formats the message * into a buffer, and sets things up so that the next call to dlerror() * will return the message. */ void _rtld_error(const char *fmt, ...) { static char buf[512]; va_list ap; va_start(ap, fmt); xvsnprintf(buf, sizeof buf, fmt, ap); error_message = buf; va_end(ap); }
int fmtstr(char *outbuf, size_t length, const char *fmt, ...) { va_list ap; int ret; va_start(ap, fmt); ret = xvsnprintf(outbuf, length, fmt, ap); va_end(ap); return ret; }
int maxlen_snprintf(char *str, const char *format, ...) { va_list arglist; int retval; va_start(arglist, format); retval = xvsnprintf(str, MAXLEN, format, arglist); va_end(arglist); return retval; }
int printflike3 xsnprintf(char *buf, size_t len, const char *fmt, ...) { va_list ap; int i; va_start(ap, fmt); i = xvsnprintf(buf, len, fmt, ap); va_end(ap); return (i); }
int sqlquery_snprintf(char *str, const char *format, ...) { va_list arglist; int retval; va_start(arglist, format); retval = xvsnprintf(str, QUERY_STR_LEN, format, arglist); va_end(arglist); return retval; }
int xsnprintf(char *str, size_t size, const char *format, ...) { va_list arglist; int retval; va_start(arglist, format); retval = xvsnprintf(str, size, format, arglist); va_end(arglist); return retval; }
int xsnprintf(char *str, size_t len, const char *fmt, ...) { va_list ap; int i; va_start(ap, fmt); i = xvsnprintf(str, len, fmt, ap); va_end(ap); return i; }
/* our version of snprintf */ int #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS)) _xsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, ...) #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */ xsnprintf(char *str, size_t size, const char *format, ...) #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */ { va_list ap; int ret = 0; va_start(ap, format); #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS)) ret = _xvsnprintf(filename, lineno, str, size, format, ap); #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */ ret = xvsnprintf(str, size, format, ap); #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */ va_end(ap); return ret; }
void compileError(const char *fmt, ... ) { va_list argptr; char msg[MAXERROR]; Int16 linenum,charnum; compile_error=TRUE; va_start(argptr,fmt); xvsnprintf(msg,(Int32)MAXERROR,fmt,argptr); va_end(argptr); linenum = GetLineNum()+1; charnum = GetCharNum(); xprintf("compile error: %s\n",msg); xprintf("near line: %d\n",linenum); }
static void real_plog(int lvl, const char *fmt, va_list vargs) { char msg[1024]; char efmt[1024]; char *ptr = msg; static char last_msg[1024]; static int last_count = 0, last_lvl = 0; if (!(xlog_level & lvl)) return; #ifdef DEBUG_MEM # if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_VERIFY) checkup_mem(); # endif /* not defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_VERIFY) */ #endif /* DEBUG_MEM */ /* * Note: xvsnprintf() may call plog() if a truncation happened, but the * latter has some code to break out of an infinite loop. See comment in * xsnprintf() below. */ xvsnprintf(ptr, 1023, expand_error(fmt, efmt, 1024), vargs); ptr += strlen(ptr); if (*(ptr-1) == '\n') *--ptr = '\0'; #ifdef HAVE_SYSLOG if (syslogging) { switch (lvl) { /* from mike <*****@*****.**> */ case XLOG_FATAL: lvl = LOG_CRIT; break; case XLOG_ERROR: lvl = LOG_ERR; break; case XLOG_USER: lvl = LOG_WARNING; break; case XLOG_WARNING: lvl = LOG_WARNING; break; case XLOG_INFO: lvl = LOG_INFO; break; case XLOG_DEBUG: lvl = LOG_DEBUG; break; case XLOG_MAP: lvl = LOG_DEBUG; break; case XLOG_STATS: lvl = LOG_INFO; break; default: lvl = LOG_ERR; break; } syslog(lvl, "%s", msg); return; } #endif /* HAVE_SYSLOG */ *ptr++ = '\n'; *ptr = '\0'; /* * mimic syslog behavior: only write repeated strings if they differ */ switch (last_count) { case 0: /* never printed at all */ last_count = 1; if (strlcpy(last_msg, msg, sizeof(last_msg)) >= sizeof(last_msg)) /* don't use xstrlcpy here (recursive!) */ fprintf(stderr, "real_plog: string \"%s\" truncated to \"%s\"\n", last_msg, msg); last_lvl = lvl; show_time_host_and_name(lvl); /* mimic syslog header */ __IGNORE(fwrite(msg, ptr - msg, 1, logfp)); fflush(logfp); break; case 1: /* item printed once, if same, don't repeat */ if (STREQ(last_msg, msg)) { last_count++; } else { /* last msg printed once, new one differs */ /* last_count remains at 1 */ if (strlcpy(last_msg, msg, sizeof(last_msg)) >= sizeof(last_msg)) /* don't use xstrlcpy here (recursive!) */ fprintf(stderr, "real_plog: string \"%s\" truncated to \"%s\"\n", last_msg, msg); last_lvl = lvl; show_time_host_and_name(lvl); /* mimic syslog header */ __IGNORE(fwrite(msg, ptr - msg, 1, logfp)); fflush(logfp); } break; case 100: /* * Don't allow repetitions longer than 100, so you can see when something * cycles like crazy. */ show_time_host_and_name(last_lvl); xsnprintf(last_msg, sizeof(last_msg), "last message repeated %d times\n", last_count); __IGNORE(fwrite(last_msg, strlen(last_msg), 1, logfp)); fflush(logfp); last_count = 0; /* start from scratch */ break; default: /* item repeated multiple times */ if (STREQ(last_msg, msg)) { last_count++; } else { /* last msg repeated+skipped, new one differs */ show_time_host_and_name(last_lvl); xsnprintf(last_msg, sizeof(last_msg), "last message repeated %d times\n", last_count); __IGNORE(fwrite(last_msg, strlen(last_msg), 1, logfp)); if (strlcpy(last_msg, msg, 1024) >= 1024) /* don't use xstrlcpy here (recursive!) */ fprintf(stderr, "real_plog: string \"%s\" truncated to \"%s\"\n", last_msg, msg); last_count = 1; last_lvl = lvl; show_time_host_and_name(lvl); /* mimic syslog header */ __IGNORE(fwrite(msg, ptr - msg, 1, logfp)); fflush(logfp); } break; } }
*dst_len_out = len_out; } return dst; } int xsnprintf( char * restrict s, size_t n, const char * restrict format, ...) { va_list arg; va_start(arg, format); int ret = xvsnprintf(s, n, format, arg); va_end(arg); return ret; } int xvsnprintf( char * restrict s, size_t n, const char * restrict format, va_list arg) { int ret = vsnprintf(s, n, format, arg); if ((ret < 0) || ((size_t)ret >= n)) { abort();