i-bash/lib/sh/makepath.c

129 lines
3.2 KiB
C
Raw Normal View History

2000-03-17 21:46:59 +00:00
/* makepath.c - glue PATH and DIR together into a full pathname. */
/* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2000-03-17 21:46:59 +00:00
2009-01-12 13:36:28 +00:00
Bash 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.
2000-03-17 21:46:59 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
2000-03-17 21:46:59 +00:00
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
2001-11-13 17:56:06 +00:00
#include <bashansi.h>
2000-03-17 21:46:59 +00:00
#include "shell.h"
#include <tilde/tilde.h>
#ifndef NULL
# define NULL 0
#endif
/* MAKE SURE THESE AGREE WITH ../../externs.h. */
#ifndef MP_DOTILDE
# define MP_DOTILDE 0x01
# define MP_DOCWD 0x02
# define MP_RMDOT 0x04
2009-01-12 13:36:28 +00:00
# define MP_IGNDOT 0x08
2000-03-17 21:46:59 +00:00
#endif
extern char *get_working_directory __P((char *));
2009-01-12 13:36:28 +00:00
static char *nullpath = "";
2000-03-17 21:46:59 +00:00
/* Take PATH, an element from, e.g., $CDPATH, and DIR, a directory name,
and paste them together into PATH/DIR. Tilde expansion is performed on
PATH if (flags & MP_DOTILDE) is non-zero. If PATH is NULL or the empty
string, it is converted to the current directory. A full pathname is
used if (flags & MP_DOCWD) is non-zero, otherwise `./' is used. If
(flags & MP_RMDOT) is non-zero, any `./' is removed from the beginning
2009-01-12 13:36:28 +00:00
of DIR. If (flags & MP_IGNDOT) is non-zero, a PATH that is "." or "./"
is ignored. */
2000-03-17 21:46:59 +00:00
#define MAKEDOT() \
do { \
2001-11-13 17:56:06 +00:00
xpath = (char *)xmalloc (2); \
2000-03-17 21:46:59 +00:00
xpath[0] = '.'; \
xpath[1] = '\0'; \
pathlen = 1; \
} while (0)
char *
sh_makepath (path, dir, flags)
2001-11-13 17:56:06 +00:00
const char *path, *dir;
2000-03-17 21:46:59 +00:00
int flags;
{
int dirlen, pathlen;
2001-11-13 17:56:06 +00:00
char *ret, *xpath, *xdir, *r, *s;
2000-03-17 21:46:59 +00:00
if (path == 0 || *path == '\0')
{
if (flags & MP_DOCWD)
{
xpath = get_working_directory ("sh_makepath");
if (xpath == 0)
{
ret = get_string_value ("PWD");
if (ret)
xpath = savestring (ret);
}
if (xpath == 0)
MAKEDOT();
else
pathlen = strlen (xpath);
}
else
MAKEDOT();
}
2009-01-12 13:36:28 +00:00
else if ((flags & MP_IGNDOT) && path[0] == '.' && (path[1] == '\0' ||
path[1] == '/' && path[2] == '\0'))
{
xpath = nullpath;
pathlen = 0;
}
2000-03-17 21:46:59 +00:00
else
{
2002-07-17 14:10:11 +00:00
xpath = ((flags & MP_DOTILDE) && *path == '~') ? bash_tilde_expand (path, 0) : (char *)path;
2000-03-17 21:46:59 +00:00
pathlen = strlen (xpath);
}
2001-11-13 17:56:06 +00:00
xdir = (char *)dir;
dirlen = strlen (xdir);
2000-03-17 21:46:59 +00:00
if ((flags & MP_RMDOT) && dir[0] == '.' && dir[1] == '/')
{
2001-11-13 17:56:06 +00:00
xdir += 2;
2000-03-17 21:46:59 +00:00
dirlen -= 2;
}
2001-11-13 17:56:06 +00:00
r = ret = (char *)xmalloc (2 + dirlen + pathlen);
2001-04-06 19:14:31 +00:00
s = xpath;
while (*s)
*r++ = *s++;
2009-01-12 13:36:28 +00:00
if (s > xpath && s[-1] != '/')
2001-04-06 19:14:31 +00:00
*r++ = '/';
2001-11-13 17:56:06 +00:00
s = xdir;
2001-04-06 19:14:31 +00:00
while (*r++ = *s++)
;
2009-01-12 13:36:28 +00:00
if (xpath != path && xpath != nullpath)
2000-03-17 21:46:59 +00:00
free (xpath);
return (ret);
}