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 > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Hostname not fully qualified.. sdspawankumar SUN Solaris 3 03-05-2008 08:15 AM
running a command without having to provide the path Terrible Shell Programming and Scripting 2 12-28-2006 07:22 PM
Please help me find the PATH that I need to be on :) trmn8r SUN Solaris 7 05-19-2006 04:09 PM
How to find whether the given path is in Network yessowmya Shell Programming and Scripting 0 11-16-2005 10:40 AM
Mod probe can't find module ppp0 skotapal Filesystems, Disks and Memory 1 04-28-2003 09:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 01-10-2006
erwinfletch erwinfletch is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 1
find the fully-qualified path for the app my module is running in

Hi-

I need the cpp call that will tell me the full path to the app I'm running in. For example, I'm running in a loaded library for either mozilla or firefox, but would like to know the full path to the executable

/usr/bin/firefox
/usr/bin/mozilla
/usr/local/firefox1_5

etc...

(For windows programmers, it would be the Linux version of GetModuleFileNameW)

Thanks!
  #2 (permalink)  
Old 01-10-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,748
There is no one perfect way to do this in Linux/Unix.

argv[0] may have it, unless the file was exec'd, getcwd() may be it, or
you can try a directory search.

This is because files can be run in a lot of different ways, and Linux does not have a registry.
  #3 (permalink)  
Old 01-11-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by jim mcnamara
There is no one perfect way to do this in Linux/Unix.
I agree. But the following solution might work for the OP (Caution.. a /proc based solution.)

Code:
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/* Finds the path containing the currently running program executable.
  The path is placed into BUFFER, which is of length LEN. Returns
  the number of characters in the path, or -1 on error. */

size_t get_executable_path (char* buffer, size_t len)
{
 char* path_end;
 /* Read the target of /proc/self/exe. */
 if (readlink ("/proc/self/exe", buffer, len) <= 0)
  return -1;
 /* Find the last occurrence of a forward slash, the path separator. */
 path_end = strrchr (buffer, '/');
 if (path_end == NULL)
  return -1;
 /* Advance to the character past the last slash. */
 ++path_end;
 /* Obtain the directory containing the program by truncating the
   path after the last slash. */
 *path_end = '\0';
 /* The length of the path is the number of characters up through the
   last slash. */
 return (size_t) (path_end - buffer);
}

int main ()
{
 char path[PATH_MAX];
 get_executable_path (path, sizeof (path));
 printf ("this program is in the directory %s\n", path);
 return 0;
}
This solution fixes problems such as env settings which are exclusive for the app, and which are required to be set by the user prior to executing the app.

I picked this up from the net a long time ago.

Last edited by vino; 01-11-2006 at 01:11 AM..
  #4 (permalink)  
Old 01-11-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,748
That will work fine for Linux or other versions of unix that support /proc.
  #5 (permalink)  
Old 01-12-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Yet another way would be to use the results of

/proc/proc-id/maps

This would be a longer solution (compared to the /proc/self/exe) because you will have to read through each line till you reach one which has your app-name in it. From there, its an easy ride.

Yes, these solutions are for /proc based OS's.

Vino
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 03:40 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