Provided by: manpages-ja-dev_0.5.0.0.20221215+dfsg-1_all bug

名前

       strcat, strncat - 二つの文字列を連結する

書式

       #include <string.h>

       char *strcat(char *dest, const char *src);

       char *strncat(char *dest, const char *src, size_t n);

説明

       strcat()   関数は、dest 文字列の後に src 文字列を付け加える。 その際に、dest の最後にある終端のヌルバイト
       ('\0')  は上書きされ、新たに生成された文字列の末尾に終端のヌルバイトが付与される。  二つの文字列  srcdest は重なってはならない。 また、文字列 dest は、連結後の結果を格納するのに 十分な大きさでなければならな
       い。 dest が十分な大きさでない場合、プログラムがどのような動作をするか分からない。 バッファーオーバーラン
       はセキュアなプログラムを攻撃する際に好んで使われる方法である。

       strncat()  も同様だが、以下の点が異なる。

       *  src のうち最大 n バイトが使用される。

       *  srcn バイト以上の場合、 src はヌル終端されている必要はない。

       strcat()  と同じく、dest に格納される結果の文字列は常にヌル終端される。

       srcn バイト以上の場合、 strncat() は destn+1 バイトを書き込む (src からの n バイトと終端のヌルバ
       イトである)。 したがって、dest の大きさは最低でも strlen(dest)+n+1 でなければ ならない。

       strncat()  の簡単な実装は以下のような感じであろう:

           char *
           strncat(char *dest, const char *src, size_t n)
           {
               size_t dest_len = strlen(dest);
               size_t i;

               for (i = 0 ; i < n && src[i] != '\0' ; i++)
                   dest[dest_len + i] = src[i];
               dest[dest_len + i] = '\0';

               return dest;
           }

返り値

       strcat()  関数と strncat()  関数は、結果としてできる文字列 dest へのポインターを返す。

属性

       この節で使用されている用語の説明については、 attributes(7) を参照。

       ┌────────────────────┬───────────────┬─────────┐
       │インターフェース属性      │
       ├────────────────────┼───────────────┼─────────┤
       │strcat(), strncat() │ Thread safety │ MT-Safe │
       └────────────────────┴───────────────┴─────────┘

準拠

       POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.

注意

       いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。

           size_t strlcat(char *dest, const char *src, size_t size);

       この関数は、ヌル終端された文字列 src を文字列 dest に追加する。 具体例には、 sizestrlen(dest)  より大
       きい場合には最大で  size-strlen(dest)-1  バイトを src からコピーし、 結果の末尾に終端のヌルバイトを追加す
       る。 この関数では strcat() のバッファーオーバーランが発生するという問題が修正されているが、  size  が小さ
       すぎた場合にはデータが失われる問題には、  依然として呼び出し側で対処する必要がある。 この関数は strlcat()
       が作成しようとした文字列の長さを返す。 返り値が size 以上の場合、 データロスが発生している。 データロスが
       問題となる場合は、 呼び出し側で、 呼び出し前に引数をチェックするか、 この関数の返り値を検査するかのいずれ
       かをしなければならない。 strlcat() は glibc には存在せず、 POSIX による標準化もされていないが、 Linux  で
       は libbsd ライブラリ経由で利用できる。

       Because  strcat()   and strncat()  must find the null byte that terminates the string dest using a search
       that starts at the beginning of the string, the execution time of these functions scales according to the
       length  of  the  string dest.  This can be demonstrated by running the program below.  (If the goal is to
       concatenate many strings to one target, then manually copying the bytes from  each  source  string  while
       maintaining a pointer to the end of the target string will provide better performance.)

   プログラムのソース

       #include <stdint.h>
       #include <string.h>
       #include <time.h>
       #include <stdio.h>

       int
       main(int argc, char *argv[])
       {
       #define LIM 4000000
           char p[LIM + 1];    /* +1 for terminating null byte */
           time_t base;

           base = time(NULL);
           p[0] = '\0';

           for (int j = 0; j < LIM; j++) {
               if ((j % 10000) == 0)
                   printf("%d %jd\n", j, (intmax_t) (time(NULL) - base));
               strcat(p, "a");
           }
       }

関連項目

       bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wcscat(3), wcsncat(3)

この文書について

       この  man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告
       に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。