grub/kern/emu/misc.c
2010-04-27 20:55:12 +05:30

173 lines
2.6 KiB
C

#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/env.h>
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/i18n.h>
#include <grub/time.h>
#include <grub/emu/misc.h>
int verbosity;
void
grub_util_warn (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, _("%s: warn:"), program_name);
fprintf (stderr, " ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ".\n");
fflush (stderr);
}
void
grub_util_info (const char *fmt, ...)
{
if (verbosity > 0)
{
va_list ap;
fprintf (stderr, _("%s: info:"), program_name);
fprintf (stderr, " ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ".\n");
fflush (stderr);
}
}
void
grub_util_error (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, _("%s: error:"), program_name);
fprintf (stderr, " ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ".\n");
exit (1);
}
void *
xmalloc (grub_size_t size)
{
void *p;
p = malloc (size);
if (! p)
grub_util_error ("out of memory");
return p;
}
void *
xrealloc (void *ptr, grub_size_t size)
{
ptr = realloc (ptr, size);
if (! ptr)
grub_util_error ("out of memory");
return ptr;
}
char *
xstrdup (const char *str)
{
size_t len;
char *newstr;
len = strlen (str);
newstr = (char *) xmalloc (len + 1);
memcpy (newstr, str, len + 1);
return newstr;
}
#ifndef HAVE_VASPRINTF
int
vasprintf (char **buf, const char *fmt, va_list ap)
{
/* Should be large enough. */
*buf = xmalloc (512);
return vsprintf (*buf, fmt, ap);
}
#endif
#ifndef HAVE_ASPRINTF
int
asprintf (char **buf, const char *fmt, ...)
{
int status;
va_list ap;
va_start (ap, fmt);
status = vasprintf (*buf, fmt, ap);
va_end (ap);
return status;
}
#endif
char *
xasprintf (const char *fmt, ...)
{
va_list ap;
char *result;
va_start (ap, fmt);
if (vasprintf (&result, fmt, ap) < 0)
{
if (errno == ENOMEM)
grub_util_error ("out of memory");
return NULL;
}
return result;
}
void
grub_exit (void)
{
exit (1);
}
grub_uint64_t
grub_get_time_ms (void)
{
struct timeval tv;
gettimeofday (&tv, 0);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
grub_uint32_t
grub_get_rtc (void)
{
struct timeval tv;
gettimeofday (&tv, 0);
return (tv.tv_sec * GRUB_TICKS_PER_SECOND
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
* GRUB_TICKS_PER_SECOND / 1000000));
}