The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 09:39 PM
$PWD shows absolute path vs path w/symbolic links kornshellmaven Shell Programming and Scripting 3 06-13-2007 12:15 PM
vi - replacing a relative path with absolute path in a file Yinzer955i UNIX for Dummies Questions & Answers 2 09-07-2006 11:47 AM
cc path problem - no acceptable path found kendokendokendo UNIX for Dummies Questions & Answers 2 09-12-2005 09:52 AM
how to check if path is present? svennie UNIX for Dummies Questions & Answers 3 11-02-2004 06:13 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2006
reldb reldb is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 19
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
  #2 (permalink)  
Old 10-01-2006
nathan nathan is offline VIP Member  
Supporter
  
 

Join Date: Jul 2006
Posts: 156
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
  #3 (permalink)  
Old 10-01-2006
reldb reldb is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 19
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
  #4 (permalink)  
Old 10-04-2006
alex blanco alex blanco is offline
Registered User
  
 

Join Date: Jun 2001
Location: Mexico city
Posts: 36
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
  #5 (permalink)  
Old 10-04-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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;
}
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0