linux系统库函数之strcat、strncat

来源:互联网 发布:windows平板刷安卓 编辑:程序博客网 时间:2024/06/05 18:18
164 #ifndef __HAVE_ARCH_STRCAT
165 /**
166  * strcat - Append one %NUL-terminated string to another
167  * @dest: The string to be appended to
168  * @src: The string to append to it
169  */
170 #undef strcat
171 char *strcat(char *dest, const char *src)
172 {
173         char *tmp = dest;
174 
175         while (*dest)
176                 dest++;
177         while ((*dest++ = *src++) != '\0')
178                 ;
179         return tmp;
180 }
181 EXPORT_SYMBOL(strcat);

182 #endif

用于将src所指向的字符串拷贝到dest所指向字符串末尾。

184 #ifndef __HAVE_ARCH_STRNCAT
185 /**
186  * strncat - Append a length-limited, %NUL-terminated string to another
187  * @dest: The string to be appended to
188  * @src: The string to append to it
189  * @count: The maximum numbers of bytes to copy
190  *
191  * Note that in contrast to strncpy(), strncat() ensures the result is
192  * terminated.
193  */
194 char *strncat(char *dest, const char *src, size_t count)
195 {
196         char *tmp = dest;
197 
198         if (count) {
199                 while (*dest)
200                         dest++;
201                 while ((*dest++ = *src++) != 0) {
202                         if (--count == 0) {
203                                 *dest = '\0';
204                                 break;
205                         }
206                 }
207         }
208         return tmp;
209 }
210 EXPORT_SYMBOL(strncat);
211 #endif

同strcat函数不同的是,如果没有到字符串结尾,它只拷贝count大小字节数据,并且在dest末尾加上字符串的结束符'/0'