Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page How to get a process's executable name during execution
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-06-2007   #1 (permalink)
chhaba
Just Joined!
 
Join Date: Jun 2007
Posts: 2
How to get a process's executable name during execution

Hi all,

I have created a library file suppose lib.o
I want to create a function in this library, so that any program compiled with this library will print the name of the executable.

e.g.
myprog.c

int main(){

function_from_liberary();
}

and liberary file
lib.c contains

void function_from_liberary() {

printf ("\nExecutable name is %s",what_to_write_here);
}


i compile using

gcc -c lib.c

gcc -g -o myprog myprog.c lib.o

Please help

Thanx and regards
__________________
Have a nice time
'C'hhaba
chhaba is offline   Reply With Quote
Old 06-06-2007   #2 (permalink)
rajeshk
Just Joined!
 
Join Date: Jan 2006
Location: India
Posts: 52
Send a message via Yahoo to rajeshk
void function_from_liberary() {

printf ("\nExecutable name is %s",argv[0]);
}
rajeshk is offline   Reply With Quote
Old 06-06-2007   #3 (permalink)
the_unforgiven
Linux User
 
Join Date: Oct 2004
Location: /dev/random
Posts: 404
Quote:
Originally Posted by rajeshk View Post
void function_from_liberary() {

printf ("\nExecutable name is %s",argv[0]);
}
No, this is not possible - argv is local to main() only - if main() is declared as
Code:
int main(int argc, char** argv)
- which is the de-facto standard.

However, you can make function_from_library() take a const char* - which would be passed from main() as argv[0].
__________________
The Unforgiven
Registered Linux User #358564
the_unforgiven is offline   Reply With Quote
Old 06-06-2007   #4 (permalink)
the_unforgiven
Linux User
 
Join Date: Oct 2004
Location: /dev/random
Posts: 404
Quote:
Originally Posted by the_unforgiven View Post
No, this is not possible - argv is local to main() only - if main() is declared as
Code:
int main(int argc, char** argv)
- which is the de-facto standard.

However, you can make function_from_library() take a const char* - which would be passed from main() as argv[0].
Or, better still, you could use the file /proc/<PID>/cmdline - pid can be got with getpid().
__________________
The Unforgiven
Registered Linux User #358564
the_unforgiven is offline   Reply With Quote
Old 06-07-2007   #5 (permalink)
chhaba
Just Joined!
 
Join Date: Jun 2007
Posts: 2
error: dereferencing pointer to incomplete type

Hi all,
I have changed my program as follows. but it gives error:
line 7: error: dereferencing pointer to incomplete type

1. #include<stdio.h>
2. #include<sys/stat.h>
3. #include<stdlib.h>
4. void execname() {
5. struct task_struct *my;
6. my = find_task_by_id(getpid());
7. printf("%s",my->comm); error: dereferencing pointer to incomplete type
8.
9. }
10. int main()
11. {
12. execname();
13. }


what's wrong with it
__________________
Have a nice time
'C'hhaba
chhaba is offline   Reply With Quote
Old 06-17-2008   #6 (permalink)
suppressingfire
Just Joined!
 
Join Date: Jun 2008
Posts: 3
__progname

__progname (and __progname_full) are possible ways to access the executable's name at runtime without having to store off values from argv**. I don't believe it's entirely standard, but it's available and is what glibc's assert uses.

extern const char *__progname;

void print_progname() {
printf("Name: %s\n", __progname);
}
suppressingfire is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Executable Programs Bailz Linux Newbie 1 12-13-2006 02:51 PM
can't I make something executable ONLY? samf Redhat / Fedora Linux Help 4 12-25-2005 07:02 PM
how to find the executable prosh0t SuSE Linux Help 8 08-08-2004 07:24 AM
Calling executable file inside a kernel module Majid The Linux Kernel 0 07-28-2004 10:57 AM
creating a shortcut with bat to launch an executable smtp Linux Programming & Scripting 6 03-30-2004 12:24 AM




All times are GMT. The time now is 05:56 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0