1 /* makepath.c - glue PATH and DIR together into a full pathname. */
3 /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
33 #include <tilde/tilde.h>
39 /* MAKE SURE THESE AGREE WITH ../../externs.h. */
42 # define MP_DOTILDE 0x01
43 # define MP_DOCWD 0x02
44 # define MP_RMDOT 0x04
47 extern char *get_working_directory __P((char *));
49 /* Take PATH, an element from, e.g., $CDPATH, and DIR, a directory name,
50 and paste them together into PATH/DIR. Tilde expansion is performed on
51 PATH if (flags & MP_DOTILDE) is non-zero. If PATH is NULL or the empty
52 string, it is converted to the current directory. A full pathname is
53 used if (flags & MP_DOCWD) is non-zero, otherwise `./' is used. If
54 (flags & MP_RMDOT) is non-zero, any `./' is removed from the beginning
59 xpath = xmalloc (2); \
66 sh_makepath (path, dir, flags)
73 if (path == 0 || *path == '\0')
77 xpath = get_working_directory ("sh_makepath");
80 ret = get_string_value ("PWD");
82 xpath = savestring (ret);
87 pathlen = strlen (xpath);
94 xpath = ((flags & MP_DOTILDE) && *path == '~') ? bash_tilde_expand (path) : path;
95 pathlen = strlen (xpath);
98 dirlen = strlen (dir);
99 if ((flags & MP_RMDOT) && dir[0] == '.' && dir[1] == '/')
105 ret = xmalloc (2 + dirlen + pathlen);
107 if (xpath[pathlen - 1] != '/')
109 ret[pathlen++] = '/';
112 strcpy (ret + pathlen, dir);