Added .gitignore
[comos.git] / kernel / general.c
blob159c955ddb6411aae55c6dd4214742bb51bfbec0
1 #include "general.h"
2 #include "stdint.h"
3 #include "stdlib.h"
4 #include "console.h"
6 int strcmp(const char *str1, const char *str2)
8 // the actual compare is done by memcmp()
9 size_t len1, len2, smallest_len;
10 int cmp;
11 len1 = strlen(str1);
12 len2 = strlen(str2);
13 smallest_len = len1;
14 if (len2 < smallest_len){
15 smallest_len = len2;
17 // compare as many bytes as both strings have
18 cmp = memcmp(str1, str2, smallest_len);
19 if (cmp == 0){
20 // the bits we compared were identical, but...
21 if (len1 > len2){
22 // str1 was longer than str2
23 return -1;
24 } else if (len1 < len2){
25 // str2 was longer than str1
26 return 1;
27 } else {
28 // yes, they were exactly the same
29 return 0;
31 } else {
32 // the bits we compared weren't the same
33 // any extra bits after what we compared
34 // won't make any difference
35 return cmp;
39 void panic_internal(const char *file, int line, const char *fmt, ...)
41 cli();
42 va_list ap;
43 console_set_fcolour(get_vterm(0), COLOR_BRIGHT_RED); // set a noticable colour
44 console_printf(get_vterm(0), "*** PANIC (%s:%d): ", file, line);
45 // print the actual message
46 va_start(ap, fmt);
47 console_vprintf(get_vterm(0), fmt, ap);
48 va_end(ap);
49 // print a newline
50 console_putchar(get_vterm(0), '\n');
51 // completely halt
52 hlt();
55 // Compare two strings. Should return -1 if
56 // str1 < str2, 0 if they are equal or 1 otherwise.
58 int strcmp(char *str1, char *str2)
60 int i = 0;
61 int failed = 0;
62 while(str1[i] != '\0' && str2[i] != '\0')
64 if(str1[i] != str2[i])
66 failed = 1;
67 break;
69 i++;
71 // why did the loop exit?
72 if( (str1[i] == '\0' && str2[i] != '\0') || (str1[i] != '\0' && str2[i] == '\0') )
73 failed = 1;
75 return failed;
79 // Copy the NULL-terminated string src into dest, and
80 // return dest.
81 char *strcpy(char *dest, const char *src)
83 memcpy(dest, src, strlen(src) + 1); // copy entire string + null byte
84 return dest;
87 char *strncpy(char *dest, const char *src, size_t n)
89 size_t len = strlen(src);
90 if (len > n){
91 len = n;
93 memcpy(dest, src, len);
94 // pad the remainder of the string with null bytes!
95 memset(dest + len, 0, n - len);
96 return dest;
99 // Concatenate the NULL-terminated string src onto
100 // the end of dest, and return dest.
102 char *strcat(char *dest, const char *src)
104 while (*dest != 0)
106 *dest = *dest++;
111 *dest++ = *src++;
113 while (*src != 0);
114 return dest;
118 int strlen(char *src)
120 int i = 0;
121 while (*src++)
122 i++;
123 return i;
126 char * strtok( char * s1, const char * s2 )
128 static char * tmp = NULL;
129 const char * p = s2;
131 if ( s1 != NULL )
133 /* new string */
134 tmp = s1;
136 else
138 /* old string continued */
139 if ( tmp == NULL )
141 /* No old string, no new string, nothing to do */
142 return NULL;
144 s1 = tmp;
147 /* skipping leading s2 characters */
148 while ( *p && *s1 )
150 if ( *s1 == *p )
152 /* found seperator; skip and start over */
153 ++s1;
154 p = s2;
155 continue;
157 ++p;
160 if ( ! *s1 )
162 /* no more to parse */
163 return ( tmp = NULL );
166 /* skipping non-s2 characters */
167 tmp = s1;
168 while ( *tmp )
170 p = s2;
171 while ( *p )
173 if ( *tmp == *p++ )
175 /* found seperator; overwrite with '\0', position tmp, return */
176 *tmp++ = '\0';
177 return s1;
180 ++tmp;
183 /* parsed to end of string */
184 return ( tmp = NULL );