New functions for converting path name.
- From: "Shigio Yamaguchi" <shigio tamacom com>
- To: gtk-devel-list gnome org
- Cc: shigio gnu org
- Subject: New functions for converting path name.
- Date: Sat, 27 Apr 2002 12:58:23 +0900
Hello,
I have made two library functions and its test suite for glib.
G_abs2rel(3) make a relative path name from an absolute path.
G_rel2abs(3) make an absolute path name from a relative path.
I think it is useful as part of glib library. What do you think?
The package is here.
http://www.tamacom.com/pathconvert/glibpath.tar.gz
You can build and test this package like this:
## BUILD
% make
cc -I/usr/local/include -c grel2abs.c
cc -I/usr/local/include -c gabs2rel.c
ar -cq libpath.a grel2abs.o gabs2rel.o
%
## TEST
% make test
chmod u+w t
(cd t; make test)
cc -o rel2abs rel2abs.c ../libpath.a
cc -o abs2rel abs2rel.c ../libpath.a
perl ./test.pl
TEST start .......................................................... COMPLETED.
%
Here is the manual. Thank you in advance.
-------------------------------------------------------------------------------
NAME
g_abs2rel - make a relative path name from an absolute path
SYNOPSIS
char *
g_abs2rel(const char *path, const char *base, char *result, size_t size);
DESCRIPTION
The g_abs2rel() function makes a relative path name from an absolute path
name path based on a directory base and copies the resulting path name
into the memory referenced by result. The result argument must refer to
a buffer capable of storing at least size characters.
The resulting path name may include symbolic links. The g_abs2rel()
function doesn't check whether or not any path exists.
RETURN VALUES
The g_abs2rel() function returns relative path name on success. If an
error occurs, it returns NULL.
ERRORS
The g_abs2rel() function may fail and set the external variable errno to
indicate the error.
[EINVAL] The base directory isn't an absolute path name or the
size argument is zero.
[ERANGE] The size argument is greater than zero but smaller
than the length of the pathname plus 1.
EXAMPLE
char result[MAXPATHLEN];
char *path = g_abs2rel("/usr/src/sys", "/usr/local/lib", result, MAXPATHLEN);
yields:
path == "../../src/sys"
Similarly,
path1 = g_abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN);
path2 = g_abs2rel("/usr/src/sys", "/usr/src/sys",
result, MAXPATHLEN);
yields:
path1 == "src/sys"
path2 == "."
BUGS
If the base directory includes symbolic links, the g_abs2rel() function
produces the wrong path. For example, if '/sys' is a symbolic link to
'/usr/src/sys',
char *path = g_abs2rel("/usr/local/lib", "/sys", result, MAXPATHLEN);
yields:
path == "../usr/local/lib" /* It's wrong!! */
You should convert the base directory into a real path in advance.
path = g_abs2rel("/sys/kern", realpath("/sys", resolvedname), result,
MAXPATHLEN);
yields:
path == "../../../sys/kern" /* It's correct but ... */
That is correct, but a little redundant. If you wish get the simple an-
swer 'kern', do the following.
path = g_abs2rel(realpath("/sys/kern", r1), realpath("/sys", r2),
result, MAXPATHLEN);
The realpath() function assures correct result, but don't forget that
realpath() requires that all but the last component of the path exist.
SEE ALSO
g_rel2abs(3)
AUTHORS
Shigio Yamaguchi (shigio tamacom com)
-------------------------------------------------------------------------------
NAME
g_rel2abs - make an absolute path name from a relative path
SYNOPSIS
char *
g_rel2abs(const char *path, const char *base, char *result, size_t size);
DESCRIPTION
The g_rel2abs() function makes an absolute path name from a relative path
name path based on a directory base and copies the resulting path name
into the memory referenced by result. The result argument must refer to
a buffer capable of storing at least size character
The resulting path name may include symbolic links. g_abs2rel() doesn't
check whether or not any path exists.
RETURN VALUES
The g_rel2abs() function returns absolute path name on success. If an
error occurs, it returns NULL.
ERRORS
The g_rel2abs() function may fail and set the external variable errno to
indicate the error.
[EINVAL] The base directory isn't an absolute path name or the
size argument is zero.
[ERANGE] The size argument is greater than zero but smaller
than the length of the pathname plus 1
EXAMPLE
char result[MAXPATHLEN];
char *path = g_rel2abs("../../src/sys", "/usr/local/lib", result,
MAXPATHLEN);
yields:
path == "/usr/src/sys"
Similarly,
path1 = g_rel2abs("src/sys", "/usr", result, MAXPATHLEN);
path2 = g_rel2abs(".", "/usr/src/sys", result, MAXPATHLEN);
yields:
path1 == "/usr/src/sys"
path2 == "/usr/src/sys"
SEE ALSO
g_abs2rel(3)
AUTHORS
Shigio Yamaguchi (shigio tamacom com)
-------------------------------------------------------------------------------
--
Shigio Yamaguchi - Tama Communications Corporation
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]