cmdline.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* cmdline.c - linux command line handling */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/lib/cmdline.h>
  20. #include <grub/misc.h>
  21. static unsigned int check_arg (char *c, int *has_space)
  22. {
  23. int space = 0;
  24. unsigned int size = 0;
  25. while (*c)
  26. {
  27. if (*c == '\\' || *c == '\'' || *c == '"')
  28. size++;
  29. else if (*c == ' ')
  30. space = 1;
  31. size++;
  32. c++;
  33. }
  34. if (space)
  35. size += 2;
  36. if (has_space)
  37. *has_space = space;
  38. return size;
  39. }
  40. unsigned int grub_loader_cmdline_size (int argc, char *argv[])
  41. {
  42. int i;
  43. unsigned int size = 0;
  44. for (i = 0; i < argc; i++)
  45. {
  46. size += check_arg (argv[i], 0);
  47. size++; /* Separator space or NULL. */
  48. }
  49. if (size == 0)
  50. size = 1;
  51. return size;
  52. }
  53. grub_err_t
  54. grub_create_loader_cmdline (int argc, char *argv[], char *buf,
  55. grub_size_t size, enum grub_verify_string_type type)
  56. {
  57. int i, space;
  58. unsigned int arg_size;
  59. char *c, *orig_buf = buf;
  60. for (i = 0; i < argc; i++)
  61. {
  62. c = argv[i];
  63. arg_size = check_arg(argv[i], &space);
  64. arg_size++; /* Separator space or NULL. */
  65. if (size < arg_size)
  66. break;
  67. size -= arg_size;
  68. if (space)
  69. *buf++ = '"';
  70. while (*c)
  71. {
  72. if (*c == '\\' || *c == '\'' || *c == '"')
  73. *buf++ = '\\';
  74. *buf++ = *c;
  75. c++;
  76. }
  77. if (space)
  78. *buf++ = '"';
  79. *buf++ = ' ';
  80. }
  81. /* Replace last space with null. */
  82. if (i)
  83. buf--;
  84. *buf = 0;
  85. return grub_verify_string (orig_buf, type);
  86. }