cs-3516-assignment-1/server/request_handling.c

31 lines
862 B
C
Raw Normal View History

2018-09-09 02:53:45 +00:00
#include <stdlib.h>
/**
* Identical to strcpy, but strips a trailing '\r\n'
*
* @param dest Buffer to copy to
* @param src Buffer to copy from
*
* @return The destination buffer
*/
static char *strcpy_no_crlf(char *dest, const char *src) {
char *dest_cursor;
const char *src_cursor;
char *last_dest_cursor = NULL;
const char *last_src_cursor = NULL;
for (dest_cursor = dest, src_cursor = src; *src_cursor != '\0'; src_cursor++) {
if (last_src_cursor != NULL && *last_src_cursor == '\r' && *last_src_cursor == '\n') {
// Replace the \r with a null term.
*last_dest_cursor = '\0';
return dest;
}
*dest_cursor = *src_cursor;
last_dest_cursor = dest_cursor;
last_src_cursor = src_cursor;
dest_cursor++;
}
*dest_cursor = '\0';
return dest;
}