Browse code

Rewrite of cli_chomp

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@626 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/06/22 19:58:58
Showing 3 changed files
... ...
@@ -1,3 +1,8 @@
1
+Tue Jun 22 11:58:06 BST 2004 (njh/trog)
2
+---------------------------------------
3
+  * libclamav/str.c:	Rewrote cli_chomp() as discussed in the clamav-devel
4
+  	mailing list
5
+
1 6
 Tue Jun 22 05:09:54 BST 2004 (njh)
2 7
 ----------------------------------
3 8
   * clamav-milter:	Avoid unlocking an already unlocked mutex in
... ...
@@ -73,7 +73,7 @@ short int *cl_hex2str(const char *hex)
73 73
 		val = c;
74 74
 		if((c = cli_hex2int(hex[i+1])) >= 0) {
75 75
 		    val = (val << 4) + c;
76
-		} else { 
76
+		} else {
77 77
 		    free(str);
78 78
 		    return NULL;
79 79
 		}
... ...
@@ -122,26 +122,30 @@ int cli_strbcasestr(const char *haystack, const char *needle)
122 122
     return !strcasecmp(pt, needle);
123 123
 }
124 124
 
125
-void cli_chomp(char *string)
125
+/*
126
+ * Remove trailing NL and CR characters from the end of the given string.
127
+ * Return the new length of the string (ala strlen)
128
+ */
129
+int
130
+cli_chomp(char *string)
126 131
 {
127
-	size_t l = strlen(string);
132
+	int l;
128 133
 
134
+	if(string == NULL)
135
+		return -1;
129 136
 
130
-    if(l == 0)
131
-	return;
137
+	l  = strlen(string);
132 138
 
133
-    --l;
134
-    if((string[l] == '\n') || (string[l] == '\r')) {
135
-	string[l] = '\0';
139
+	if(l == 0)
140
+		return 0;
136 141
 
137
-	if(l > 0) {
138
-	    --l;
139
-	    if(string[l] == '\r')
140
-		string[l] = '\0';
141
-	}
142
-    }
143
-}
142
+	--l;
143
+
144
+	while((l >= 0) && ((string[l] == '\n') || (string[l] == '\r')))
145
+		string[l--] = '\0';
144 146
 
147
+	return l + 1;
148
+}
145 149
 
146 150
 /*
147 151
  * char *cli_strok(const char *line, int fieldno, char *delim)
... ...
@@ -20,7 +20,7 @@
20 20
 #define __STRINGS_H
21 21
 
22 22
 int cli_strbcasestr(const char *haystack, const char *needle);
23
-void cli_chomp(char *string);
23
+int	cli_chomp(char *string);
24 24
 char *cli_strtok(const char *line, int field, const char *delim);
25 25
 
26 26
 #endif