Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-24-2004, 11:52 AM
|
#1
|
LQ Newbie
Registered: Oct 2004
Posts: 14
Rep:
|
Absolute path
Hi! All:
Is there a system funtion to convert a relative path to an absolute path?
For example: If my current working directory is: "/home/abc/xyz"
when I pass: "../", it will return me: "/home/abc"
Thanks!
|
|
|
10-24-2004, 12:09 PM
|
#2
|
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938
Rep:
|
The environment variable $PWD will return the absolute path to your currect working directory...other than that nothing springs to mind. Perhaps if you told us what you want to do with it?
|
|
|
10-24-2004, 12:12 PM
|
#3
|
Member
Registered: Jun 2004
Location: unknown place in NYC
Distribution: Ubuntu
Posts: 377
Rep:
|
that easy just type "cd" my it self it will return to what ever you home dir
i think that's what u were trying to say
|
|
|
10-24-2004, 12:17 PM
|
#4
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
Using BASH, you can just do The backticks (`) enclose an external command; the results of which (on standard-output) are used in place of the string. So `pwd` will always give you the current working directory (that's what the pwd command does). The relative path can just be appended to it.
In C, you can do something similar:
Code:
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void getdirname(char * relpath) {
char * abspath = getcwd(NULL,0);
char * buffer = (char*)malloc(strlen(abspath)+strlen(relpath)+1);
sprintf(buffer,"%s%s",abspath,relpath);
free(abspath);
return buffer;
}
You should call free() on the return value to deallocate the memory.
I haven't tested that but you get the idea.
Last edited by rjlee; 10-24-2004 at 12:18 PM.
|
|
|
10-24-2004, 12:37 PM
|
#5
|
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938
Rep:
|
Quote:
Originally posted by Paxmaster
that easy just type "cd" my it self it will return to what ever you home dir
i think that's what u were trying to say
|
I don't think that's what he's after...
|
|
|
10-24-2004, 01:03 PM
|
#6
|
LQ Newbie
Registered: Oct 2004
Posts: 14
Original Poster
Rep:
|
Sorry I didn't make it clear, I mean in C programming, not in shell.
Let's say I have two directories:
/aaa/bbb1/ccc1
/aaa/bbb2/ccc2
If I am in ccc1, then ../../bbb2 should return me:
/aaa/bbb2
Or, if I pass: /aaa/bbb1/../bbb2, the function should still return me:
/aaa/bbb2
The purpose is just to display the directory name correctly. I remember in
Java there is something called "canonical path", wonder if C has a similar
thing?
|
|
|
10-02-2007, 03:11 AM
|
#7
|
Member
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42
Rep:
|
Quote:
Originally Posted by qspares
The purpose is just to display the directory name correctly. I remember in
Java there is something called "canonical path", wonder if C has a similar
thing?
|
Probably you're looking for realpath.
Code:
realpath - resolve pathname
SYNOPSIS
#include <stdlib.h>
char *realpath(const char *file_name, char *resolved_name);
|
|
|
All times are GMT -5. The time now is 04:50 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|