[asterisk-commits] tilghman: trunk r97364 - in /trunk: ./ main/cli.c main/editline/readline.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jan 8 18:52:00 CST 2008


Author: tilghman
Date: Tue Jan  8 18:51:59 2008
New Revision: 97364

URL: http://svn.digium.com/view/asterisk?view=rev&rev=97364
Log:
Merged revisions 97350 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r97350 | tilghman | 2008-01-08 18:44:14 -0600 (Tue, 08 Jan 2008) | 5 lines

Allow filename completion on zero-length modules, remove a memory leak, remove
a file descriptor leak, and make filename completion thread-safe.
Patched and tested by tilghman.
(Closes issue #11681)

........

Modified:
    trunk/   (props changed)
    trunk/main/cli.c
    trunk/main/editline/readline.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-1.4-merged' - no diff available.

Modified: trunk/main/cli.c
URL: http://svn.digium.com/view/asterisk/trunk/main/cli.c?view=diff&rev=97364&r1=97363&r2=97364
==============================================================================
--- trunk/main/cli.c (original)
+++ trunk/main/cli.c Tue Jan  8 18:51:59 2008
@@ -121,7 +121,7 @@
 
 static char *complete_fn(const char *word, int state)
 {
-	char *c;
+	char *c, *d;
 	char filename[256];
 
 	if (word[0] == '/')
@@ -129,13 +129,15 @@
 	else
 		snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_MODULE_DIR, word);
 
-	/* XXX the following function is not reentrant, so we better not use it */
-	c = filename_completion_function(filename, state);
+	c = d = filename_completion_function(filename, state);
 	
 	if (c && word[0] != '/')
 		c += (strlen(ast_config_AST_MODULE_DIR) + 1);
+	if (c)
+		c = ast_strdup(c);
+	free(d);
 	
-	return c ? ast_strdup(c) : c;
+	return c;
 }
 
 static char *handle_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Modified: trunk/main/editline/readline.c
URL: http://svn.digium.com/view/asterisk/trunk/main/editline/readline.c?view=diff&rev=97364&r1=97363&r2=97364
==============================================================================
--- trunk/main/editline/readline.c (original)
+++ trunk/main/editline/readline.c Tue Jan  8 18:51:59 2008
@@ -1189,68 +1189,63 @@
 
 /*
  * return first found file name starting by the ``text'' or NULL if no
- * such file can be found
- * value of ``state'' is ignored
+ * such file can be found.
+ * The first ``state'' matches are ignored.
  *
  * it's caller's responsibility to free returned string
  */
 char *
 filename_completion_function(const char *text, int state)
 {
-	static DIR *dir = NULL;
-	static char *filename = NULL, *dirname = NULL;
-	static size_t filename_len = 0;
+	DIR *dir = NULL;
+	char *filename = NULL, *dirname = NULL;
+	size_t filename_len = 0;
 	struct dirent *entry;
 	char *temp;
 	size_t len;
-
-	if (state == 0 || dir == NULL) {
-		if (dir != NULL) {
-			closedir(dir);
-			dir = NULL;
-		}
-		temp = strrchr(text, '/');
-		if (temp) {
-			temp++;
-			filename = realloc(filename, strlen(temp) + 1);
-			(void) strcpy(filename, temp);
-			len = temp - text;	/* including last slash */
-			dirname = realloc(dirname, len + 1);
-			(void) strncpy(dirname, text, len);
-			dirname[len] = '\0';
-		} else {
-			filename = strdup(text);
-			dirname = NULL;
-		}
-
-		/* support for ``~user'' syntax */
-		if (dirname && *dirname == '~') {
-			temp = tilde_expand(dirname);
-			dirname = realloc(dirname, strlen(temp) + 1);
-			(void) strcpy(dirname, temp);	/* safe */
-			free(temp);	/* no longer needed */
-		}
-		/* will be used in cycle */
-		filename_len = strlen(filename);
-		if (filename_len == 0)
-			return (NULL);	/* no expansion possible */
-
-		dir = opendir(dirname ? dirname : ".");
-		if (!dir)
-			return (NULL);	/* cannot open the directory */
-	}
+	int count = 0;
+
+	temp = strrchr(text, '/');
+	if (temp) {
+		temp++;
+		filename = realloc(filename, strlen(temp) + 1);
+		(void) strcpy(filename, temp);
+		len = temp - text;	/* including last slash */
+		dirname = realloc(dirname, len + 1);
+		(void) strncpy(dirname, text, len);
+		dirname[len] = '\0';
+	} else {
+		filename = strdup(text);
+		dirname = NULL;
+	}
+
+	/* support for ``~user'' syntax */
+	if (dirname && *dirname == '~') {
+		temp = tilde_expand(dirname);
+		dirname = realloc(dirname, strlen(temp) + 1);
+		(void) strcpy(dirname, temp);	/* safe */
+		free(temp);	/* no longer needed */
+	}
+	/* will be used in cycle */
+	filename_len = strlen(filename);
+
+	dir = opendir(dirname ? dirname : ".");
+	if (!dir)
+		return (NULL);	/* cannot open the directory */
+
 	/* find the match */
 	while ((entry = readdir(dir)) != NULL) {
 		/* otherwise, get first entry where first */
 		/* filename_len characters are equal	  */
-		if (entry->d_name[0] == filename[0]
+		if (
 #if defined(__SVR4) || defined(__linux__)
-		    && strlen(entry->d_name) >= filename_len
+		    strlen(entry->d_name) >= filename_len
 #else
-		    && entry->d_namlen >= filename_len
+		    entry->d_namlen >= filename_len
 #endif
 		    && strncmp(entry->d_name, filename,
-			filename_len) == 0)
+			filename_len) == 0
+			&& (state-- == 0))
 			break;
 	}
 
@@ -1272,6 +1267,7 @@
 			strcat(temp, "/");	/* safe */
 	} else
 		temp = NULL;
+	closedir(dir);
 
 	return (temp);
 }




More information about the asterisk-commits mailing list