grub-mkrelpath.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* grub-mkrelpath.c - make a system path relative to its root */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/emu/misc.h>
  24. #include <grub/i18n.h>
  25. #define _GNU_SOURCE 1
  26. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  27. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  28. #include <argp.h>
  29. #pragma GCC diagnostic error "-Wmissing-prototypes"
  30. #pragma GCC diagnostic error "-Wmissing-declarations"
  31. #include "progname.h"
  32. struct arguments
  33. {
  34. char *pathname;
  35. };
  36. static struct argp_option options[] = {
  37. { 0, 0, 0, 0, 0, 0 }
  38. };
  39. static error_t
  40. argp_parser (int key, char *arg, struct argp_state *state)
  41. {
  42. /* Get the input argument from argp_parse, which we
  43. know is a pointer to our arguments structure. */
  44. struct arguments *arguments = state->input;
  45. switch (key)
  46. {
  47. case ARGP_KEY_ARG:
  48. if (state->arg_num == 0)
  49. arguments->pathname = xstrdup (arg);
  50. else
  51. {
  52. /* Too many arguments. */
  53. fprintf (stderr, _("Unknown extra argument `%s'."), arg);
  54. fprintf (stderr, "\n");
  55. argp_usage (state);
  56. }
  57. break;
  58. case ARGP_KEY_NO_ARGS:
  59. fprintf (stderr, "%s", _("No path is specified.\n"));
  60. argp_usage (state);
  61. exit (1);
  62. break;
  63. default:
  64. return ARGP_ERR_UNKNOWN;
  65. }
  66. return 0;
  67. }
  68. static struct argp argp = {
  69. options, argp_parser, N_("PATH"),
  70. N_("Transform a system filename into GRUB one."),
  71. NULL, NULL, NULL
  72. };
  73. int
  74. main (int argc, char *argv[])
  75. {
  76. char *relpath;
  77. struct arguments arguments;
  78. grub_util_host_init (&argc, &argv);
  79. memset (&arguments, 0, sizeof (struct arguments));
  80. /* Check for options. */
  81. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  82. {
  83. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  84. exit(1);
  85. }
  86. relpath = grub_make_system_path_relative_to_its_root (arguments.pathname);
  87. printf ("%s\n", relpath);
  88. free (relpath);
  89. return 0;
  90. }