]>
git.proxmox.com Git - grub2.git/blob - include/grub/misc.h
1 /* misc.h - prototypes for misc functions */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
20 #ifndef GRUB_MISC_HEADER
21 #define GRUB_MISC_HEADER 1
24 #include <grub/types.h>
25 #include <grub/symbol.h>
28 /* GCC version checking borrowed from glibc. */
29 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
30 # define GNUC_PREREQ(maj,min) \
31 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
33 # define GNUC_PREREQ(maj,min) 0
36 /* Does this compiler support compile-time error attributes? */
38 # define ATTRIBUTE_ERROR(msg) \
39 __attribute__ ((__error__ (msg)))
41 # define ATTRIBUTE_ERROR(msg)
44 #define ALIGN_UP(addr, align) \
45 ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
46 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
47 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
49 #define grub_dprintf(condition, fmt, args...) grub_real_dprintf(__FILE__, __LINE__, condition, fmt, ## args)
50 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
51 #define grub_memcpy(d,s,n) grub_memmove ((d), (s), (n))
53 void *EXPORT_FUNC(grub_memmove
) (void *dest
, const void *src
, grub_size_t n
);
54 char *EXPORT_FUNC(grub_strcpy
) (char *dest
, const char *src
);
55 char *EXPORT_FUNC(grub_strncpy
) (char *dest
, const char *src
, int c
);
56 char *EXPORT_FUNC(grub_stpcpy
) (char *dest
, const char *src
);
59 grub_strcat (char *dest
, const char *src
)
66 while ((*p
= *src
) != '\0')
76 grub_strncat (char *dest
, const char *src
, int c
)
83 while ((*p
= *src
) != '\0' && c
--)
94 /* Prototypes for aliases. */
96 int EXPORT_FUNC(memcmp
) (const void *s1
, const void *s2
, grub_size_t n
);
97 void *EXPORT_FUNC(memmove
) (void *dest
, const void *src
, grub_size_t n
);
98 void *EXPORT_FUNC(memcpy
) (void *dest
, const void *src
, grub_size_t n
);
99 void *EXPORT_FUNC(memset
) (void *s
, int c
, grub_size_t n
);
102 int EXPORT_FUNC(grub_memcmp
) (const void *s1
, const void *s2
, grub_size_t n
);
103 int EXPORT_FUNC(grub_strcmp
) (const char *s1
, const char *s2
);
104 int EXPORT_FUNC(grub_strncmp
) (const char *s1
, const char *s2
, grub_size_t n
);
106 char *EXPORT_FUNC(grub_strchr
) (const char *s
, int c
);
107 char *EXPORT_FUNC(grub_strrchr
) (const char *s
, int c
);
108 int EXPORT_FUNC(grub_strword
) (const char *s
, const char *w
);
109 char *EXPORT_FUNC(grub_strstr
) (const char *haystack
, const char *needle
);
110 int EXPORT_FUNC(grub_isspace
) (int c
);
111 int EXPORT_FUNC(grub_isprint
) (int c
);
116 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z');
122 return (c
>= '!' && c
<= '~');
128 return (c
>= '0' && c
<= '9');
134 if (c
>= 'A' && c
<= 'Z')
135 return c
- 'A' + 'a';
143 if (c
>= 'a' && c
<= 'z')
144 return c
- 'a' + 'A';
150 grub_strcasecmp (const char *s1
, const char *s2
)
154 if (grub_tolower (*s1
) != grub_tolower (*s2
))
161 return (int) grub_tolower (*s1
) - (int) grub_tolower (*s2
);
165 grub_strncasecmp (const char *s1
, const char *s2
, grub_size_t n
)
170 while (*s1
&& *s2
&& --n
)
172 if (grub_tolower (*s1
) != grub_tolower (*s2
))
179 return (int) grub_tolower (*s1
) - (int) grub_tolower (*s2
);
183 unsigned long EXPORT_FUNC(grub_strtoul
) (const char *str
, char **end
, int base
);
184 unsigned long long EXPORT_FUNC(grub_strtoull
) (const char *str
, char **end
, int base
);
185 char *EXPORT_FUNC(grub_strdup
) (const char *s
);
186 char *EXPORT_FUNC(grub_strndup
) (const char *s
, grub_size_t n
);
187 void *EXPORT_FUNC(grub_memset
) (void *s
, int c
, grub_size_t n
);
188 grub_size_t
EXPORT_FUNC(grub_strlen
) (const char *s
);
189 int EXPORT_FUNC(grub_printf
) (const char *fmt
, ...) __attribute__ ((format (printf
, 1, 2)));
190 int EXPORT_FUNC(grub_printf_
) (const char *fmt
, ...) __attribute__ ((format (printf
, 1, 2)));
191 int EXPORT_FUNC(grub_puts
) (const char *s
);
192 int EXPORT_FUNC(grub_puts_
) (const char *s
);
193 void EXPORT_FUNC(grub_real_dprintf
) (const char *file
,
195 const char *condition
,
196 const char *fmt
, ...) __attribute__ ((format (printf
, 4, 5)));
197 int EXPORT_FUNC(grub_vprintf
) (const char *fmt
, va_list args
);
198 int EXPORT_FUNC(grub_sprintf
) (char *str
, const char *fmt
, ...) __attribute__ ((format (printf
, 2, 3)));
199 int EXPORT_FUNC(grub_vsprintf
) (char *str
, const char *fmt
, va_list args
);
200 void EXPORT_FUNC(grub_exit
) (void) __attribute__ ((noreturn
));
201 void EXPORT_FUNC(grub_abort
) (void) __attribute__ ((noreturn
));
202 grub_size_t
EXPORT_FUNC(grub_utf8_to_ucs4
) (grub_uint32_t
*dest
,
203 grub_size_t destsize
,
204 const grub_uint8_t
*src
,
206 const grub_uint8_t
**srcend
);
207 grub_uint64_t
EXPORT_FUNC(grub_divmod64
) (grub_uint64_t n
,
208 grub_uint32_t d
, grub_uint32_t
*r
);
210 #ifdef NEED_ENABLE_EXECUTE_STACK
211 void EXPORT_FUNC(__enable_execute_stack
) (void *addr
);
214 /* Inline functions. */
216 static inline unsigned int
220 return (unsigned int) (-x
);
222 return (unsigned int) x
;
226 grub_max (long x
, long y
)
234 /* Rounded-up division */
235 static inline unsigned int
236 grub_div_roundup (unsigned int x
, unsigned int y
)
238 return (x
+ y
- 1) / y
;
241 /* Reboot the machine. */
242 void EXPORT_FUNC (grub_reboot
) (void);
244 #ifdef GRUB_MACHINE_PCBIOS
245 /* Halt the system, using APM if possible. If NO_APM is true, don't
246 * use APM even if it is available. */
247 void EXPORT_FUNC (grub_halt
) (int no_apm
);
249 void EXPORT_FUNC (grub_halt
) (void);
252 #endif /* ! GRUB_MISC_HEADER */