OSDN Git Service

(split) DP: release pages (catch up to 3.50).
[linuxjm/LDP_man-pages.git] / release / man2 / readlink.2
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" And Copyright (C) 2011 Guillem Jover <guillem@hadrons.org>
3 .\" All rights reserved.
4 .\"
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed by the University of
17 .\"     California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\"    may be used to endorse or promote products derived from this software
20 .\"    without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\" %%%LICENSE_END
34 .\"
35 .\"     @(#)readlink.2  6.8 (Berkeley) 3/10/91
36 .\"
37 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
38 .\" Modified Tue Jul  9 23:55:17 1996 by aeb
39 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
40 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
41 .\"     Added text on dynamically allocating buffer + example program
42 .\"
43 .\"*******************************************************************
44 .\"
45 .\" This file was generated with po4a. Translate the source file.
46 .\"
47 .\"*******************************************************************
48 .TH READLINK 2 2011\-09\-20 Linux "Linux Programmer's Manual"
49 .SH 名前
50 readlink \- シンボリックリンクの値を読む
51 .SH 書式
52 \fB#include <unistd.h>\fP
53 .sp
54 \fBssize_t readlink(const char *\fP\fIpath\fP\fB, char *\fP\fIbuf\fP\fB, size_t
55 \fP\fIbufsiz\fP\fB);\fP
56 .sp
57 .in -4n
58 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
59 .in
60 .sp
61 .ad l
62 \fBreadlink\fP():
63 .RS 4
64 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
65 .RE
66 .ad b
67 .SH 説明
68 \fBreadlink\fP()  は \fIpath\fP で与えられたシンボリックリンクの内容を \fIbuf\fP バッファーへ格納する、 \fIbuf\fP のサイズは
69 \fIbufsiz\fP である。 \fBreadlink\fP()  は NULL バイトを \fIbuf\fP に追加しない。
70 その内容全てを格納するのにバッファーが小さ過ぎる場合は、 (\fIbufsiz\fP バイトの長さに) 内容を切り詰める。
71 .SH 返り値
72 成功すると、 \fBreadlink\fP()  は \fIbuf\fP に格納されたバイト数を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP
73 にエラーを示す値を設定する。
74 .SH エラー
75 .TP 
76 \fBEACCES\fP
77 パスのディレクトリ部分に検索許可が与えられていない (\fBpath_resolution\fP(7)  も参照すること)。
78 .TP 
79 \fBEFAULT\fP
80 \fIbuf\fP がプロセスに割り当てられたアドレス空間の外を指している。
81 .TP 
82 \fBEINVAL\fP
83 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
84 .\" if bufsiz==0.  However, the in the kernel syscall, bufsiz is signed,
85 .\" and this error can also occur if bufsiz < 0.
86 .\" See: http://thread.gmane.org/gmane.linux.man/380
87 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
88 \fIbufsiz\fP が正でない。
89 .TP 
90 \fBEINVAL\fP
91 指定したファイルがシンボリックリンクでない。
92 .TP 
93 \fBEIO\fP
94 ファイルシステムの読み込み中に I/O エラーが起こった。
95 .TP 
96 \fBELOOP\fP
97 パス名にシンボリックリンクが多すぎる。
98 .TP 
99 \fBENAMETOOLONG\fP
100 パス名かパス名の一部分が長過ぎる。
101 .TP 
102 \fBENOENT\fP
103 その名前のファイルが存在しない。
104 .TP 
105 \fBENOMEM\fP
106 十分なカーネルメモリーがない。
107 .TP 
108 \fBENOTDIR\fP
109 パスのディレクトリ部分がディレクトリでない。
110 .SH 準拠
111 4.4BSD (\fBreadlink\fP()  は 4.2BSD で初めて登場した), POSIX.1\-2001.
112 .SH 注意
113 バージョン 2.4 以前の glibc (バージョン 2.4 を含む) では、 \fBreadlink\fP()  の返り値の型は \fIint\fP
114 で宣言されていた。現在では、返り値の型は \fIssize_t\fP である (返り値 \fIssize_t\fP は POSIX.1\-2001 で (新たに)
115 必須となった)。
116
117 静的な大きさのバッファを使うと、シンボリックリンクの内容を
118 格納するのに十分な領域がない場合がある。
119 バッファに必要なサイズは、そのシンボリックリンクに対して \fBlstat\fP(2)
120 の呼び出しで返される \fIstat.st_size\fP の値から取得できる。
121 ただし、 \fBreadlink\fP() が書き込んだバイト数をチェックして、
122 シンボリックリンクのサイズが \fBlstat\fP(2) と \fBreadlink\fP() の呼び出し
123 の間で増えていないことを確認すべきである。
124 \fBreadlink\fP() 用のバッファを動的に割り当てる方法でも、
125 バッファサイズとして \fIPATH_MAX\fP を使用する場合に共通する移植性の
126 問題を解決することができる。なぜなら、POSIX では、
127 システムがそのような上限値を定義していない場合には、
128 \fIPATH_MAX\fP が定義されることが保証されていないからである。
129 .SH 例
130 以下のプログラムは、 \fBreadlink\fP() が必要とするバッファを、
131 \fBlstat\fP() が提供する情報に基づいて動的に割り当てる。
132 また、両方の呼び出し間で競合条件がないことを保証している。
133 .nf
134
135 #include <sys/types.h>
136 #include <sys/stat.h>
137 #include <stdio.h>
138 #include <stdlib.h>
139 #include <unistd.h>
140
141 int
142 main(int argc, char *argv[])
143 {
144     struct stat sb;
145     char *linkname;
146     ssize_t r;
147
148     if (argc != 2) {
149         fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
150         exit(EXIT_FAILURE);
151     }
152
153     if (lstat(argv[1], &sb) == \-1) {
154         perror("lstat");
155         exit(EXIT_FAILURE);
156     }
157
158     linkname = malloc(sb.st_size + 1);
159     if (linkname == NULL) {
160         fprintf(stderr, "insufficient memory\en");
161         exit(EXIT_FAILURE);
162     }
163
164     r = readlink(argv[1], linkname, sb.st_size + 1);
165
166     if (r < 0) {
167         perror("lstat");
168         exit(EXIT_FAILURE);
169     }
170
171     if (r > sb.st_size) {
172         fprintf(stderr, "symlink increased in size "
173                         "between lstat() and readlink()\en");
174         exit(EXIT_FAILURE);
175     }
176
177     linkname[sb.st_size] = \(aq\e0\(aq;
178
179     printf("\(aq%s\(aq points to \(aq%s\(aq\en", argv[1], linkname);
180
181     exit(EXIT_SUCCESS);
182 }
183 .fi
184 .SH 関連項目
185 \fBreadlink\fP(1), \fBlstat\fP(2), \fBreadlinkat\fP(2), \fBstat\fP(2), \fBsymlink\fP(2),
186 \fBpath_resolution\fP(7), \fBsymlink\fP(7)
187 .SH この文書について
188 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.50 の一部
189 である。プロジェクトの説明とバグ報告に関する情報は
190 http://www.kernel.org/doc/man\-pages/ に書かれている。