function prototypes don't need extern. (trivial)
[coreboot.git] / src / include / string.h
1 #ifndef STRING_H
2 #define STRING_H
3
4 #include <stddef.h>
5 #include <stdlib.h>
6
7 void *memcpy(void *dest, const void *src, size_t n);
8 void *memmove(void *dest, const void *src, size_t n);
9 void *memset(void *s, int c, size_t n);
10 int memcmp(const void *s1, const void *s2, size_t n);
11 void *malloc(size_t size);
12 int sprintf(char * buf, const char *fmt, ...);
13
14 // yes, linux has fancy ones. We don't care. This stuff gets used 
15 // hardly at all. And the pain of including those files is just too high.
16
17 //extern inline void strcpy(char *dst, char *src) {while (*src) *dst++ = *src++;}
18
19 //extern inline int strlen(char *src) { int i = 0; while (*src++) i++; return i;}
20
21 static inline size_t strnlen(const char *src, size_t max) 
22
23         size_t i = 0;
24         while((*src++) && (i < max)) {
25                 i++;
26         }
27         return i;
28 }
29
30 static inline size_t strlen(const char *src)
31 {
32         size_t i = 0;
33         while(*src++) {
34                 i++;
35         }
36         return i;
37 }
38
39 static inline char *strchr(const char *s, int c)
40 {
41         for (; *s; s++) {
42                 if (*s == c)
43                         return (char *) s;
44         }       
45         return 0;
46 }
47
48 static inline char *strdup(const char *s)
49 {   
50         size_t sz = strlen(s) + 1;
51         char *d = malloc(sz);
52         memcpy(d, s, sz);
53         return d;
54 }
55
56 static inline char *strncpy(char *to, const char *from, int count)
57 {
58         register char *ret = to;
59
60         while (count > 0) {
61                 count--;
62                 if ((*to++ = *from++) == '\0')
63                         break;
64         }
65
66         while (count > 0) {
67                 count--;
68                 *to++ = '\0';
69         }
70         return ret;
71 }
72
73 static inline int strcmp(const char *s1, const char *s2)
74 {   
75         int r;
76
77         while ((r = (*s1 - *s2)) == 0 && *s1) {
78                 s1++;
79                 s2++;
80         }
81         return r;
82 }  
83
84 static inline int isspace(int c)
85 {
86         switch (c) {
87         case ' ': case '\f': case '\n':
88         case '\r': case '\t': case '\v':
89                 return 1;
90         default:
91                 return 0;
92         }
93 }
94
95 static inline int isdigit(int c)
96 {
97         return (c >= '0' && c <= '9');
98 }
99
100 static inline int isxdigit(int c)
101 {
102         return ((c >= '0' && c <= '9') ||
103                 (c >= 'a' && c <= 'f') ||
104                 (c >= 'A' && c <= 'F'));
105 }
106
107 static inline int isupper(int c)
108 {
109         return (c >= 'A' && c <= 'Z');
110 }
111
112 static inline int islower(int c)
113 {
114         return (c >= 'a' && c <= 'z');
115 }
116
117 static inline int toupper(int c)
118 {
119         if (islower(c))
120                 c -= 'a'-'A';
121         return c;
122 }
123
124 static inline int tolower(int c)
125 {
126         if (isupper(c))
127                 c -= 'A'-'a';
128         return c;
129 }
130 #endif /* STRING_H */