The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get path and check in if statement eltinator Shell Programming and Scripting 6 04-08-2008 06:39 PM
$PWD shows absolute path vs path w/symbolic links kornshellmaven Shell Programming and Scripting 3 06-13-2007 09:15 AM
vi - replacing a relative path with absolute path in a file Yinzer955i UNIX for Dummies Questions & Answers 2 09-07-2006 08:47 AM
cc path problem - no acceptable path found kendokendokendo UNIX for Dummies Questions & Answers 2 09-12-2005 06:52 AM
how to check if path is present? svennie UNIX for Dummies Questions & Answers 3 11-02-2004 02:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2006
Registered User
 

Join Date: Jul 2006
Posts: 17
Stumble this Post!
how to check the actual path instead of link path

Hi

I have a path link
/test/df/link1/actual/file1

here link1 is actually a softlink link1= a/b

i need to print the ACTUAL FULL path instead of a linked path

is there any direct command to print the actual path of any linked path
eg
showPhyscialPath /test/df/link1/actual/file1 and it prints the full path

i know that i can do using
cd /test/df/link1/actual/file1/
and then pwd -P
shows the actual full path, i dont want to change the directory and then used pwd

Thaknks
Rel
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-01-2006
Supporter
 

Join Date: Jul 2006
Posts: 156
Stumble this Post!
You can use 'ls' with the -L option.
Code:
       -L, --dereference
              when showing file information for a symbolic link, show information for  the
              file the link references rather than for the link itself
Reply With Quote
  #3 (permalink)  
Old 10-01-2006
Registered User
 

Join Date: Jul 2006
Posts: 17
Stumble this Post!
thanks for reply
but ls -L just works for link and does not work for any directory after link or before link

if i do
ls -L link1 then it will show the actual right side value of link1

but if we do
ls -L link1/newdir then it wont show anything

in my case i dont even which one is link, but i want to know the actual path if there is any link anywhere in the path
Reply With Quote
  #4 (permalink)  
Old 10-03-2006
Registered User
 

Join Date: Jun 2001
Location: Mexico city
Posts: 36
Stumble this Post!
well, maybe it going to be little hard to understand, but I hope it helps in another case.

When I want to catch somebody's real path of a process execution (I already know the process ID) I used to use "pwdx PID" this is a Solaris utility (you can check it doing a: man proc)

"proc" is a set of tools some times weird, some times very helpful.

Hope it works
Reply With Quote
  #5 (permalink)  
Old 10-04-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,854
Stumble this Post!
There's a reason for this. To find out what a link "really is" requires making a system call, readlink. In most circumstances, open (the system call used to open a file) is the only call that does this automatically. I don't know of a command line utility this does the readlink. A long time ago, we needed something to do this - here is a small utility to do that. This could be written in other languages, it happens to be C.
Code:
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <pwd.h>

#define ck_null(x) if((x)==NULL){perror(""); exit(EXIT_FAILURE);}
#define ck_ltz(x) if((x)==(-1)){perror(""); exit(EXIT_FAILURE);}
#ifndef PATH_MAX
#define PATH_MAX 255
#endif
#define DEPTH 16

size_t signal_arglen =0;
char signal_buffer[PATH_MAX+1]={0x0};

void sig_int(int signo)
{
	write(STDERR_FILENO,signal_buffer,signal_arglen);
	exit(EXIT_FAILURE);
}

void expand_path(char *path)
{
	char *prepend=malloc(2*PATH_MAX);
    char *ptr=path;    
	ck_null(prepend);

	if(memcmp(path,"~/",2)==0 || memcmp(path,"./",2)==0)
	{
		struct passwd *pw=NULL;
	    char tmp[PATH_MAX+1]={0x0};	    
	    
		ptr++;       
		switch(*path)
		{
			case '.':				
				sprintf(prepend,"%s%s", getcwd(tmp,PATH_MAX), ptr);
				break;
			case '~':  /* not normally executed ~/ expanded by shell */
				pw=getpwuid(getuid() );
				sprintf(prepend,"%s%s", pw->pw_dir, ptr);
				break;
			default:
				break;			
		}
		strcpy(path,prepend);
    }
    free(prepend);	
}


void get_link_info(char *path)
{
	char buf[PATH_MAX+1]={0x0};
	ssize_t buflen=readlink(path,buf,PATH_MAX);
	
	if(buflen>0)
	{
	    char *p=strrchr(path,'/');	
	    if(p==NULL) 
	        p=path;	   	   
	    buf[buflen]=0x0;
	    strcpy(p,buf);
    }	
}

void recurse_path(char elements[][PATH_MAX],char *path)
{
	int i=0;
    char *p=path;
    char *q=NULL;
        
    if(*p=='/') i=(-1); 
    if(strchr(path,'/')==NULL)
    {
    	strcpy(elements[0],path);	
    }
    else
    {  
		for(p=path;*p;p++)
		{
			if(*p=='/')			
				q=elements[++i];				
    	    *q++=*p;
		}	
     }
}

char  *get_path_elements(char *dest, char *path)
{
	char *working=calloc(1,PATH_MAX*2);
	int i=0;
	char elements[DEPTH][PATH_MAX]={0x0};
	
	ck_null(working);	
	recurse_path(elements,path);
	while(*elements[i])	
	{
		strcat(working,elements[i++]);
		get_link_info(working);	
	}
	strcpy(dest,working);
	free(working);
	return dest;
}

int main(int argc, char *argv[])
{
	char path[PATH_MAX*2]={0x0};
	char working_path[PATH_MAX*2]={0x0};
	int i=1;
    char *p=argv[i];

	sprintf(signal_buffer,"%s interrupt\n",argv[0]);
    signal_arglen=strlen(signal_buffer);	
	signal(SIGINT,sig_int);
	if (argc >1)
	{
	    while(*p)
	    {
	    	strcpy(working_path,p);
	    	expand_path(working_path);
	    	printf("%s\n",
	    	        get_path_elements(path, working_path));
	    	i++;
	    	p=argv[i];
	    }
	}
	else
	{
		while(fgets(working_path,sizeof(working_path),stdin)!=NULL)
		{
			expand_path(working_path);
	    	printf("%s\n",
	    	        get_path_elements(path, working_path));		
		}
	}
	return 0;
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0