Find the answer to your Linux question:
Results 1 to 6 of 6
I'm recompiling my Windows based software to Linux, and the drive letters are becoming a great hurdle. Many files are accessed with their Drive letters in the program. For example, ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    2

    Emulate DOS/Windows Drive Letters

    I'm recompiling my Windows based software to Linux, and the drive letters are becoming a great hurdle.

    Many files are accessed with their Drive letters in the program. For example, O:REPORT.TXT, F:\DATA\CUSTLIST.CSV etc.,

    I'm able to recompile it perfectly in Linux. But I need to change everywhere the program is explicitly specifiying the path. If I change the path, then it will cease to work with Windows, unless it is controlled by an IF.

    I'm not using Wine - my code has been natively compiled into Linux. So, other than changing my code, is there any way to 'emulate' drive letters in Linux?

    Thanks.

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    It sounds like you are using a lot of hard references in your program which is a bad idea on many levels. I would suggest better use of variables and probably use a config file in conjunction with it to assign locations. That way you can share more of the code base without having to stretch to hacks and workarounds and it will also benefit you in the future should you move any of your reports in that you won't need to recompile.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Big Tom is correct. For instance, you could have, in your configuration file on Windows:
    Code:
    LOGFILE_LOCATION=C:\Documents and Settings\USER\log.txt
    But on Linux:
    Code:
    LOGFILE_LOCATION=/home/USER/log
    If for some reason it must be in the program, and may not be in a configuration file, you have two avenues depending on your language. Most of the higher-level languages have a way of detecting what operating system is being used, so you could begin your program with:
    Code:
    if(runningOnWindows())
    {
        LOGFILE_LOCATION = "C:\Documents and Settings\USER\log.txt";
        /* more initialization */
    }
    else
    {
        LOGFILE_LOCATION = "/home/USER/log";
        /* more initialization */
    }
    If your program is written in C or C++, you can do the same by employing preprocessor macros:
    Code:
    #ifdef _WIN32
    #define LOGFILE_LOCATION "C:\Documents and Settings\USER\log.txt"
    #else
    #define LOGFILE_LOCATION "/home/USER/log"
    #endif
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Apr 2009
    Posts
    2
    Thank you Cabhan.

    I've an existing codebase that is very large and has too many direct references to files in different paths. I've been looking to a solution where I can 'emulate' Windows like path in Linux so that I need not change the code.

    Guess there is no way than to change the code.. Your code snippet has been very useful. Thank you.

  5. #5
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,114
    I am not a programmer, so I wouldn't know how to
    implement this. File operations are handled by standard
    library functions. Say, you write small substitute functions
    that you include instead of the standard ones.

    Your function accepts the Microsoft type paths as arguments,
    translates them to Unix style paths, and then invokes the
    standard function.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You should note that Windows posix C API's that you would also us in Linux (open(), close(), etc) will take Unix paths with the forward slash delimiter. So, first you want to change all the back-slashes to forward-slashes. Second, the major issue is the drive designator. What I do is to build the path strings on the fly using a const char* for a prefix which will resolve to "c:" or "d:" on Windows, and "" on Linux/Unix, so I do something like this:

    char pathbuf[256];
    sprintf(pathbuf, "%s%s", DrivePrefix, filepath);
    fd = open(pathbuf, options);

    This is just a crude example, but you get the gist I hope.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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