Professional Web Applications Themes

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

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

  1. #1

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

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

  2. #2

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

    Alain Birtz <ca> wrote: 

    #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.

    --
    *--------------------------------------------------------*
    | ^Nothing is foolproof to a sufficiently talented fool^ |
    | Heath Raftery, HRSoftWorks _\|/_ |
    *______________________________________m_('.')_m__ _______*
    Heath Guest

  3. #3

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

    In article <bmfh57$q79$newcastle.edu.au>,
    Heath Raftery <com> wrote:
     
    >
    > #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.[/ref]

    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>

    Miro Guest

  4. #4

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

    In article (Dans l'article)
    <mit.edu>,
    Miro Jurisic <org> wrote (écrivait):
     
    >
    > 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.[/ref]

    If argc is greater than 0 and argv[0][0] is not null, then argv[0] should
    be the program name (ANSI C). I agree we have no guarantee that this
    actually is the relative path (as invoked from the command line) of the
    tool, although this is what common shell programs provide by default. I
    also agree we can fool a tool easily with exec*.

    I always have considered the portable way is to use argv[0], notify the
    user if it's not available and let them be responsible if they fool us with
    exec* or any other trick (after all, they might as well copy/rename the
    binary).

    However, I wasn't aware of the CFBundle way. What does CFBundle has to do
    with a command line tool? Could you please elaborate?

    Paul

    --
    Philosophie de baignoire - consultations sur rendez-vous.

    NPDS: http://newton.kallisys.net:8080/
    Apache: http://www.kallisys.com/
    Paul Guest

  5. #5

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

    Hi Paul,

    Paul Guyot <com> writes: 

    But AFAIK ANSI C does not (and can not) define what a "program name"
    is. We could expect the name of the file used for the process image.
    But Unix is an environment where this expectation is very often false.

    benny
    Benjamin Guest

  6. #6

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

    In article <free.fr>,
    Paul Guyot <com> wrote:
     

    Sorry, I misspoke, it was late. I meant GetProcessBundleLocation, not CFBundle.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  7. #7

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

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > Sorry, I misspoke, it was late. I meant GetProcessBundleLocation, not
    > CFBundle.[/ref]

    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/
    Tom Guest

  8. #8

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

    Alain Birtz wrote:
     

    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


    Mike Guest

  9. #9

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

    In article (Dans l'article) <tph-FE0C59.10302014102003localhost>,
    Tom Harrington <no.spam.dammit.net> wrote (écrivait):
     

    Actually, as long as you have an FSRef to the file, you can get the path
    using FSRefMakePath.

    Paul

    --
    Philosophie de baignoire - consultations sur rendez-vous.

    NPDS: http://newton.kallisys.net:8080/
    Apache: http://www.kallisys.com/
    Paul Guest

  10. #10

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

    In article <free.fr>,
    Paul Guyot <com> wrote:
     
    >
    > Actually, as long as you have an FSRef to the file, you can get the path
    > using FSRefMakePath.[/ref]

    Thanks, I didn't know that. Using FSRefMakePath does seem to work. I'm
    guessing that the resulting path is UTF-8 encoded. The docs on
    FSRefMakePath don't explain that, but the fact that it's declared as
    UInt8 * suggests that this is the case.

    So based on this, the Carbon/CF way to get the path would be (without
    error checking):

    ProcessSerialNumber psn = {0, kCurrentProcess};
    FSRef processRef;
    UInt8 *path;

    GetProcessBundleLocation(&psn, &processRef);
    path = (UInt8 *)malloc(MAXPATHLEN * sizeof(UInt8));
    FSRefMakePath(&processRef, path, MAXPATHLEN);
    printf("Path: %s\n", path);

    --
    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/
    Tom Guest

Similar Threads

  1. msyql command line tool can't handle del character
    By gaetanoortisi@yahoo.it in forum MySQL
    Replies: 1
    Last Post: March 4th, 07:30 PM
  2. RUN/execute a Command-Line command from an ASP page
    By Lucas Cowald in forum ASP.NET Building Controls
    Replies: 1
    Last Post: October 22nd, 04:26 PM
  3. How to run a command line tool?
    By Laura in forum Mac Programming
    Replies: 5
    Last Post: October 8th, 03:42 PM
  4. using a directory name as a command line argument
    By Brian Harnish in forum PERL Miscellaneous
    Replies: 1
    Last Post: August 12th, 03:23 AM
  5. Command line to start Elements in specific directory
    By JoshOJ in forum Adobe Photoshop Elements
    Replies: 0
    Last Post: July 10th, 01:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139