find the fully-qualified path for the app my module is running in


 
Thread Tools Search this Thread
Top Forums Programming find the fully-qualified path for the app my module is running in
# 1  
Old 01-10-2006
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  
Old 01-10-2006
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  
Old 01-11-2006
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  
Old 01-11-2006
That will work fine for Linux or other versions of unix that support /proc.
# 5  
Old 01-12-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to fix Python path for some script/app?

Hello, i have: # python -V Python 2.7.6 But original for my CentOS is 2.3 or 2.4 my python folder: /root/python2.7.6 (inside are folders like lib, include, bin, share) I launched app iotop: # iotop -od 6 Traceback (most recent call last): File "/usr/bin/iotop", line 16, in... (3 Replies)
Discussion started by: postcd
3 Replies

2. Programming

Running bin file from a module

Hi I actually wrote a simple module and I need to call a bin file from that module. Could you give me some hints how to do that? (I tried with stdlib.h and 'system()', but cannot call that function from a module). Regards. (28 Replies)
Discussion started by: Chrisdot
28 Replies

3. Solaris

Can't change fully qualified host name

I tried changing my /etc/inet/hosts file for my server to: <ip address> <hostname> <fqdn> but when I go to reboot the file changes right back to: <ip address> <hostname> how do I get the <fqdn> to stick on a reboot. Thanks (2 Replies)
Discussion started by: jastanle84
2 Replies

4. AIX

AIX 6.1 app running on 5.x?

Hi, A quick question. If I build an application on AIX 6.1 TL3 using XL C/C++ 8.0 and Oracle 10g, can I then take those binaries and run them on AIX 5.3 and previous? Regards Kevin (3 Replies)
Discussion started by: KevB
3 Replies

5. Shell Programming and Scripting

how to get fully qualified path name

hi actually i want to get fully qualified path name of the file when the file name is entered as command line argument while running a shell script ex. if i run the shell as $./test.sh ./nsdnet_file.csv the it should display me the full path of the file like /dialp/Release/bin/nsdnet_file.csv... (3 Replies)
Discussion started by: priyanka3006
3 Replies

6. Windows & DOS: Issues & Discussions

Display running 'app' in terminal titlebar?

Hi. I was, not too long ago, an OS X home user. One of the things I remember from using the Apple-installed Terminal is: whenever an executable that took more than a split second to do its thing was running, its name would appear in the title bar in a way similar to "Terminal: ssh" or "Terminal:... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

7. IP Networking

Unable to ping freebsd machine using fully qualified domain name

hi all. am unable to ping a freebsd machine using fully qualified domain name from a windows machine. i have already set the fqdn for the machine. plz advise me. thanks. (2 Replies)
Discussion started by: coolatt
2 Replies

8. Programming

Running app after logout and monitoring

Hello! I just programmed a very simple app, it's function is to report every minute the state of the memory and cpu and put it to and xml. All this stuff is working ok since I'm logged in into the machine, but i want it to run as a service, how can I do that?? P.S: Any one knows how can I... (1 Reply)
Discussion started by: ncatdesigner
1 Replies

9. Solaris

Hostname not fully qualified..

Hi Friends.. I have a small problem with the hostname of my system.I had installed Solaris 10 X86 on Vmware in my windows 2000 system.After booting of my solaris system,if i give check-hostname command it says ,, hostname is not fully qualified ,,change the hostname to hostname.xxx.xxxxxx.com... (3 Replies)
Discussion started by: sdspawankumar
3 Replies

10. Programming

Need help with C app on Oracle running on Solaris 6

Hello, I would really appreciate some help with a problem my current client is having. He has an old legacy app that does his company's financial accounting and ERP for manufacturing, etc. The app was written by a company called "Just in time" in Austin, TX, and they are no longer in business. ... (2 Replies)
Discussion started by: w0lf
2 Replies
Login or Register to Ask a Question