[Asterisk-Dev] strdupa not common, take 2

Thorsten Lockert tholo at sigmasoft.com
Sun Apr 27 14:04:22 MST 2003


strdupa() is not at all common -- this diff provides a private
definition for systems using GCC, and changes the code to use
that.

Index: indications.c
===================================================================
RCS file: /usr/cvsroot/asterisk/indications.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 indications.c
--- indications.c	16 Mar 2003 22:37:29 -0000	1.1.1.2
+++ indications.c	27 Apr 2003 21:05:34 -0000
@@ -140,7 +140,7 @@
 
 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
 {
-	char *s, *data = strdupa(playlst); /* cute */
+	char *s, *data = ast_strdupa(playlst); /* cute */
 	struct playtones_def d = { vol, -1, 0, 1, NULL};
 	char *stringp=NULL;
 	if (!data)
Index: channels/chan_modem.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_modem.c,v
retrieving revision 1.2
diff -u -r1.2 chan_modem.c
--- channels/chan_modem.c	27 Apr 2003 18:13:11 -0000	1.2
+++ channels/chan_modem.c	27 Apr 2003 21:05:51 -0000
@@ -808,7 +808,7 @@
 	char *piece;
 	int start, finish,x;
 	unsigned int group = 0;
-	char *copy = strdupa(s);
+	char *copy = ast_strdupa(s);
 	char *stringp=NULL;
 	if (!copy) {
 		ast_log(LOG_ERROR, "Out of memory\n");
Index: include/asterisk/channel.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/channel.h,v
retrieving revision 1.8
diff -u -r1.8 channel.h
--- include/asterisk/channel.h	27 Apr 2003 20:43:12 -0000	1.8
+++ include/asterisk/channel.h	27 Apr 2003 21:06:01 -0000
@@ -708,6 +708,17 @@
 #endif
 }
 
+#if !defined(ast_strdupa) && defined(__GNUC__)
+# define ast_strdupa(s)									\
+  (__extension__										\
+    ({													\
+      __const char *__old = (s);						\
+      size_t __len = strlen (__old) + 1;				\
+      char *__new = (char *) __builtin_alloca (__len);	\
+      (char *) memcpy (__new, __old, __len);			\
+    }))
+#endif
+
 #ifdef DO_CRASH
 #define CRASH do { fprintf(stderr, "!! Forcing immediate crash a-la abort !!\n"); *((int *)0) = 0; } while(0)
 #else
Index: res/res_indications.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_indications.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 res_indications.c
--- res/res_indications.c	16 Mar 2003 22:37:31 -0000	1.1.1.2
+++ res/res_indications.c	27 Apr 2003 21:06:03 -0000
@@ -248,7 +248,7 @@
 			if (!strcasecmp(v->name, "description")) {
 				strncpy(tones->description, v->value, sizeof(tones->description)-1);
 			} else if (!strcasecmp(v->name,"ringcadance")) {
-				char *ring,*rings = strdupa(v->value);
+				char *ring,*rings = ast_strdupa(v->value);
 				c = rings;
 				ring = strsep(&c,",");
 				while (ring) {
@@ -270,7 +270,7 @@
 					ring = strsep(&c,",");
 				}
 			} else if (!strcasecmp(v->name,"alias")) {
-				char *countries = strdupa(v->value);
+				char *countries = ast_strdupa(v->value);
 				c = countries;
 				country = strsep(&c,",");
 				while (country) {
Index: res/res_parking.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_parking.c,v
retrieving revision 1.3
diff -u -r1.3 res_parking.c
--- res/res_parking.c	27 Apr 2003 18:13:11 -0000	1.3
+++ res/res_parking.c	27 Apr 2003 21:06:03 -0000
@@ -611,7 +611,7 @@
 	char *c=NULL;
 	int start=0, finish=0,x;
 	unsigned int group = 0;
-	copy = strdupa(s);
+	copy = ast_strdupa(s);
 	if (!copy) {
 		ast_log(LOG_ERROR, "Out of memory\n");
 		return 0;
--
Thorsten Lockert      | tholo at sigmasoft.com | Universe, n.:
2121 N. Lakeshore Dr. | tholo at openbsd.org   |         The problem.
Chapel Hill, NC 27514 |                     |



More information about the asterisk-dev mailing list