Skip to content
Browse files

Minor code refactoring and cleanup

Cleaned up a few variables and types to better match their
purpose. Remove an incrementor from str_copy().
  • Loading branch information...
1 parent 73d2355 commit c4668ef3986dbf095ba2b89895dc55e68ee95d22 @jbittel committed Jan 23, 2012
Showing with 7 additions and 9 deletions.
  1. +1 −1 httpry.c
  2. +5 −7 utility.c
  3. +1 −1 utility.h
View
2 httpry.c
@@ -592,7 +592,7 @@ void print_stats() {
((pkt_stats.ps_recv * 60) / run_time), ((num_parsed * 60) / run_time));
}
} else if (pcap_hnd) {
- PRINT("%d http packets parsed", num_parsed);
+ PRINT("%u http packets parsed", num_parsed);
}
return;
View
12 utility.c
@@ -18,15 +18,14 @@
the string in place and returning a pointer to the (potentially)
new starting point */
char *str_strip_whitespace(char *str) {
- int len;
+ size_t len = strlen(str);
#ifdef DEBUG
ASSERT(str);
ASSERT(strlen(str) > 0);
#endif
while (isspace(*str)) str++;
- len = strlen(str);
while (len && isspace(*(str + len - 1)))
*(str + (len--) - 1) = '\0';
@@ -70,21 +69,20 @@ int str_compare(const char *str1, const char *str2) {
return tolower(*str1) - *str2;
}
-/* Copy at most len characters from src to dest, including
- the end of string terminator. Returns the total number of
+/* Copy at most len characters from src to dest, guaranteeing
+ dest will be properly terminated. Returns the total number of
characters copied, not including the string terminator. */
int str_copy(char *dest, const char *src, size_t len) {
- int i = 0;
+ const char *start = dest;
if (len > 0) {
while ((*src != '\0') && --len) {
*dest++ = *src++;
- i++;
}
*dest = '\0';
}
- return i;
+ return dest - start;
}
/* Wrapper function around str_copy() that first allocates
View
2 utility.h
@@ -14,7 +14,7 @@
char *str_strip_whitespace(char *str);
char *str_tolower(char *str);
int str_compare(const char *str1, const char *str2);
-int str_copy(char *str1, const char *str2, size_t len);
+int str_copy(char *dest, const char *src, size_t len);
char *str_duplicate(const char *str);
unsigned int hash_str(char *key, unsigned int hashsize);

0 comments on commit c4668ef

Please sign in to comment.
Something went wrong with that request. Please try again.