LinuxQuestions.org
Help answer threads with 0 replies.
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 03-11-2008, 08:17 PM   #1
mirsoft
LQ Newbie
 
Registered: Oct 2007
Posts: 15

Rep: Reputation: 0
Do there have an Xlib API to get the current application directionary path?


like the title, any one can help me ?
3Q
 
Old 03-11-2008, 08:49 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
No - there is no Xlib API...
... but argv[0] should give you what you're looking for.
 
Old 03-11-2008, 08:58 PM   #3
mirsoft
LQ Newbie
 
Registered: Oct 2007
Posts: 15

Original Poster
Rep: Reputation: 0
thanks, do c runtime lib have this api to do this. if have , could you give me a simple.
 
Old 03-12-2008, 12:44 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sure -
Code:
#include <stdio.h>
#include <unistd.h>

int
main (int argc, char *argv[])
{
  char buffer[80];
  printf ("argv[0]: %s...\n", argv[0]);
  printf ("getcwd (): %s...\n", getcwd (buffer, sizeof (buffer)));
  return 0;
}
Quote:
$ gcc -o hello hello.c
$ `pwd`/hello
argv[0]: /home/paulsm/hello...
getcwd (): /home/paulsm...
 
Old 03-13-2008, 12:51 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
But there is no portable way of finding a process’s actual path (paulsm4 forces the issue by calling the process a certain way). In particular, a process might not even have a path any more.

If you’re on linux, there is a non-portable, somewhat reliable method of finding this out (it uses the /proc filesystem):
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
	char proc_path[20];
	char path[PATH_MAX];
	int i;

	sprintf(proc_path, "/proc/%d/exe", getpid());
	if((i = readlink(proc_path, path, PATH_MAX-1)) == -1)
		exit(EXIT_FAILURE);	
	path[i] = 0;

	printf("process path is \"%s\".\n", path);

	return 0;
}
 
  


Reply

Tags
path, xlib



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
Prevent XLIB application from closing when close button pressed fcdev Programming 1 11-11-2007 01:47 PM
double current directory in path Saulo SUSE / openSUSE 5 04-20-2006 06:19 PM
how to get absolute path of current executing C/C++ prog? Thinking Programming 2 10-10-2005 10:27 AM
Placing current path in the prompt zepplin611 Linux - General 2 05-13-2004 12:07 PM
How to change the path of the current directory ? ndha Linux - General 7 12-03-2003 11:05 AM

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

All times are GMT -5. The time now is 11:34 AM.

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