From: Austin Ziegler Date: 2005-09-14T04:45:48+09:00 Subject: Re: File.split edge cases On 9/13/05, Berger, Daniel wrote: >>> # pseudo-code >>> if path.root? or path.empty? >>> return path, "" >>> else >>> return File.dirname(path), File.basename(path) >>> end >> Hmmm. I don't think that's right, still. "ls //" is valid on Unix, so >> the existing behaviour is *correct*, if mildly unnerving. > I don't know that just because "ls" handles it one can conclude that > it's correct behavior on Unix. But, again, without a POSIX document > sitting in front of me, I'm just not sure. My Google skills are > failing me. #include #include #include #include #include #include int main(int argc, char* argv) { struct dirent* entry; DIR* dir = opendir("//tmp"); if (!dir) { fprintf(stderr, "Error opening //tmp (%s).\n", strerror(errno)); return 1; } errno = 0; while (entry = readdir(dir)) { printf("% 40s\n", entry->d_name); } if (errno) { fprintf(stderr, "Error reading //tmp (%s).\n", strerror(errno)); return 2; } if (dir) { closedir(dir); } return 0; } This should always work on POSIX systems, per: http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html See 3.266 specifically. Also: http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html See 4.11 specifically. >> However, on Windows, "dir \\" is not correct just as "dir C:\\" will >> not be correct. Better to specifically check for the UNC-path and >> drive-path cases. Ruby should also be smart enough to handle the \\.\ >> and \\?\ special prefixes. I can write up a summary of what these are >> for and how they should be handled, if that would help. > I think that would be good, along with a series of test cases that > could be incorporated as part of the Ruby distro. I already have > quite a few for File.basename and File.dirname (as part of > win32-file), though I have to admit I didn't test with "\\.\" or > "\\?\", though I do have generic UNC path tests. I'll go through my notes and put something together; it may take a little time, as \\?\C:\ is valid, but \\?\\server\share isn't; it's subtly different for \\.\ and \\?\ use with UNC paths. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca