Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to get the command line tool directory (written in C)

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  signal call failing on 10.1?  
Author Message
Alain Birtz

External


Since: Oct 27, 2003
Posts: 84



(Msg. 1) Posted: Mon Oct 13, 2003 12:48 pm
Post subject: How to get the command line tool directory (written in C)
Archived from groups: comp>sys>mac>programmer>help (more info?)

How to get the path of the command line tool programaticaly ?
(after the command line tool was called from the Terminal)
Back to top
Login to vote
Miro Jurisic

External


Since: May 10, 2004
Posts: 1194



(Msg. 2) Posted: Mon Oct 13, 2003 5:09 pm
Post subject: Re: How to get the command line tool directory (written in C) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <bmfh57$q79$2@seagoon.newcastle.edu.au>,
Heath Raftery <hraftery.TakeThisOut@myrealbox.com> wrote:

> Alain Birtz <abz.TakeThisOut@videotron.ca> wrote:
> > How to get the path of the command line tool programaticaly ?
> > (after the command line tool was called from the Terminal)
>
> #include <stdio.h>
>
> int main(int argc, char* argv[])
> {
> printf("The tool path is: %s\n", argv[0]);
> return 0;
> }
>
> In other words, the first argument passed to a command line tool is the line
> used to invoke it.

Not necessarily. It is trivial to launch an executable in a way that that is not
true. There is no cross-platform way to accomplish this, in ANSI nor in POSIX
nor in BSD (that I know of), so just call CFBundle to get the URL to the main
bundle.

meeroh

--
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>
Back to top
Login to vote
Paul Guyot

External


Since: Jul 11, 2003
Posts: 21



(Msg. 3) Posted: Tue Oct 14, 2003 3:34 am
Post subject: Re: How to get the command line tool directory (written in C) [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Login to vote
Tom Harrington

External


Since: Aug 19, 2003
Posts: 1916



(Msg. 4) Posted: Tue Oct 14, 2003 5:30 am
Post subject: Re: How to get the command line tool directory (written in C) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <macdev-C46EF6.10060514102003.DeleteThis@senator-bedfellow.mit.edu>,
Miro Jurisic <macdev.DeleteThis@meeroh.org> wrote:

> In article <spam-37012A.08344214102003.DeleteThis@news1-1.free.fr>,
> Paul Guyot <spam.DeleteThis@kallisys.com> wrote:
>
> > However, I wasn't aware of the CFBundle way. What does CFBundle has to do
> > with a command line tool? Could you please elaborate?
>
> Sorry, I misspoke, it was late. I meant GetProcessBundleLocation, not
> CFBundle.

That doesn't really answer the question of getting the path to a command
line tool, though. Unless I've missed something in my attempt below
(borrowed mostly from an Apple example):

ProcessSerialNumber psn = {0, kCurrentProcess};
FSRef processRef;
FSSpec fileSpec;
HFSUniStr255 *uniFileName;
FSCatalogInfo processInfo;
CFStringRef myPath;

uniFileName = MemAlloc(sizeof(HFSUniStr255));
GetProcessBundleLocation(&psn, &processRef);
FSGetCatalogInfo(&processRef, kFSCatInfoNodeFlags, &processInfo,
uniFileName, &fileSpec, NULL);

myPath = CFStringCreateWithCharacters(kCFAllocatorDefault,
uniFileName->unicode, uniFileName->length);
CFShow(myPath);

After compiling to "a.out", the last line always prints exactly that:
"a.out", with no information about the path to the executable.

I thought maybe the answer was in the FSCatalogInfo, but docs on that
structure indicate otherwise. Unless you're thinking of repeatedly
looking up parentDirID fields until one reaches the top of the file tree.

--
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 1.4: Best cleanup yet, gets files other tools miss.
See http://www.atomicbird.com/
Back to top
Login to vote
Mike Hall

External


Since: Aug 11, 2003
Posts: 7



(Msg. 5) Posted: Tue Oct 14, 2003 12:31 pm
Post subject: Re: How to get the command line tool directory (written in C) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Alain Birtz wrote:

>How to get the path of the command line tool programaticaly ?
>(after the command line tool was called from the Terminal)

Two ideas:
- use the 'which' command or the shell's builtin 'type' command
$ which ls
/bin/ls

- or get the user's PATH, split it, and go looking for it yourself
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Error -2110 in open command - Not sure which group this should go to, but I will post here because it is related to development. I am a newbie with r...

change key mapping for Option and Command on Mac OS 9.x - Hi, I am doggedly trying to use a PC keyboard on my G4 here at work. :) It's running OS 9.0.4. I've found articles ex...
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]