Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / emu / misc.c
blob21954ed50d209d76504153aa74e0e80291168d9d
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <config-util.h>
20 #include <config.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
36 #include <grub/mm.h>
37 #include <grub/err.h>
38 #include <grub/env.h>
39 #include <grub/types.h>
40 #include <grub/misc.h>
41 #include <grub/i18n.h>
42 #include <grub/time.h>
43 #include <grub/emu/misc.h>
45 #ifdef HAVE_DEVICE_MAPPER
46 # include <libdevmapper.h>
47 #endif
49 #ifdef HAVE_SYS_PARAM_H
50 # include <sys/param.h>
51 #endif
53 #ifdef HAVE_SYS_MOUNT_H
54 # include <sys/mount.h>
55 #endif
57 #ifdef HAVE_SYS_MNTTAB_H
58 # include <stdio.h> /* Needed by sys/mnttab.h. */
59 # include <sys/mnttab.h>
60 #endif
62 #ifdef HAVE_SYS_MKDEV_H
63 # include <sys/mkdev.h> /* makedev */
64 #endif
66 int verbosity;
68 void
69 grub_util_warn (const char *fmt, ...)
71 va_list ap;
73 fprintf (stderr, _("%s: warning:"), program_name);
74 fprintf (stderr, " ");
75 va_start (ap, fmt);
76 vfprintf (stderr, fmt, ap);
77 va_end (ap);
78 fprintf (stderr, ".\n");
79 fflush (stderr);
82 void
83 grub_util_info (const char *fmt, ...)
85 if (verbosity > 0)
87 va_list ap;
89 fprintf (stderr, _("%s: info:"), program_name);
90 fprintf (stderr, " ");
91 va_start (ap, fmt);
92 vfprintf (stderr, fmt, ap);
93 va_end (ap);
94 fprintf (stderr, ".\n");
95 fflush (stderr);
99 void
100 grub_util_error (const char *fmt, ...)
102 va_list ap;
104 fprintf (stderr, _("%s: error:"), program_name);
105 fprintf (stderr, " ");
106 va_start (ap, fmt);
107 vfprintf (stderr, fmt, ap);
108 va_end (ap);
109 fprintf (stderr, ".\n");
110 exit (1);
113 void *
114 xmalloc (grub_size_t size)
116 void *p;
118 p = malloc (size);
119 if (! p)
120 grub_util_error ("%s", _("out of memory"));
122 return p;
125 void *
126 xrealloc (void *ptr, grub_size_t size)
128 ptr = realloc (ptr, size);
129 if (! ptr)
130 grub_util_error ("%s", _("out of memory"));
132 return ptr;
135 char *
136 xstrdup (const char *str)
138 size_t len;
139 char *newstr;
141 len = strlen (str);
142 newstr = (char *) xmalloc (len + 1);
143 memcpy (newstr, str, len + 1);
145 return newstr;
148 #ifndef HAVE_VASPRINTF
151 vasprintf (char **buf, const char *fmt, va_list ap)
153 /* Should be large enough. */
154 *buf = xmalloc (512);
156 return vsnprintf (*buf, 512, fmt, ap);
159 #endif
161 #ifndef HAVE_ASPRINTF
164 asprintf (char **buf, const char *fmt, ...)
166 int status;
167 va_list ap;
169 va_start (ap, fmt);
170 status = vasprintf (buf, fmt, ap);
171 va_end (ap);
173 return status;
176 #endif
178 char *
179 xasprintf (const char *fmt, ...)
181 va_list ap;
182 char *result;
184 va_start (ap, fmt);
185 if (vasprintf (&result, fmt, ap) < 0)
187 if (errno == ENOMEM)
188 grub_util_error ("%s", _("out of memory"));
189 return NULL;
192 return result;
195 void
196 grub_exit (void)
198 exit (1);
201 grub_uint64_t
202 grub_get_time_ms (void)
204 struct timeval tv;
206 gettimeofday (&tv, 0);
208 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
211 grub_uint32_t
212 grub_get_rtc (void)
214 struct timeval tv;
216 gettimeofday (&tv, 0);
218 return (tv.tv_sec * GRUB_TICKS_PER_SECOND
219 + (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
220 * GRUB_TICKS_PER_SECOND / 1000000));
223 char *
224 canonicalize_file_name (const char *path)
226 char *ret;
227 #ifdef __MINGW32__
228 ret = xmalloc (PATH_MAX);
229 if (!_fullpath (ret, path, PATH_MAX))
230 return NULL;
231 #elif defined (PATH_MAX)
232 ret = xmalloc (PATH_MAX);
233 if (!realpath (path, ret))
234 return NULL;
235 #else
236 ret = realpath (path, NULL);
237 #endif
238 return ret;
241 #ifdef HAVE_DEVICE_MAPPER
242 static void device_mapper_null_log (int level __attribute__ ((unused)),
243 const char *file __attribute__ ((unused)),
244 int line __attribute__ ((unused)),
245 int dm_errno __attribute__ ((unused)),
246 const char *f __attribute__ ((unused)),
247 ...)
252 grub_device_mapper_supported (void)
254 static int supported = -1;
256 if (supported == -1)
258 struct dm_task *dmt;
260 /* Suppress annoying log messages. */
261 dm_log_with_errno_init (&device_mapper_null_log);
263 dmt = dm_task_create (DM_DEVICE_VERSION);
264 supported = (dmt != NULL);
265 if (dmt)
266 dm_task_destroy (dmt);
268 /* Restore the original logger. */
269 dm_log_with_errno_init (NULL);
272 return supported;
274 #endif /* HAVE_DEVICE_MAPPER */