.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Sat Jul 24 18:11:47 1993 by Rik Faith (faith@cs.unc.edu) .\" 2007-06-15, Marc Boyer + mtk .\" Improve discussion of strncat(). .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi .\" all rights reserved. .\" Translated Mon Jan 20 22:47:14 JST 1997 .\" by YOSHINO Takashi .\" Updated & Modified Fri Feb 18 00:30:00 JST 2005 .\" by Yuichi SATO .\" Updated 2007-07-04, Akihiro MOTOKI , LDP v2.58 .\" Updated 2012-05-29, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" .TH STRCAT 3 2014\-01\-20 GNU "Linux Programmer's Manual" .SH 名前 strcat, strncat \- 二つの文字列を連結する .SH 書式 .nf \fB#include \fP .sp \fBchar *strcat(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB);\fP .sp \fBchar *strncat(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP .fi .SH 説明 \fBstrcat\fP() 関数は、\fIdest\fP 文字列の後に \fIsrc\fP 文字列を付け加える。 その際に、\fIdest\fP の最後にある終端のヌルバイト (\(aq\e0\(aq) は上書きされ、新たに生成された文字列の末尾に終端のヌルバイトが付与される。 二つの文字列 \fIsrc\fP と \fIdest\fP は重なってはならない。 また、文字列 \fIdest\fP は、連結後の結果を格納するのに 十分な大きさでなければならない。 \fIdest\fP が十分な大きさでない場合、プログラムがどのような動作をするか分からない。 バッファーオーバーランはセキュアなプログラムを攻撃する際に好んで使われる方法である。 .PP \fBstrncat\fP() も同様だが、以下の点が異なる。 .IP * 3 \fIsrc\fP のうち最大 \fIn\fP バイトが使用される。 .IP * \fIsrc\fP が \fIn\fP バイト以上の場合、 \fIsrc\fP はヌル終端されている必要はない。 .PP \fBstrcat\fP() と同じく、\fIdest\fP に格納される結果の文字列は常にヌル終端される。 .PP \fIsrc\fP が \fIn\fP バイト以上の場合、 \fBstrncat\fP() は \fIdest\fP に \fIn+1\fP バイトを書き込む (\fIsrc\fP からの \fIn\fP バイトと終端のヌルバイトである)。 したがって、\fIdest\fP の大きさは最低でも \fIstrlen(dest)+n+1\fP でなければ ならない。 \fBstrncat\fP() の簡単な実装は以下のような感じであろう: .in +4n .nf 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] != \(aq\e0\(aq ; i++) dest[dest_len + i] = src[i]; dest[dest_len + i] = \(aq\e0\(aq; return dest; } .fi .in .SH 返り値 \fBstrcat\fP() 関数と \fBstrncat\fP() 関数は、結果としてできる文字列 \fIdest\fP へのポインターを返す。 .SH 属性 .SS "マルチスレッディング (pthreads(7) 参照)" 関数 \fBstrcat\fP() と \fBstrncat\fP() はスレッドセーフである。 .SH 準拠 SVr4, 4.3BSD, C89, C99. .SH 注意 いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。 size_t strlcat(char *dest, const char *src, size_t size); .\" https://lwn.net/Articles/506530/ この関数は、ヌル終端された文字列 \fIsrc\fP を文字列 \fIdest\fP に追加する。 具体例には、 \fIsize\fP が \fIstrlen(dest)\fP より大きい場合には最大で \fIsize\-strlen(dest)\-1\fP バイトを \fIsrc\fP からコピーし、 結果の末尾に終端のヌルバイトを追加する。 この関数では \fBstrcat\fP() のバッファーオーバーランが発生するという問題が修正されているが、 \fIsize\fP が小さすぎた場合にはデータが失われる問題には、 依然として呼び出し側で対処する必要がある。 この関数は \fBstrlcat\fP() が作成しようとした文字列の長さを返す。 返り値が \fIsize\fP 以上の場合、 データロスが発生している。 データロスが問題となる場合は、 呼び出し側で、 呼び出し前に引き数をチェックするか、 この関数の返り値を検査するかのいずれかをしなければならない。 \fBstrlcat\fP() は glibc には存在せず、 POSIX による標準化もされていないが、 Linux では \fIlibbsd\fP ライブラリ経由で利用できる。 .SH 関連項目 \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBstrcpy\fP(3), \fBstring\fP(3), \fBstrncpy\fP(3), \fBwcscat\fP(3), \fBwcsncat\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。