relpath.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/util/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/misc.h>
  24. #include <grub/emu/misc.h>
  25. #include <grub/emu/hostdisk.h>
  26. #include <grub/emu/getroot.h>
  27. #include <grub/charset.h>
  28. #include <grub/util/windows.h>
  29. #include <windows.h>
  30. #include <winioctl.h>
  31. static size_t
  32. tclen (const TCHAR *s)
  33. {
  34. const TCHAR *s0 = s;
  35. while (*s)
  36. s++;
  37. return s - s0;
  38. }
  39. char *
  40. grub_make_system_path_relative_to_its_root (const char *path)
  41. {
  42. TCHAR *dirwindows, *mntpointwindows;
  43. TCHAR *ptr;
  44. size_t offset, flen;
  45. TCHAR *ret;
  46. char *cret;
  47. dirwindows = grub_util_get_windows_path (path);
  48. if (!dirwindows)
  49. return xstrdup (path);
  50. mntpointwindows = grub_get_mount_point (dirwindows);
  51. if (!mntpointwindows)
  52. {
  53. offset = 0;
  54. if (dirwindows[0] && dirwindows[1] == ':')
  55. offset = 2;
  56. }
  57. offset = tclen (mntpointwindows);
  58. free (mntpointwindows);
  59. flen = tclen (dirwindows);
  60. if (offset > flen)
  61. {
  62. offset = 0;
  63. if (dirwindows[0] && dirwindows[1] == ':')
  64. offset = 2;
  65. }
  66. ret = xmalloc (sizeof (ret[0]) * (flen - offset + 2));
  67. if (dirwindows[offset] != '\\'
  68. && dirwindows[offset] != '/'
  69. && dirwindows[offset])
  70. {
  71. ret[0] = '\\';
  72. memcpy (ret + 1, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
  73. }
  74. else
  75. memcpy (ret, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
  76. free (dirwindows);
  77. for (ptr = ret; *ptr; ptr++)
  78. if (*ptr == '\\')
  79. *ptr = '/';
  80. cret = grub_util_tchar_to_utf8 (ret);
  81. free (ret);
  82. return cret;
  83. }