From: short <> Date: Sun, 25 Nov 2001 23:04:51 +0000 (+0000) Subject: * new common/misc.c/ X-Git-Url: https://git.jankratochvil.net/?a=commitdiff_plain;h=2a6284cf1a380b55f3ad2bf71a63946e2595b812;hp=9d80f3acb04fd990119dbefc10aaf5a56de13ea9;p=gnokii.git * new common/misc.c/ * g{,v}asprintf() - "asprintf()" compatibility emulation * ARRAY_LEN() - sizeof(x)/sizeof(*x) * SAFE_STRNCPY{,_SIZEOF}() - strncpy with variable-size autodetection * G_GNUC_PRINTF - GCC attribute from glib * N_(x) - missing in localization macros --- diff --git a/common/misc.c b/common/misc.c index d9c57ad..14bc240 100644 --- a/common/misc.c +++ b/common/misc.c @@ -11,6 +11,14 @@ $Id$ $Log$ + Revision 1.1.1.1.6.1 2001/11/25 23:04:51 short + * new common/misc.c/ + * g{,v}asprintf() - "asprintf()" compatibility emulation + * ARRAY_LEN() - sizeof(x)/sizeof(*x) + * SAFE_STRNCPY{,_SIZEOF}() - strncpy with variable-size autodetection + * G_GNUC_PRINTF - GCC attribute from glib + * N_(x) - missing in localization macros + Revision 1.1.1.1 2001/11/25 21:59:04 short :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001 @@ -46,6 +54,7 @@ */ #include +#include #include "misc.h" int GetLine(FILE *File, char *Line, int count) @@ -126,4 +135,49 @@ inline char *GetModel (const char *num) return (GetPhoneModel(num)->model); } +#ifndef HAVE_VASPRINTF +/* Adapted from snprintf(3) man page: */ +int gvasprintf(char **destp,const char *fmt,va_list ap) +{ +int n,size=0x100; +char *p,*pnew; + if (!(p=malloc(size))) { + *destp=NULL; + return(-1); + } + for (;;) { + /* Try to print in the allocated space. */ + n=gvsprintf(p,size,fmt,ap); + /* If that worked, return the string. */ + if (n>-1 && n-1) /* glibc 2.1 */ + size=n+1; /* precisely what is needed */ + else /* glibc 2.0 */ + size*=2; /* twice the old size */ + if (!(pnew=realloc(p,size))) { + free(p); + *destp=NULL; + return(-1); + } + p=pnew; + } +} +#endif + +#ifndef HAVE_ASPRINTF +int gasprintf(char **destp,const char *fmt,...) +{ +va_list ap; +int r; + + va_start(ap,fmt); + r=gvasprintf(destp,fmt,ap); + va_end(ap); + return(r); +} +#endif diff --git a/common/phones/atgen.c b/common/phones/atgen.c index fb1da2e..3f46041 100644 --- a/common/phones/atgen.c +++ b/common/phones/atgen.c @@ -14,6 +14,14 @@ phones. See README for more details on supported mobile phones. $Log$ + Revision 1.1.1.1.6.1 2001/11/25 23:04:51 short + * new common/misc.c/ + * g{,v}asprintf() - "asprintf()" compatibility emulation + * ARRAY_LEN() - sizeof(x)/sizeof(*x) + * SAFE_STRNCPY{,_SIZEOF}() - strncpy with variable-size autodetection + * G_GNUC_PRINTF - GCC attribute from glib + * N_(x) - missing in localization macros + Revision 1.1.1.1 2001/11/25 21:59:11 short :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001 @@ -54,9 +62,6 @@ #include "links/cbus.h" -#define ARRAY_LEN(x) (sizeof((x))/sizeof((x)[0])) - - static GSM_Error Initialise(GSM_Data *setupdata, GSM_Statemachine *state); static GSM_Error Functions(GSM_Operation op, GSM_Data *data, GSM_Statemachine *state); static GSM_Error Reply(int messagetype, unsigned char *buffer, int length, GSM_Data *data); diff --git a/configure.in b/configure.in index c3ab23c..2b2676a 100644 --- a/configure.in +++ b/configure.in @@ -437,7 +437,7 @@ AC_PROG_GCC_TRADITIONAL AC_FUNC_MEMCMP AC_TYPE_SIGNAL AC_FUNC_STRFTIME -AC_CHECK_FUNCS(mktime select strdup strstr strtol strtok strsep snprintf) +AC_CHECK_FUNCS(mktime select strdup strstr strtol strtok strsep snprintf vsnprintf asprintf vasprintf) CFLAGS="$CFLAGS $NLS_CFLAGS" LIBS="$LIBS $NLS_LIBS" diff --git a/include/misc.h b/include/misc.h index 3e91ff0..5581450 100644 --- a/include/misc.h +++ b/include/misc.h @@ -13,6 +13,14 @@ Header file for miscellaneous defines, typedefs etc. $Log$ + Revision 1.1.1.1.6.1 2001/11/25 23:04:51 short + * new common/misc.c/ + * g{,v}asprintf() - "asprintf()" compatibility emulation + * ARRAY_LEN() - sizeof(x)/sizeof(*x) + * SAFE_STRNCPY{,_SIZEOF}() - strncpy with variable-size autodetection + * G_GNUC_PRINTF - GCC attribute from glib + * N_(x) - missing in localization macros + Revision 1.1.1.1 2001/11/25 21:59:21 short :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001 @@ -48,6 +56,28 @@ #define bool int #endif +#define ARRAY_LEN(x) (sizeof((x))/sizeof((x)[0])) + +#define SAFE_STRNCPY(dest,src,n) do { \ + strncpy((dest),(src),(n)); \ + if ((n)>0) \ + (dest)[(n)-1]='\0'; \ + } while (0) + +#define SAFE_STRNCPY_SIZEOF(dest,src) \ + SAFE_STRNCPY((dest),(src),sizeof((dest))) + +/* Stolen from : */ +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) +#define G_GNUC_PRINTF( format_idx, arg_idx ) \ + __attribute__((format (printf, format_idx, arg_idx))) +#else /* !__GNUC__ */ +#define G_GNUC_PRINTF( format_idx, arg_idx ) +#endif /* !__GNUC__ */ + +#define GNOKII_MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define GNOKII_MIN(a, b) (((a) < (b)) ? (a) : (b)) + /* A define to make debug printfs neat */ #ifndef DEBUG #define dprintf(a...) do { } while (0) @@ -61,6 +91,23 @@ #else # define gsprintf(a, b, c...) sprintf(a, c) #endif +#ifdef HAVE_VSNPRINTF +# define gvsprintf(a, b, c...) vsnprintf(a, b, c) +#else +# define gvsprintf(a, b, c...) vsprintf(a, c) +#endif +#ifdef HAVE_ASPRINTF +# define gasprintf(a...) asprintf(a) +#else +#include +extern int gasprintf(char **destp,const char *fmt,...); +#endif +#ifdef HAVE_VASPRINTF +# define gvasprintf(a...) vasprintf(a) +#else +#include +extern int gvasprintf(char **destp,const char *fmt,va_list ap); +#endif /* Get rid of long defines. Use #if __unices__ */ #define __unices__ defined(__svr4__) || defined(__FreeBSD__) || defined(__bsdi__) @@ -76,6 +123,7 @@ #else #define _(x) (x) #endif /* USE_NLS */ +#define N_(x) (x) /* Definitions for u8, u16, u32 and u64, borrowed from /usr/src/linux/include/asm-i38/types.h */