Is this a "safe" way to get the current path?

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Is this a "safe" way to get the current path?

Post by earlz »

I have been working on getting a platform independent way to get the current path

right now to get the path of what was called I do this:

Code: Select all

void GetOpen86Dir(char *arg){ //arg is argv[0]
    char *end=arg+strlen(arg);
    while(arg<end){
        if(*end=='/' || *end=='\\'){
            end++; //save the '/'
            *end='\0';
            strcpy(open86_dir,arg);
            return;
            }
        end--;
    }
    strcpy(open86_dir,"");
    printf("Warning!! Cannot figure out open86's directory--so using \'./\'");
}
}
basically what that does is scan the argv[0] from it's end until it finds a / or \, and upon finding that it adds a \0 to it to null terminate it and then copies it to my open86_dir pre-allocated string

now, is this a safe way to obtain the path? in some OS's will it fail?(it works in windows, and haven't tested it anywhere else)
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

When I run this:

Code: Select all

int main(int argc, char * argv[], char *envp[]) {
	printf("Path: %s\n", argv[0]);
}
I get this result:

Code: Select all

Path: ./p
Uhh, I'm on Linux.....Am I doing something wrong?
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

No, you aren't doing anything wrong. Quoting the C standard, chapter 5.1.2.2.1 "Program startup":
— If the value of argc is greater than zero, the array members argv[0] through
argv[argc-1] inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The
intent is to supply to the program information determined prior to program startup
from elsewhere in the hosted environment. If the host environment is not capable of
supplying strings with letters in both uppercase and lowercase, the implementation
shall ensure that the strings are received in lowercase.
— If the value of argc is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0] shall be the null character if the
program name is not available from the host environment. If the value of argc is
greater than one, the strings pointed to by argv[1] through argv[argc-1]
represent the program parameters.
Read, the things written into argv are implementation-defined. All bets are off as to whether argv[0] contains an absolute or relative path, or any path at all. Besides, the current directory need not be the same as the program's directory. Paths are platform-dependent, so there is no way to determine them platform-independantly.
Every good solution is obvious once you've found it.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,

If you're willing to accept "compiler dependant" code that is platform independant, then GCC's libraries have "get_current_dir_name()", "getwd()", "getcwd()", "basename()" and "dirname()' - all of which might help...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply