grub-mkrelpath.c 2.64 KB
Newer Older
1 2 3
/* grub-mkrelpath.c - make a system path relative to its root */
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2009,2010 Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

20 21
#include <config.h>

22
#include <stdio.h>
23
#include <string.h>
24
#include <grub/util/misc.h>
BVK Chaitanya's avatar
BVK Chaitanya committed
25
#include <grub/emu/misc.h>
26
#include <grub/i18n.h>
27 28

#define _GNU_SOURCE	1
29 30
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
31
#include <argp.h>
32 33
#pragma GCC diagnostic error "-Wmissing-prototypes"
#pragma GCC diagnostic error "-Wmissing-declarations"
34

35 36
#include "progname.h"

37 38 39 40 41 42 43 44
struct arguments
{
  char *pathname;
};

static struct argp_option options[] = {
  { 0, 0, 0, 0, 0, 0 }
};
45

46 47
static error_t
argp_parser (int key, char *arg, struct argp_state *state)
48
{
49 50 51 52 53 54 55 56 57 58 59 60
  /* Get the input argument from argp_parse, which we
     know is a pointer to our arguments structure. */
  struct arguments *arguments = state->input;

  switch (key)
    {
    case ARGP_KEY_ARG:
      if (state->arg_num == 0)
	arguments->pathname = xstrdup (arg);
      else
	{
	  /* Too many arguments. */
61 62
	  fprintf (stderr, _("Unknown extra argument `%s'."), arg);
	  fprintf (stderr, "\n");
63 64 65 66 67 68 69 70 71 72 73 74
	  argp_usage (state);
	}
      break;
    case ARGP_KEY_NO_ARGS:
      fprintf (stderr, "%s", _("No path is specified.\n"));
      argp_usage (state);
      exit (1);
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
75 76
}

77 78
static struct argp argp = {
  options, argp_parser, N_("PATH"),
79
  N_("Transform a system filename into GRUB one."),
80 81 82
  NULL, NULL, NULL
};

83 84 85
int
main (int argc, char *argv[])
{
86 87
  char *relpath;
  struct arguments arguments;
88

89
  grub_util_host_init (&argc, &argv);
90

91
  memset (&arguments, 0, sizeof (struct arguments));
92

93 94
  /* Check for options.  */
  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
95
    {
96 97
      fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
      exit(1);
98 99
    }

100
  relpath = grub_make_system_path_relative_to_its_root (arguments.pathname);
101 102 103 104 105
  printf ("%s\n", relpath);
  free (relpath);

  return 0;
}