blob: fafb861982cff31c8446ede006b5a20b6c3003e5 [file] [log] [blame]
Jari Aaltob72432f1999-02-19 17:11:39 +00001/* shell.c -- tilde utility functions that are normally provided by
2 bash when readline is linked as part of the shell. */
3
Jari Aalto31859422009-01-12 13:36:28 +00004/* Copyright (C) 1998-2009 Free Software Foundation, Inc.
Jari Aaltob72432f1999-02-19 17:11:39 +00005
6 This file is part of the GNU Tilde Library.
7
Jari Aalto31859422009-01-12 13:36:28 +00008 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 Aaltob72432f1999-02-19 17:11:39 +000011 (at your option) any later version.
12
Jari Aalto31859422009-01-12 13:36:28 +000013 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 Aaltob72432f1999-02-19 17:11:39 +000016 GNU General Public License for more details.
17
Jari Aalto31859422009-01-12 13:36:28 +000018 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 Aaltob72432f1999-02-19 17:11:39 +000021
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)
48extern struct passwd *getpwuid ();
49#endif /* !HAVE_GETPW_DECLS */
50
51char *
52get_env_value (varname)
53 char *varname;
54{
55 return ((char *)getenv (varname));
56}
57
Chet Rameyac50fba2014-02-26 09:36:43 -050058/* If we're not using $HOME, assume that the passwd file information won't
59 change while this shell instance is running. */
Jari Aaltob72432f1999-02-19 17:11:39 +000060char *
61get_home_dir ()
62{
Chet Rameyac50fba2014-02-26 09:36:43 -050063 static char *home_dir = (char *)NULL;
Jari Aaltob72432f1999-02-19 17:11:39 +000064 struct passwd *entry;
65
Chet Rameyac50fba2014-02-26 09:36:43 -050066 if (home_dir)
67 return (home_dir);
68
69#if defined (HAVE_GETPWUID)
Jari Aaltob72432f1999-02-19 17:11:39 +000070 entry = getpwuid (getuid ());
71 if (entry)
Chet Rameyac50fba2014-02-26 09:36:43 -050072 home_dir = savestring (entry->pw_dir);
73#endif
74
75#if defined (HAVE_GETPWENT)
76 endpwent (); /* some systems need this */
77#endif
78
Jari Aaltob72432f1999-02-19 17:11:39 +000079 return (home_dir);
80}