2009-10-28 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / include / grub / misc.h
blobfaa2d5c42ad9c1d3c7cb57979dd347793df969f7
1 /* misc.h - prototypes for misc functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2008 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
23 #include <stdarg.h>
24 #include <grub/types.h>
25 #include <grub/symbol.h>
26 #include <grub/err.h>
28 #define ALIGN_UP(addr, align) \
29 ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
30 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
31 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
33 #define grub_dprintf(condition, fmt, args...) grub_real_dprintf(__FILE__, __LINE__, condition, fmt, ## args)
34 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
35 #define grub_memcpy(d,s,n) grub_memmove ((d), (s), (n))
37 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
38 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
39 char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
40 char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src);
42 static inline char *
43 grub_strcat (char *dest, const char *src)
45 char *p = dest;
47 while (*p)
48 p++;
50 while ((*p = *src) != '\0')
52 p++;
53 src++;
56 return dest;
59 static inline char *
60 grub_strncat (char *dest, const char *src, int c)
62 char *p = dest;
64 while (*p)
65 p++;
67 while ((*p = *src) != '\0' && c--)
69 p++;
70 src++;
73 *p = '\0';
75 return dest;
78 /* Prototypes for aliases. */
79 #ifndef GRUB_UTIL
80 int EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
81 void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
82 void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
83 void *EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
84 #endif
86 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
87 int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
88 int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
90 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
91 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
92 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
93 char *EXPORT_FUNC(grub_strstr) (const char *haystack, const char *needle);
94 int EXPORT_FUNC(grub_isspace) (int c);
95 int EXPORT_FUNC(grub_isprint) (int c);
97 static inline int
98 grub_isalpha (int c)
100 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
103 static inline int
104 grub_isgraph (int c)
106 return (c >= '!' && c <= '~');
109 static inline int
110 grub_isdigit (int c)
112 return (c >= '0' && c <= '9');
115 static inline int
116 grub_tolower (int c)
118 if (c >= 'A' && c <= 'Z')
119 return c - 'A' + 'a';
121 return c;
124 static inline int
125 grub_toupper (int c)
127 if (c >= 'a' && c <= 'z')
128 return c - 'a' + 'A';
130 return c;
133 static inline int
134 grub_strcasecmp (const char *s1, const char *s2)
136 while (*s1 && *s2)
138 if (grub_tolower (*s1) != grub_tolower (*s2))
139 break;
141 s1++;
142 s2++;
145 return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
148 static inline int
149 grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
151 if (n == 0)
152 return 0;
154 while (*s1 && *s2 && --n)
156 if (grub_tolower (*s1) != grub_tolower (*s2))
157 break;
159 s1++;
160 s2++;
163 return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
167 unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
168 unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
169 char *EXPORT_FUNC(grub_strdup) (const char *s);
170 char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n);
171 void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
172 grub_size_t EXPORT_FUNC(grub_strlen) (const char *s);
173 int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
174 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
175 const int line,
176 const char *condition,
177 const char *fmt, ...) __attribute__ ((format (printf, 4, 5)));
178 int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
179 int EXPORT_FUNC(grub_sprintf) (char *str, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
180 int EXPORT_FUNC(grub_vsprintf) (char *str, const char *fmt, va_list args);
181 void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
182 void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
183 grub_uint8_t *EXPORT_FUNC(grub_utf16_to_utf8) (grub_uint8_t *dest,
184 grub_uint16_t *src,
185 grub_size_t size);
186 grub_ssize_t EXPORT_FUNC(grub_utf8_to_ucs4) (grub_uint32_t *dest,
187 grub_size_t destsize,
188 const grub_uint8_t *src,
189 grub_size_t srcsize,
190 const grub_uint8_t **srcend);
191 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
192 grub_uint32_t d, grub_uint32_t *r);
194 #ifdef NEED_ENABLE_EXECUTE_STACK
195 void EXPORT_FUNC(__enable_execute_stack) (void *addr);
196 #endif
198 /* Inline functions. */
200 static inline unsigned int
201 grub_abs (int x)
203 if (x < 0)
204 return (unsigned int) (-x);
205 else
206 return (unsigned int) x;
209 static inline long
210 grub_max (long x, long y)
212 if (x > y)
213 return x;
214 else
215 return y;
218 /* Rounded-up division */
219 static inline unsigned int
220 grub_div_roundup (unsigned int x, unsigned int y)
222 return (x + y - 1) / y;
225 #endif /* ! GRUB_MISC_HEADER */