Beispiel #1
0
/******************************************************************************
 * sprintf_log(...)
 * Creates a logging string from the variadic arguments in the way of sprintf()
 * but allocates the memory for it if needed.
 *****************************************************************************/
char *
sprintf_log(char * str, char * format, va_list ap)
{
  va_list cp_ap;
  size_t new_len;
  size_t s_len =
    str == NULL ? 0 : sizeof(&str) / sizeof(char);

#ifdef __va_copy
  __va_copy (cp_ap, ap);
#else
  cp_ap = ap;
#endif
  /* check out whether the current string buffer size is sufficent */
  new_len = vsnprintf(str, s_len, format, ap);

  if (new_len >= s_len) {
    /* Reallocate buffer, now that we know how much space is needed. */
    new_len += 1;
    str = (char *) realloc(str, new_len);

    if (str != NULL) {
      /* print again. */
      vsnprintf(str, new_len, format, cp_ap);
    }
  }

  return str;
}
Beispiel #2
0
void SC_StringBuffer::vappendf(const char* fmt, va_list ap)
{
	va_list ap2;
	size_t remaining = getRemaining();

	// Calling vsnprintf may invalidate vargs, so keep a copy
#ifdef __va_copy
	__va_copy(ap2, ap);
#else
	ap2 = ap;
#endif

	// NOTE: This only works since glibc 2.0.6!
	int size = vsnprintf(mPtr, remaining, fmt, ap);
	va_end(ap);

	// size returned excludes trailing \0
	if (size++ > 0) {
		if ((size_t)size > remaining) {
			growBy(size - remaining);
			vsnprintf(mPtr, size, fmt, ap2);
		}
		mPtr += size-1; // omit trailing \0
	}

	va_end(ap2);
}
Beispiel #3
0
int vasprintf(char **ret, const char *fmt, va_list args) {
    int rv;
    *ret = NULL;
    va_list args2;
/* vsnprintf can destroy args, so we need to copy it for the second call */
#ifdef __va_copy
    /* non-standard macro, but usually exists */
    __va_copy(args2, args);
#elif defined(va_copy)
    /* C99 macro. We compile with -std=c89 but you never know */
    va_copy(args2, args);
#else
    /* Ancient compiler. This usually works but there are no guarantees. */
    memcpy(args2, args, sizeof(va_list));
#endif
    rv = vsnprintf(NULL, 0, fmt, args);
    va_end(args);
    if (rv < 0) {
        return rv;
    }
    *ret = (char*)malloc(++rv); /* vsnprintf doesn't count \0 */
    if (*ret == NULL) {
        return -1;
    }
    rv = vsnprintf(*ret, rv, fmt, args2);
    va_end(args2);
    if (rv < 0) {
        free(*ret);
    }
    return rv;
}
Beispiel #4
0
static int oc_vasprintf(char **strp, const char *fmt, va_list ap)
{
    va_list ap2;
    char *res = NULL;
    int len = 160, len2;
    int ret = 0;
    int errno_save = -ENOMEM;

    res = malloc(160);
    if (!res)
        goto err;

    /* Use a copy of 'ap', preserving it in case we need to retry into
       a larger buffer. 160 characters should be sufficient for most
       strings in openconnect. */
#ifdef HAVE_VA_COPY
    va_copy(ap2, ap);
#elif defined (HAVE___VA_COPY)
    __va_copy(ap2, ap);
#else
#error No va_copy()!
    // You could try this.
    ap2 = ap;
    // Or this
    *ap2 = *ap;
#endif
    len = vsnprintf(res, 160, fmt, ap2);
    va_end(ap2);

    if (len < 0) {
printf_err:
        errno_save = errno;
        free(res);
        res = NULL;
        goto err;
    }
    if (len >=0 && len < 160)
        goto out;

    free(res);
    res = malloc(len+1);
    if (!res)
        goto err;

    len2 = vsnprintf(res, len+1, fmt, ap);
    if (len2 < 0 || len2 > len)
        goto printf_err;

    ret = 0;
    goto out;

err:
    errno = errno_save;
    ret = -1;
out:
    *strp = res;
    return ret;
}
Beispiel #5
0
void message(const char *format, ...) 
{ 
  va_list args, args2; 
  va_start(args, format);
  __va_copy(args2, args); 

  print_log(TERM_FG_WHITE "[Message]", format, args); 
  network_log(format, args2);

  va_end(args); 
  va_end(args2); 
}  
Beispiel #6
0
void udhcp_logging(int level, const char *fmt, ...)
{
	va_list p;
	va_list p2;

	va_start(p, fmt);
	__va_copy(p2, p);
	if(!daemonized) {
		vprintf(fmt, p);
		putchar('\n');
	}
	vsyslog(level, fmt, p2);
	va_end(p);
}
Beispiel #7
0
/* INTERNAL: Convert wide va_list to a single 'delim'-separated wide string, with an
 * extra '\0' to terminate it.
 */
static MSVCRT_wchar_t *msvcrt_valisttos(const MSVCRT_wchar_t *arg0, va_list alist, MSVCRT_wchar_t delim)
{
  va_list alist2;
  unsigned long len;
  const MSVCRT_wchar_t *arg;
  MSVCRT_wchar_t *p, *ret;

#ifdef HAVE_VA_COPY
  va_copy(alist2,alist);
#else
# ifdef HAVE___VA_COPY
  __va_copy(alist2,alist);
# else
  alist2 = alist;
# endif
#endif

  if (!arg0)
  {
      /* Return NULL for an empty environment list */
      return NULL;
  }

  /* get length */
  arg = arg0;
  len = 0;
  do {
      len += strlenW(arg) + 1;
      arg = va_arg(alist, MSVCRT_wchar_t*);
  } while (arg != NULL);

  ret = MSVCRT_malloc((len + 1) * sizeof(MSVCRT_wchar_t));
  if (!ret)
    return NULL;

  /* fill string */
  arg = arg0;
  p = ret;
  do {
      len = strlenW(arg);
      memcpy(p, arg, len * sizeof(MSVCRT_wchar_t));
      p += len;
      *p++ = delim;
      arg = va_arg(alist2, MSVCRT_wchar_t*);
  } while (arg != NULL);
  if (delim && p > ret) p[-1] = 0;
  else *p = 0;
  return ret;
}
Beispiel #8
0
/* INTERNAL: Convert ansi va_list to a single 'delim'-separated wide string, with an
 * extra '\0' to terminate it.
 */
static MSVCRT_wchar_t *msvcrt_valisttos_aw(const char *arg0, va_list alist, MSVCRT_wchar_t delim)
{
  va_list alist2;
  unsigned long len;
  const char *arg;
  MSVCRT_wchar_t *p, *ret;

#ifdef HAVE_VA_COPY
  va_copy(alist2,alist);
#else
# ifdef HAVE___VA_COPY
  __va_copy(alist2,alist);
# else
  alist2 = alist;
# endif
#endif

  if (!arg0)
  {
      /* Return NULL for an empty environment list */
      return NULL;
  }

  /* get length */
  arg = arg0;
  len = 0;
  do {
      len += MultiByteToWideChar(CP_ACP, 0, arg, -1, NULL, 0);
      arg = va_arg(alist, char*);
  } while (arg != NULL);

  ret = MSVCRT_malloc((len + 1) * sizeof(MSVCRT_wchar_t));
  if (!ret)
    return NULL;

  /* fill string */
  arg = arg0;
  p = ret;
  do {
      p += MultiByteToWideChar(CP_ACP, 0, arg, strlen(arg), p, len - (p - ret));
      *p++ = delim;
      arg = va_arg(alist2, char*);
  } while (arg != NULL);
  if (delim && p > ret) p[-1] = 0;
  else *p = 0;
  return ret;
}
Beispiel #9
0
int
PyArg_VaParse(PyObject *args, char *format, va_list va)
{
	va_list lva;

#ifdef VA_LIST_IS_ARRAY
	memcpy(lva, va, sizeof(va_list));
#else
#ifdef __va_copy
	__va_copy(lva, va);
#else
	lva = va;
#endif
#endif

	return vgetargs1(args, format, &lva, 0);
}
Beispiel #10
0
/// Equivalent to a vsprintf on the string.
int ostringstream::vformat (const char* fmt, va_list args)
{
#if HAVE_VA_COPY
    va_list args2;
#else
    #define args2 args
    #undef __va_copy
    #define __va_copy(x,y)
#endif
    int rv, space;
    do {
  space = remaining();
  __va_copy (args2, args);
  if (0 > (rv = vsnprintf (ipos(), space, fmt, args2)))
      return rv;
    } while (rv >= space && rv < (int)overflow(rv+1));
    SetPos (pos() + min (rv, space));
    return rv;
}
Beispiel #11
0
char* vnsprintf(const char* format, va_list args)
{
    static FILE* dummy = fopen("/dev/null", "wb");
    unsigned     chars_written;
    char*        ret;
    va_list      args_copy;

  #ifdef __va_copy
    __va_copy (args_copy, args);
  #else
    args_copy = args;
  #endif
    chars_written = vfprintf(dummy, format, args);
    ret = xmalloc<char>(chars_written + 1);
    ret[chars_written] = 255;
    args = args_copy;
    vsprintf(ret, format, args);
    assert(ret[chars_written] == 0);
    return ret;
}
Beispiel #12
0
/* vsprintf() w/ allocation.  The result must be free'd!
 */
static
char*
vallocPrintf(const char *format, va_list args)
{
    va_list nargs;
    char* ret=NULL;
    int size, size2;

    /* May use a va_list only *once* (on some implementations it may
	 * be a reference to something that holds internal state information
	 */
    __va_copy(nargs, args);

    /* Take advantage of the fact that sprintf will tell us how much space to allocate */
    size=vsnprintf("",0,format,nargs);

    va_end(nargs);

    if (size<=0) {
        fprintf(stderr, "vaprintf: Failed to convert format '%s'\n",format);
        goto fail;
    }
    ret=malloc(size+1);
    if (!ret) {
        fprintf(stderr, "vaprintf: Failed to allocate memory for format '%s'\n",format);
        goto fail;
    }
    size2=vsnprintf(ret,size+1,format,args);
    if (size!=size2) {
        fprintf(stderr, "vaprintf: Format yielded different size %d %d : %s\n",size,size2,format);
        goto fail;
    }

    return ret;
    fail:
    free(ret);
    return NULL;
}
Beispiel #13
0
Buffer<Allocator>& Buffer<Allocator>::writevf(const char *fmt, va_list ap)
{
	va_list ap2;
	__va_copy(ap2, ap); // unfortunately va_copy is only exposed for C99/C++11 or later

	size_t size    = _end - _pos;
	size_t written = vsnprintf((char*) _pos, size, fmt, ap);

	if (written > size) {
		// buffer was too small (+1 for zero byte)
		ensure_capacity(written + 1);

		size    = _end - _pos;
		written = vsnprintf((char*) _pos, size, fmt, ap2);
		assert(written <= size);
	}

	_pos += written;

	va_end(ap2);

	return *this;
}
Beispiel #14
0
void STACK_ARGS DCanvas::DrawTexture (FTexture *img, int x0, int y0, int tags_first, ...)
{
	FTexture::Span unmaskedSpan[2];
	const FTexture::Span **spanptr, *spans;
	static BYTE identitymap[256];
	static short bottomclipper[MAXWIDTH], topclipper[MAXWIDTH];
	va_list tags;
	DWORD tag;
	INTBOOL boolval;
	int intval;

	if (img == NULL || img->UseType == FTexture::TEX_Null)
	{
		return;
	}

	int texwidth = img->GetScaledWidth();
	int texheight = img->GetScaledHeight();

	int windowleft = 0;
	int windowright = texwidth;
	int dclip = this->GetHeight();
	int uclip = 0;
	int lclip = 0;
	int rclip = this->GetWidth();
	int destwidth = windowright << FRACBITS;
	int destheight = texheight << FRACBITS;
	int top = img->GetScaledTopOffset();
	int left = img->GetScaledLeftOffset();
	fixed_t alpha = FRACUNIT;
	int fillcolor = -1;
	const BYTE *translation = NULL;
	INTBOOL alphaChannel = false;
	INTBOOL flipX = false;
	fixed_t shadowAlpha = 0;
	int shadowColor = 0;
	int virtWidth = this->GetWidth();
	int virtHeight = this->GetHeight();
	INTBOOL keepratio = false;
	ERenderStyle style = STYLE_Count;

	x0 <<= FRACBITS;
	y0 <<= FRACBITS;

	spanptr = &spans;

	// Parse the tag list for attributes
	va_start (tags, tags_first);
	tag = tags_first;

	while (tag != TAG_DONE)
	{
		va_list *more_p;
		DWORD data;

		switch (tag)
		{
		case TAG_IGNORE:
		default:
			data = va_arg (tags, DWORD);
			break;

		case TAG_MORE:
			more_p = va_arg (tags, va_list *);
			va_end (tags);
#ifdef __GNUC__
			__va_copy (tags, *more_p);
#else
			tags = *more_p;
#endif
			break;

		case DTA_DestWidth:
			destwidth = va_arg (tags, int) << FRACBITS;
			break;

		case DTA_DestHeight:
			destheight = va_arg (tags, int) << FRACBITS;
			break;

		case DTA_Clean:
			boolval = va_arg (tags, INTBOOL);
			if (boolval)
			{
				x0 = (x0 - 160*FRACUNIT) * CleanXfac + (Width * (FRACUNIT/2));
				y0 = (y0 - 100*FRACUNIT) * CleanYfac + (Height * (FRACUNIT/2));
				destwidth = texwidth * CleanXfac * FRACUNIT;
				destheight = texheight * CleanYfac * FRACUNIT;
			}
			break;

		case DTA_CleanNoMove:
			boolval = va_arg (tags, INTBOOL);
			if (boolval)
			{
				destwidth = texwidth * CleanXfac * FRACUNIT;
				destheight = texheight * CleanYfac * FRACUNIT;
			}
			break;

		case DTA_320x200:
			boolval = va_arg (tags, INTBOOL);
			if (boolval)
			{
				virtWidth = 320;
				virtHeight = 200;
			}
			break;

		case DTA_HUDRules:
			{
				bool xright = x0 < 0;
				bool ybot = y0 < 0;
				intval = va_arg (tags, int);

				if (hud_scale)
				{
					x0 *= CleanXfac;
					if (intval == HUD_HorizCenter)
						x0 += Width * FRACUNIT / 2;
					else if (xright)
						x0 = Width * FRACUNIT + x0;
					y0 *= CleanYfac;
					if (ybot)
						y0 = Height * FRACUNIT + y0;
					destwidth = texwidth * CleanXfac * FRACUNIT;
					destheight = texheight * CleanYfac * FRACUNIT;
				}
				else
				{
					if (intval == HUD_HorizCenter)
						x0 += Width * FRACUNIT / 2;
					else if (xright)
						x0 = Width * FRACUNIT + x0;
					if (ybot)
						y0 = Height * FRACUNIT + y0;
				}
			}
			break;

		case DTA_VirtualWidth:
			virtWidth = va_arg (tags, int);
			break;
			
		case DTA_VirtualHeight:
			virtHeight = va_arg (tags, int);
			break;

		case DTA_Alpha:
			alpha = MIN<fixed_t> (FRACUNIT, va_arg (tags, fixed_t));
			break;

		case DTA_AlphaChannel:
			alphaChannel = va_arg (tags, INTBOOL);
			break;

		case DTA_FillColor:
			fillcolor = va_arg (tags, int);
			break;

		case DTA_Translation:
			translation = va_arg (tags, const BYTE *);
			break;

		case DTA_FlipX:
			flipX = va_arg (tags, INTBOOL);
			break;

		case DTA_TopOffset:
			top = va_arg (tags, int);
			break;

		case DTA_LeftOffset:
			left = va_arg (tags, int);
			break;

		case DTA_CenterOffset:
			if (va_arg (tags, int))
			{
				left = texwidth / 2;
				top = texheight / 2;
			}
			break;

		case DTA_CenterBottomOffset:
			if (va_arg (tags, int))
			{
				left = texwidth / 2;
				top = texheight;
			}
			break;

		case DTA_WindowLeft:
			windowleft = va_arg (tags, int);
			break;

		case DTA_WindowRight:
			windowright = va_arg (tags, int);
			break;

		case DTA_ClipTop:
			uclip = va_arg (tags, int);
			if (uclip < 0)
			{
				uclip = 0;
			}
			break;

		case DTA_ClipBottom:
			dclip = va_arg (tags, int);
			if (dclip > this->GetHeight())
			{
				dclip = this->GetHeight();
			}
			break;

		case DTA_ClipLeft:
			lclip = va_arg (tags, int);
			if (lclip < 0)
			{
				lclip = 0;
			}
			break;

		case DTA_ClipRight:
			rclip = va_arg (tags, int);
			if (rclip > this->GetWidth())
			{
				rclip = this->GetWidth();
			}
			break;

		case DTA_ShadowAlpha:
			shadowAlpha = MIN<fixed_t> (FRACUNIT, va_arg (tags, fixed_t));
			break;

		case DTA_ShadowColor:
			shadowColor = va_arg (tags, int);
			break;

		case DTA_Shadow:
			boolval = va_arg (tags, INTBOOL);
			if (boolval)
			{
				shadowAlpha = FRACUNIT/2;
				shadowColor = 0;
			}
			else
			{
				shadowAlpha = 0;
			}
			break;

		case DTA_Masked:
			boolval = va_arg (tags, INTBOOL);
			if (boolval)
			{
				spanptr = &spans;
			}
			else
			{
				spanptr = NULL;
			}
			break;

		case DTA_KeepRatio:
			keepratio = va_arg (tags, INTBOOL);
			break;

		case DTA_RenderStyle:
			style = ERenderStyle(va_arg (tags, int));
			break;
		}
Beispiel #15
0
/*
 * parse scanf format string 
 */
int
__scanf(const char *input, FILE *stream, bool stream_or_memory,
                                       const char *fmt, va_list ap)
{
    unsigned int assign_count = 0;
    va_list ap_copy, ap_tmp;
    int width, base = 0;
    char *p, *s;
    long long num_res = 0;
    unsigned long long num_ures = 0;
    bool num_unsigned = false;
    bool suppress = false;
    bool use_width = false;
    bool using_nth = false;
    /* length modifiers */
    bool lm_h, lm_hh, lm_l, lm_ll, lm_j, lm_z, lm_t, lm_L;
    int arg_nth = 0;
    int i = 0;
    bool list_not;
    char *list_start, *list_end;
    unsigned char *user_us;
    unsigned long *user_ul;
    unsigned long long *user_ull;
    unsigned short *user_ush;
    unsigned int *user_ui;
    uintmax_t *user_uj;
    char *user_s = NULL;
    long *user_l;
    long long *user_ll;
    short *user_sh;
    int *user_i;
    intmax_t *user_j;
    size_t *user_z;
    ptrdiff_t *user_t;
    void **user_pp;
#ifndef CONFIG_WITHOUT_FLOATING
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
    long double *user_ld;
#endif /* CONFIG_WITHOUT_LONG_DOUBLE */
    double *user_d;
    float *user_f;
#endif /* CONFIG_WITHOUT_FLOATING */
    struct st_mem_stream mem_stream;
    mem_stream.stream_or_memory = stream_or_memory;
    mem_stream.mem = (char**)&input;
    mem_stream.stream = stream;
    mem_stream.last_stream_char = '\0';
    mem_stream.read_bytes = 0;

    __va_copy(ap_copy, ap);

    if (stream != NULL)
        lock_stream(stream);

    for (p = (char*)fmt; *p != '\0'; p++) {
        use_width = false;
        width = 1;
        num_unsigned = false;
        suppress = false;
        num_res = 0;
        num_ures = 0;
        lm_h = false;
        lm_hh = false;
        lm_l = false;
        lm_ll = false;
        lm_j = false;
        lm_z = false;
        lm_t = false;
        lm_L = false;
        if (*p != '%') {
char_comp:
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (isspace(*p)) {
                while (isspace(*s)) {
                    __inc_next_char(&mem_stream);
                    s = __get_next_char(&mem_stream);
                    if (__is_eof(s)) {
                        goto eof_failure;
                    }
                }
            } else {
                if (*p == *s) {
                    __inc_next_char(&mem_stream);
                } else {
                    goto matching_failure;
                }
            }
            continue;
        }
        /* *p == '%' */
again_inc:
        p++;
again:
        switch(*p) {
        case '%':
            goto char_comp;
            break;
        case '*':
            suppress = true;
            goto again_inc;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            i = *p - '0';
            for (p++; p && (unsigned int)(*p - '0') < 10; p++) {
                i = i * 10 + (*p - '0');
            }
            if (*p == '$') {
                using_nth = true;
                if (i == 0) {
                    goto matching_failure;
                } else {
                    arg_nth = i;
                }
                p++;
            } else {
                width = i;
                if (width != 0) {
                    use_width = true;
                }
            }
            goto again;
        /* Length modifiers */
        case 'h':
            if (lm_h) {
                lm_hh = true;
                lm_h = false;
            } else {
                lm_h = true;
            }
            goto again_inc;
        case 'l':
            if (lm_l) {
                lm_ll = true;
                lm_l = false;
            } else {
                lm_l = true;
            }
            goto again_inc;
        case 'j':
            lm_j = true;
            goto again_inc;
        case 'z':
            lm_z = true;
            goto again_inc;
        case 't':
            lm_t = true;
            goto again_inc;
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
        case 'L':
            lm_L = true;
            goto again_inc;
#endif /* CONFIG_WITHOUT_LONG_DOUBLE */
        /* Numbers */
        case 'd':
            base = 10;
            goto number;
        case 'i':
            base = 0;
            goto number;
        case 'o':
            base = 8;
            num_unsigned = true;
            goto number;
        case 'u':
            base = 10;
            num_unsigned = true;
            goto number;
        case 'x':
        case 'X':
            base = 16;
            num_unsigned = true;
number:
            eat_spaces(&mem_stream);
            if (!use_width) {
                width = 0;
            }
    
            errno = 0; /* Check for strto* errors */
            if (suppress) {
                (void)__strtoll(NULL, base, width, get_next_char, inc_next_char,
                                                    (void*)&mem_stream);
            } else {
                if (num_unsigned) {
                    uintmax_t num_tmp;

                    errno = 0;
                    num_tmp = __strtoull(NULL, base, width, get_next_char,
                                               inc_next_char, (void*)&mem_stream);

                    if(errno == EINVAL) {
                        goto matching_failure;
                    }

                    assign_count++;

                    if (lm_ll) {
                        GET_ARG(user_ull, unsigned long long*);
                        *user_ull = (unsigned long long)num_tmp;
                    } else if (lm_l) {
                        GET_ARG(user_ul, unsigned long*);
                        *user_ul = (unsigned long)num_tmp;
                    } else if (lm_hh) {
                        GET_ARG(user_us, unsigned char*);
                        *user_us = (unsigned char)num_tmp;
                    } else if (lm_h) {
                        GET_ARG(user_ush, unsigned short*);
                        *user_ush = (unsigned short)num_tmp;
                    } else if (lm_j) {
                        GET_ARG(user_uj, uintmax_t*);
                        *user_uj = (uintmax_t)num_tmp;
                    } else if (lm_z) {
                        GET_ARG(user_z, size_t*);
                        *user_z = (size_t)num_tmp;
                    } else if (lm_t) {
                        GET_ARG(user_t, ptrdiff_t*);
                        *user_t = (unsigned long)num_tmp;
                    } else {
                        GET_ARG(user_ui, unsigned int*);
                        *user_ui = (unsigned int)num_tmp;
                    }
                } else {
                    intmax_t num_tmp;

                    errno = 0;
                    num_tmp = __strtoll(NULL, base, width, get_next_char,
                                               inc_next_char, (void*)&mem_stream);

                    if(errno == EINVAL) {
                        goto matching_failure;
                    }

                    assign_count++;

                    if (lm_ll) {
                        GET_ARG(user_ll, long long*);
                        *user_ll = (long long)num_tmp;
                    } else if (lm_l) {
                        GET_ARG(user_l, long*);
                        *user_l = (long int)num_tmp;
                    } else if (lm_hh) {
                        GET_ARG(user_s, char*);
                        *user_s = (char)num_tmp;
                    } else if (lm_h) {
                        GET_ARG(user_sh, short*);
                        *user_sh = (short)num_tmp;
                    } else if (lm_j) {
                        GET_ARG(user_j, intmax_t*);
                        *user_j = (intmax_t)num_tmp;
                    } else if (lm_z) {
                        GET_ARG(user_z, size_t*);
                        *user_z = (signed long)num_tmp;
                    } else if (lm_t) {
                        GET_ARG(user_t, ptrdiff_t*);
                        *user_t = (ptrdiff_t)num_tmp;
                    } else {
                        GET_ARG(user_i, int*);
                        *user_i = (int)num_tmp;
                    }
                }
            }
            /*
            if (errno == EINVAL) {
                goto matching_failure;
            }
            */
            break;
#ifndef CONFIG_WITHOUT_FLOATING
        case 'A':
        case 'a':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
        case 'G':
        case 'g':
            eat_spaces(&mem_stream);
            if (!use_width) {
                width = 0;
            }

            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (*s == 'i' || *s == 'I') {
                if (!eat_infinity(&mem_stream)) {
                    s = __get_next_char(&mem_stream);
                    if (*s == '\0') {
                        goto input_failure;
                    } else {
                        goto matching_failure;
                    }
                }
                /*
                if (!suppress) {
                    assign_count++;
                    if (lm_l) {
                        GET_ARG(user_d, double*);
                        *user_d = INFINITY;
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
                    } else if (lm_L) {
                        GET_ARG(user_ld, long double*);
                        *user_ld = INFINITY;
#endif
                    } else {
                        GET_ARG(user_f, float*);
                        *user_f = INFINITY;
                    }
                }
                */
            } else if (*s == 'n' || *s == 'N') {
                if (!eat_nan(&mem_stream)) {
                    goto matching_failure;
                }
                /*
                if (!suppress) {
                    assign_count++;
                    if (lm_l) {
                        GET_ARG(user_d, double*);
                        *user_d = NAN;
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
                    } else if (lm_L) {
                        GET_ARG(user_ld, long double*);
                        *user_ld = NAN;
#endif
                    } else {
                        GET_ARG(user_f, float*);
                        *user_f = NAN;
                    }
                }
                */
            } else {
                if (suppress) {
                    (void)__strtold(NULL, width, get_next_char, inc_next_char,
                                                    (void*)&mem_stream);
                } else {
                    assign_count++;
                    if (lm_l) {
                        GET_ARG(user_d, double*);
                        *user_d = (double)__strtold(NULL, width, get_next_char,
                                        inc_next_char, (void*)&mem_stream);
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
                    } else if (lm_L) {
                        GET_ARG(user_ld, long double*);
                        *user_ld = (long double)__strtold(NULL, width,
                                                          get_next_char,
                                                          inc_next_char,
                                                          (void*)&mem_stream);
#endif /* CONFIG_WITHOUT_LONG_DOUBLE */
                    } else {
                        GET_ARG(user_f, float*);
                        *user_f = (float)__strtold(NULL, width, get_next_char,
                                        inc_next_char, (void*)&mem_stream);
                    }
                }
            }
            break;
#endif /* CONFIG_WITHOUT_FLOATING */
        case 'S':
            lm_l = true;
        case 's':
            eat_spaces(&mem_stream);
            if (!suppress) {
                GET_ARG(user_s, char*);
                i = 0;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            while (s != NULL && *s != '\0' && !isspace(*s)) {
                if (use_width) {
                    if (width > 0) {
                        width--;
                    } else {
                        break;
                    }
                }
                if (!suppress) {
                    user_s[i] = *s;
                    i++;
                }
                __inc_next_char(&mem_stream);
                s = __get_next_char(&mem_stream);
            }
            if (!suppress) {
                assign_count++;
                user_s[i] = '\0';
            }
            break;
        case '[':
            list_not = false;
            p++;
            if (*p == '^') {
                list_not = true;
                p++;
            }
            if (*p == '\0') {
                goto matching_failure;
            }
            list_start = p;
            p++;
            while (*p != ']') {
                if (*p == '\0') {
                    goto matching_failure;
                }
                p++;
            }
            list_end = p;

            if (!suppress) {
                GET_ARG(user_s, char*);
                i = 0;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            while (s != NULL && *s != '\0' &&
                        check_list(*s, list_start, list_end, list_not)) {
                if (use_width) {
                    if (width > 0) {
                        width--;
                    } else {
                        break;
                    }
                }
                if (!suppress) {
                    user_s[i] = *s;
                    i++;
                }
                __inc_next_char(&mem_stream);
                s = __get_next_char(&mem_stream);
            }
            if (!suppress) {
                assign_count++;
                user_s[i] = '\0';
            }
            break;
        case 'C':
            lm_l = true;
        case 'c':
            use_width = true;
            if (!suppress) {
                GET_ARG(user_s, char*);
                i = 0;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            for (; width > 0 && s != NULL && *s != '\0'; width--) {
                if (!suppress) {
                    user_s[i] = *s;
                    i++;
                }
                __inc_next_char(&mem_stream);
                s = __get_next_char(&mem_stream);
            }
            if (__is_eof(s) && width > 0) {
                goto eof_failure;
            }
            if (!suppress) {
                assign_count++;
                user_s[i] = '\0';
            }
            break;
        case 'p':
            eat_spaces(&mem_stream);
            if (!use_width) {
                width = 0;
            } else if (width < 2) {
                goto matching_failure;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (*s != '0') {
                goto matching_failure;
            }
            __inc_next_char(&mem_stream);
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (*s != 'x' && *s != 'X') {
                goto matching_failure;
            }
            __inc_next_char(&mem_stream);
            width -= 2;
            if (suppress) {
                (void)__strtoll(NULL, 16, width, get_next_char, inc_next_char,
                                                    (void*)&mem_stream);
            } else {
                assign_count++;
                GET_ARG(user_pp, void**);
                *user_pp = (void*)(long)__strtoll(NULL, 16, width, get_next_char,
                                    inc_next_char, (void*)&mem_stream);
            }
            break;
        case 'n':
            if (lm_ll) {
                GET_ARG(user_ll, long long*);
                *user_ll = (long long)mem_stream.read_bytes;
            } else if (lm_l) {
                GET_ARG(user_l, long*);
                *user_l = (long)mem_stream.read_bytes;
            } else if (lm_hh) {
                GET_ARG(user_s, char*);
                *user_s = (char)mem_stream.read_bytes;
            } else if (lm_h) {
                GET_ARG(user_sh, short*);
                *user_sh = (short)mem_stream.read_bytes;
            } else if (lm_j) {
                GET_ARG(user_j, intmax_t*);
                *user_j = (intmax_t)mem_stream.read_bytes;
            } else if (lm_z) {
                GET_ARG(user_i, int*);
                *user_i = (int)mem_stream.read_bytes;
            } else if (lm_t) {
                GET_ARG(user_t, ptrdiff_t*);
                *user_t = (ptrdiff_t)mem_stream.read_bytes;
            } else {
                GET_ARG(user_i, int*);
                *user_i = (int)mem_stream.read_bytes;
            }
            break;
        default:
            if (*p == '\0') {
                break;
            }
        }
    }

matching_failure:
    if (stream != NULL) {
        unlock_stream(stream);
    }

    return assign_count;
    /* XXX: va_end */

input_failure:
    if (stream != NULL) {
        unlock_stream(stream);
    }
    return EOF;

eof_failure:
    if (assign_count > 0) {
        goto matching_failure;
    } else {
        goto input_failure;
    }

}
Beispiel #16
0
static int mprCoreStrcat(MPR_LOC_DEC(ctx, loc), char **destp, int destMax, 
	int existingLen, const char *delim, const char *src, va_list args)
{
	va_list		ap;
	char		*dest, *str, *dp;
	int			sepLen, addBytes, required;

	mprAssert(destp);
	mprAssert(destMax >= 0);
	mprAssert(src);

	dest = *destp;
	sepLen = (delim) ? strlen(delim) : 0;

#ifdef __va_copy
	__va_copy(ap, args);
#else
	ap = args;
#endif
	addBytes = 0;
	if (existingLen > 0) {
		addBytes += sepLen;
	}
	str = (char*) src;

	while (str) {
		addBytes += strlen(str);
		str = va_arg(ap, char*);
		if (str) {
			addBytes += sepLen;
		}
	}

	required = existingLen + addBytes + 1;
	if (destMax > 0 && required >= destMax) {
		mprAssert(0);
		return MPR_ERR_WONT_FIT;
	}

	if (ctx != 0) {
		if (dest == 0) {
			dest = (char*) mprAllocBlock(MPR_LOC_PASS(ctx, loc), required);
		} else {
			dest = (char*) mprReallocBlock(MPR_LOC_PASS(ctx, loc), dest, 
				required);
		}
	} else {
		dest = (char*) *destp;
	}

	dp = &dest[existingLen];
	if (delim && existingLen > 0) {
		strcpy(dp, delim);
		dp += sepLen;
	}

	if (addBytes > 0) {
#ifdef __va_copy
		__va_copy(ap, args);
#else
		ap = args;
#endif
		str = (char*) src;
		while (str) {
			strcpy(dp, str);
			dp += strlen(str);
			str = va_arg(ap, char*);
			if (delim && str) {
				strcpy(dp, delim);
				dp += sepLen;
			}
		}
	} else if (dest == 0) {
Beispiel #17
0
Datei: conf.c Projekt: rvs/libtc
static int
getentry(tcconf_section_t *ts, tcc_entry *te, char *fmt,
	 va_list args, char **tail)
{
    char *f = fmt, *p;
    int n = 0;
    tclist_item_t *li = NULL;
    tcc_value *tv;
    va_list ac;

    while((p = strchr(f, '%')) != NULL){
	void *dest;
	int type = 0;

	if((tv = tclist_next(te->value.values, &li)) == NULL)
	    break;

	f = p;

	if(tv->type == TCC_REF && strcmp(tv->value.string, "NULL")){
	    tcconf_section_t *rs = tcref(ts);
	    tcc_entry *re;
	    int r;

	    if(!(re = getvalue(rs->sec, tv->value.string, &rs))){
		n = -n;
		break;
	    }
#ifdef __va_copy
	    __va_copy(ac, args);
#else
	    ac = args;
#endif
	    r = getentry(ts, re, f, ac, &f);
	    tcfree(rs);
	    if(r < 0){
		n += -r;
		break;
	    }
	    n += r;
	    while(r--)
		va_arg(args, void *);
	    continue;
	}

	dest = va_arg(args, void *);
	f++;

	while(!(type & TCC_TYPEMASK) && *f){
	    switch(*f){
	    case 's':
		type |= TCC_STRING;
		break;

	    case 'd':
	    case 'i':
		type |= TCC_INTEGER;
		break;

	    case 'f':
		type |= TCC_FLOAT;
		break;

	    case 'l':
		type |= TCC_LONG;
		break;

	    case 'u':
		type |= TCC_UNSIGNED;
		break;

	    case 'z':
		type |= TCC_IGNORE;
		break;
	    }
	    f++;
	}

	if(cp_val(ts, tv, type, dest) < 0){
	    fprintf(stderr, "Type mismatch in '%s'.\n", te->value.key);
	    n = -n;
	    break;
	} else {
	    n++;
	}
    }

    if(tail)
	*tail = f;

    if(li)
	tclist_unlock(te->value.values, li);

    return n;
}
int
vasprintf( char **strp, const char *format, va_list ap )
{
	int size, check_size;
	char *buf = NULL;

#ifdef HAVE_VA_COPY
	va_list aq;

	va_copy(aq, ap);
#else
#  ifdef HAVE___VA_COPY
	va_list aq;

	__va_copy(aq, ap);
#  endif
#endif

	*strp = NULL;

#ifndef HAVE_C99_VSNPRINTF
	{
		int res;
		char *tmp;

		size = 128;
		do {
			size *= 2;
			if (!(tmp = realloc(buf, size))) {
				if (buf)
					free(buf);
				return -1;
			}
			buf = tmp;
			/* XXX we're assuming here there's no va_copy on this system */
			res = vsnprintf(buf, size, format, ap);
		} while (res == -1);
	}
#else
	{
		char tmp[2];

		/* on Solaris vsnprintf fails if buf is empty, so use a small one */
		size = vsnprintf(tmp, sizeof(tmp), format, ap);
		if (!(buf = malloc(size + 1)))
			return -1;
	}
#endif

#ifdef HAVE_VA_COPY
	check_size = vsnprintf(buf, size + 1, format, aq);
	va_end(aq);
#else
#  ifdef HAVE___VA_COPY
	check_size = vsnprintf(buf, size + 1, format, aq);
	va_end(aq);
#  else
	check_size = vsnprintf(buf, size + 1, format, ap);
#  endif
#endif
	assert(check_size <= size);
	*strp = buf;
	return 0;
}
Beispiel #19
0
void
__kmp_str_buf_vprint(
    kmp_str_buf_t *  buffer,
    char const *     format,
    va_list          args
) {

    KMP_STR_BUF_INVARIANT( buffer );

    for ( ; ; ) {

        int const free = buffer->size - buffer->used;
        int       rc;
        int       size;

        // Try to format string.
        {
            /*
                On Linux* OS Intel(R) 64, vsnprintf() modifies args argument, so vsnprintf() crashes if it
                is called for the second time with the same args. To prevent the crash, we have to
                pass a fresh intact copy of args to vsnprintf() on each iteration.

                Unfortunately, standard va_copy() macro is not available on Windows* OS. However, it
                seems vsnprintf() does not modify args argument on Windows* OS.
            */

            #if ! KMP_OS_WINDOWS
                va_list _args;
                __va_copy( _args, args );  // Make copy of args.
                #define args _args         // Substitute args with its copy, _args.
            #endif // KMP_OS_WINDOWS
            rc = vsnprintf( buffer->str + buffer->used, free, format, args );
            #if ! KMP_OS_WINDOWS
                #undef args                // Remove substitution.
                va_end( _args );
            #endif // KMP_OS_WINDOWS
        }

        // No errors, string has been formatted.
        if ( rc >= 0 && rc < free ) {
            buffer->used += rc;
            break;
        }; // if

        // Error occured, buffer is too small.
        if ( rc >= 0 ) {
            // C99-conforming implementation of vsnprintf returns required buffer size.
            size = buffer->used + rc + 1;
        } else {
            // Older implementations just return -1. Double buffer size.
            size = buffer->size * 2;
        }; // if

        // Enlarge buffer.
        __kmp_str_buf_reserve( buffer, size );

        // And try again.

    }; // forever

    KMP_DEBUG_ASSERT( buffer->size > 0 );
    KMP_STR_BUF_INVARIANT( buffer );

} // __kmp_str_buf_vprint