int execute(char **output, const char *fmt, ...) { va_list ap; FILE *fp; char *command; static char outbuf[OUTBUF_SIZE]; unsigned int answer_len; int i; va_start(ap, fmt); assert(vasprintf(&command, fmt, ap) > 0); va_end(ap); #if defined(DEBUG) printf("COMMAND => %s\n", command); #endif assert(fp = popen(command, "r")); fgets(outbuf, sizeof(outbuf)-1, fp); if (output) { answer_len = strnlen(outbuf, OUTBUF_SIZE - 1); assert(answer_len <= (OUTBUF_SIZE - 1)); for (i = 0; i <= answer_len; i++) if (outbuf[i] == '\n') outbuf[i] = '\0'; #if defined(DEBUG) printf("RESULT => %s\n" "answer_len => %u\n", outbuf, answer_len); #endif if (answer_len > 1) { *output = malloc(answer_len); strcpy(*output, outbuf); } else *output = 0; } free(command); return WEXITSTATUS(pclose(fp)); }
int dpl_dbuf_add_printf(dpl_dbuf_t *nbuf, const char *fmt, ...) { char *str = NULL; unsigned char *data = NULL; int len; int ret; va_list ap; va_start(ap, fmt); len = vasprintf(&str, fmt, ap); if (len == -1) { ret = 0; goto end; } data = buf_recompute_length(nbuf, len); if (NULL == data) { ret = 0; goto end; } memcpy(data + nbuf->data_max, str, len); nbuf->data = data; nbuf->data_max += len; ret = 1; end: free (str); va_end(ap); return ret; }
const wchar_t * wfmt(const char * format, ...) { static wchar_t strWChar[512]; strWChar[0] = 0; if(!format) return (const wchar_t *) strWChar; if(strcmp(format, "") == 0) return (const wchar_t *) strWChar; char * tmp = NULL; va_list va; va_start(va, format); if((vasprintf(&tmp, format, va) >= 0) && tmp) { int bt; int strlength = strlen(tmp); bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512 ); free(tmp); tmp = 0; if(bt > 0) { strWChar[bt] = 0; return (const wchar_t *) strWChar; } } va_end(va); if(tmp) free(tmp); return (const wchar_t *) strWChar; }
static jl_value_t *jl_vexceptionf(jl_datatype_t *exception_type, const char *fmt, va_list args) { if (exception_type == NULL) { jl_printf(JL_STDERR, "ERROR: "); jl_vprintf(JL_STDERR, fmt, args); jl_printf(JL_STDERR, "\n"); jl_exit(1); } char *str = NULL; int ok = vasprintf(&str, fmt, args); jl_value_t *msg; if (ok < 0) { // vasprintf failed msg = jl_cstr_to_string("internal error: could not display error message"); } else { msg = jl_pchar_to_string(str, strlen(str)); free(str); } JL_GC_PUSH1(&msg); jl_value_t *e = jl_new_struct(exception_type, msg); JL_GC_POP(); return e; }
SOL_API int sol_buffer_insert_vprintf(struct sol_buffer *buf, size_t pos, const char *fmt, va_list args) { char *s; ssize_t len; struct sol_str_slice slice; int r; SOL_NULL_CHECK(buf, -EINVAL); SOL_NULL_CHECK(fmt, -EINVAL); SOL_INT_CHECK(pos, > buf->used, -EINVAL); if (pos == buf->used) return sol_buffer_append_vprintf(buf, fmt, args); len = vasprintf(&s, fmt, args); if (len < 0) return -errno; slice = SOL_STR_SLICE_STR(s, len); r = sol_buffer_insert_slice(buf, pos, slice); free(s); return r; }
void check_string_literal( FILE* fp, const char* s, char *buf, ... ) { char * b; va_list ap; va_start(ap,buf); printf(s); // expected-warning {{format string is not a string literal}} vprintf(s,ap); // expected-warning {{format string is not a string literal}} fprintf(fp,s); // expected-warning {{format string is not a string literal}} vfprintf(fp,s,ap); // expected-warning {{format string is not a string literal}} asprintf(&b,s); // expected-warning {{format string is not a string lit}} vasprintf(&b,s,ap); // expected-warning {{format string is not a string literal}} sprintf(buf,s); // expected-warning {{format string is not a string literal}} snprintf(buf,2,s); // expected-warning {{format string is not a string lit}} __builtin___sprintf_chk(buf,0,-1,s); // expected-warning {{format string is not a string literal}} __builtin___snprintf_chk(buf,2,0,-1,s); // expected-warning {{format string is not a string lit}} vsprintf(buf,s,ap); // expected-warning {{format string is not a string lit}} vsnprintf(buf,2,s,ap); // expected-warning {{format string is not a string lit}} vsnprintf(buf,2,global_fmt,ap); // expected-warning {{format string is not a string literal}} __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // expected-warning {{format string is not a string lit}} __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}} vscanf(s, ap); // expected-warning {{format string is not a string literal}} // rdar://6079877 printf("abc" "%*d", 1, 1); // no-warning printf("abc\ def" "%*d", 1, 1); // no-warning // <rdar://problem/6079850>, allow 'unsigned' (instead of 'int') to be used for both // the field width and precision. This deviates from C99, but is reasonably safe // and is also accepted by GCC. printf("%*d", (unsigned) 1, 1); // no-warning }
void GeoIP_printf(void (*f)(char *), const char *str,...) { va_list params; char * f_str; int silence; if (f == NULL) return; va_start(params, str); #if defined(HAVE_VASPRINTF) silence = vasprintf(&f_str, str, params); #elif defined (HAVE_VSNPRINTF) f_str = malloc(4096); if ( f_str ) silence = vsnprintf(f_str, 4096, str, params); #else f_str = malloc(4096); if ( f_str ) silence = vsprintf(f_str, str, params); #endif va_end(params); if ( f_str == NULL ) return; (*f)(f_str); free(f_str); }
bool write_ribfile(const NonDestructiveTriMesh &mesh, const std::vector<float> &x, const char *filename_format, ...) { #ifdef _MSC_VER va_list ap; va_start(ap, filename_format); int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count +1; // terminating '\0' char *filename=new char[len]; vsprintf(filename, filename_format, ap); std::ofstream output(filename, std::ofstream::binary); delete[] filename; va_end(ap); #else va_list ap; va_start(ap, filename_format); char *filename; vasprintf(&filename, filename_format, ap); std::ofstream output(filename, std::ofstream::binary); std::free(filename); va_end(ap); #endif if(!output.good()) return false; return write_ribfile(mesh, x, output); }
static void text_draw(float x, float y, const char* fmt, ...) { char* string, *ch; va_list args; va_start(args, fmt); if (-1 == vasprintf(&string, fmt, args)) return; for (ch = string; *ch; ++ch) { struct FONT_Glyph glyph; uint16_t u, v; GLYPH_Get(*ch, &glyph, &u, &v); draw_quad_st(GLYPH_Texture(), x - glyph.x, y - glyph.y, glyph.width, glyph.height, (float) u / GLYPH_ATLAS_SIZE, (float) v / GLYPH_ATLAS_SIZE, (float)(u + glyph.width) / GLYPH_ATLAS_SIZE, (float)(v + glyph.height) / GLYPH_ATLAS_SIZE); x += glyph.xOffset; } free(string); }
int sock_send_reply(int socket, int reply_code, char *reply, ...) { msg mess; char *ans; memset(&mess, 0, sizeof(msg)); va_list ap; va_start(ap, reply); if (vasprintf(&ans, reply, ap) < 0) { log(LOG_WARNING, "vasprintf error : %m"); va_end(ap); return (1); } va_end(ap); mess.op = OP_ANSWER; mess.version = SAUTHPF_PROTO_VERSION; mess.data.answer.answer_code = reply_code; strlcpy(mess.data.answer.answer_msg, ans, sizeof(mess.data.answer.answer_msg)); free(ans); if (sock_send_msg(socket, &mess, NULL)) return (1); return (0); }
char * xvasprintf (const char *format, va_list args) { char *result; /* Recognize the special case format = "%s...%s". It is a frequently used idiom for string concatenation and needs to be fast. We don't want to have a separate function xstrcat() for this purpose. */ { size_t argcount = 0; const char *f; for (f = format;;) { if (*f == '\0') /* Recognized the special case of string concatenation. */ return xstrcat (argcount, args); if (*f != '%') break; f++; if (*f != 's') break; f++; argcount++; } } if (vasprintf (&result, format, args) < 0) { if (errno == ENOMEM) xalloc_die (); return NULL; } return result; }
/* Debug messages. */ void guestfs___debug (guestfs_h *g, const char *fs, ...) { va_list args; char *msg; int len; /* The cpp macro "debug" has already checked that g->verbose is true * before calling this function, but we check it again just in case * anyone calls this function directly. */ if (!g->verbose) return; va_start (args, fs); len = vasprintf (&msg, fs, args); va_end (args); if (len < 0) return; guestfs___call_callbacks_message (g, GUESTFS_EVENT_LIBRARY, msg, len); free (msg); }
static int perf_gtk__error(const char *format, va_list args) { char *msg; GtkWidget *dialog; if (!perf_gtk__is_active_context(pgctx) || vasprintf(&msg, format, args) < 0) { fprintf(stderr, "Error:\n"); vfprintf(stderr, format, args); fprintf(stderr, "\n"); return -1; } dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(pgctx->main_window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "<b>Error</b>\n\n%s", msg); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); free(msg); return 0; }
ssize_t exechelp_log(const char *id, const char *fmt, va_list args) { pthread_mutex_lock(&log_mutex); ssize_t ret = 0; char *buf; char *buf2; int fd = exechelp_log_get_handle(id, 0); if (fd == -1) { pthread_mutex_unlock(&log_mutex); return -1; } if (vasprintf(&buf, fmt, args) == -1) { pthread_mutex_unlock(&log_mutex); return -1; } if (id) { if (asprintf(&buf2, "[%s] %s", id, buf) == -1) { free(buf); pthread_mutex_unlock(&log_mutex); return -1; } ret += write(fd, buf2, strlen(buf2)); free(buf2); free(buf); } else { ret += write(fd, buf, strlen(buf)); free(buf); } pthread_mutex_unlock(&log_mutex); return ret; }
void rset_setfield(rset_t* rset, unsigned int field, const char* fmt, ...) { char* str = NULL; va_list ap; if (NULL == rset || NULL == fmt || NULL == rset->fields) { errno = EFAULT; return; } if (field >= rset->num_fields) { errno = EINVAL; return; } va_start(ap, fmt); vasprintf(&str, fmt, ap); va_end(ap); if (NULL == str) return; free(rset->fields[field]); rset->fields[field] = str; errno = 0; }
void reply_with_perror_errno (int err, const char *fs, ...) { CLEANUP_FREE char *buf1 = NULL; CLEANUP_FREE char *buf2 = NULL; va_list args; int r; va_start (args, fs); r = vasprintf (&buf1, fs, args); va_end (args); if (r == -1) { error: perror ("vasprintf"); exit (EXIT_FAILURE); } r = asprintf (&buf2, "%s: %s", buf1, strerror (err)); if (r == -1) goto error; send_error (err, buf2); }
struct progressCBdata *winProgressBar(int width, int height, char *title, char *text, ...) { struct progressCBdata *data; char *buf = NULL; va_list args; int llen; newtComponent t, f, scale, label; va_start(args, text); if (vasprintf(&buf, text, args) != -1) { va_end(args); newtCenteredWindow(width, height, title); t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP); newtTextboxSetText(t, buf); llen = strlen(buf); free(buf); label = newtLabel(llen + 1, 1, "-"); f = newtForm(NULL, NULL, 0); newtFormAddComponent(f, t); scale = newtScale(3, 3, width - 6, 100); newtFormAddComponent(f, scale); newtDrawForm(f); newtRefresh(); if ((data = malloc(sizeof(struct progressCBdata))) == NULL) { logMessage(ERROR, "%s: %d: %m", __func__, __LINE__); abort(); } data->scale = scale; data->label = label; return data; } return NULL; }
dbi_result dbi_conn_queryf(dbi_conn Conn, const char *formatstr, ...) { dbi_conn_t *conn = Conn; char *statement; dbi_result_t *result; va_list ap; if (!conn || !(conn->connection)) return NULL; _reset_conn_error(conn); va_start(ap, formatstr); vasprintf(&statement, formatstr, ap); va_end(ap); _logquery(conn, "[queryf] %s\n", statement); result = conn->driver->functions->query(conn, statement); if (result == NULL) { _error_handler(conn, DBI_ERROR_DBD); } free(statement); return (dbi_result)result; }
/* * Send output to our connected socket (jailproc most likely) * via a printf-style interface */ void transmit(const char *fmt, ...) { ssize_t sent = 0; int bytes_to_send; va_list ap; char *sstring; char *my_string; /* Combine fmt with our given arguments */ va_start(ap, fmt); bytes_to_send = vasprintf(&my_string, fmt, ap); va_end(ap); sstring = my_string; if (bytes_to_send != -1) { for ( ; ; ) { sent = send(active_fd, sstring, bytes_to_send, NULL); if (sent == -1) { dk_err("send(2) error"); // goto get_out; break; } if (sent == bytes_to_send) { // goto get_out; break; } bytes_to_send -= sent; sstring = &my_string[sent]; } } // get_out: free(my_string); }
/* * Like printf, only we append to a buffer. */ protected int file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) { int len; char *buf, *newstr; len = vasprintf(&buf, fmt, ap); if (len < 0) goto out; if (ms->o.buf != NULL) { len = asprintf(&newstr, "%s%s", ms->o.buf, buf); free(buf); if (len < 0) goto out; free(ms->o.buf); buf = newstr; } ms->o.buf = buf; return 0; out: file_error(ms, errno, "vasprintf failed"); return -1; }
static int dialog_display_error_va(vlc_dialog_provider *p_provider, const char *psz_title, const char *psz_fmt, va_list ap) { vlc_mutex_lock(&p_provider->lock); if (p_provider->cbs.pf_display_error == NULL) { vlc_mutex_unlock(&p_provider->lock); return VLC_EGENERIC; } char *psz_text; if (vasprintf(&psz_text, psz_fmt, ap) == -1) { vlc_mutex_unlock(&p_provider->lock); return VLC_ENOMEM; } p_provider->cbs.pf_display_error(p_provider->p_cbs_data, psz_title, psz_text); free(psz_text); vlc_mutex_unlock(&p_provider->lock); return VLC_SUCCESS; }
extern "C" void ThrowMsg(const char * title, const char * format, ...) { if(!title && !format) { return; } else if(title && !format) { throwMessageHandler.ThrowMessage(title, NULL); return; } char *tmp=0; va_list va; va_start(va, format); if((vasprintf(&tmp, format, va)>=0) && tmp) { throwMessageHandler.ThrowMessage(title, tmp); } va_end(va); if(tmp) free(tmp); }
void __nsock_log_internal(nsock_pool nsp, nsock_loglevel_t loglevel, const char *file, int line, const char *func, const char *format, ...) { struct nsock_log_rec rec; va_list args; int rc; va_start(args, format); rec.level = loglevel; rec.time = nsock_tod; rec.file = file; rec.line = line; rec.func = func; rc = vasprintf(&rec.msg, format, args); if (rc >= 0) { mspool *ms = (mspool *)nsp; ms->logger(nsp, &rec); free(rec.msg); } va_end(args); }
void bb_verror_msg(const char *s, va_list p, const char* strerr) { /* va_copy is used because it is not portable * to use va_list p twice */ va_list p2; va_copy(p2, p); if (!s) /* nomsg[_and_die] uses NULL fmt */ s = ""; /* some libc don't like printf(NULL) */ if (logmode & LOGMODE_STDIO) { fflush(stdout); fprintf(stderr, "%s: ", applet_name); vfprintf(stderr, s, p); if (!strerr) fputs(msg_eol, stderr); else fprintf(stderr, "%s%s%s", s[0] ? ": " : "", strerr, msg_eol); } if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) { if (!strerr) vsyslog(LOG_ERR, s, p2); else { char *msg; if (vasprintf(&msg, s, p2) < 0) { fprintf(stderr, "%s: %s\n", applet_name, bb_msg_memory_exhausted); xfunc_die(); } syslog(LOG_ERR, "%s: %s", msg, strerr); free(msg); } } va_end(p2); }
SOL_API int sol_flow_send_error_packet(struct sol_flow_node *src, int code, const char *msg_fmt, ...) { struct sol_flow_packet *packet; va_list args; char *msg = NULL; if (msg_fmt) { int r; va_start(args, msg_fmt); r = vasprintf(&msg, msg_fmt, args); if (r < 0) { SOL_WRN("Failed to alloc error msg"); msg = NULL; } va_end(args); } packet = sol_flow_packet_new_error(code, msg); free(msg); SOL_NULL_CHECK(packet, -ENOMEM); return sol_flow_send_packet(src, SOL_FLOW_NODE_PORT_ERROR, packet); }
/** Execute string. * @param format format of string to execute, arguments can be the same as * for vasprintf. */ void LuaContext::do_string(const char *format, ...) { MutexLocker lock(__lua_mutex); va_list arg; va_start(arg, format); char *s; if (vasprintf(&s, format, arg) == -1) { throw Exception("LuaContext::do_string: Could not form string"); } int rv = 0; int errfunc = __enable_tracebacks ? 1 : 0; rv = (luaL_loadstring(__L, s) || lua_pcall(__L, 0, LUA_MULTRET, errfunc)); free(s); va_end(arg); if ( rv != 0 ) { std::string errmsg = lua_tostring(__L, -1); lua_pop(__L, 1); throw LuaRuntimeException("do_string", errmsg.c_str()); } }
void pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...) { char *fp, *extra; va_list ap; int i; extra = NULL; if (fmt != NULL) { va_start(ap, fmt); i = vasprintf(&extra, fmt, ap); va_end(ap); if (i < 0 || extra == NULL) fatal("%s: vasprintf failed", __func__); } if (key_is_cert(key)) { fp = sshkey_fingerprint(key->cert->signature_key, options.fingerprint_hash, SSH_FP_DEFAULT); auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s", key_type(key), key->cert->key_id, (unsigned long long)key->cert->serial, key_type(key->cert->signature_key), fp == NULL ? "(null)" : fp, extra == NULL ? "" : ", ", extra == NULL ? "" : extra); free(fp); } else { fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT); auth_info(authctxt, "%s %s%s%s", key_type(key), fp == NULL ? "(null)" : fp, extra == NULL ? "" : ", ", extra == NULL ? "" : extra); free(fp); } free(extra); }
static void ferrout(char *fmt, ...) { sigset_t block, oblock; struct ferrlist *f; va_list ap; char *p; va_start(ap, fmt); if (ferr == 0) vfprintf(stderr, fmt, ap); else { sigemptyset(&block); sigaddset(&block, SIGINT); sigprocmask(SIG_BLOCK, &block, &oblock); if (vasprintf(&p, fmt, ap) == -1 || (f = malloc(sizeof(*f))) == NULL) { va_end(ap); va_start(ap, fmt); flsh_errs(); vfprintf(stderr, fmt, ap); fputs("pr: memory allocation failed\n", stderr); exit(1); } f->next = NULL; f->buf = p; if (ferrhead == NULL) ferrhead = f; if (ferrtail) ferrtail->next = f; ferrtail = f; sigprocmask(SIG_SETMASK, &oblock, NULL); } va_end(ap); }
int sec_vfprintf(FILE * f, const char *fmt, va_list ap) { char *buf = NULL; void *enc; int len; if (!ftp->sec_complete) return vfprintf(f, fmt, ap); if (vasprintf(&buf, fmt, ap) == -1) { printf(_("Failed to allocate memory.\n")); return -1; } len = (*ftp->mech->encode) (ftp->app_data, buf, strlen(buf), ftp->command_prot, &enc); if (len < 0) { printf(_("Failed to encode command.\n")); free(buf); return -1; } if (b64_encode(enc, len, &buf) < 0) { printf(_("Out of memory base64-encoding.\n")); free(buf); return -1; } if (ftp->command_prot == prot_safe) fprintf(f, "MIC %s", buf); else if (ftp->command_prot == prot_private) fprintf(f, "ENC %s", buf); else if (ftp->command_prot == prot_confidential) fprintf(f, "CONF %s", buf); free(buf); return 0; }
static void _vdlogf(FILE *io, const char *file, int line, const char *fn, const char *fmt, va_list ap) { char *msg, tstamp[128]; int n; struct tm t; time_t now = time(NULL); if (gmtime_r(&now, &t) == NULL) { pgr_abort(ABORT_UNKNOWN); } n = strftime(tstamp, sizeof(tstamp), "%c", &t); if (n < 0) { pgr_abort(ABORT_UNKNOWN); } n = vasprintf(&msg, fmt, ap); if (n < 0) { pgr_abort(ABORT_MEMFAIL); } fprintf(io, "[%s] DEBUG %s:%d %s() - %s\n", tstamp, file, line, fn, msg); free(msg); }