Skip to content

Commit a61dba0

Browse files
tleguernpasis
authored andcommitted
Move declarations of xmpp_{v,}snprintf.
Use HAVE_SNPRINTF and HAVE_VSNPRINTF to conditionnaly use an internal version of these functions or use the libc one.
1 parent 60cfcc7 commit a61dba0

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ libstrophe_la_SOURCES = src/auth.c src/conn.c src/ctx.c \
2525
src/snprintf.c src/sock.c src/stanza.c src/thread.c \
2626
src/tls_openssl.c src/util.c src/rand.c src/uuid.c \
2727
src/common.h src/hash.h src/md5.h src/ostypes.h src/parser.h \
28-
src/sasl.h src/scram.h src/sha1.h src/sock.h src/thread.h src/tls.h \
29-
src/util.h src/rand.h
28+
src/sasl.h src/scram.h src/sha1.h src/snprintf.h src/sock.h \
29+
src/thread.h src/tls.h src/util.h src/rand.h
3030

3131
if PARSER_EXPAT
3232
libstrophe_la_SOURCES += src/parser_expat.c

src/common.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "util.h"
2929
#include "parser.h"
3030
#include "rand.h"
31+
#include "snprintf.h"
3132

3233
/** run-time context **/
3334

@@ -266,8 +267,4 @@ void disconnect_mem_error(xmpp_conn_t * const conn);
266267
void auth_handle_open(xmpp_conn_t * const conn);
267268
void auth_handle_component_open(xmpp_conn_t * const conn);
268269

269-
/* replacement snprintf and vsnprintf */
270-
int xmpp_snprintf (char *str, size_t count, const char *fmt, ...);
271-
int xmpp_vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
272-
273270
#endif /* __LIBSTROPHE_COMMON_H__ */

src/snprintf.c

+10-47
Original file line numberDiff line numberDiff line change
@@ -64,49 +64,27 @@
6464
/* JAM: changed declarations to xmpp_snprintf and xmpp_vsnprintf to
6565
avoid namespace collision. */
6666

67-
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
67+
#include "snprintf.h"
6868

69-
#include <string.h>
70-
#include <ctype.h>
71-
#include <sys/types.h>
72-
73-
/* Define this as a fall through, HAVE_STDARG_H is probably already set */
74-
75-
#define HAVE_VARARGS_H
76-
#define HAVE_STDARG_H /* JAM: set always */
69+
/* varargs declarations: */
7770

71+
#include <stdarg.h>
72+
#define VA_LOCAL_DECL va_list ap
73+
#define VA_START(f) va_start(ap, f)
74+
#define VA_END va_end(ap)
7875

79-
/* varargs declarations: */
76+
#ifndef HAVE_VSNPRINTF
8077

81-
#if defined(HAVE_STDARG_H)
82-
# include <stdarg.h>
83-
# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
84-
# define VA_LOCAL_DECL va_list ap
85-
# define VA_START(f) va_start(ap, f)
86-
# define VA_SHIFT(v,t) ; /* no-op for ANSI */
87-
# define VA_END va_end(ap)
88-
#else
89-
# if defined(HAVE_VARARGS_H)
90-
# include <varargs.h>
91-
# undef HAVE_STDARGS
92-
# define VA_LOCAL_DECL va_list ap
93-
# define VA_START(f) va_start(ap) /* f is ignored! */
94-
# define VA_SHIFT(v,t) v = va_arg(ap,t)
95-
# define VA_END va_end(ap)
96-
# else
97-
/*XX ** NO VARARGS ** XX*/
98-
# endif
99-
#endif
78+
#include <string.h>
79+
#include <ctype.h>
80+
#include <sys/types.h>
10081

10182
#ifdef HAVE_LONG_DOUBLE
10283
#define LDOUBLE long double
10384
#else
10485
#define LDOUBLE double
10586
#endif
10687

107-
int xmpp_snprintf (char *str, size_t count, const char *fmt, ...);
108-
int xmpp_vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
109-
11088
static int dopr (char *buffer, size_t maxlen, const char *format,
11189
va_list args);
11290
static int fmtstr (char *buffer, size_t *currlen, size_t maxlen,
@@ -725,7 +703,6 @@ static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
725703
return 1;
726704
}
727705

728-
#ifndef HAVE_VSNPRINTF
729706
int xmpp_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
730707
{
731708
if (str != NULL)
@@ -736,28 +713,14 @@ int xmpp_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
736713

737714
#ifndef HAVE_SNPRINTF
738715
/* VARARGS3 */
739-
#ifdef HAVE_STDARGS
740716
int xmpp_snprintf (char *str,size_t count,const char *fmt,...)
741-
#else
742-
int xmpp_snprintf (va_alist) va_dcl
743-
#endif
744717
{
745-
#ifndef HAVE_STDARGS
746-
char *str;
747-
size_t count;
748-
char *fmt;
749-
#endif
750718
VA_LOCAL_DECL;
751719
int total;
752720

753721
VA_START (fmt);
754-
VA_SHIFT (str, char *);
755-
VA_SHIFT (count, size_t );
756-
VA_SHIFT (fmt, char *);
757722
total = xmpp_vsnprintf(str, count, fmt, ap);
758723
VA_END;
759724
return total;
760725
}
761726
#endif /* !HAVE_SNPRINTF */
762-
763-
#endif /* !HAVE_SNPRINTF */

src/snprintf.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Patrick Powell 1995
3+
* This code is based on code written by Patrick Powell (papowell@astart.com)
4+
* It may be used for any purpose as long as this notice remains intact
5+
* on all source code distributions
6+
*/
7+
8+
/** @file
9+
* Compatibility wrappers for OSes lacking snprintf(3) and/or vsnprintf(3).
10+
*/
11+
12+
#ifndef __LIBSTROPHE_SNPRINTF_H__
13+
#define __LIBSTROPHE_SNPRINTF_H__
14+
15+
#include <stddef.h>
16+
#include <stdarg.h>
17+
18+
#if defined(HAVE_SNPRINTF) || defined(HAVE_VSNPRINTF)
19+
#include <stdio.h>
20+
#endif
21+
22+
#ifdef HAVE_SNPRINTF
23+
#define xmpp_snprintf snprintf
24+
#else
25+
int xmpp_snprintf(char *str, size_t count, const char *fmt, ...);
26+
#endif
27+
28+
#ifdef HAVE_VSNPRINTF
29+
#define xmpp_vsnprintf vsnprintf
30+
#else
31+
int xmpp_vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
32+
#endif
33+
34+
#endif /* __LIBSTROPHE_SNPRINTF_H__ */

tests/test_snprintf.c

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <stdio.h>
9+
#include <string.h>
910

1011
/* We don't have separated header file, so include this instead of common.h */
1112
#include "snprintf.c"

0 commit comments

Comments
 (0)