Example #1
0
void XMLPrinter::Print( const char* format, ... )
{
    va_list     va;
    va_start( va, format );

    if ( _fp ) {
        vfprintf( _fp, format, va );
    }
    else {
        // This seems brutally complex. Haven't figured out a better
        // way on windows.
#ifdef _MSC_VER
        int len = -1;
        int expand = 1000;
        while ( len < 0 ) {
            len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va );
            if ( len < 0 ) {
                expand *= 3/2;
                _accumulator.PushArr( expand );
            }
        }
        char* p = _buffer.PushArr( len ) - 1;
        memcpy( p, _accumulator.Mem(), len+1 );
#else
        int len = vsnprintf( 0, 0, format, va );
        // Close out and re-start the va-args
        va_end( va );
        va_start( va, format );
        char* p = _buffer.PushArr( len ) - 1;
        vsnprintf( p, len+1, format, va );
#endif
    }
    va_end( va );
}
char*
strdup_printf(char *fmt, ...)
{
    // Good for glibc 2.1 and above. Fedora5 is 2.4.

    char           *ret_str = NULL;
    va_list         ap;
    int             bytes_to_allocate;

    va_start(ap, fmt);
    bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);

    // Add one for '\0'
    bytes_to_allocate++;

    ret_str = (char *)malloc(bytes_to_allocate * sizeof(char));
    if (ret_str == NULL) {
        fprintf(stderr, "failed to allocate memory\n");
        return NULL;
    }

    va_start(ap, fmt);
    bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
    va_end(ap);

    return ret_str;
}
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;
}
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);
}
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 #6
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 #7
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 #8
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 #9
0
static void
error(Pk11Install_Error errcode, ...)
#endif
{

    va_list ap;
    char *errstr;
    Pk11Install_ErrorHandler handler;

    if (!errorHandlerLock) {
        errorHandlerLock = PR_NewLock();
    }

    PR_Lock(errorHandlerLock);

    handler = errorHandler;

    PR_Unlock(errorHandlerLock);

    if (handler) {
#ifdef OSF1
        va_start(ap);
        errstr = PR_vsmprintf(errorString[va_arg(ap, Pk11Install_Error)], ap);
#else
        va_start(ap, errcode);
        errstr = PR_vsmprintf(errorString[errcode], ap);
#endif
        handler(errstr);
        PR_smprintf_free(errstr);
        va_end(ap);
    }
}
Example #10
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 #11
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 #12
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;
}
Example #13
0
void
Mvprintw(int y, int x, char *fmt, /* args */ ...)
{
	va_list	args;
	char	line[MAX_INPUT];

	if (ScreenMode) {
		va_start(args, fmt);
		vsprintf(line, fmt, args);
		va_end(args);
		(void) mvprintw(y, x, "%s", line);
		return;
	} else {
		if ((y == Lasty) && (x > Lastx)) {	/* same line */
			Lastx = x;
			printf(" ");
		} else {
			Lastx = 0;
			Lasty = y;
			printf("\n");
		}
		va_start(args, fmt);
		vprintf(fmt, args);
		va_end(args);
	}
}
Example #14
0
static char *
fmtstr(const char* fmt, ...)
{
    static char buf[1];
    va_list ap; 
    int len; 
    char* str;

    va_start(ap, fmt); 
    len = vsnprintf(buf, 1, fmt, ap); /* vsnprintf(NULL, 0, ...) should work. bnc#762684 */
    va_end(ap); 
    if (len <= 0)
    {
        fprintf(stderr, "*Err: vsnprintf(%s) = %d\n", fmt, len);
        return NULL; 
    }
    str = (char*)malloc(len+1); 
    if (str == NULL)
    {
        fprintf(stderr, "*Err: malloc(%d) failed\n", len+1);
        return NULL; 
    }
    va_start(ap, fmt); 
    vsnprintf(str, len+1, fmt, ap); 
    va_end(ap); 
    return str; 
}
Example #15
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 #16
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 #17
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 #18
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 #19
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 #20
0
void nprintf(usbdevice* kb, int nodenumber, usbmode* mode, const char* format, ...){
    if(!kb)
        return;
    usbprofile* profile = &kb->profile;
    va_list va_args;
    int fifo;
    if(nodenumber >= 0){
        // If node number was given, print to that node (if open)
        if((fifo = kb->outfifo[nodenumber])){
            va_start(va_args, format);
            if(mode)
                dprintf(fifo, "mode %d ", INDEX_OF(mode, profile->mode) + 1);
            vdprintf(fifo, format, va_args);
        }
        return;
    }
    // Otherwise, print to all nodes
    for(int i = 0; i < OUTFIFO_MAX; i++){
        if((fifo = kb->outfifo[i])){
            va_start(va_args, format);
            if(mode)
                dprintf(fifo, "mode %d ", INDEX_OF(mode, profile->mode) + 1);
            vdprintf(fifo, format, va_args);
        }
    }
}
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);
}
Example #22
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;
}
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);
}
Example #24
0
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 #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
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 #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
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 #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
/**
 * Print error message
 */
void LOW_helper_msglog::printPerror( const int inErrno, const char *inFormat, ...)
{
  va_list inParamList;
  int     length = 0;
  char    dummy;
  char    *buffer;

  // calculate length
  va_start( inParamList, inFormat);
  length += vsnprintf( &dummy, 0, inFormat, inParamList);
  va_end( inParamList);
  if ( inErrno > 0 && inErrno < sys_nerr-1 ) {
    length += strlen( ": ");
    length += strlen( sys_errlist[inErrno]);
  }
  length ++; // the null char
  
  buffer = new char[length];
  
  va_start( inParamList, inFormat);
  vsprintf( buffer, inFormat, inParamList);
  va_end( inParamList);
  if ( inErrno > 0 && inErrno < sys_nerr-1 ) {
    strcat( buffer, ": ");
    strcat( buffer, sys_errlist[inErrno]);
  }

  printError( "%s (Error# %d)", buffer, inErrno);
  
  delete[] buffer;
}