LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 10-24-2004, 11:52 AM   #1
qspares
LQ Newbie
 
Registered: Oct 2004
Posts: 14

Rep: Reputation: 0
Question 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!
 
Old 10-24-2004, 12:09 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
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?
 
Old 10-24-2004, 12:12 PM   #3
Paxmaster
Member
 
Registered: Jun 2004
Location: unknown place in NYC
Distribution: Ubuntu
Posts: 377

Rep: Reputation: 30
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
 
Old 10-24-2004, 12:17 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
Using BASH, you can just do
Code:
`pwd`$path
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.
 
Old 10-24-2004, 12:37 PM   #5
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
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...
 
Old 10-24-2004, 01:03 PM   #6
qspares
LQ Newbie
 
Registered: Oct 2004
Posts: 14

Original Poster
Rep: Reputation: 0
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?
 
Old 10-02-2007, 03:11 AM   #7
gloriant
Member
 
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42

Rep: Reputation: 15
Quote:
Originally Posted by qspares View Post
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);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
HTML + href absolute path does not work blizunt7 Programming 3 11-21-2005 04:59 PM
how to get absolute path of current executing C/C++ prog? Thinking Programming 2 10-10-2005 10:27 AM
absolute path to qmail in sme 5.6? dopper Linux - Software 0 10-28-2003 01:48 PM


All times are GMT -5. The time now is 05:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration