Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1 | /* shell.c -- tilde utility functions that are normally provided by |
| 2 | bash when readline is linked as part of the shell. */ |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | /* Copyright (C) 1998-2009 Free Software Foundation, Inc. |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of the GNU Tilde Library. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | The GNU Tilde Library is free software: you can redistribute it and/or |
| 9 | modify it under the terms of the GNU General Public License as published |
| 10 | by the Free Software Foundation, either version 3 of the License, or |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | The GNU Tilde Library is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with the GNU Tilde Library. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 21 | |
| 22 | #if defined (HAVE_CONFIG_H) |
| 23 | # include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #if defined (HAVE_UNISTD_H) |
| 27 | # ifdef _MINIX |
| 28 | # include <sys/types.h> |
| 29 | # endif |
| 30 | # include <unistd.h> |
| 31 | #endif /* HAVE_UNISTD_H */ |
| 32 | |
| 33 | #if defined (HAVE_STDLIB_H) |
| 34 | # include <stdlib.h> |
| 35 | #else |
| 36 | # include "ansi_stdlib.h" |
| 37 | #endif /* HAVE_STDLIB_H */ |
| 38 | |
| 39 | #if defined (HAVE_STRING_H) |
| 40 | # include <string.h> |
| 41 | #else |
| 42 | # include <strings.h> |
| 43 | #endif /* !HAVE_STRING_H */ |
| 44 | |
| 45 | #include <pwd.h> |
| 46 | |
| 47 | #if !defined (HAVE_GETPW_DECLS) |
| 48 | extern struct passwd *getpwuid (); |
| 49 | #endif /* !HAVE_GETPW_DECLS */ |
| 50 | |
| 51 | char * |
| 52 | get_env_value (varname) |
| 53 | char *varname; |
| 54 | { |
| 55 | return ((char *)getenv (varname)); |
| 56 | } |
| 57 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 58 | /* If we're not using $HOME, assume that the passwd file information won't |
| 59 | change while this shell instance is running. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 60 | char * |
| 61 | get_home_dir () |
| 62 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 63 | static char *home_dir = (char *)NULL; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 64 | struct passwd *entry; |
| 65 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 66 | if (home_dir) |
| 67 | return (home_dir); |
| 68 | |
| 69 | #if defined (HAVE_GETPWUID) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 70 | entry = getpwuid (getuid ()); |
| 71 | if (entry) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 72 | home_dir = savestring (entry->pw_dir); |
| 73 | #endif |
| 74 | |
| 75 | #if defined (HAVE_GETPWENT) |
| 76 | endpwent (); /* some systems need this */ |
| 77 | #endif |
| 78 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 79 | return (home_dir); |
| 80 | } |