.\" Copyright (C) 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:06:49 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl) .\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl) .\" 2007-06-15, Marc Boyer + mtk .\" Improve discussion of strncpy(). .\" .\"******************************************************************* .\" .\" 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:35:40 JST 1997 .\" by YOSHINO Takashi .\" Updated 2012-05-29, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" .TH STRCPY 3 2014\-03\-04 GNU "Linux Programmer's Manual" .SH 名前 strcpy, strncpy \- 文字列をコピーする .SH 書式 .nf \fB#include \fP .sp \fBchar *strcpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB);\fP .sp \fBchar *strncpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP .fi .SH 説明 \fBstrcpy\fP() 関数は \fIsrc\fP が指す文字列を末尾のヌルバイト (\(aq\e0\(aq) も含めて \fIdest\fP が指すバッファにコピーする。 二つの文字列は重なってはならない。受け側の文字列 \fIdest\fP は コピーを受け取るのに十分な大きさでなければならない。 \fIバッファオーバーランに気を付けること!\fP (「バグ」の節を参照) .PP \fBstrncpy\fP() 関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。 .PP \fIsrc\fP の長さが \fIn\fP よりも短い場合、 \fBstrncpy\fP() は \fIdest\fP に追加のヌルバイトを書き込み、全部で \fIn\fP バイトが書き込まれるようにする。 .PP \fBstrncpy\fP() の簡単な実装は以下のような感じであろう: .in +4n .nf char * strncpy(char *dest, const char *src, size_t n) { size_t i; for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++) dest[i] = src[i]; for ( ; i < n; i++) dest[i] = \(aq\e0\(aq; return dest; } .fi .in .SH 返り値 \fBstrcpy\fP() 関数と \fBstrncpy\fP() 関数は 受け側の文字列\fIdest\fPへのポインタを返す。 .SH 属性 .SS "マルチスレッディング (pthreads(7) 参照)" 関数 \fBstrcpy\fP() と \fBstrncpy\fP() はスレッドセーフである。 .SH 準拠 SVr4, 4.3BSD, C89, C99. .SH 注意 \fBstrncpy\fP() は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。 \fBstrncpy\fP() の正しい (かつ意図された) 用途は、 C 文字列の固定長バッファへのコピーを、 バッファがオーバーフローしないことと、 宛先バッファの未使用バイトが 0 で埋められることの両方を保証しつつ行うことである。 (宛先バッファを 0 で埋めるのは、 たいていの場合、 バッファを媒体に書き込んだり、別のプロセスにプロセス間通信を用いて送信したりした場合に情報洩れを防ぐためである)。 \fIsrc\fP の最初の \fIn\fP バイトに終端のヌルバイトがない場合、 \fBstrncpy\fP() は \fIdest\fP に終端されていない文字列を生成する。 \fIbuf\fP の長さが \fIbuflen\fP の場合、以下のようにして強制的に終端することができる。 .in +4n .nf strncpy(buf, str, buflen \- 1); if (buflen > 0) buf[buflen \- 1]= \(aq\e0\(aq; .fi .in .PP (もちろん、上記の方法では、 \fIsrc\fP に入っている情報が \fIbuflen\ \-\ 1\fP バイトよりも多い場合には、 \fIdest\fP へのコピー時に情報が失われるという事実は無視している。) いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。 size_t strlcpy(char *dest, const char *src, size_t size); .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html .\" "strlcpy and strlcat - consistent, safe, string copy and concatenation" .\" 1999 USENIX Annual Technical Conference .\" https://lwn.net/Articles/506530/ この関数は \fBstrncpy\fP() と同様だが、 最大でも \fIsize\-1\fP バイトしか \fIdest\fP にコピーをせず、 末尾への終端のヌルバイトの追加が必ず行われ、 宛先バッファ (の未使用部分) へのヌルバイトの書き込みが行われない。 この関数では \fBstrcpy\fP() や \fBstrncpy\fP() の持つ問題のいくつかが修正されているが、 \fIsize\fP が小さすぎた場合にはデータが失われる問題には、 依然として呼び出し側で対処する必要がある。 この関数の返り値は \fIsrc\fP の長さである。 これにより、 末尾の切り詰めが行われたかを簡単に検出することができる。 返り値が \fIsize\fP 以上の場合には、 末尾の切り詰めが発生している。 データロスが問題となる場合は、 呼び出し側で、 呼び出し前に引き数をチェックするか、 この関数の返り値を検査するかのいずれかをしなければならない。 \fBstrlcpy\fP() は glibc には存在せず、 POSIX による標準化もされていないが、 Linux では \fIlibbsd\fP ライブラリ経由で利用できる。 .SH バグ \fBstrcpy\fP() の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。 固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。 プログラムでデータをバッファに読み込んだりコピーしたりする場合には、 必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。 プログラマがオーバーフローが不可能だと示せる場合には このチェックは不要かもしれないが、十分注意すること。 長い間には、不可能だったことが可能になるような方法でプログラムが 変更されることもあるからだ。 .SH 関連項目 \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3), \fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。