* new common/misc.c/
authorshort <>
Sun, 25 Nov 2001 23:04:51 +0000 (23:04 +0000)
committershort <>
Sun, 25 Nov 2001 23:04:51 +0000 (23:04 +0000)
  * 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

common/misc.c
common/phones/atgen.c
configure.in
include/misc.h

index d9c57ad..14bc240 100644 (file)
   $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 <string.h>
+#include <stdlib.h>
 #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<size) {
+                       *destp=p;
+                       return(n);
+                       }
+               /* Else try again with more space. */
+               if (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
index fb1da2e..3f46041 100644 (file)
   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);
index c3ab23c..2b2676a 100644 (file)
@@ -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"
index 3e91ff0..5581450 100644 (file)
   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
 
   #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 <glib.h>: */
+#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)
 #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 <stdarg.h>
+extern int gasprintf(char **destp,const char *fmt,...);
+#endif
+#ifdef HAVE_VASPRINTF
+# define gvasprintf(a...) vasprintf(a)
+#else
+#include <stdarg.h>
+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__)
 #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 */