Linux: Relative Links and fopen

This is a discussion on Linux: Relative Links and fopen within the C++ Programming forums, part of the General Programming Boards category; Lets just say I'm in /root/, and I want to run the file in /usr/local/bin/blah The program blah needs the ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    27

    Linux: Relative Links and fopen

    Lets just say I'm in /root/, and I want to run the file in /usr/local/bin/blah
    The program blah needs the file /usr/local/conf/data, so it uses:
    Code:
    fopen("../../conf/data");
    and it fails because it can't find that file. But if the user is in /usr/local/bin and executes blah, it works.

    What is the best approach to making it so the file uses relative links from the programs folder, and not the folder the user is in?

    Thank yas

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    why not just use the absolute path?
    fopen("/usr/local/conf/data");

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    27
    That's what I'll be doing for this program, but lets say some other program I make I want to be able to install in different directories. How do I know where to get the configuration file?

    For example: Apache's HTTPD is installable in any directory, and it still finds its conf files :P

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > How do I know where to get the configuration file?
    Well if this is meant to be used by any user, you probably want to start by looking in the user's HOME directory.

    Try this for finding out where the program exists. From there, you can create a relative path to the programs config
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <unistd.h>
    #include <string.h>
    
    /*
     * Searches in several places for where the program may exist.
     */
    void finder ( char *progname ) {
        if ( progname == NULL || *progname == '\0' ) {
            /* you would only get this from people playing games with exec() */
            /* or unusual free-standing implementations, in which case all bets are off */
            fprintf( stderr, "No program name specified\n" );
        } else
        if ( progname[0] == '.' ) {
            /* relative to current dir */
            char buff[PATH_MAX];
            if ( getcwd(buff, sizeof buff) != NULL ) {
                printf( "Rel Path=%s/%s\n", buff, progname );
            } else {
                fprintf( stderr, "Can't getcwd\n" );
            }
        } else
        if ( progname[0] == '/' ) {
            /* absolute path */
            printf( "Abs Path=%s\n", progname );
        } else {
            /* search PATH environment variable */
            char    *env = getenv( "PATH" );
            if ( env != NULL ) {
                char buff[PATH_MAX];
                int  n;
                while ( *env &&
                        sscanf( env, "%[^:]%n", buff, &n ) == 1 ) {
                    FILE *fp;
                    buff[n] = '\0';
                    strcat( buff, "/" );
                    strcat( buff, progname );
                    if ( (fp=fopen(buff,"r")) != NULL ) {
                        fclose(fp);
                        printf( "Env Path=%s\n", buff );
                        break;
                    }
                    env += n;
                    if ( *env == ':' ) env++;
                }
            } else {
                fprintf( stderr, "Can't getenv\n" );
            }
        }
    }
    
    int main ( int argc, char *argv[] ) {
        finder(argv[0]);
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21