Skip to content
Snippets Groups Projects
Commit 2391ec9c authored by Dirk Zimoch's avatar Dirk Zimoch
Browse files

added helper functions unavailable in vxWorks

parent e0f45e09
No related branches found
No related tags found
1 merge request!7Submodule merge
#include <stdio.h>
#include <stdlib.h>
#include "asprintf.h"
int vasprintf(char** pbuffer, const char* format, va_list ap)
{
va_list ap2;
int len;
FILE* f;
/* print to null device to get required buffer length */
f = fopen("/null","w");
if (f == NULL) return -1;
__va_copy(ap2, ap);
len = vfprintf(f, format, ap2);
va_end(ap2);
fclose(f);
if (len < 0) return len;
*pbuffer = malloc(len+1);
if (*pbuffer == NULL) return -1;
return vsprintf(*pbuffer, format, ap);
}
int asprintf(char** pbuffer, const char* format, ...)
{
va_list ap;
int len;
va_start(ap, format);
len = vasprintf(pbuffer, format, ap);
va_end(ap);
return len;
}
#ifndef asprintf_h
#define asprintf_h
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
int asprintf(char** pbuffer, const char* format, ...) __attribute__((format(printf,2,3)));
int vasprintf(char** pbuffer, const char* format, va_list ap) __attribute__((format(printf,2,0)));
#ifdef __cplusplus
}
#endif
#endif
strdup.c 0 → 100644
#include <string.h>
#include <stdlib.h>
char *strdup(const char *s)
{
char *d = malloc(strlen(s)+1);
if (d) strcpy(d,s);
return d;
}
char *strndup(const char *s, size_t n)
{
size_t l;
char *d;
l = strlen(s);
if (n > l) n = l;
d = malloc(n+1);
strncpy(d,s,l);
d[n] = 0;
return d;
}
strdup.h 0 → 100644
#ifndef strdup_h
#define strdup_h
#ifdef __cplusplus
extern "C" {
#endif
char *strdup(const char *s);
char *strndup(const char *s, size_t n);
#ifdef __cplusplus
}
#endif
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment