[asterisk-commits] russell: branch russell/ast_cli_tls r38169 - /team/russell/ast_cli_tls/cli.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Jul 24 10:34:31 MST 2006


Author: russell
Date: Mon Jul 24 12:34:30 2006
New Revision: 38169

URL: http://svn.digium.com/view/asterisk?rev=38169&view=rev
Log:
tweak this code to make it more readable by using an anonmymous struct to
hold the buf size as well as the buffer itself

Modified:
    team/russell/ast_cli_tls/cli.c

Modified: team/russell/ast_cli_tls/cli.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_cli_tls/cli.c?rev=38169&r1=38168&r2=38169&view=diff
==============================================================================
--- team/russell/ast_cli_tls/cli.c (original)
+++ team/russell/ast_cli_tls/cli.c Mon Jul 24 12:34:30 2006
@@ -64,37 +64,36 @@
 
 void ast_cli(int fd, char *fmt, ...)
 {
-	void *buf;
-	unsigned int *len;
-	char *str;
+	struct {
+		size_t len;
+		char str[1];
+	} *buf;
 	int res;
 	va_list ap;
 
 	pthread_once(&ast_cli_buf_once, ast_cli_buf_key_create);
-	if (!(len = buf = pthread_getspecific(ast_cli_buf_key))) {
-		if (!(len = buf = ast_malloc(AST_CLI_MAXSTRLEN + sizeof(*len))))
+	if (!(buf = pthread_getspecific(ast_cli_buf_key))) {
+		if (!(buf = ast_malloc(AST_CLI_MAXSTRLEN + sizeof(*buf))))
 			return;
-		*len = AST_CLI_MAXSTRLEN;
+		buf->len = AST_CLI_MAXSTRLEN;
 		pthread_setspecific(ast_cli_buf_key, buf);
 	}
-	str = buf + sizeof(*len);
 
 	va_start(ap, fmt);
-	res = vsnprintf(str, *len, fmt, ap);
-	while (res >= *len) {
-		*len *= 2;
-		if (!(len = buf = ast_realloc(buf, *len + sizeof(*len)))) {
+	res = vsnprintf(buf->str, buf->len, fmt, ap);
+	while (res >= buf->len) {
+		buf->len *= 2;
+		if (!(buf = ast_realloc(buf, buf->len + sizeof(*buf)))) {
 			va_end(ap);
 			return;
 		}
 		pthread_setspecific(ast_cli_buf_key, buf);
-		str = buf + sizeof(*len);
-		res = vsnprintf(str, *len, fmt, ap);
+		res = vsnprintf(buf->str, buf->len, fmt, ap);
 	}
 	va_end(ap);
 
 	if (res > 0)
-		ast_carefulwrite(fd, str, strlen(str), 100);
+		ast_carefulwrite(fd, buf->str, strlen(buf->str), 100);
 }
 
 static AST_LIST_HEAD_STATIC(helpers, ast_cli_entry);



More information about the asterisk-commits mailing list