Skip to content

Commit

Permalink
Add strcasecmp() to string.h
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Nov 11, 2023
1 parent bf89653 commit 38af645
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
18 changes: 9 additions & 9 deletions ncc/examples/mini_bbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,12 @@ void on_incoming_data(u64 socket_id, u64 num_bytes)

if (p_user->state == STATE_WRITE_MSG)
{
bool done = (
strcmp(read_buf, "Done\n") == 0 ||
strcmp(read_buf, "Done\r\n") == 0 ||
strcmp(read_buf, "done\n") == 0 ||
strcmp(read_buf, "done\r\n") == 0
);
// Strip trailing whitespace from the buffer
rstrip_ws(read_buf);

if (done)
if (strcasecmp(read_buf, "done") == 0)
{
// Strip trailing whitespace
// Strip trailing whitespace in the message buffer
rstrip_ws(p_user->msg_buf);

// If this is an empty message, do nothing
Expand All @@ -386,9 +382,13 @@ void on_incoming_data(u64 socket_id, u64 num_bytes)
return;
}

// Add back a newline on the message buffer because
// we stripped whitespace earlier
strncat(p_user->msg_buf, "\n", 1);

// Append new line to the user's message buffer
size_t msg_len = strlen(p_user->msg_buf);
size_t len = strlen(read_buf);

size_t max_chars = MAX_MSG_LEN - 1 - msg_len;
strncat(p_user->msg_buf, read_buf, max_chars);

Expand Down
22 changes: 22 additions & 0 deletions ncc/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __STRING_H__

#include <stddef.h>
#include <ctype.h>

#ifndef memcpy
#define memcpy(dst, src, num_bytes) asm (dst, src, num_bytes) -> void { syscall memcpy; }
Expand Down Expand Up @@ -37,6 +38,27 @@ int strcmp(char* a, char* b)
return 0;
}

// Perform case-insensitive string comparison
// This function is non-standard, but offered by many compilers.
int strcasecmp(char* a, char* b)
{
for (size_t i = 0;; ++i)
{
char ch_a = tolower(a[i]);
char ch_b = tolower(b[i]);

if (ch_a < ch_b)
return -1;
else if (ch_a > ch_b)
return 1;

if (ch_a == 0)
break;
}

return 0;
}

int strncmp(char* a, char* b, size_t num)
{
for (size_t i = 0; i < num; ++i)
Expand Down

0 comments on commit 38af645

Please sign in to comment.