* command.c (examine_cd): Likewise. Additionally used g_strdup to

not accidentally modify an environment variable.
	* complete.c (command_completion_function): Likewise.
	(filename_completion_function): Likewise. (try_complete): Likewise.
	(variable_completion_function): Likewise.
This commit is contained in:
Roland Illig 2004-09-25 14:34:27 +00:00
parent a3c3564d4d
commit 9f6a39e83f
3 changed files with 30 additions and 18 deletions

View File

@ -5,6 +5,11 @@
* view.c (hex_search): Added const qualifier. * view.c (hex_search): Added const qualifier.
* boxes.c (display_unit): Likewise. * boxes.c (display_unit): Likewise.
* command.c (examine_cd): Likewise. Additionally used g_strdup to
not accidentally modify an environment variable.
* complete.c (command_completion_function): Likewise.
(filename_completion_function): Likewise. (try_complete): Likewise.
(variable_completion_function): Likewise.
* file.c (check_hardlinks): Likewise. * file.c (check_hardlinks): Likewise.
* find.c (find_do_view_edit): Likewise. (find_file): Likewise. * find.c (find_do_view_edit): Likewise. (find_file): Likewise.
* global.h (home_dir): Likewise. * global.h (home_dir): Likewise.

View File

@ -57,7 +57,8 @@ examine_cd (char *path)
{ {
int result, qlen; int result, qlen;
char *path_tilde; char *path_tilde;
char *p, *q, *r, *s, *t, c; char *p, *q, *r, *s, c;
const char *t;
/* Tilde expansion */ /* Tilde expansion */
path_tilde = tilde_expand (path); path_tilde = tilde_expand (path);
@ -107,7 +108,8 @@ examine_cd (char *path)
/* CDPATH handling */ /* CDPATH handling */
if (*q != PATH_SEP && !result) { if (*q != PATH_SEP && !result) {
p = getenv ("CDPATH"); char * const cdpath = g_strdup (getenv ("CDPATH"));
char *p = cdpath;
if (p == NULL) if (p == NULL)
c = 0; c = 0;
else else
@ -126,6 +128,7 @@ examine_cd (char *path)
*s = c; *s = c;
p = s + 1; p = s + 1;
} }
g_free (cdpath);
} }
g_free (q); g_free (q);
g_free (path_tilde); g_free (path_tilde);

View File

@ -64,7 +64,7 @@ filename_completion_function (char *text, int state)
/* If we're starting the match process, initialize us a bit. */ /* If we're starting the match process, initialize us a bit. */
if (!state){ if (!state){
char *temp; const char *temp;
g_free (dirname); g_free (dirname);
g_free (filename); g_free (filename);
@ -229,7 +229,7 @@ variable_completion_function (char *text, int state)
{ {
static char **env_p; static char **env_p;
static int varlen, isbrace; static int varlen, isbrace;
char *p = 0; const char *p = NULL;
if (!state){ /* Initialization stuff */ if (!state){ /* Initialization stuff */
isbrace = (text [1] == '{'); isbrace = (text [1] == '{');
@ -396,7 +396,7 @@ hostname_completion_function (char *text, int state)
static char * static char *
command_completion_function (char *text, int state) command_completion_function (char *text, int state)
{ {
static char *path_end; static const char *path_end;
static int isabsolute; static int isabsolute;
static int phase; static int phase;
static int text_len; static int text_len;
@ -418,7 +418,7 @@ command_completion_function (char *text, int state)
"shift", "source", "suspend", "test", "times", "trap", "type", "shift", "source", "suspend", "test", "times", "trap", "type",
"typeset", "ulimit", "umask", "unalias", "unset", "wait", 0 "typeset", "ulimit", "umask", "unalias", "unset", "wait", 0
}; };
char *p, *found; char *found;
if (!state) { /* Initialize us a little bit */ if (!state) { /* Initialize us a little bit */
isabsolute = strchr (text, PATH_SEP) != 0; isabsolute = strchr (text, PATH_SEP) != 0;
@ -427,10 +427,10 @@ command_completion_function (char *text, int state)
words = bash_reserved; words = bash_reserved;
phase = 0; phase = 0;
text_len = strlen (text); text_len = strlen (text);
if (!path && (path = getenv ("PATH")) != NULL) { if (!path && (path = g_strdup (getenv ("PATH"))) != NULL) {
p = path = g_strdup (path); char *p = path;
path_end = strchr (p, 0); path_end = strchr (p, 0);
while ((p = strchr (p, PATH_ENV_SEP))) { while ((p = (char *) strchr (p, PATH_ENV_SEP))) {
*p++ = 0; *p++ = 0;
} }
} }
@ -438,7 +438,7 @@ command_completion_function (char *text, int state)
} }
if (isabsolute) { if (isabsolute) {
p = filename_completion_function (text, state); char *p = filename_completion_function (text, state);
if (!p) if (!p)
look_for_executables = 0; look_for_executables = 0;
return p; return p;
@ -476,7 +476,7 @@ command_completion_function (char *text, int state)
cur_word = concat_dir_and_file (expanded, text); cur_word = concat_dir_and_file (expanded, text);
g_free (expanded); g_free (expanded);
canonicalize_pathname (cur_word); canonicalize_pathname (cur_word);
cur_path = strchr (cur_path, 0) + 1; cur_path = (char *) strchr (cur_path, 0) + 1;
init_state = state; init_state = state;
} }
found = found =
@ -495,12 +495,14 @@ command_completion_function (char *text, int state)
path = NULL; path = NULL;
return NULL; return NULL;
} }
if ((p = strrchr (found, PATH_SEP)) != NULL) { { char *p;
if ((p = (char *) strrchr (found, PATH_SEP)) != NULL) {
p++; p++;
p = g_strdup (p); p = g_strdup (p);
g_free (found); g_free (found);
return p; return p;
} }
}
return found; return found;
} }
@ -665,11 +667,11 @@ try_complete (char *text, int *start, int *end, int flags)
} }
if (flags & INPUT_COMPLETE_COMMANDS) if (flags & INPUT_COMPLETE_COMMANDS)
p = strrchr (word, '`'); p = (char *) strrchr (word, '`');
if (flags & (INPUT_COMPLETE_COMMANDS | INPUT_COMPLETE_VARIABLES)) if (flags & (INPUT_COMPLETE_COMMANDS | INPUT_COMPLETE_VARIABLES))
q = strrchr (word, '$'); q = (char *) strrchr (word, '$');
if (flags & INPUT_COMPLETE_HOSTNAMES) if (flags & INPUT_COMPLETE_HOSTNAMES)
r = strrchr (word, '@'); r = (char *) strrchr (word, '@');
if (q && q [1] == '(' && INPUT_COMPLETE_COMMANDS){ if (q && q [1] == '(' && INPUT_COMPLETE_COMMANDS){
if (q > p) if (q > p)
p = q + 1; p = q + 1;
@ -722,7 +724,8 @@ try_complete (char *text, int *start, int *end, int flags)
if (!strncmp (p, "cd", 2)) if (!strncmp (p, "cd", 2))
for (p += 2; *p && p < q && (*p == ' ' || *p == '\t'); p++); for (p += 2; *p && p < q && (*p == ' ' || *p == '\t'); p++);
if (p == q){ if (p == q){
char *cdpath = getenv ("CDPATH"); char * const cdpath_ref = g_strdup (getenv ("CDPATH"));
char *cdpath = cdpath_ref;
char c, *s, *r; char c, *s, *r;
if (cdpath == NULL) if (cdpath == NULL)
@ -730,9 +733,9 @@ try_complete (char *text, int *start, int *end, int flags)
else else
c = ':'; c = ':';
while (!matches && c == ':'){ while (!matches && c == ':'){
s = strchr (cdpath, ':'); s = (char *) strchr (cdpath, ':');
if (s == NULL) if (s == NULL)
s = strchr (cdpath, 0); s = (char *) strchr (cdpath, 0);
c = *s; c = *s;
*s = 0; *s = 0;
if (*cdpath){ if (*cdpath){
@ -745,6 +748,7 @@ try_complete (char *text, int *start, int *end, int flags)
*s = c; *s = c;
cdpath = s + 1; cdpath = s + 1;
} }
g_free (cdpath_ref);
} }
} }
} }