6 int strcmp(const char *str1
, const char *str2
)
8 // the actual compare is done by memcmp()
9 size_t len1
, len2
, smallest_len
;
14 if (len2
< smallest_len
){
17 // compare as many bytes as both strings have
18 cmp
= memcmp(str1
, str2
, smallest_len
);
20 // the bits we compared were identical, but...
22 // str1 was longer than str2
24 } else if (len1
< len2
){
25 // str2 was longer than str1
28 // yes, they were exactly the same
32 // the bits we compared weren't the same
33 // any extra bits after what we compared
34 // won't make any difference
39 void panic_internal(const char *file
, int line
, const char *fmt
, ...)
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
47 console_vprintf(get_vterm(0), fmt
, ap
);
50 console_putchar(get_vterm(0), '\n');
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)
62 while(str1[i] != '\0' && str2[i] != '\0')
64 if(str1[i] != str2[i])
71 // why did the loop exit?
72 if( (str1[i] == '\0' && str2[i] != '\0') || (str1[i] != '\0' && str2[i] == '\0') )
79 // Copy the NULL-terminated string src into dest, and
81 char *strcpy(char *dest
, const char *src
)
83 memcpy(dest
, src
, strlen(src
) + 1); // copy entire string + null byte
87 char *strncpy(char *dest
, const char *src
, size_t n
)
89 size_t len
= strlen(src
);
93 memcpy(dest
, src
, len
);
94 // pad the remainder of the string with null bytes!
95 memset(dest
+ len
, 0, n
- len
);
99 // Concatenate the NULL-terminated string src onto
100 // the end of dest, and return dest.
102 char *strcat(char *dest, const char *src)
118 int strlen(char *src)
126 char * strtok( char * s1
, const char * s2
)
128 static char * tmp
= NULL
;
138 /* old string continued */
141 /* No old string, no new string, nothing to do */
147 /* skipping leading s2 characters */
152 /* found seperator; skip and start over */
162 /* no more to parse */
163 return ( tmp
= NULL
);
166 /* skipping non-s2 characters */
175 /* found seperator; overwrite with '\0', position tmp, return */
183 /* parsed to end of string */
184 return ( tmp
= NULL
);