Tama Communications Corporation
Top | Profile | Publication | Contents | Free Software | Contact

=> Top > Free Software > PathConvert  [Japanese]

o File path name conversion tool

This project is to develop utilities which make conversion between absolute path name and relative path name.

PathConvert.tar.gz -  download

* What's New

Removed bash script 'abs2rel' because it is not maintained well.
Added LGPL version of C library.
Mac OS X seems to use rel2abs.
Replaced Makefile with more generic one and fixed README.
A bug in rel2abs() was fixed.
Abs2rel() function was adopted by GNU GLOBAL source code tag system.
File::PathConvert module was merged into File::Spec was included by Perl developping version (5.005_55).
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(<target>, <base directory>, <result buffer>, <buffer length>)

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(<target>, <base directory>, <result buffer>, <buffer length>)

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
% 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 shigio  wheel  26 Dec 29 14:18 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/shigio
% ln -sfr /etc/hosts .
% ls -l hosts
lrwxr-xr-x  1 shigio  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/shigio/tmp			# relative
% ls -l /home/shigio/tmp/tty.c
lrwxrwxrwx  1 shigio  users  31 Jul 13 13:28 /home/shigio/tmp/tty.c ->
% ln -sfa kern/tty.c /home/shigio/tmp			# absolute
% ls -l /home/shigio/tmp/tty.c
lrwxrwxrwx  1 shigio  users  31 Jul 13 13:28 /home/shigio/tmp/tty.c ->
% ln -sf kern/tty.c /home/shigio/tmp			# no specification
% ls -l /home/shigio/tmp/tty.c
lrwxrwxrwx  1 shigio  users  10 Jul 13 18:08 /home/shigio/tmp/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/shigio/tmp
% ls -l /home/shigio/tmp/noexist.c 
lrwxrwxrwx  1 shigio  users  36 Jul 13 17:53 /home/shigio/tmp/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