ReactOS  0.4.14-dev-77-gd9e7c48
asprintf.c File Reference
#include <wintirpc.h>
#include <stdio.h>
#include <varargs.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
Include dependency graph for asprintf.c:

Go to the source code of this file.

Macros

#define VA_COPY(dest, src)   (dest) = (src)
 
#define INIT_SZ   128
 

Functions

int vasprintf (char **str, const char *fmt, va_list ap)
 
int asprintf (char **str, const char *fmt,...)
 

Macro Definition Documentation

◆ INIT_SZ

#define INIT_SZ   128

Definition at line 46 of file asprintf.c.

◆ VA_COPY

#define VA_COPY (   dest,
  src 
)    (dest) = (src)

Definition at line 41 of file asprintf.c.

Function Documentation

◆ asprintf()

int asprintf ( char **  str,
const char fmt,
  ... 
)

Definition at line 95 of file asprintf.c.

96 {
97  va_list ap;
98  int ret;
99 
100  *str = NULL;
101  va_start(ap, fmt);
102  ret = vasprintf(str, fmt, ap);
103  va_end(ap);
104 
105  return ret;
106 }
int vasprintf(char **str, const char *fmt, va_list ap)
Definition: asprintf.c:49
#define va_end(ap)
Definition: acmsvcex.h:90
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
int ret
#define va_start(ap, A)
Definition: acmsvcex.h:91
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
Definition: dsound.c:943

Referenced by __rpc_taddr2uaddr_af().

◆ vasprintf()

int vasprintf ( char **  str,
const char fmt,
va_list  ap 
)

Definition at line 49 of file asprintf.c.

50 {
51  int ret = -1;
52  va_list ap2;
53  char *string, *newstr;
54  size_t len;
55 
56  VA_COPY(ap2, ap);
57  if ((string = malloc(INIT_SZ)) == NULL)
58  goto fail;
59 
60  ret = _vsnprintf(string, INIT_SZ, fmt, ap2);
61  if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
62  *str = string;
63  } else if (ret == INT_MAX || ret < 0) { /* Bad length */
64  goto fail;
65  } else { /* bigger than initial, realloc allowing for nul */
66  len = (size_t)ret + 1;
67  if ((newstr = realloc(string, len)) == NULL) {
68  free(string);
69  goto fail;
70  } else {
71  va_end(ap2);
72  VA_COPY(ap2, ap);
73  ret = _vsnprintf(newstr, len, fmt, ap2);
74  if (ret >= 0 && (size_t)ret < len) {
75  *str = newstr;
76  } else { /* failed with realloc'ed string, give up */
77  free(newstr);
78  goto fail;
79  }
80  }
81  }
82  va_end(ap2);
83  return (ret);
84 
85 fail:
86  *str = NULL;
87  errno = ENOMEM;
88  va_end(ap2);
89  return (-1);
90 }
#define realloc
Definition: debug_ros.c:6
#define INT_MAX
Definition: limits.h:40
#define free
Definition: debug_ros.c:5
int errno
Definition: arc.h:48
#define va_end(ap)
Definition: acmsvcex.h:90
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
#define VA_COPY(dest, src)
Definition: asprintf.c:41
__kernel_size_t size_t
Definition: linux.h:237
#define INIT_SZ
Definition: asprintf.c:46
int ret
GLenum GLsizei len
Definition: glext.h:6722
char string[160]
Definition: util.h:11
#define _vsnprintf
Definition: xmlstorage.h:202
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
#define malloc
Definition: debug_ros.c:4
Definition: dsound.c:943

Referenced by asprintf().