get the path of current running program


 
Thread Tools Search this Thread
Top Forums Programming get the path of current running program
# 1  
Old 02-17-2009
get the path of current running program

How does the program know the full path of itself when the program is running in certain diretory? BTW, I have no "argv" information of main() functino.

(The program is written in C++ on linux platform)
# 2  
Old 02-17-2009
The call to getcwd() returns the path to the current directory. argv[0] contains the invocation of the current program, and can be used to infer the directory of the path to itself -- but not necessarily. If the program was invoked without any directory information and is invoked along the PATH variable, you can grab the PATH variable from the environ (getenv("PATH")) and traverse each element looking for the program itself (basename of argv[0]).

There's a function called realpath() which canonicalizes the pathname you provide to its argument. However, it's recommended NOT to use this in general, because you have to allocate memory for the return argument, but there's no way of knowing how large that memory allocation should be. (But realistically you can get away with 1k).

Unfortunately, realpath doesn't do any of the "real work" for you. You still have to traverse PATH to find the program's home directory.
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 get the current directory Path?

How to get the current directory Path in your prompt? i am getting a $ mark only in my prompt? Please help me with this (3 Replies)
Discussion started by: Abhishek0683
3 Replies

2. Shell Programming and Scripting

Execute C program in Current shell

Hello, I have a c program executable which I need to run inside a shell script. But the c program runs in a subshell because of which all the actions done by the c program is not available to the current shell. Is there any way to execute a C program binary executable in the current shell? (4 Replies)
Discussion started by: sachinverma
4 Replies

3. UNIX for Dummies Questions & Answers

Setting the current directory path to terminal title

In ubuntu, I want to update the title of the terminal window with the current directory path. Any ideas how this can be achieved? ---------- Post updated at 02:22 PM ---------- Previous update was at 02:08 PM ---------- Done it ---------- Post updated at 02:30 PM ---------- Previous update... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

5. UNIX for Dummies Questions & Answers

tag/mark a file with current absolute path

Hi all, I need to mark a file with it's current location in the file system before being moved. This will enable the file to be restored back to it's original location. Can anyone provide any ideas about the best way to do this, at present i'm trying to use readlink -m to strip off the... (1 Reply)
Discussion started by: skinnygav
1 Replies

6. Programming

Current and Peak Memory consumption of my program?

Hi, my C++ program does some memory consuming tasks and runs several minutes. During execution time, I want to print information on the current memory consumption and peak memory consumption of my program into a logfile. How can I retrieve this information, i.e. which functions are available... (0 Replies)
Discussion started by: DarthVader77
0 Replies

7. UNIX for Advanced & Expert Users

list users current path

Looking for a command which shows the current path of other users. Similiar to the PWD command. Need to be able to see which part of the system the user is logged on to. Thanks G (1 Reply)
Discussion started by: Gandalf77
1 Replies

8. Shell Programming and Scripting

want the current directory without the absolute path

Hi guys I'm trying to move an empty directory to the $TRASH directory. Say the directory i have is ./hello/hello1/hello2 and i'm in hello2, and i want hello2 moved. this code: TRASH=$home/deleted find "$TRASH/$1" -type d -exec rmdir { } \; 2>/dev/null mv -f $1 $TRASH 2>/dev/null works... (2 Replies)
Discussion started by: olimiles
2 Replies

9. UNIX for Dummies Questions & Answers

Question about Restricting Search path of FIND to current directory

Hi, By default FIND command searches for matching files in all the subdirectories within the specified path. Is there a way to restrict FIND command's search path to only the specified directory and NOT TO scan its subdirectories. Any help would be more than appreciated. Thanks and Regards (2 Replies)
Discussion started by: super_duper_guy
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