+ // they are on the same drive, find out how much of the current directory
+ // is in the absolute filename
+ i = ABSOLUTE_NAME_START;
+ while (i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i]) {
+ i++;
+ }
+
+ if (i == cdLen && (absoluteFilename[i] == G_DIR_SEPARATOR || absoluteFilename[i-1] == G_DIR_SEPARATOR)) {
+ // the whole current directory name is in the file name,
+ // so we just trim off the current directory name to get the
+ // current file name.
+ if (absoluteFilename[i] == G_DIR_SEPARATOR) {
+ // a directory name might have a trailing slash but a relative
+ // file name should not have a leading one...
+ i++;
+ }
+
+ strcpy(relativeFilename, &absoluteFilename[i]);
+ return relativeFilename;
+ }
+
+ // The file is not in a child directory of the current directory, so we
+ // need to step back the appropriate number of parent directories by
+ // using "..\"s. First find out how many levels deeper we are than the
+ // common directory
+ afMarker = i;
+ levels = 1;
+
+ // count the number of directory levels we have to go up to get to the
+ // common directory
+ while (i < cdLen) {
+ i++;
+ if (currentDirectory[i] == G_DIR_SEPARATOR) {
+ // make sure it's not a trailing slash
+ i++;
+ if (currentDirectory[i] != '\0') {
+ levels++;
+ }
+ }
+ }
+
+ // move the absolute filename marker back to the start of the directory name
+ // that it has stopped in.
+ while (afMarker > 0 && absoluteFilename[afMarker-1] != G_DIR_SEPARATOR) {
+ afMarker--;
+ }
+
+ // check that the result will not be too long
+ if (levels * 3 + afLen - afMarker > MAXPATHLEN) {
+ return NULL;
+ }
+
+ // add the appropriate number of "..\"s.
+ rfMarker = 0;
+ for (i = 0; i < levels; i++) {
+ relativeFilename[rfMarker++] = '.';
+ relativeFilename[rfMarker++] = '.';
+ relativeFilename[rfMarker++] = G_DIR_SEPARATOR;
+ }
+
+ // copy the rest of the filename into the result string
+ strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);
+
+ return relativeFilename;