the legacy of dos

Let me just say that developing cross-platform software is one of the more royal pains out there. Java, built from the ground up with cross-platform in mind does a better job than most to maintain a level of abstraction for the developer. However, a universal sticking point is what character the directory separator is.

Unix, the Web, and the rest of the sane world use ‘/’ to separate directories. This makes sense, since in most modern (hell, nearly all) programming languages, the ‘\’ is used as a prefix to an escape character. So the following looks acceptable:

char * file = "/home/mark/input.txt";

However, Windows, tipping its hat to is DOS legacy, has retained the ‘\’ for directory separation, much to the consternation of the entire software community. Here is a very interesting take on why DOS even has this legacy. Bottom line — unlike the other OS of the day (unix), DOS didn’t support directories… it just had one root filesystem. This led the clever developers to use the ‘/’ character as the command line switch — like “xcopy /Y”. On Unix-like systems, “-” or “–” is used — “cp -R *.txt”

The result is, that if your platform is windows, the above code turns into:

char * file = "C:\\home\\mark\\input.txt";

You might think, “Hey that’s not that bad…” However, if you need to make it cross platform, you end up with something like this (Using a C++ pseudo code example)

#ifdef UNIX
#define DIRSEP "/"
#define ROOT "/"
#else
#define ROOT "C:\\"
#define DIRSEP "/"
#endif

ostringstream ostr;
ostr << ROOT << "home" << DIRSEP << "mark" << DIRSEP << "input.txt";

Bottom line… ugliness. All programming languages have to deal with this in some form. There are limitations to the levels of abstractions that even managed code can give you. Inevitably, the abstraction crumbles when you hit the filesystem — which is not nearly as complicated as some other OS coupled features.

I had always hoped that Java would come out with a universal “language” (if you will) to access the filesystem that was OS independent. For a language that is always touted as a “write once, run anywhere” language, the filesystem presents a tough hurdle early on.

  • Another note, Java gives you no help on what an absolute path will look like (/ for unix and C:\ for windows). I agree that paths should not be hard coded. However in order to manipulate paths (especially absolute) given at run time, the program can't do it without some knowledge of OS context.

    If you look at the code of a substantial cross-platform Java application (like Azureurs), you find countless numbers of switch or if statements around what OS you are running. Granted many of those are found in the GUI code, but the result is any time Java has to reach out of its own VM, it must become OS context sensitive.

    This doesn't prevent Java from being "write once, run anywhere," it just makes for more ugliness than I would like.
  • I know Java has more facilities for this than most languages. However given the abysmal shell support windows has compared to unix or linux counterparts, any reliance on shell constructs is usually a death-knell for cross-platform code. There isn't a built-in equivalent in C++ and the equivalent in perl is cumbersome.

    Its more a rant that given the ubiquitousness of the web, Microsoft hasn't adopted a layout similar to the web/*nix (more because of the web than anything else). I don't know if its stubbornness or compatibility (my guess is a bit of both). Possibly their database-like file system that was originally part of Longhorn would have done this...

    I also think given the state of virtualization, Microsoft should have more power to make sweeping improvements like this, given you should be able to maintain backwards compatibility with a VM embedded in the OS.
  • In Java, use File.pathSeparator or File.pathSeparatorChar. You shouldn't hardcode paths anyway. Construct paths at runtime.

    Also check out System.getProperty (keys).

    Also, Windows will accept forward-slashed paths. You can also do some tricks like... cd /d %AppData% to move around a file system from the command prompt. Use set to see what your paths are.
blog comments powered by Disqus