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, ...
- 04-18-2009 #1Just 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.
- 04-18-2009 #2Linux 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.
- 04-18-2009 #3
Big Tom is correct. For instance, you could have, in your configuration file on Windows:
But on Linux:Code:LOGFILE_LOCATION=C:\Documents and Settings\USER\log.txt
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:LOGFILE_LOCATION=/home/USER/log
If your program is written in C or C++, you can do the same by employing preprocessor macros:Code:if(runningOnWindows()) { LOGFILE_LOCATION = "C:\Documents and Settings\USER\log.txt"; /* more initialization */ } else { LOGFILE_LOCATION = "/home/USER/log"; /* more initialization */ }
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
- 04-19-2009 #4Just 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.
- 04-20-2009 #5
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.
- 04-20-2009 #6Linux Guru
- 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!


Reply With Quote