linux系统库函数之strcmp、strncmp

来源:互联网 发布:ubuntu 16.04进入图形 编辑:程序博客网 时间:2024/06/05 20:28
240 #ifndef __HAVE_ARCH_STRCMP
241 /**
242  * strcmp - Compare two strings
243  * @cs: One string
244  * @ct: Another string
245  */
246 #undef strcmp
247 int strcmp(const char *cs, const char *ct)
248 {
249         unsigned char c1, c2;
250 
251         while (1) {
252                 c1 = *cs++;
253                 c2 = *ct++;
254                 if (c1 != c2)
255                         return c1 < c2 ? -1 : 1;
256                 if (!c1)
257                         break;
258         }
259         return 0;
260 }
261 EXPORT_SYMBOL(strcmp);

262 #endif

字符串比较函数,如果两个字符串相等,则返回0,否则返回非零值。

264 #ifndef __HAVE_ARCH_STRNCMP
265 /**
266  * strncmp - Compare two length-limited strings
267  * @cs: One string
268  * @ct: Another string
269  * @count: The maximum number of bytes to compare
270  */
271 int strncmp(const char *cs, const char *ct, size_t count)
272 {
273         unsigned char c1, c2;
274 
275         while (count) {
276                 c1 = *cs++;
277                 c2 = *ct++;
278                 if (c1 != c2)
279                         return c1 < c2 ? -1 : 1;
280                 if (!c1)
281                         break;
282                 count--;
283         }
284         return 0;
285 }
286 EXPORT_SYMBOL(strncmp);
287 #endif

同样的是,如果没有遇到字符串结束符,只比较cuont大小数据。