LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2003, 07:16 AM   #1
mvendramini
Member
 
Registered: Apr 2003
Location: Brazil
Distribution: Slackware
Posts: 35

Rep: Reputation: 15
app.path in linux?


Hello there.

I'm trying to get the filepath (/home/user/myapp)

on Linux, in C++. In windows, argv[0] would do the job:
(i.e "C:\windows\desktop\myapp")

but in linux it does only contain the filename "myapp". What gives?
 
Old 09-13-2003, 08:02 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
care to actually tell us what you're doing and what language you're doing it in? we're not psychic....
 
Old 09-13-2003, 08:47 AM   #3
mvendramini
Member
 
Registered: Apr 2003
Location: Brazil
Distribution: Slackware
Posts: 35

Original Poster
Rep: Reputation: 15
but... i said C++...

I want to retrieve the entire filepath for my app. i.e I have a program in:
/home/user/myapp.

And in myapp code I want to have a string containing "/home/user/myapp". Here's how I have done in windows:

Code:
int main(int argc, char *argv[]){
char path[300];

path = argv[0];
printf("%s", path);

return 0;
}
Obviously, this program outputs its own filepath (i.e "C:\windows\desktop\myapp.exe") if ran on windows. What about Linux then?
 
Old 09-13-2003, 09:15 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yes, this is a bit of a problem in Linux, as argv[0] contains the commandline the program was started with. If you start your program specifying the full path of the executable, then argv[0] will contain the full path.

I've been puzzled by this as well, but I found two ways of getting the full path. It is a bit more complicated though.

Here are 2 examples of how to do this in C. I don't know enough about C++ to do it in proper C++. Anyways, you can use it (almost) unmodified in C++ I think.
Code:
/* getexepath1.c
 * ~~~~~~~~~~~~~
 * This uses the environment variable "_" to read
 * the relative path of the executable.
 * This is not available allways.  I assume it's
 * set by the shell, and not all do this.
 * Anyways, using bash on linux or BSD it will work.
 *
 * From this relative path the full path can be
 * resolved by the realpath() function.
 * Which is also not reliable on all systems.
 * (see BUGS section in "man 3 realpath")
 * On linux it generally works OK.
 */

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

int main ()
{
     char *exepath;
     char fullpath[PATH_MAX+1];
     
     if ((exepath = getenv("_")) == NULL) {
	  fprintf(stderr, "Could not read path from environment.\n");
	  exit(EXIT_FAILURE);
     }
     if (realpath(exepath, fullpath) == NULL) {
	  fprintf(stderr, "Error resolving full path.\n");
	  exit(EXIT_FAILURE);
     }

     printf("Path is: %s\n", exepath);
     printf("Full path is: %s\n", fullpath);
     return 0;
}
Code:
/* getexepath2.c
 * ~~~~~~~~~~~~~
 * This uses the /proc filesystem.  This may be
 * not available on all systems, but on Linux it
 * usually is.  However it is possible to compile
 * a linux kernel that does not provide
 * the /proc filesystem.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define MAXPATHLEN 200   /* make this larger if you need to. */

int main ()
{
     int length;
     char fullpath[MAXPATHLEN];
     
     /* /proc/self is a symbolic link to the process-ID subdir
      * of /proc, e.g. /proc/4323 when the pid of the process
      * of this program is 4323.
      *
      * Inside /proc/<pid> there is a symbolic link to the
      * executable that is running as this <pid>.  This symbolic
      * link is called "exe".
      *
      * So if we read the path where the symlink /proc/self/exe
      * points to we have the full path of the executable.
      */


     length = readlink("/proc/self/exe", fullpath, sizeof(fullpath));
     
     /* Catch some errors: */
     if (length < 0) {
	  fprintf(stderr, "Error resolving symlink /proc/self/exe.\n");
	  exit(EXIT_FAILURE);
     }
     if (length >= MAXPATHLEN) {
	  fprintf(stderr, "Path too long. Truncated.\n");
	  exit(EXIT_FAILURE);
     }

     /* I don't know why, but the string this readlink() function 
      * returns is appended with a '@'.
      */
     fullpath[length] = '\0';       /* Strip '@' off the end. */

     printf("Full path is: %s\n", fullpath);
     return 0;
}

Last edited by Hko; 09-13-2003 at 09:19 AM.
 
Old 09-13-2003, 09:31 AM   #5
mvendramini
Member
 
Registered: Apr 2003
Location: Brazil
Distribution: Slackware
Posts: 35

Original Poster
Rep: Reputation: 15
Thanks for the reply. It will do fine for my purpose.
 
Old 11-30-2003, 01:23 PM   #6
nsilva
LQ Newbie
 
Registered: Nov 2003
Location: Purdue University
Distribution: Slackware 9.1
Posts: 1

Rep: Reputation: 0
The file name that the link points to is copied into buffer. This file name string is not null-terminated.
That is why you see an @ there. It must be manually null terminated.
 
Old 12-01-2003, 10:26 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Ah! OK, so it could be anything instead o a '@'....
Fortunately, -more or less by accident- I did the right thing.
Thanks.
 
Old 12-01-2003, 01:20 PM   #8
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Use getcwd() it's POSIX standard.
Code:
#include <unistd.h>                         
#include <stdlib.h>
#include <stdio.h>            
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
#define zout(z) memset(z,0x00,sizeof(z))
/* calls to get the current working directory                                
       one of these will work: getcwd() is POSIX  
       char *getcwd(char *buf,size_t size);
       
       
       These other calls are prototyped only some circumstances:
       
     
       char *get_current_dir_name(void);    
       char *getwd(char *buf);              
****/
       
int main(int argc, char *argv[]){
       char dirname[PATH_MAX+1];
       zout(dirname);
       if( getcwd(dirname,sizeof(dirname))!=NULL) printf("%s\n",dirname);
       return 0;
}
 
Old 12-02-2003, 09:24 AM   #9
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I'm not sure, but I don't think getcwd is quite what was wanted here. As I understand it, the original poster wanted to know the path to his app. The current working diretory is not necessarily where that app resides. For instance, if you had your app somewhere in the path (e.g. /usr/local/bin) and were in your home directory when you executed it, your current working directory would be ~, not /usr/local/. Or if you had an app in ~/myapps/app, and just executed it from the ~ directory by giving the path to it...
 
Old 12-02-2003, 11:28 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
That's right. It was about finding the full path of the executable file.
 
Old 12-02-2003, 01:13 PM   #11
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Yup. You're correct
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if an app is running and if it's not launch this app Coume Linux - General 3 07-28-2008 02:34 AM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
setting path for app n libraries install shizzles Linux - Software 3 07-18-2005 12:26 PM
What is the best prebuilt computer for Linux? What's the best Linux 3D CAD app? t3gah Linux - Hardware 2 04-20-2005 01:42 PM
Installing an App on Linux - wrong path? Rory in Toronto Linux - Newbie 6 03-30-2004 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration