-/* fn_normalize(): This very OS-dependent routine canonicalizes
- file name FN1. The file name should not need to be the name of an
- existing file. Returns a malloc()'d copy of the canonical name.
- This function must always succeed; if it needs to bail out then it
- should return xstrdup(FN1). */
-#ifdef unix
-char *
-fn_normalize (const char *file_name)
-{
- const char *src;
- char *fn1, *fn2, *dest;
- int maxlen;
-
- if (fn_is_special (file_name))
- return xstrdup (file_name);
-
- fn1 = fn_tilde_expand (file_name);
-
- /* Follow symbolic links. */
- for (;;)
- {
- fn2 = fn1;
- fn1 = fn_readlink (fn1);
- if (!fn1)
- {
- fn1 = fn2;
- break;
- }
- free (fn2);
- }
-
- maxlen = strlen (fn1) * 2;
- if (maxlen < 31)
- maxlen = 31;
- dest = fn2 = xmalloc (maxlen + 1);
- src = fn1;
-
- if (*src == '/')
- *dest++ = *src++;
- else
- {
- errno = 0;
- while (getcwd (dest, maxlen - (dest - fn2)) == NULL && errno == ERANGE)
- {
- maxlen *= 2;
- dest = fn2 = xrealloc (fn2, maxlen + 1);
- errno = 0;
- }
- if (errno)
- {
- free (fn1);
- free (fn2);
- return NULL;
- }
- dest = strchr (fn2, '\0');
- if (dest - fn2 >= maxlen)
- {
- int ofs = dest - fn2;
- maxlen *= 2;
- fn2 = xrealloc (fn2, maxlen + 1);
- dest = fn2 + ofs;
- }
- if (dest[-1] != '/')
- *dest++ = '/';
- }
-
- for (;;)
- {
- int c, f;
-
- c = *src++;
-
- f = 0;
- if (c == '/' || c == 0)
- {
- /* remove `./', `../' from directory */
- if (dest[-1] == '.' && dest[-2] == '/')
- dest--;
- else if (dest[-1] == '.' && dest[-2] == '.' && dest[-3] == '/')
- {
- dest -= 3;
- if (dest == fn2)
- dest++;
- while (dest[-1] != '/')
- dest--;
- }
- else if (dest[-1] != '/') /* remove extra slashes */
- f = 1;
-
- if (c == 0)
- {
- if (dest[-1] == '/' && dest > fn2 + 1)
- dest--;
- *dest = 0;
- free (fn1);
-
- return xrealloc (fn2, strlen (fn2) + 1);
- }
- }
- else
- f = 1;
-
- if (f)
- {
- if (dest - fn2 >= maxlen)
- {
- int ofs = dest - fn2;
- maxlen *= 2;
- fn2 = xrealloc (fn2, maxlen + 1);
- dest = fn2 + ofs;
- }
- *dest++ = c;
- }
- }
-}
-#elif defined (__WIN32__)
-char *
-fn_normalize (const char *fn1)
-{
- DWORD len;
- DWORD success;
- char *fn2;
-
- /* Don't change special file names. */
- if (is_special_file_name (file_name))
- return xstrdup (file_name);
-
- /* First find the required buffer length. */
- len = GetFullPathName (fn1, 0, NULL, NULL);
- if (!len)
- {
- fn2 = xstrdup (fn1);
- return fn2;
- }
-
- /* Then make a buffer that big. */
- fn2 = xmalloc (len);
- success = GetFullPathName (fn1, len, fn2, NULL);
- if (success >= len || success == 0)
- {
- free (fn2);
- fn2 = xstrdup (fn1);
- return fn2;
- }
- return fn2;
-}
-#elif __BORLANDC__
-char *
-fn_normalize (const char *fn1)
-{
- char *fn2 = _fullpath (NULL, fn1, 0);
- if (fn2)
- {
- char *cp;
- for (cp = fn2; *cp; cp++)
- *cp = toupper ((unsigned char) (*cp));
- return fn2;
- }
- return xstrdup (fn1);
-}
-#elif __DJGPP__
-char *
-fn_normalize (const char *fn1)
-{
- char *fn2 = xmalloc (1024);
- _fixpath (fn1, fn2);
- fn2 = xrealloc (fn2, strlen (fn2) + 1);
- return fn2;
-}
-#else /* not Lose32, Unix, or DJGPP */
-char *
-fn_normalize (const char *fn)
-{
- return xstrdup (fn);
-}
-#endif /* not Lose32, Unix, or DJGPP */
-