git.annexia.org
/
libguestfs.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fish: -i option should fail if / is not mountable, but warn about others.
[libguestfs.git]
/
fish
/
tilde.c
diff --git
a/fish/tilde.c
b/fish/tilde.c
index
64b5b39
..
8ee1437
100644
(file)
--- a/
fish/tilde.c
+++ b/
fish/tilde.c
@@
-13,7
+13,7
@@
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc.,
675 Mass Ave, Cambridge, MA 02139,
USA.
+ * Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA.
*/
#include <config.h>
*/
#include <config.h>
@@
-28,8
+28,9
@@
#include "fish.h"
#include "fish.h"
-static char *expand_home (c
onst char *
);
+static char *expand_home (c
har *orig, const char *append
);
static const char *find_home_for_username (const char *, size_t);
static const char *find_home_for_username (const char *, size_t);
+static const char *find_home_for_current_user (void);
/* This is called from the script loop if we find a candidate for
* ~username (tilde-expansion).
/* This is called from the script loop if we find a candidate for
* ~username (tilde-expansion).
@@
-39,13
+40,11
@@
try_tilde_expansion (char *str)
{
assert (str[0] == '~');
{
assert (str[0] == '~');
- /* Expand current user's home directory. By simple experimentation
- * I found out that bash always uses $HOME.
- */
+ /* Expand "~" to current user's home directory. */
if (str[1] == '\0') /* ~ */
if (str[1] == '\0') /* ~ */
- return expand_home (NULL);
+ return expand_home (
str,
NULL);
else if (str[1] == '/') /* ~/... */
else if (str[1] == '/') /* ~/... */
- return expand_home (&str[1]);
+ return expand_home (
str,
&str[1]);
/* Try expanding the part up to the following '\0' or '/' as a
* username from the password file.
/* Try expanding the part up to the following '\0' or '/' as a
* username from the password file.
@@
-58,11
+57,11
@@
try_tilde_expansion (char *str)
home = find_home_for_username (&str[1], len);
if (home) {
home = find_home_for_username (&str[1], len);
if (home) {
- len = strlen (home) + strlen (rest);
+ len = strlen (home) + strlen (rest)
+ 1
;
str = malloc (len);
if (str == NULL) {
perror ("malloc");
str = malloc (len);
if (str == NULL) {
perror ("malloc");
- exit (
1
);
+ exit (
EXIT_FAILURE
);
}
strcpy (str, home);
strcat (str, rest);
}
strcpy (str, home);
strcat (str, rest);
@@
-76,20
+75,27
@@
try_tilde_expansion (char *str)
/* Return $HOME + append string. */
static char *
/* Return $HOME + append string. */
static char *
-expand_home (const char *append)
+expand_home (c
har *orig, c
onst char *append)
{
const char *home;
int len;
char *str;
home = getenv ("HOME");
{
const char *home;
int len;
char *str;
home = getenv ("HOME");
- if (!home) home = "~";
+ if (!home) {
+ /* $HOME not set, bash can look up the current user in the
+ * password file and find their home that way. (RHBZ#617440).
+ */
+ home = find_home_for_current_user ();
+ if (!home)
+ return orig;
+ }
- len = strlen (home) + (append ? strlen (append) : 0);
+ len = strlen (home) + (append ? strlen (append) : 0)
+ 1
;
str = malloc (len);
if (str == NULL) {
perror ("malloc");
str = malloc (len);
if (str == NULL) {
perror ("malloc");
- exit (
1
);
+ exit (
EXIT_FAILURE
);
}
strcpy (str, home);
}
strcpy (str, home);
@@
-116,3
+122,18
@@
find_home_for_username (const char *username, size_t ulen)
return NULL;
}
return NULL;
}
+
+static const char *
+find_home_for_current_user (void)
+{
+ struct passwd *pw;
+ uid_t euid = geteuid ();
+
+ setpwent ();
+ while ((pw = getpwent ()) != NULL) {
+ if (pw->pw_uid == euid)
+ return pw->pw_dir;
+ }
+
+ return NULL;
+}