This project is to develop utilities which make conversion between absolute path name and relative path name.
Download: PathConvert.tar.gz

What's New

2006/7/11
Removed bash script 'abs2rel' because it is not maintained well.
2005/12/19
Added LGPL version of C library.
2005/8/9
Mac OS X seems to use rel2abs.
2004/8/29
Replaced Makefile with more generic one and fixed README.
2004/2/9
A bug in rel2abs() was fixed.
2002/2/14
Abs2rel() function was adopted by GNU GLOBAL source code tag system.
1999/2/15
File::PathConvert module was merged into File::Spec was included by Perl developping version (5.005_55).
1999/2/3
Added File-PathConvert-0.85 written by Barrie Slaymaker.

Motive

There are two method to specify a file or a directory in UNIX file system. One is 'absolute path' and the other is 'relative path'. We can select suitable method from them but we don't have any standardized measure for converting them each other.

This project intend to make UNIX file system more useful by offering some converting tools between absolute path and relative path.

Result

This project's results are followings:

PathConvert library for C language

Two functions are available.

abs2rel(): Convert absolute path into relative path.

abs2rel(, , , )

char result[MAXPATHLEN];

abs2rel("/usr/src/sys", "/usr/local/lib", result, MAXPATHLEN);
printf("path1 = %s\n", result);
abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN);
printf("path2 = %s\n", result);
abs2rel("/usr/src/sys", "/usr/src/sys", result, MAXPATHLEN);
printf("path3 = %s\n", result);
(Result of above program)
path1 = ../../src/sys
path2 = src/sys
path3 = .
rel2abs(): Convert relative path into absolute path.

rel2abs(, , , )

char result[MAXPATHLEN];

rel2abs("../../src/sys", "/usr/local/lib", result, MAXPATHLEN);
printf("path1 = %s\n", result);
rel2abs("src/sys", "/usr", result, MAXPATHLEN);
printf("path2 = %s\n", result);
rel2abs(".", "/usr/src/sys", result, MAXPATHLEN);
printf("path3 = %s\n", result);
(Result of above program)
path1 = /usr/src/sys
path2 = /usr/src/sys
path3 = /usr/src/sys

Custom lndir command

Lndir duplicates directory tree and makes symbolic links to original files. This command is derived from X11 and slightly modified to accept absolute path name and generate relative symbolic links.

Execution image

% mkdir /tmp/sys_shadow
% lndir /usr/src/sys /tmp/sys_shadow
/usr/src/sys/compile:
/usr/src/sys/conf:
/usr/src/sys/ddb:
/usr/src/sys/dev:
	.
	.
	.
% cd /tmp/sys_shadow
% ls -lL Makefile
-rw-r--r--  1 root  wheel  190 Jun 14  1996 Makefile
% ls -l Makefile
lrwxr-xr-x  1 owner  wheel  26 Dec 29 14:18 Makefile -> ../../usr/src/sys/Makefile

Custom lndir generates relative symbolic links with absolute path argument. Original version generates absolute one. Shadow tree constructed by relative symbolic links is useful becase we can move it easily and we can mount it from other machine remotely.

Custom ln command

Custom ln command is modified version of ln. We added two options, -a(--abosolute) and -r(--relative) to the original ln to make possible to specify the type of symbolic link.

It is convenient in the following case:

  • You want to make relative symbolic link with absolute path specification.
  • You want to make absolute symbolic link with relative path specification.

Ex1: Making relative symbolic link by using absolute path name.

% cd /home/owner
% ln -sfr /etc/hosts .
% ls -l hosts
lrwxr-xr-x  1 owner  users  15 Nov 21 15:36 hosts -> ../../etc/hosts

Ex2: Making symbolic link in other than current directory

% cd /usr/src/sys
% ls -l kern/tty.c
-rw-r--r--  1 root  wheel  56525 Jul  3 09:27 kern/tty.c
% ln -sfr kern/tty.c /home/owner/tmp		# relative
% ls -l /home/owner/tmp/tty.c
lrwxrwxrwx  1 owner  users  31 Jul 13 13:28 /home/owner/tmp/tty.c -> ../../../usr/src/sys/kern/tty.c
% ln -sfa kern/tty.c /home/owner/tmp		# absolute
% ls -l /home/owner/tmp/tty.c
lrwxrwxrwx  1 owner  users  31 Jul 13 13:28 /home/owner/tmp/tty.c -> /usr/src/sys/kern/tty.c
% ln -sf kern/tty.c /home/owner/tmp		# no specification
% ls -l /home/owner/tmp/tty.c
lrwxrwxrwx  1 owner  users  10 Jul 13 18:08 /home/owner/tmp/tty.c -> kern/tty.c

In original ln(1), all done is copy the exact source path string into the link as-is. So, in the last example above, the symbolic link has a incorrect value.

Ex3. Making symbolic link to non existent file.

% cd /usr/src/sys
% ls -l kern/noexist.c
ls: kern/noexist.c: No such file or directory
% ln -sr kern/noexist.c /home/owner/tmp
% ls -l /home/owner/tmp/noexist.c
lrwxrwxrwx  1 owner  users  36 Jul 13 17:53 /home/owner/tmp/noexist.c -> ../../../usr/src/sys/kern/noexist.c
(correct symbolic link though does not exist.)

It is not a bug but a specification to be able to create symbolic link on the file which does not exist. In a job, it is convenient to create only symbolic link before creating the file which is linked by the symbolic link. The implementation of the -a option and the -r option follows the method.

Ex4: create shadow tree using relative symbolic links

% cd /usr/src/sys
% set obj=/usr/obj`pwd`
% mkdir -p $obj
% find * -type d -exec mkdir $obj/'{}' ';'
% find * -type f -exec ln -sr '{}' $obj/'{}' ';'

This code does the same job as lndir above.

File::PathConvert module for perl5

Perl version of abs2rel() and rel2abs().

This module was merged into File::Spec module which is included by perl-5.6. Please see File::Spec module


Copyright 2014, Tama Communications Cooporation.