misc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 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 <errno.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <grub/mm.h>
  28. #include <grub/err.h>
  29. #include <grub/env.h>
  30. #include <grub/types.h>
  31. #include <grub/misc.h>
  32. #include <grub/i18n.h>
  33. #include <grub/time.h>
  34. #include <grub/emu/misc.h>
  35. int verbosity;
  36. void
  37. grub_util_warn (const char *fmt, ...)
  38. {
  39. va_list ap;
  40. fprintf (stderr, _("%s: warning:"), program_name);
  41. fprintf (stderr, " ");
  42. va_start (ap, fmt);
  43. vfprintf (stderr, fmt, ap);
  44. va_end (ap);
  45. fprintf (stderr, ".\n");
  46. fflush (stderr);
  47. }
  48. void
  49. grub_util_info (const char *fmt, ...)
  50. {
  51. if (verbosity > 0)
  52. {
  53. va_list ap;
  54. fprintf (stderr, _("%s: info:"), program_name);
  55. fprintf (stderr, " ");
  56. va_start (ap, fmt);
  57. vfprintf (stderr, fmt, ap);
  58. va_end (ap);
  59. fprintf (stderr, ".\n");
  60. fflush (stderr);
  61. }
  62. }
  63. void
  64. grub_util_error (const char *fmt, ...)
  65. {
  66. va_list ap;
  67. fprintf (stderr, _("%s: error:"), program_name);
  68. fprintf (stderr, " ");
  69. va_start (ap, fmt);
  70. vfprintf (stderr, fmt, ap);
  71. va_end (ap);
  72. fprintf (stderr, ".\n");
  73. exit (1);
  74. }
  75. void *
  76. xmalloc (grub_size_t size)
  77. {
  78. void *p;
  79. p = malloc (size);
  80. if (! p)
  81. grub_util_error ("%s", _("out of memory"));
  82. return p;
  83. }
  84. void *
  85. xrealloc (void *ptr, grub_size_t size)
  86. {
  87. ptr = realloc (ptr, size);
  88. if (! ptr)
  89. grub_util_error ("%s", _("out of memory"));
  90. return ptr;
  91. }
  92. char *
  93. xstrdup (const char *str)
  94. {
  95. size_t len;
  96. char *newstr;
  97. len = strlen (str);
  98. newstr = (char *) xmalloc (len + 1);
  99. memcpy (newstr, str, len + 1);
  100. return newstr;
  101. }
  102. #if !defined (GRUB_MKFONT) && !defined (GRUB_BUILD)
  103. char *
  104. xasprintf (const char *fmt, ...)
  105. {
  106. va_list ap;
  107. char *result;
  108. va_start (ap, fmt);
  109. result = grub_xvasprintf (fmt, ap);
  110. va_end (ap);
  111. if (!result)
  112. grub_util_error ("%s", _("out of memory"));
  113. return result;
  114. }
  115. #endif
  116. #if !defined (GRUB_MACHINE_EMU) || defined (GRUB_UTIL)
  117. void
  118. grub_exit (void)
  119. {
  120. exit (1);
  121. }
  122. #endif
  123. grub_uint64_t
  124. grub_get_time_ms (void)
  125. {
  126. struct timeval tv;
  127. gettimeofday (&tv, 0);
  128. return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
  129. }
  130. size_t
  131. grub_util_get_image_size (const char *path)
  132. {
  133. FILE *f;
  134. size_t ret;
  135. off_t sz;
  136. f = grub_util_fopen (path, "rb");
  137. if (!f)
  138. grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
  139. fseeko (f, 0, SEEK_END);
  140. sz = ftello (f);
  141. if (sz < 0)
  142. grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
  143. if (sz != (size_t) sz)
  144. grub_util_error (_("file `%s' is too big"), path);
  145. ret = (size_t) sz;
  146. fclose (f);
  147. return ret;
  148. }
  149. void
  150. grub_util_load_image (const char *path, char *buf)
  151. {
  152. FILE *fp;
  153. size_t size;
  154. grub_util_info ("reading %s", path);
  155. size = grub_util_get_image_size (path);
  156. fp = grub_util_fopen (path, "rb");
  157. if (! fp)
  158. grub_util_error (_("cannot open `%s': %s"), path,
  159. strerror (errno));
  160. if (fread (buf, 1, size, fp) != size)
  161. grub_util_error (_("cannot read `%s': %s"), path,
  162. strerror (errno));
  163. fclose (fp);
  164. }