1997-01-04 22:09:20 +00:00
|
|
|
|
/* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
|
|
|
|
|
|
|
|
|
|
/* Copyright (C) 1988,1989 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
This file is part of GNU Readline, a library for reading lines
|
|
|
|
|
of text with interactive input and history editing.
|
|
|
|
|
|
|
|
|
|
Readline is free software; you can redistribute it and/or modify it
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
2000-03-19 21:18:09 +00:00
|
|
|
|
Free Software Foundation; either version 2, or (at your option) any
|
1997-01-04 22:09:20 +00:00
|
|
|
|
later version.
|
|
|
|
|
|
|
|
|
|
Readline is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with Readline; see the file COPYING. If not, write to the Free
|
2000-03-19 21:18:09 +00:00
|
|
|
|
Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
#if defined (HAVE_CONFIG_H)
|
|
|
|
|
# include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
#if defined (HAVE_UNISTD_H)
|
1998-04-28 06:22:20 +00:00
|
|
|
|
# ifdef _MINIX
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
# endif
|
1997-06-07 12:17:44 +00:00
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
|
#if defined (HAVE_STRING_H)
|
|
|
|
|
# include <string.h>
|
|
|
|
|
#else /* !HAVE_STRING_H */
|
|
|
|
|
# include <strings.h>
|
|
|
|
|
#endif /* !HAVE_STRING_H */
|
|
|
|
|
|
|
|
|
|
#if defined (HAVE_STDLIB_H)
|
|
|
|
|
# include <stdlib.h>
|
|
|
|
|
#else
|
|
|
|
|
# include "ansi_stdlib.h"
|
|
|
|
|
#endif /* HAVE_STDLIB_H */
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#if defined (HAVE_PWD_H)
|
1997-01-04 22:09:20 +00:00
|
|
|
|
#include <pwd.h>
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#endif
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
#include "tilde.h"
|
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#if defined (TEST) || defined (STATIC_MALLOC)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
static void *xmalloc (), *xrealloc ();
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#else
|
2003-08-31 17:55:46 +00:00
|
|
|
|
# include "xmalloc.h"
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#endif /* TEST || STATIC_MALLOC */
|
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
|
#if !defined (HAVE_GETPW_DECLS)
|
2006-03-27 22:47:12 +00:00
|
|
|
|
# if defined (HAVE_GETPWUID)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
extern struct passwd *getpwuid PARAMS((uid_t));
|
2006-03-27 22:47:12 +00:00
|
|
|
|
# endif
|
|
|
|
|
# if defined (HAVE_GETPWNAM)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
extern struct passwd *getpwnam PARAMS((const char *));
|
2006-03-27 22:47:12 +00:00
|
|
|
|
# endif
|
1997-01-04 22:09:20 +00:00
|
|
|
|
#endif /* !HAVE_GETPW_DECLS */
|
|
|
|
|
|
|
|
|
|
#if !defined (savestring)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
#define savestring(x) strcpy ((char *)xmalloc (1 + strlen (x)), (x))
|
1997-01-04 22:09:20 +00:00
|
|
|
|
#endif /* !savestring */
|
|
|
|
|
|
|
|
|
|
#if !defined (NULL)
|
|
|
|
|
# if defined (__STDC__)
|
|
|
|
|
# define NULL ((void *) 0)
|
|
|
|
|
# else
|
|
|
|
|
# define NULL 0x0
|
|
|
|
|
# endif /* !__STDC__ */
|
|
|
|
|
#endif /* !NULL */
|
|
|
|
|
|
1999-05-28 01:59:23 +00:00
|
|
|
|
/* If being compiled as part of bash, these will be satisfied from
|
|
|
|
|
variables.o. If being compiled as part of readline, they will
|
|
|
|
|
be satisfied from shell.o. */
|
2003-08-31 17:55:46 +00:00
|
|
|
|
extern char *sh_get_home_dir PARAMS((void));
|
|
|
|
|
extern char *sh_get_env_value PARAMS((const char *));
|
1999-05-28 01:59:23 +00:00
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
|
/* The default value of tilde_additional_prefixes. This is set to
|
|
|
|
|
whitespace preceding a tilde so that simple programs which do not
|
|
|
|
|
perform any word separation get desired behaviour. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
static const char *default_prefixes[] =
|
|
|
|
|
{ " ~", "\t~", (const char *)NULL };
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
/* The default value of tilde_additional_suffixes. This is set to
|
|
|
|
|
whitespace or newline so that simple programs which do not
|
|
|
|
|
perform any word separation get desired behaviour. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
static const char *default_suffixes[] =
|
|
|
|
|
{ " ", "\n", (const char *)NULL };
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
/* If non-null, this contains the address of a function that the application
|
|
|
|
|
wants called before trying the standard tilde expansions. The function
|
|
|
|
|
is called with the text sans tilde, and returns a malloc()'ed string
|
|
|
|
|
which is the expansion, or a NULL pointer if the expansion fails. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
tilde_hook_func_t *tilde_expansion_preexpansion_hook = (tilde_hook_func_t *)NULL;
|
1997-06-07 12:17:44 +00:00
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
|
/* If non-null, this contains the address of a function to call if the
|
|
|
|
|
standard meaning for expanding a tilde fails. The function is called
|
|
|
|
|
with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
|
|
|
|
|
which is the expansion, or a NULL pointer if there is no expansion. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
tilde_hook_func_t *tilde_expansion_failure_hook = (tilde_hook_func_t *)NULL;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
/* When non-null, this is a NULL terminated array of strings which
|
|
|
|
|
are duplicates for a tilde prefix. Bash uses this to expand
|
|
|
|
|
`=~' and `:~'. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
char **tilde_additional_prefixes = (char **)default_prefixes;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
/* When non-null, this is a NULL terminated array of strings which match
|
|
|
|
|
the end of a username, instead of just "/". Bash sets this to
|
|
|
|
|
`:' and `=~'. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
char **tilde_additional_suffixes = (char **)default_suffixes;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
2003-08-31 17:55:46 +00:00
|
|
|
|
static int tilde_find_prefix PARAMS((const char *, int *));
|
|
|
|
|
static int tilde_find_suffix PARAMS((const char *));
|
|
|
|
|
static char *isolate_tilde_prefix PARAMS((const char *, int *));
|
|
|
|
|
static char *glue_prefix_and_suffix PARAMS((char *, const char *, int));
|
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
|
/* Find the start of a tilde expansion in STRING, and return the index of
|
|
|
|
|
the tilde which starts the expansion. Place the length of the text
|
|
|
|
|
which identified this tilde starter in LEN, excluding the tilde itself. */
|
|
|
|
|
static int
|
|
|
|
|
tilde_find_prefix (string, len)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
const char *string;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
int *len;
|
|
|
|
|
{
|
|
|
|
|
register int i, j, string_len;
|
2000-03-19 21:18:09 +00:00
|
|
|
|
register char **prefixes;
|
|
|
|
|
|
|
|
|
|
prefixes = tilde_additional_prefixes;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
string_len = strlen (string);
|
|
|
|
|
*len = 0;
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (*string == '\0' || *string == '~')
|
1997-01-04 22:09:20 +00:00
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
|
|
if (prefixes)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < string_len; i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; prefixes[j]; j++)
|
|
|
|
|
{
|
|
|
|
|
if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
|
|
|
|
|
{
|
|
|
|
|
*len = strlen (prefixes[j]) - 1;
|
|
|
|
|
return (i + *len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (string_len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find the end of a tilde expansion in STRING, and return the index of
|
|
|
|
|
the character which ends the tilde definition. */
|
|
|
|
|
static int
|
|
|
|
|
tilde_find_suffix (string)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
const char *string;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
|
|
|
|
register int i, j, string_len;
|
1997-06-07 12:17:44 +00:00
|
|
|
|
register char **suffixes;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
suffixes = tilde_additional_suffixes;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
string_len = strlen (string);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < string_len; i++)
|
|
|
|
|
{
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#if defined (__MSDOS__)
|
|
|
|
|
if (string[i] == '/' || string[i] == '\\' /* || !string[i] */)
|
|
|
|
|
#else
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (string[i] == '/' /* || !string[i] */)
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#endif
|
1997-01-04 22:09:20 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (j = 0; suffixes && suffixes[j]; j++)
|
|
|
|
|
{
|
|
|
|
|
if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
|
|
|
|
|
return (i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return a new string which is the result of tilde expanding STRING. */
|
|
|
|
|
char *
|
|
|
|
|
tilde_expand (string)
|
2001-04-11 02:33:07 +00:00
|
|
|
|
const char *string;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
1997-06-07 12:17:44 +00:00
|
|
|
|
char *result;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
int result_size, result_index;
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
result_index = result_size = 0;
|
2013-10-29 20:53:09 +00:00
|
|
|
|
if ((result = strchr (string, '~')))
|
2003-08-31 17:55:46 +00:00
|
|
|
|
result = (char *)xmalloc (result_size = (strlen (string) + 16));
|
1997-06-07 12:17:44 +00:00
|
|
|
|
else
|
2003-08-31 17:55:46 +00:00
|
|
|
|
result = (char *)xmalloc (result_size = (strlen (string) + 1));
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
/* Scan through STRING expanding tildes as we come to them. */
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
register int start, end;
|
|
|
|
|
char *tilde_word, *expansion;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
/* Make START point to the tilde which starts the expansion. */
|
|
|
|
|
start = tilde_find_prefix (string, &len);
|
|
|
|
|
|
|
|
|
|
/* Copy the skipped text into the result. */
|
|
|
|
|
if ((result_index + start + 1) > result_size)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
strncpy (result + result_index, string, start);
|
|
|
|
|
result_index += start;
|
|
|
|
|
|
|
|
|
|
/* Advance STRING to the starting tilde. */
|
|
|
|
|
string += start;
|
|
|
|
|
|
|
|
|
|
/* Make END be the index of one after the last character of the
|
|
|
|
|
username. */
|
|
|
|
|
end = tilde_find_suffix (string);
|
|
|
|
|
|
|
|
|
|
/* If both START and END are zero, we are all done. */
|
|
|
|
|
if (!start && !end)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Expand the entire tilde word, and copy it into RESULT. */
|
2003-08-31 17:55:46 +00:00
|
|
|
|
tilde_word = (char *)xmalloc (1 + end);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
strncpy (tilde_word, string, end);
|
|
|
|
|
tilde_word[end] = '\0';
|
|
|
|
|
string += end;
|
|
|
|
|
|
|
|
|
|
expansion = tilde_expand_word (tilde_word);
|
|
|
|
|
free (tilde_word);
|
|
|
|
|
|
|
|
|
|
len = strlen (expansion);
|
2001-04-11 02:33:07 +00:00
|
|
|
|
#ifdef __CYGWIN__
|
2000-03-19 21:18:09 +00:00
|
|
|
|
/* Fix for Cygwin to prevent ~user/xxx from expanding to //xxx when
|
2001-04-11 02:33:07 +00:00
|
|
|
|
$HOME for `user' is /. On cygwin, // denotes a network drive. */
|
2000-03-19 21:18:09 +00:00
|
|
|
|
if (len > 1 || *expansion != '/' || *string != '/')
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
if ((result_index + len + 1) > result_size)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
|
strcpy (result + result_index, expansion);
|
|
|
|
|
result_index += len;
|
|
|
|
|
}
|
1997-01-04 22:09:20 +00:00
|
|
|
|
free (expansion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result[result_index] = '\0';
|
|
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
|
}
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
/* Take FNAME and return the tilde prefix we want expanded. If LENP is
|
|
|
|
|
non-null, the index of the end of the prefix into FNAME is returned in
|
|
|
|
|
the location it points to. */
|
|
|
|
|
static char *
|
|
|
|
|
isolate_tilde_prefix (fname, lenp)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
const char *fname;
|
1997-06-07 12:17:44 +00:00
|
|
|
|
int *lenp;
|
|
|
|
|
{
|
|
|
|
|
char *ret;
|
|
|
|
|
int i;
|
|
|
|
|
|
2003-08-31 17:55:46 +00:00
|
|
|
|
ret = (char *)xmalloc (strlen (fname));
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#if defined (__MSDOS__)
|
|
|
|
|
for (i = 1; fname[i] && fname[i] != '/' && fname[i] != '\\'; i++)
|
|
|
|
|
#else
|
1997-06-07 12:17:44 +00:00
|
|
|
|
for (i = 1; fname[i] && fname[i] != '/'; i++)
|
2000-03-19 21:18:09 +00:00
|
|
|
|
#endif
|
1997-06-07 12:17:44 +00:00
|
|
|
|
ret[i - 1] = fname[i];
|
|
|
|
|
ret[i - 1] = '\0';
|
|
|
|
|
if (lenp)
|
|
|
|
|
*lenp = i;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/* Public function to scan a string (FNAME) beginning with a tilde and find
|
|
|
|
|
the portion of the string that should be passed to the tilde expansion
|
|
|
|
|
function. Right now, it just calls tilde_find_suffix and allocates new
|
|
|
|
|
memory, but it can be expanded to do different things later. */
|
|
|
|
|
char *
|
|
|
|
|
tilde_find_word (fname, flags, lenp)
|
|
|
|
|
const char *fname;
|
|
|
|
|
int flags, *lenp;
|
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
char *r;
|
|
|
|
|
|
|
|
|
|
x = tilde_find_suffix (fname);
|
|
|
|
|
if (x == 0)
|
|
|
|
|
{
|
|
|
|
|
r = savestring (fname);
|
|
|
|
|
if (lenp)
|
|
|
|
|
*lenp = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
r = (char *)xmalloc (1 + x);
|
|
|
|
|
strncpy (r, fname, x);
|
|
|
|
|
r[x] = '\0';
|
|
|
|
|
if (lenp)
|
|
|
|
|
*lenp = x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
/* Return a string that is PREFIX concatenated with SUFFIX starting at
|
|
|
|
|
SUFFIND. */
|
|
|
|
|
static char *
|
|
|
|
|
glue_prefix_and_suffix (prefix, suffix, suffind)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
char *prefix;
|
|
|
|
|
const char *suffix;
|
1997-06-07 12:17:44 +00:00
|
|
|
|
int suffind;
|
|
|
|
|
{
|
|
|
|
|
char *ret;
|
|
|
|
|
int plen, slen;
|
|
|
|
|
|
|
|
|
|
plen = (prefix && *prefix) ? strlen (prefix) : 0;
|
|
|
|
|
slen = strlen (suffix + suffind);
|
2003-08-31 17:55:46 +00:00
|
|
|
|
ret = (char *)xmalloc (plen + slen + 1);
|
2000-03-19 21:18:09 +00:00
|
|
|
|
if (plen)
|
1997-06-07 12:17:44 +00:00
|
|
|
|
strcpy (ret, prefix);
|
|
|
|
|
strcpy (ret + plen, suffix + suffind);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
|
/* Do the work of tilde expansion on FILENAME. FILENAME starts with a
|
1997-06-07 12:17:44 +00:00
|
|
|
|
tilde. If there is no expansion, call tilde_expansion_failure_hook.
|
|
|
|
|
This always returns a newly-allocated string, never static storage. */
|
1997-01-04 22:09:20 +00:00
|
|
|
|
char *
|
|
|
|
|
tilde_expand_word (filename)
|
2001-04-11 02:33:07 +00:00
|
|
|
|
const char *filename;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
1997-06-07 12:17:44 +00:00
|
|
|
|
char *dirname, *expansion, *username;
|
|
|
|
|
int user_len;
|
|
|
|
|
struct passwd *user_entry;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (filename == 0)
|
1997-01-04 22:09:20 +00:00
|
|
|
|
return ((char *)NULL);
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (*filename != '~')
|
|
|
|
|
return (savestring (filename));
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
/* A leading `~/' or a bare `~' is *always* translated to the value of
|
|
|
|
|
$HOME or the home directory of the current user, regardless of any
|
|
|
|
|
preexpansion hook. */
|
|
|
|
|
if (filename[1] == '\0' || filename[1] == '/')
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
1997-06-07 12:17:44 +00:00
|
|
|
|
/* Prefix $HOME to the rest of the string. */
|
2001-04-11 02:33:07 +00:00
|
|
|
|
expansion = sh_get_env_value ("HOME");
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
/* If there is no HOME variable, look up the directory in
|
|
|
|
|
the password database. */
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (expansion == 0)
|
2001-04-11 02:33:07 +00:00
|
|
|
|
expansion = sh_get_home_dir ();
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
return (glue_prefix_and_suffix (expansion, filename, 1));
|
1997-01-04 22:09:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
username = isolate_tilde_prefix (filename, &user_len);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (tilde_expansion_preexpansion_hook)
|
|
|
|
|
{
|
|
|
|
|
expansion = (*tilde_expansion_preexpansion_hook) (username);
|
|
|
|
|
if (expansion)
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
1997-06-07 12:17:44 +00:00
|
|
|
|
dirname = glue_prefix_and_suffix (expansion, filename, user_len);
|
|
|
|
|
free (username);
|
|
|
|
|
free (expansion);
|
|
|
|
|
return (dirname);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
}
|
1997-06-07 12:17:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* No preexpansion hook, or the preexpansion hook failed. Look in the
|
|
|
|
|
password database. */
|
|
|
|
|
dirname = (char *)NULL;
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#if defined (HAVE_GETPWNAM)
|
1997-06-07 12:17:44 +00:00
|
|
|
|
user_entry = getpwnam (username);
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#else
|
|
|
|
|
user_entry = 0;
|
|
|
|
|
#endif
|
1997-06-07 12:17:44 +00:00
|
|
|
|
if (user_entry == 0)
|
|
|
|
|
{
|
|
|
|
|
/* If the calling program has a special syntax for expanding tildes,
|
|
|
|
|
and we couldn't find a standard expansion, then let them try. */
|
|
|
|
|
if (tilde_expansion_failure_hook)
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
1997-06-07 12:17:44 +00:00
|
|
|
|
expansion = (*tilde_expansion_failure_hook) (username);
|
|
|
|
|
if (expansion)
|
|
|
|
|
{
|
|
|
|
|
dirname = glue_prefix_and_suffix (expansion, filename, user_len);
|
|
|
|
|
free (expansion);
|
|
|
|
|
}
|
1997-01-04 22:09:20 +00:00
|
|
|
|
}
|
1997-06-07 12:17:44 +00:00
|
|
|
|
/* If we don't have a failure hook, or if the failure hook did not
|
|
|
|
|
expand the tilde, return a copy of what we were passed. */
|
|
|
|
|
if (dirname == 0)
|
|
|
|
|
dirname = savestring (filename);
|
|
|
|
|
}
|
2006-12-31 09:06:30 +00:00
|
|
|
|
#if defined (HAVE_GETPWENT)
|
1997-06-07 12:17:44 +00:00
|
|
|
|
else
|
2006-12-31 09:06:30 +00:00
|
|
|
|
dirname = glue_prefix_and_suffix (user_entry->pw_dir, filename, user_len);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
free (username);
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#if defined (HAVE_GETPWENT)
|
1997-06-07 12:17:44 +00:00
|
|
|
|
endpwent ();
|
2006-03-27 22:47:12 +00:00
|
|
|
|
#endif
|
1997-01-04 22:09:20 +00:00
|
|
|
|
return (dirname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined (TEST)
|
|
|
|
|
#undef NULL
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
main (argc, argv)
|
|
|
|
|
int argc;
|
|
|
|
|
char **argv;
|
|
|
|
|
{
|
|
|
|
|
char *result, line[512];
|
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
|
{
|
|
|
|
|
printf ("~expand: ");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
|
|
if (!gets (line))
|
|
|
|
|
strcpy (line, "done");
|
|
|
|
|
|
|
|
|
|
if ((strcmp (line, "done") == 0) ||
|
|
|
|
|
(strcmp (line, "quit") == 0) ||
|
|
|
|
|
(strcmp (line, "exit") == 0))
|
|
|
|
|
{
|
|
|
|
|
done = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = tilde_expand (line);
|
|
|
|
|
printf (" --> %s\n", result);
|
|
|
|
|
free (result);
|
|
|
|
|
}
|
|
|
|
|
exit (0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void memory_error_and_abort ();
|
|
|
|
|
|
2003-08-31 17:55:46 +00:00
|
|
|
|
static void *
|
1997-01-04 22:09:20 +00:00
|
|
|
|
xmalloc (bytes)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
size_t bytes;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
{
|
2003-08-31 17:55:46 +00:00
|
|
|
|
void *temp = (char *)malloc (bytes);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
if (!temp)
|
|
|
|
|
memory_error_and_abort ();
|
|
|
|
|
return (temp);
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-31 17:55:46 +00:00
|
|
|
|
static void *
|
1997-01-04 22:09:20 +00:00
|
|
|
|
xrealloc (pointer, bytes)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
void *pointer;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
int bytes;
|
|
|
|
|
{
|
2003-08-31 17:55:46 +00:00
|
|
|
|
void *temp;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
if (!pointer)
|
2003-08-31 17:55:46 +00:00
|
|
|
|
temp = malloc (bytes);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
else
|
2003-08-31 17:55:46 +00:00
|
|
|
|
temp = realloc (pointer, bytes);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
|
|
if (!temp)
|
|
|
|
|
memory_error_and_abort ();
|
|
|
|
|
|
|
|
|
|
return (temp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
memory_error_and_abort ()
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "readline: out of virtual memory\n");
|
|
|
|
|
abort ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* compile-command: "gcc -g -DTEST -o tilde tilde.c"
|
|
|
|
|
* end:
|
|
|
|
|
*/
|
|
|
|
|
#endif /* TEST */
|