Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // realpath.c: display the absolute path to a file or directory.
- // Adam Liss, August, 2007
- // This program is provided "as-is" to the public domain, without express or
- // implied warranty, for any non-profit use, provided this notice is maintained.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <libgen.h>
- #include <limits.h>
- static char *s_pMyName;
- void usage(void);
- int main(int argc, char *argv[])
- {
- char
- sPath[PATH_MAX];
- s_pMyName = strdup(basename(argv[0]));
- if (argc < 2)
- usage();
- printf("%sn", realpath(argv[1], sPath));
- return 0;
- }
- void usage(void)
- {
- fprintf(stderr, "usage: %s PATHn", s_pMyName);
- exit(1);
- }
- $ dirname /foo/bar/baz
- /foo/bar
- $ basename /foo/bar/baz
- baz
- $ dirname $( dirname /foo/bar/baz ))
- /foo
- $ realpath ../foo
- ../foo: No such file or directory
- $ realpath /tmp/../tmp/../tmp
- /tmp
- readlink -f /path/here/..
- readlink -m /path/there/../../
- realpath -s /path/here/../../
- normalDir=`cd "${dirToNormalize}";pwd`
- echo ${normalDir}
- MY_PATH=$(readlink -f "$0")
- get_abs_path() {
- local PARENT_DIR=$(dirname "$1")
- cd "$PARENT_DIR"
- local ABS_PATH="$(pwd)"/"$(basename $1)"
- cd - >/dev/null
- echo $ABS_PATH
- }
- python -c "import os,sys; print os.path.realpath(sys.argv[1])" /foo/bar/..
- test -x /usr/bin/readlink || readlink () {
- echo $(/bin/ls -l $1 | /bin/cut -d'>' -f 2)
- }
- test -x /usr/bin/realpath || realpath () {
- local PATH=/bin:/usr/bin
- local inputpath=$1
- local changemade=1
- while [ $changemade -ne 0 ]
- do
- changemade=0
- local realpath=""
- local token=
- for token in ${inputpath//// }
- do
- case $token in
- ""|".") # noop
- ;;
- "..") # up one directory
- changemade=1
- realpath=$(dirname $realpath)
- ;;
- *)
- if [ -h $realpath/$token ]
- then
- changemade=1
- target=`readlink $realpath/$token`
- if [ "${target:0:1}" = '/' ]
- then
- realpath=$target
- else
- realpath="$realpath/$target"
- fi
- else
- realpath="$realpath/$token"
- fi
- ;;
- esac
- done
- inputpath=$realpath
- done
- echo $realpath
- }
- mkdir -p /tmp/bar
- (cd /tmp ; ln -s /tmp/bar foo; ln -s ../.././usr /tmp/bar/link2usr)
- echo `realpath /tmp/foo`
- pushd foo/bar/..
- dir=`pwd`
- popd
Add Comment
Please, Sign In to add comment