Finding the path of the C program


 
Thread Tools Search this Thread
Top Forums Programming Finding the path of the C program
# 1  
Old 10-31-2010
Finding the path of the C program

Hi All,
I have a c program called findPath.c in a path /home/harsh/c-Programs/. How can i find the path where the program is stored at runtime?? I have given the following
Code:
#include<stdio.h>
int main()
{
system("dirname $0");
return 0;
}

This is resulting in the output as
.
<single dot specifying the current directory>

Last edited by pludi; 10-31-2010 at 09:34 AM..
# 2  
Old 10-31-2010
Try the realpath() function on $0.

This is a common question and there is no completely all-purpose perfect answer because there are a lot of ways code can be invoked... realpath on $0 is a decent start.
# 3  
Old 11-01-2010
MySQL

Quote:
Originally Posted by jim mcnamara
Try the realpath() function on $0.

This is a common question and there is no completely all-purpose perfect answer because there are a lot of ways code can be invoked... realpath on $0 is a decent start.
Thanks man. found out more than a few ways through which one can find the filename of the executing program. One interesting way was through __FILE__ macro which is passed to the compiler during compilation. Are there any other macros like this that give info about the file being compiled..??
Another method was to se the arg[0] parameter. But i read that the path/file name may be or may not be passed as the 0th argument. Just curious to understand at what scenarios the 0th argument is passed and when it is not passed.. anybody knows?
# 4  
Old 11-01-2010
Quote:
Originally Posted by sreeharshasn
One interesting way was through __FILE__ macro which is passed to the compiler during compilation.
That will only give you the name of the source file being compiled - it expands to a string, so there's no way it can give you the run-time name of the file.


Quote:
Another method was to se the arg[0] parameter. But i read that the path/file name may be or may not be passed as the 0th argument. Just curious to understand at what scenarios the 0th argument is passed and when it is not passed.. anybody knows?
One example would be an entry in an inet.d file. For example, this line:

Code:
tftp        dgram   udp wait    nobody  /usr/sbin/tcpd  /usr/sbin/in.tftpd /srv/tftp

will execute the program /usr/sbin/tcpd, but the first argument to it (i.e. argv[0]) will be "/usr/sbin/in.tftpd".

One reliable way (on Linux 2.2 and later) of finding the pathname of the command executed is by looking at /proc/self/exe, which is a symlink to the executed command. It's not portable (but then neither is realpath()), but if that file doesn't exist you could always fall back on argv[0].
# 5  
Old 11-01-2010
Hi sreeharshasn,

Quote:
How can i find the path where the program is stored at runtime??
Could you tell us why are interested in getting this path?

TIA,
Loïc
# 6  
Old 11-03-2010
It is sort of a puzzle.

Last edited by sreeharshasn; 11-03-2010 at 11:48 AM..
# 7  
Old 11-03-2010
All right, you make me even more curious Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Finding the path to an executable , installed package

Hi all, Recently i wanted to see if i have openssl installed in my system (solaris 10), so i do this (not sure if this is the right way to do this) pkginfo -i | grep -i "ssl" system SUNWopenssl-commands OpenSSL Commands (Usr) system SUNWopenssl-include ... (3 Replies)
Discussion started by: javanoob
3 Replies

2. Shell Programming and Scripting

Finding a script/program on $PATH

I am trying to put together a script to find a requested script or program on the user's search path. Am trying to replace the colons separating the parts of a path with a newline to let xargs pass the directories to a list command, but I haven't gotten that far. This is my progress: echo... (7 Replies)
Discussion started by: wbport
7 Replies

3. UNIX for Advanced & Expert Users

Finding register set being used in program

How can i find( or list) contents of all registers being used by my program? Is there any system call or library available for this?:confused: At runtime in my c/c++ program. At runtime using may be some assembly hack!!!!!!!!!!! (2 Replies)
Discussion started by: amit gangarade
2 Replies

4. Fedora

set path using a shell program

Hello sir, I am using a fedora 9 system. I wanted to update the path to include the $HOME into the path. So what we do is : This will update the path. I want to do the same thing by writing it in a shell prgram. I wrote the above code in an "a.sh" file and executed it using "bash a.sh".BUt... (1 Reply)
Discussion started by: nsharath
1 Replies

5. UNIX for Dummies Questions & Answers

finding a file in its path

I have a file that has multiple entries within the Unix system. Korn shell scripts are calling this file (also a ksh) with a . in front of it, and I'm trying to determine which file it is using based on the $PATH by finding where it is located first. Any suggestion on how to go about this?... (2 Replies)
Discussion started by: gavineq
2 Replies

6. Shell Programming and Scripting

Finding path of a running script

Hi, I just want to know any code by which i can get the path of the script which i am running. This is required to store the output in the same directory from where the script is running. pwd fails if I give absolute path of script from some other directory. Thanks in advance Puneet (3 Replies)
Discussion started by: puneet
3 Replies

7. Shell Programming and Scripting

Finding relative path of a file

I have to relatively get the path of a file to use it in the script. The directory structure is /export/opt/XTools/ and under this there are several version directories - 1.0_A0, 1.0_A1, 1.0_A2 etc.,. The actual file is under these directories: installscript.sh My script should pickup the... (4 Replies)
Discussion started by: chiru_h
4 Replies

8. Programming

finding stack location in C using program

Is there a way to find the address of stack memory writing a program? Please guide me (12 Replies)
Discussion started by: jacques83
12 Replies

9. IP Networking

finding the java path in AIX

How to retrieve the path of Java bin directory in AIX (or any unix OS)?? Actually my problem is, I have a jar file called App.jar. I want to execute it by calling the javaw executable. My command is "/usr/java130/javaw -jar App.jar". I've written this command into a shell script. Now,... (2 Replies)
Discussion started by: fermisoft
2 Replies

10. UNIX for Dummies Questions & Answers

Finding current working dir path

Hi Folks, In a Unix (ksh) script, is there a way to determine the current working directory path of another logged-in user? Of course, I can use "pwd" to find my own path. But, how do I find it for another active user? Thanks for any input you can provide. LY (6 Replies)
Discussion started by: liteyear18
6 Replies
Login or Register to Ask a Question