/* misc.c - definitions of misc functions */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* 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/>.
*/
#include <stdarg.h>
static int
static int
grub_iswordseparator (int c)
{
}
/* grub_gettext_dummy is not translating anything. */
static const char *
grub_gettext_dummy (const char *s)
{
return s;
}
void *
{
char *d = (char *) dest;
const char *s = (const char *) src;
if (d < s)
while (n--)
*d++ = *s++;
else
{
d += n;
s += n;
while (n--)
*--d = *--s;
}
return dest;
}
/* GCC emits references to memcpy() for struct copies etc. */
#else
{
}
{
}
#endif
char *
{
char *p = dest;
while ((*p++ = *src++) != '\0')
;
return dest;
}
char *
{
char *p = dest;
while ((*p++ = *src++) != '\0' && --c)
;
return dest;
}
char *
{
char *d = dest;
const char *s = src;
do
*d++ = *s;
while (*s++ != '\0');
return d - 1;
}
int
{
int ret;
return ret;
}
int
{
int ret;
return ret;
}
int
grub_puts_ (const char *s)
{
return grub_puts (_(s));
}
int
{
int ret;
return ret;
}
#endif
int grub_err_printf (const char *fmt, ...)
#endif
void
const char *fmt, ...)
{
if (! debug)
return;
{
grub_refresh ();
}
}
int
{
grub_size_t s;
if (s > PREALLOC_SIZE)
{
if (!curbuf)
{
buf[PREALLOC_SIZE] = 0;
}
else
}
grub_xputs (curbuf);
return s;
}
int
{
while (n--)
{
t1++;
t2++;
}
return 0;
}
#else
{
}
#endif
int
{
{
break;
s1++;
s2++;
}
}
int
{
if (n == 0)
return 0;
{
break;
s1++;
s2++;
}
}
char *
grub_strchr (const char *s, int c)
{
do
{
if (*s == c)
return (char *) s;
}
while (*s++);
return 0;
}
char *
grub_strrchr (const char *s, int c)
{
char *p = NULL;
do
{
if (*s == c)
p = (char *) s;
}
while (*s++);
return p;
}
int
{
while (grub_iswordseparator (*haystack))
haystack++;
while (*haystack)
{
/* Crawl both the needle and the haystack word we're on. */
{
haystack++;
n_pos++;
}
/* If we reached the end of both words at the same time, the word
is found. If not, eat everything in the haystack that isn't the
next word (or the end of string) and "reset" the needle. */
return 1;
else
{
haystack++;
while (grub_iswordseparator (*haystack))
haystack++;
}
}
return 0;
}
int
grub_isspace (int c)
{
return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
}
int
grub_isprint (int c)
{
return (c >= ' ' && c <= '~');
}
unsigned long
{
unsigned long long num;
{
return ~0UL;
}
return (unsigned long) num;
}
unsigned long long
{
unsigned long long num = 0;
int found = 0;
/* Skip white spaces. */
str++;
/* Guess the base, if not specified. The prefix `0x' means 16, and
the prefix `0' means 8. */
if (str[0] == '0')
{
{
{
base = 16;
str += 2;
}
}
base = 8;
}
if (base == 0)
base = 10;
while (*str)
{
unsigned long digit;
if (digit > 9)
{
break;
}
found = 1;
/* NUM * BASE + DIGIT > ~0ULL */
{
return ~0ULL;
}
str++;
}
if (! found)
{
return 0;
}
if (end)
return num;
}
char *
grub_strdup (const char *s)
{
char *p;
p = (char *) grub_malloc (len);
if (! p)
return 0;
return grub_memcpy (p, s, len);
}
char *
{
char *p;
len = grub_strlen (s);
if (len > n)
len = n;
if (! p)
return 0;
grub_memcpy (p, s, len);
p[len] = '\0';
return p;
}
void *
{
void *p = s;
if (len >= 3 * sizeof (unsigned long))
{
unsigned long patternl = 0;
grub_size_t i;
for (i = 0; i < sizeof (unsigned long); i++)
{
*(grub_uint8_t *) p = pattern8;
p = (grub_uint8_t *) p + 1;
len--;
}
while (len >= sizeof (unsigned long))
{
*(unsigned long *) p = patternl;
p = (unsigned long *) p + 1;
len -= sizeof (unsigned long);
}
}
while (len > 0)
{
*(grub_uint8_t *) p = pattern8;
p = (grub_uint8_t *) p + 1;
len--;
}
return s;
}
#ifndef APPLE_CC
#ifdef __sun__
#else
void *memset (void *s, int c, grub_size_t n)
#endif
#else
{
return grub_memset (s, c, n);
}
#endif
grub_strlen (const char *s)
{
const char *p = s;
while (*p)
p++;
return p - s;
}
static inline void
{
while (str < p)
{
char tmp;
*str = *p;
*p = tmp;
str++;
p--;
}
}
/* Divide N by D, return the quotient, and store the remainder in *R. */
{
/* This algorithm is typically implemented by hardware. The idea
is to get the highest bit in N, 64 times, by keeping
upper(N * 2^i) = (Q * D + M), where upper
represents the high 64 bits in 128-bits space. */
grub_uint64_t q = 0;
grub_uint64_t m = 0;
/* Skip the slow computation if 32-bit arithmetic is possible. */
if (n < 0xffffffff && d < 0xffffffff)
{
if (r)
*r = ((grub_uint32_t) n) % (grub_uint32_t) d;
return ((grub_uint32_t) n) / (grub_uint32_t) d;
}
while (bits--)
{
m <<= 1;
if (n & (1ULL << 63))
m |= 1;
q <<= 1;
n <<= 1;
if (m >= d)
{
q |= 1;
m -= d;
}
}
if (r)
*r = m;
return q;
}
/* Convert a long long value to a string. This function avoids 64-bit
modular arithmetic or divisions. */
static char *
{
char *p;
if ((long long) n < 0 && c == 'd')
{
n = (unsigned long long) (-((long long) n));
*str++ = '-';
}
p = str;
if (base == 16)
do
{
unsigned d = (unsigned) (n & 0xf);
*p++ = (d > 9) ? d + 'a' - 10 : d + '0';
}
while (n >>= 4);
else
/* BASE == 10 */
do
{
n = grub_divmod64 (n, 10, &m);
*p++ = m + '0';
}
while (n);
*p = 0;
grub_reverse (str);
return p;
}
static int
{
char c;
grub_size_t n = 0;
const char *fmt;
auto void write_char (unsigned char ch);
auto void write_str (const char *s);
auto void write_fill (const char ch, int n);
{
count++;
}
void write_str (const char *s)
{
while (*s)
write_char (*s++);
}
{
int i;
for (i = 0; i < count_fill; i++)
write_char (ch);
}
while ((c = *fmt++) != 0)
{
if (c != '%')
continue;
fmt++;
fmt++;
fmt++;
fmt++;
fmt++;
fmt++;
fmt++;
c = *fmt++;
if (c == 'l')
{
c = *fmt++;
if (c == 'l')
c = *fmt++;
}
switch (c)
{
case 'p':
case 'x':
case 'u':
case 'd':
case 'c':
case 'C':
case 's':
count_args++;
break;
}
}
union
{
int i;
long l;
long long ll;
void *p;
n = 0;
while ((c = *fmt++) != 0)
{
int longfmt = 0;
int longlongfmt = 0;
const char *p;
if (c != '%')
continue;
curn = n++;
fmt++;
fmt++;
fmt++;
fmt++;
p = fmt;
{
fmt++;
}
fmt++;
c = *fmt++;
if (c == 'l')
{
c = *fmt++;
longfmt = 1;
if (c == 'l')
{
c = *fmt++;
longlongfmt = 1;
}
}
if (curn >= count_args)
continue;
switch (c)
{
case 'x':
case 'u':
case 'd':
if (longlongfmt)
else if (longfmt)
else
break;
case 'p':
case 's':
break;
case 'c':
break;
case 'C':
break;
}
}
for (n = 0; n < count_args; n++)
switch (types[n])
{
case WCHAR:
break;
case POINTER:
break;
case INT:
break;
case LONG:
break;
case LONGLONG:
break;
}
n = 0;
while ((c = *fmt++) != 0)
{
char *p;
unsigned int format1 = 0;
unsigned int format2 = ~ 0U;
int rightfill = 0;
int longfmt = 0;
int longlongfmt = 0;
int unsig = 0;
if (c != '%')
{
write_char (c);
continue;
}
curn = n++;
rescan:;
{
write_char (*fmt);
fmt++;
continue;
}
{
rightfill = 1;
fmt++;
}
p = (char *) fmt;
/* Read formatting parameters. */
while (*p && grub_isdigit (*p))
p++;
if (p > fmt)
{
char s[p - fmt + 1];
s[p - fmt] = 0;
if (s[0] == '0')
zerofill = '0';
fmt = p;
}
if (*p && *p == '.')
{
p++;
fmt++;
while (*p && grub_isdigit (*p))
p++;
if (p > fmt)
{
fmt = p;
}
}
if (*fmt == '$')
{
fmt++;
format1 = 0;
format2 = ~ 0U;
zerofill = ' ';
rightfill = 0;
goto rescan;
}
c = *fmt++;
if (c == 'l')
{
longfmt = 1;
c = *fmt++;
if (c == 'l')
{
longlongfmt = 1;
c = *fmt++;
}
}
if (curn >= count_args)
continue;
switch (c)
{
case 'p':
write_str ("0x");
c = 'x';
longlongfmt |= (sizeof (void *) == sizeof (long long));
/* Fall through. */
case 'x':
case 'u':
unsig = 1;
/* Fall through. */
case 'd':
if (longlongfmt)
else if (longfmt)
else if (unsig)
else
break;
case 'c':
break;
case 'C':
{
int shift;
unsigned mask;
if (code <= 0x7f)
{
shift = 0;
mask = 0;
}
else if (code <= 0x7ff)
{
shift = 6;
mask = 0xc0;
}
else if (code <= 0xffff)
{
shift = 12;
mask = 0xe0;
}
else if (code <= 0x1fffff)
{
shift = 18;
mask = 0xf0;
}
else if (code <= 0x3ffffff)
{
shift = 24;
mask = 0xf8;
}
else if (code <= 0x7fffffff)
{
shift = 30;
mask = 0xfc;
}
else
{
code = '?';
shift = 0;
mask = 0;
}
}
break;
case 's':
if (p)
{
len++;
grub_size_t i;
for (i = 0; i < len; i++)
write_char (*p++);
}
else
write_str ("(null)");
break;
default:
write_char (c);
break;
}
}
*str = '\0';
return count;
}
int
{
if (!n)
return 0;
n--;
}
int
{
int ret;
return ret;
}
char *
{
char *ret;
while (1)
{
if (!ret)
return NULL;
if (s <= as)
return ret;
as = s;
}
}
char *
{
char *ret;
return ret;
}
/* Abort GRUB. This function does not return. */
void
grub_abort (void)
{
grub_printf ("\nAborted.");
#ifndef GRUB_UTIL
if (grub_term_inputs)
#endif
{
grub_printf (" Press any key to exit.");
grub_getkey ();
}
grub_exit ();
}
/* GCC emits references to abort(). */
#endif
/* Some gcc versions generate a call to this function
in trampolines for nested functions. */
{
}
#endif
#if NEED_REGISTER_FRAME_INFO && !defined(GRUB_UTIL)
void __register_frame_info (void)
{
}
void __deregister_frame_info (void)
{
}
#endif
struct grub_preboot
{
};
*preboots_tail = 0;
{
{
if (err)
{
cur->preboot_rest_func ();
return err;
}
}
return err;
}
{
if (! err)
else
cur->preboot_rest_func ();
return err;
}
/* Register a preboot hook. */
struct grub_preboot *
grub_err_t (*preboot_rest_func) (void),
{
if (! preboot_func && ! preboot_rest_func)
return 0;
new_preboot = (struct grub_preboot *)
grub_malloc (sizeof (struct grub_preboot));
if (! new_preboot)
{
return 0;
}
if (cur)
{
}
else
{
new_preboot->next = 0;
}
if (new_preboot->prev)
else
return new_preboot;
}
void
{
else
else
}