8883cf71da155018031cbb53db3004086e3c5f23
[pintos-anon] / src / lib / lib.h
1 #ifndef HEADER_LIB_H
2 #define HEADER_LIB_H 1
3
4 #include <stdarg.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include "debug.h"
8
9 /* <string.h> */
10 void *memset (void *, int, size_t);
11 void *memcpy (void *, const void *, size_t);
12 void *memmove (void *, const void *, size_t);
13 void *memchr (const void *, int, size_t);
14 int memcmp (const void *, const void *, size_t);
15
16 char *strchr (const char *, int);
17 size_t strlcpy (char *, const char *, size_t);
18 size_t strlen (const char *);
19 size_t strnlen (const char *, size_t);
20 int strcmp (const char *, const char *);
21 char *strtok_r (char *, const char *, char **);
22
23 /* <stdlib.h> */
24 int atoi (const char *);
25
26 /* <stdio.h> */
27 int vsnprintf (char *, size_t, const char *, va_list) PRINTF_FORMAT (3, 0);
28 int snprintf (char *, size_t, const char *, ...) PRINTF_FORMAT (3, 4);
29
30 /* <ctype.h> */
31 static inline int islower (int c) { return c >= 'a' && c <= 'z'; }
32 static inline int isupper (int c) { return c >= 'A' && c <= 'Z'; }
33 static inline int isalpha (int c) { return islower (c) || isupper (c); }
34 static inline int isdigit (int c) { return c >= '0' && c <= '9'; }
35 static inline int isalnum (int c) { return isalpha (c) || isdigit (c); }
36 static inline int isxdigit (int c) {
37   return isdigit (c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
38 }
39 static inline int isspace (int c) { return strchr (" \f\n\r\t\v", c) != NULL; }
40 static inline int isgraph (int c) { return c >= 33 && c < 127; }
41 static inline int isprint (int c) { return c >= 32 && c < 127; }
42 static inline int iscntrl (int c) { return c >= 0 && c < 32; }
43 static inline int isascii (int c) { return c >= 0 && c < 128; }
44 static inline int ispunct (int c) {
45   return isprint (c) && !isalnum (c) && !isspace (c);
46 }
47 \f
48 /* Nonstandard. */
49
50 /* Yields X rounded up to the nearest multiple of STEP.
51    For X >= 0, STEP >= 1 only. */
52 #define ROUND_UP(X, STEP) (((X) + (STEP) - 1) / (STEP) * (STEP))
53
54 /* Yields X divided by STEP, rounded up.
55    For X >= 0, STEP >= 1 only. */
56 #define DIV_ROUND_UP(X, STEP) (((X) + (STEP) - 1) / (STEP))
57
58 /* Yields X rounded down to the nearest multiple of STEP.
59    For X >= 0, STEP >= 1 only. */
60 #define ROUND_DOWN(X, STEP) ((X) / (STEP) * (STEP))
61
62 /* There is no DIV_ROUND_DOWN.   It would be simply X / STEP. */
63
64 void vprintk (const char *, va_list) PRINTF_FORMAT (1, 0);
65 void printk (const char *, ...) PRINTF_FORMAT (1, 2);
66 void hex_dump (const void *, size_t size, bool ascii);
67
68 #endif /* lib.h */