grub-mkrelpath.c 2.3 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 <grub/util/misc.h>
  20. #include <grub/i18n.h>
  21. #include <getopt.h>
  22. #include "progname.h"
  23. static struct option options[] =
  24. {
  25. {"help", no_argument, 0, 'h'},
  26. {"version", no_argument, 0, 'V'},
  27. {0, 0, 0, 0},
  28. };
  29. static void
  30. usage (int status)
  31. {
  32. if (status)
  33. fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
  34. else
  35. printf ("\
  36. Usage: %s [OPTIONS] PATH\n\
  37. \n\
  38. Make a system path relative to it's root.\n\
  39. \n\
  40. Options:\n\
  41. -h, --help display this message and exit\n\
  42. -V, --version print version information and exit\n\
  43. \n\
  44. Report bugs to <%s>.\n", program_name, PACKAGE_BUGREPORT);
  45. exit (status);
  46. }
  47. int
  48. main (int argc, char *argv[])
  49. {
  50. char *argument, *relpath;
  51. set_program_name (argv[0]);
  52. grub_util_init_nls ();
  53. /* Check for options. */
  54. while (1)
  55. {
  56. int c = getopt_long (argc, argv, "hV", options, 0);
  57. if (c == -1)
  58. break;
  59. else
  60. switch (c)
  61. {
  62. case 'h':
  63. usage (0);
  64. break;
  65. case 'V':
  66. printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  67. return 0;
  68. default:
  69. usage (1);
  70. break;
  71. }
  72. }
  73. if (optind >= argc)
  74. {
  75. fprintf (stderr, "No path is specified.\n");
  76. usage (1);
  77. }
  78. if (optind + 1 != argc)
  79. {
  80. fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
  81. usage (1);
  82. }
  83. argument = argv[optind];
  84. relpath = make_system_path_relative_to_its_root (argument);
  85. printf ("%s\n", relpath);
  86. free (relpath);
  87. return 0;
  88. }