Example #1
0
/* Issue a debug message. */
void
AG_Debug(void *p, const char *fmt, ...)
{
#ifdef AG_DEBUG
	AG_Object *obj = p;
	va_list args;
	
	if (obj != NULL && obj->debugFn != NULL) {
		char *buf, *p;

		va_start(args, fmt);
		Vasprintf(&buf, fmt, args);
		p = &buf[strlen(buf)-1];
		if (*p == '\n') { *p = '\0'; }
		obj->debugFn(obj, obj->debugPtr, buf);
		Free(buf);
		va_end(args);
	} else {
		if (agDebugLvl >= 1 || (obj != NULL && OBJECT_DEBUG(obj))) {
			va_start(args, fmt);
			if (obj != NULL) {
				if (OBJECT(obj)->name[0] != '\0') {
					printf("%s: ", OBJECT(obj)->name);
				} else {
					printf("<%p>: ", obj);
				}
			}
			vprintf(fmt, args);
			va_end(args);
		}
	}
#endif /* AG_DEBUG */
}
Example #2
0
static int parse_env_file_push(const char *key, char *value, void *userdata) {
        const char *k;
        va_list* ap = (va_list*) userdata;
        va_list aq;

        va_copy(aq, *ap);

        while ((k = va_arg(aq, const char *))) {
                char **v;

                v = va_arg(aq, char **);

                if (streq(key, k)) {
                        va_end(aq);
                        free(*v);
                        *v = value;
                        return 1;
                }
        }

        va_end(aq);

        free(value);
        return 0;
}
Example #3
0
File: bee2.c Project: agievich/bee2
static err_t beeSprintf(blob_t* str, const char* format,...)
{
	va_list args;
	size_t offset;
	int count;
	// строка не пустая?
	offset = blobSize(*str);
	if (offset)
	{
		// проверка
		if (((char*)*str)[offset - 1] != '\0')
			return ERR_BAD_INPUT;
		// подправка
		--offset;
	}
	// сколько требуется символов?
	va_start(args, format);
	count = _vscprintf(format, args);
	va_end(args);
	if (count < 0)
		return ERR_BAD_INPUT;
	// выделение памяти
	*str = blobResize(*str, offset + (size_t)count + 1);
	if (*str == 0)
		return ERR_OUTOFMEMORY;
	// печать в строку
	va_start(args, format);
	vsprintf_s((char*)*str + offset, count + 1, format, args);
	va_end(args);
	// выйти
	return ERR_OK;
}
Example #4
0
int
ACE_Process_Options::command_line (const ACE_TCHAR *format, ...)
{
  // Store all ... args in argp.
  va_list argp;
  va_start (argp, format);

  if (command_line_buf_len_ < 1)
    {
      va_end (argp);
      return -1;
    }

#if !defined (ACE_LACKS_VSNPRINTF) || defined (ACE_HAS_TRIO)
  // vsnprintf the format and args into command_line_buf__.
  ACE_OS::vsnprintf (command_line_buf_,
                     command_line_buf_len_,
                     format,
                     argp);
#else
  // sprintf the format and args into command_line_buf__.
  ACE_OS::vsprintf (command_line_buf_,
                    format,
                    argp);
#endif

  // Useless macro.
  va_end (argp);

  command_line_argv_calculated_ = false;
  return 0;
}
Example #5
0
int gp_io_printf(gp_io *io, const char *fmt, ...)
{
	va_list va, vac;
	size_t size;
	int ret;
	char buf[1024];
	char *bufp = buf;

	va_start(va, fmt);
	va_copy(vac, va);
	size = vsnprintf(buf, sizeof(buf), fmt, vac);
	va_end(vac);

	if (size >= sizeof(buf)) {
		bufp = malloc(size+1);
		if (!bufp)
			return 1;

		vsnprintf(bufp, size, fmt, va);
	}

	ret = gp_io_flush(io, bufp, size);

	if (size >= sizeof(buf))
		free(bufp);

	va_end(va);

	return ret;
}
void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
{
	va_list ap;
	char *buf;
	int buflen;
	int len;

	if (!wpa_msg_cb)
		return;

	va_start(ap, fmt);
	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
	va_end(ap);

	buf = os_malloc(buflen);
	if (buf == NULL) {
		wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
			   "message buffer");
		return;
	}
	va_start(ap, fmt);
	len = vsnprintf(buf, buflen, fmt, ap);
	va_end(ap);
	wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
	bin_clear_free(buf, buflen);
}
void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
{
	va_list ap;
	char *buf;
	int buflen;
	int len;

	va_start(ap, fmt);
	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
	va_end(ap);

	buf = os_malloc(buflen);
	if (buf == NULL) {
		wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
			   __func__);
		return;
	}
	va_start(ap, fmt);
	len = vsnprintf(buf, buflen, fmt, ap);
	va_end(ap);
	wpa_printf(level, "%s", buf);
	if (wpa_msg_cb)
		wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
	os_free(buf);
}
int mp_init_multi(mp_int *mp, ...) 
{
    mp_err res = MP_OKAY;      /* Assume ok until proven otherwise */
    int n = 0;                 /* Number of ok inits */
    mp_int* cur_arg = mp;
    va_list args;

    va_start(args, mp);        /* init args to next argument from caller */
    while (cur_arg != NULL) {
        if (mp_init(cur_arg) != MP_OKAY) {
            /* Oops - error! Back-track and mp_clear what we already
               succeeded in init-ing, then return error.
            */
            va_list clean_args;
            
            /* end the current list */
            va_end(args);
            
            /* now start cleaning up */            
            cur_arg = mp;
            va_start(clean_args, mp);
            while (n--) {
                mp_clear(cur_arg);
                cur_arg = va_arg(clean_args, mp_int*);
            }
            va_end(clean_args);
            res = MP_MEM;
            break;
        }
        n++;
        cur_arg = va_arg(args, mp_int*);
    }
Example #9
0
w_string_t *w_string_make_printf(const char *format, ...)
{
  w_string_t *s;
  int len;
  char *buf;
  va_list args;

  va_start(args, format);
  // Get the length needed
  len = vsnprintf(NULL, 0, format, args);
  va_end(args);

  s = malloc(sizeof(*s) + len + 1);
  if (!s) {
    perror("no memory available");
    abort();
  }

  s->refcnt = 1;
  s->len = len;
  s->slice = NULL;
  buf = (char*)(s + 1);
  va_start(args, format);
  vsnprintf(buf, len + 1, format, args);
  va_end(args);
  s->buf = buf;
  s->hval = w_hash_bytes(buf, len, 0);

  return s;
}
Example #10
0
char *av_asprintf(const char *fmt, ...)
{
    char *p = NULL;
    va_list va;
    int len;

    va_start(va, fmt);
    len = vsnprintf(NULL, 0, fmt, va);
    va_end(va);
    if (len < 0)
        goto end;

    p = av_malloc(len + 1);
    if (!p)
        goto end;

    va_start(va, fmt);
    len = vsnprintf(p, len + 1, fmt, va);
    va_end(va);
    if (len < 0)
        av_freep(&p);

end:
    return p;
}
Example #11
0
int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...) {
  va_list ap;
  int str_l;

  *ptr = NULL;
  va_start(ap, fmt);                            /* measure the required size */
  str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
  va_end(ap);
  assert(str_l >= 0);        /* possible integer overflow if str_m > INT_MAX */
  if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1;      /* truncate */
  /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
  if (str_m == 0) {  /* not interested in resulting string, just return size */
  } else {
    *ptr = (char *) malloc(str_m);
    if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
    else {
      int str_l2;
      va_start(ap, fmt);
      str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
      va_end(ap);
      assert(str_l2 == str_l);
    }
  }
  return str_l;
}
Example #12
0
int ioctl(int fd, int request, ...) {
	void *data;
	va_list args;
	int rc;

	if (!idesc_index_valid(fd)) {
		return SET_ERRNO(EBADF);
	}

	switch (request) {
  	case FIONBIO:
		va_start(args, request);
		rc = io_fionbio(fd, args);
		va_end(args);
		break;
	case FIONREAD:
		rc = index_descriptor_status(fd, POLLIN);
		if (rc < 0) {
			rc = SET_ERRNO(-rc);
		}
		break;
	default:
		va_start(args, request);
		data = va_arg(args, void*);
		va_end(args);

		rc = index_descriptor_ioctl(fd, request, data);
		if (rc < 0) {
			rc = SET_ERRNO(-rc);
		}
		break;
	}

	return rc;
}
Example #13
0
int bprintf(struct bprintf_buffer *dest, const char *fmt, ...) {
  va_list ap;
  int ret;
  size_t size_left = dest->size - (dest->end - dest->str);
  va_start(ap, fmt);
  ret = vsnprintf(dest->end, size_left, fmt, ap);
  va_end(ap);
  if (ret >= size_left) {
    // we seem to need to call va_start again... is this legal?
    dest->size = dest->size+ret+1-size_left;
    char *str = (char*)realloc(dest->str, dest->size);

    assert(str); // this code is full of xalloc anyways...

    dest->end = str + (dest->end - dest->str);
    dest->str = str;
    size_left = dest->size - (dest->end - dest->str);
    va_start(ap, fmt);
    ret = vsnprintf(dest->end, size_left, fmt, ap);
    va_end(ap);
    assert(ret == size_left-1 && ret > 0);
  }
  dest->end += ret;
  return ret;
}
Example #14
0
/* Raise a fatal error condition. */
void
AG_FatalError(const char *fmt, ...)
{
	va_list args;
  char *buf;

	/* Use callback if defined. The callback must gracefully exit. */
	if (agErrorCallback != NULL) {
		if (fmt != NULL) {
			va_start(args, fmt);
			Vasprintf(&buf, fmt, args);
			va_end(args);
			agErrorCallback(buf);
			abort(); /* not reached */
		} else {
			agErrorCallback(AG_GetError());
			abort(); /* not reached */
		}
	} else {
		fprintf(stderr, "Fatal error: ");
		if (fmt != NULL) {
			va_start(args, fmt);
			vfprintf(stderr, fmt, args);
			va_end(args);
		} else {
			fprintf(stderr, "%s", AG_GetError());
		}
		fprintf(stderr, "\n");
		abort();
  }
}
Example #15
0
// Spawn, taking command-line arguments array directly on the stack.
// NOTE: Must have a sentinal of NULL at the end of the args
// (none of the args may be NULL).
int
spawnl(const char *prog, const char *arg0, ...)
{
	// We calculate argc by advancing the args until we hit NULL.
	// The contract of the function guarantees that the last
	// argument will always be NULL, and that none of the other
	// arguments will be NULL.
	int argc=0;
	va_list vl;
	va_start(vl, arg0);
	while(va_arg(vl, void *) != NULL)
		argc++;
	va_end(vl);

	// Now that we have the size of the args, do a second pass
	// and store the values in a VLA, which has the format of argv
	const char *argv[argc+2];
	argv[0] = arg0;
	argv[argc+1] = NULL;

	va_start(vl, arg0);
	unsigned i;
	for(i=0;i<argc;i++)
		argv[i+1] = va_arg(vl, const char *);
	va_end(vl);
	return spawn(prog, argv);
}
Example #16
0
void WorldLog::outLog(char const* fmt, ...)
{
    if (LogWorld())
    {
        TRINITY_GUARD(ACE_Thread_Mutex, Lock);
        ASSERT(i_file);

        va_list args;
        va_start(args, fmt);
        vfprintf(i_file, fmt, args);
        //fprintf(i_file, "\n");
        va_end(args);

        fflush(i_file);
    }

    if (sLog->GetLogDB() && m_dbWorld)
    {
        va_list ap2;
        va_start(ap2, fmt);
        char nnew_str[MAX_QUERY_LEN];
        vsnprintf(nnew_str, MAX_QUERY_LEN, fmt, ap2);
        sLog->outDB(LOG_TYPE_WORLD, nnew_str);
        va_end(ap2);
    }
}
Example #17
0
   //
   // Exception::Error
   //
   [[noreturn]]
   void Exception::Error(Args const &args, char const *format, ...)
   {
      va_list varg;
      std::size_t len, pos;

      if(args.nameL)
         len = std::snprintf(nullptr, 0, "'--%s': ", args.nameL);
      else if(args.nameS)
         len = std::snprintf(nullptr, 0, "'-%c': ", args.nameS);
      else
         len = std::snprintf(nullptr, 0, "--: ");

      va_start(varg, format);
      len += std::vsnprintf(nullptr, 0, format, varg);
      va_end(varg);

      std::unique_ptr<char[]> msg{new char[++len]};

      if(args.nameL)
         pos = std::snprintf(&msg[0], len, "'--%s': ", args.nameL);
      else if(args.nameS)
         pos = std::snprintf(&msg[0], len, "'-%c': ", args.nameS);
      else
         pos = std::snprintf(&msg[0], len, "--: ");

      va_start(varg, format);
      std::vsnprintf(&msg[pos], len - pos, format, varg);
      va_end(varg);

      throw Exception(std::move(msg));
   }
Example #18
0
void BufferAppendF(Buffer *buffer, const char *format, ...)
{
    assert(buffer);
    assert(format);

    va_list ap;
    va_list aq;
    va_start(ap, format);
    va_copy(aq, ap);

    int printed = vsnprintf(buffer->buffer + buffer->used, buffer->capacity - buffer->used, format, aq);
    if (printed >= (buffer->capacity - buffer->used))
    {
        /*
         * Allocate a larger buffer and retry.
         * Now is when having a copy of the list pays off :-)
         */
        ExpandIfNeeded(buffer, buffer->used + printed);

        buffer->used = 0;
        printed = vsnprintf(buffer->buffer + buffer->used, buffer->capacity - buffer->used, format, ap);
        buffer->used += printed;
    }
    else
    {
        buffer->used += printed;
    }
    va_end(aq);
    va_end(ap);
}
void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
{
	va_list ap;
	char *buf;
	int buflen;
	int len;

	va_start(ap, fmt);
	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
	va_end(ap);

	buf = os_malloc(buflen);
	if (buf == NULL) {
		wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
			   "message buffer");
		return;
	}
	va_start(ap, fmt);
	len = vsnprintf(buf, buflen, fmt, ap);
	va_end(ap);
	wpa_printf(level, "%s", buf);
	if (wpa_msg_cb)
		wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
	bin_clear_free(buf, buflen);
}
Example #20
0
void CLog(const char *fmt, ...)
{
	std::fstream clog;

	try
	{
		clog.open(Flux::string(Config->LogChannel + ".log").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);

		if(!clog.is_open())
			throw LogException("Failed to open log file.");

	}
	catch(LogException &e)
	{
		Log() << "Channel Logger Exception: " << e.GetReason();
		return;
	}

	va_list args;
	va_start(args, fmt);

	time_t t = time(NULL);
	struct tm *tm = localtime(&t);

	char buf[512];
	strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S %Y] ", tm);
	clog << buf;
	vsnprintf(buf, sizeof(buf), fmt, args);
	clog << buf << std::endl;
	va_end(args);
	va_end(args);
	clog.close();
}
void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
		    const char *fmt, ...)
{
	va_list ap;
	char *buf;
	int buflen;
	int len;

	va_start(ap, fmt);
	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
	va_end(ap);

	buf = os_malloc(buflen);
	if (buf == NULL) {
		wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
			   "message buffer");
		return;
	}
	va_start(ap, fmt);
	len = vsnprintf(buf, buflen, fmt, ap);
	va_end(ap);
	if (hostapd_logger_cb)
		hostapd_logger_cb(ctx, addr, module, level, buf, len);
	else if (addr)
		wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
			   MAC2STR(addr), buf);
	else
		wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
	bin_clear_free(buf, buflen);
}
Example #22
0
/* Print message to buffer. If buffer is large enough to hold the message,
 * return buffer. If buffer is to small, allocate large enough buffer on heap,
 * and return allocated buffer. */
int ns_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
  va_list ap_copy;
  int len;

  va_copy(ap_copy, ap);
  len = vsnprintf(*buf, size, fmt, ap_copy);
  va_end(ap_copy);

  if (len < 0) {
    /* eCos and Windows are not standard-compliant and return -1 when
     * the buffer is too small. Keep allocating larger buffers until we
     * succeed or out of memory. */
    *buf = NULL;
    while (len < 0) {
      NS_FREE(*buf);
      size *= 2;
      if ((*buf = (char *) NS_MALLOC(size)) == NULL) break;
      va_copy(ap_copy, ap);
      len = vsnprintf(*buf, size, fmt, ap_copy);
      va_end(ap_copy);
    }
  } else if (len > (int) size) {
    /* Standard-compliant code path. Allocate a buffer that is large enough. */
    if ((*buf = (char *) NS_MALLOC(len + 1)) == NULL) {
      len = -1;
    } else {
      va_copy(ap_copy, ap);
      len = vsnprintf(*buf, len + 1, fmt, ap_copy);
      va_end(ap_copy);
    }
  }

  return len;
}
Example #23
0
/*****************************************************************************
 * trace_add_formatted()
 ****************************************************************************/
int trace_add_formatted(int lcore_idx, uint32_t trace_buf_id,
                        trace_level_t level,
                        const char *fmt, ...)
{
    va_list ap;
    va_list temp_ap;
    int     datalen;
    int     ret;

    va_start(ap, fmt);
    va_copy(temp_ap, ap);

    /* Trick the library to compute the length (excluding the null byte
     * terminator, therefore the +1) for us..
     */
    datalen = vsnprintf(NULL, 0, fmt, temp_ap) + 1;

    va_end(temp_ap);
    if (datalen >= 0) {
        char data[datalen];

        vsnprintf(data, datalen, fmt, ap);
        ret = trace_add_raw(lcore_idx, trace_buf_id, level, data, datalen);
    } else {
        ret = -EPERM;
    }

    va_end(ap);
    return ret;
}
Example #24
0
std::string StringFromFormat(const char* format, ...)
{
	va_list args;
	char *buf = NULL;
#ifdef _WIN32
	int required = 0;

	va_start(args, format);
	required = _vscprintf(format, args);
	buf = new char[required + 1];
	vsnprintf(buf, required, format, args);
	va_end(args);

	buf[required] = '\0';
	std::string temp = buf;
	delete[] buf;
#else
	va_start(args, format);
	vasprintf(&buf, format, args);
	va_end(args);

	std::string temp = buf;
	free(buf);
#endif
	return temp;
}
Example #25
0
char *
_XGetLCValues(XLCd lcd, ...)
{
    va_list var;
    XlcArgList args;
    char *ret;
    int num_args;
    XLCdPublicMethodsPart *methods = XLC_PUBLIC_METHODS(lcd);

    va_start(var, lcd);
    _XlcCountVaList(var, &num_args);
    va_end(var);

    va_start(var, lcd);
    _XlcVaToArgList(var, num_args, &args);
    va_end(var);

    if (args == (XlcArgList) NULL)
        return (char *) NULL;

    ret = (*methods->get_values)(lcd, args, num_args);

    Xfree(args);

    return ret;
}
Example #26
0
File: lusol.c Project: SimonRit/RTK
void LUSOL_report(LUSOLrec *LUSOL, int msglevel, const char *format, ...)
{
  va_list ap;

  if(LUSOL == NULL) {
    va_start(ap, format);
    vfprintf(stderr, format, ap);
    va_end(ap);
  }
  else if(msglevel >= 0  /*LUSOL->luparm[2]*/) {
    if(LUSOL->writelog != NULL) {
      char buff[255];

      va_start(ap, format);
      vsprintf(buff, format, ap);
      va_end(ap);
      LUSOL->writelog(LUSOL, LUSOL->loghandle, buff);
    }
    if(LUSOL->outstream != NULL) {
      va_start(ap, format);
      vfprintf(LUSOL->outstream, format, ap);
      va_end(ap);
      fflush(LUSOL->outstream);
    }
  }
}
Example #27
0
void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
{
    va_list args;

    // prevent logging too much
    if(error_log_limit(0)) return;

    log_date(stderr);

    va_start( args, fmt );
    if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
    else            fprintf(stderr, "%s: %s: ", prefix, program_name);
    vfprintf( stderr, fmt, args );
    va_end( args );

    if(errno) {
        char buf[1024];
        fprintf(stderr, " (errno %d, %s)\n", errno, strerror_r(errno, buf, 1023));
        errno = 0;
    }
    else
        fputc('\n', stderr);

    if(error_log_syslog) {
        va_start( args, fmt );
        vsyslog(LOG_ERR,  fmt, args );
        va_end( args );
    }
}
Example #28
0
/* Simple error logging to stderr with msg caching for later re-use */
void
mpp_err_msg_cache(const char *loglevel, const char *prog, const char *fmt,...)
{
	va_list		ap;
	char		szTimeNow[18];
	int			len;

	va_start(ap, fmt);
	fprintf(stderr, "%s|%s-[%s]:-", GetTimeNow(szTimeNow), prog, loglevel);
	vfprintf(stderr, gettext(fmt), ap);
	va_end(ap);

	/* cache a copy of the message - we may need it for a report */
	va_start(ap, fmt);
	len = vsnprintf(predump_errmsg, sizeof(predump_errmsg), gettext(fmt), ap);
	va_end(ap);

	/*
	 * If the passed error string exceeds the size of the buffer, indicate that
	 * by suffixing the string with ".."
	 */
	if (len > sizeof(predump_errmsg))
	{
		int		i;
		for (i = sizeof(predump_errmsg) - 3; predump_errmsg[i]; i++)
			predump_errmsg[i] = '.';
	}
}
Example #29
0
int asprintf(char **ret, const char *format, ...)
{
    va_list ap;
    fprintf(stderr, "%s\n", "in the asprintf");

    *ret = NULL;  /* Ensure value can be passed to free() */

    va_start(ap, format);
    int count = vsnprintf(NULL, 0, format, ap);
    va_end(ap);

    if (count >= 0)
    {
        char* buffer = malloc(count + 1);
        if (buffer == NULL)
            {return -1;}

        va_start(ap, format);
        count = vsnprintf(buffer, count + 1, format, ap);
        va_end(ap);

        if (count < 0)
        {
            free(buffer);
            return count;
        }
        *ret = buffer;
    }

    return count;
}
Example #30
0
int
threadspawnl(int fd[3], char *cmd, ...)
{
	char **argv, *s;
	int n, pid;
	va_list arg;

	va_start(arg, cmd);
	for(n=0; va_arg(arg, char*) != nil; n++)
		;
	n++;
	va_end(arg);

	argv = malloc(n*sizeof(argv[0]));
	if(argv == nil)
		return -1;

	va_start(arg, cmd);
	for(n=0; (s=va_arg(arg, char*)) != nil; n++)
		argv[n] = s;
	argv[n] = 0;
	va_end(arg);

	pid = threadspawn(fd, cmd, argv);
	free(argv);
	return pid;
}