1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" And Copyright (C) 2011 Guillem Jover <guillem@hadrons.org>
3 .\" And Copyright (C) 2006, 2014 Michael Kerrisk
4 .\" All rights reserved.
6 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
38 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
39 .\" Modified Tue Jul 9 23:55:17 1996 by aeb
40 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
41 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
42 .\" Added text on dynamically allocating buffer + example program
44 .\"*******************************************************************
46 .\" This file was generated with po4a. Translate the source file.
48 .\"*******************************************************************
50 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
51 .\" all rights reserved.
52 .\" Translated 1997-12-12, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
53 .\" Updated 2005-02-10, Yuichi SATO <ysato444@yahoo.co.jp>
54 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
55 .\" Updated 2008-08-06, Akihiro MOTOKI, LDP v3.05
56 .\" Updated 2012-05-08, Akihiro MOTOKI <amotoki@gmail.com>
58 .TH READLINK 2 2014\-02\-21 Linux "Linux Programmer's Manual"
60 readlink, readlinkat \- シンボリックリンクの値を読む
63 \fB#include <unistd.h>\fP
65 \fBssize_t readlink(const char *\fP\fIpathname\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsiz\fP\fB);\fP
67 \fB#include <fcntl.h> \fP/* AT_* 定数の定義 */
68 \fB#include <unistd.h>\fP
70 \fBint readlinkat(int \fP\fIdirfd\fP\fB, const char *\fP\fIpathname\fP\fB,\fP
71 \fB char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsiz\fP\fB);\fP
75 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
81 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
90 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
98 \fBreadlink\fP() は \fIpathname\fP で与えられたシンボリックリンクの内容を \fIbuf\fP バッファーへ格納する、 \fIbuf\fP
99 のサイズは \fIbufsiz\fP である。 \fBreadlink\fP() は NULL バイトを \fIbuf\fP に追加しない。
100 その内容全てを格納するのにバッファーが小さ過ぎる場合は、 (\fIbufsiz\fP バイトの長さに) 内容を切り詰める。
102 \fBreadlinkat\fP() システムコールは \fBreadlink\fP() と全く同様に動作するが、以下で説明する点が異なる。
104 \fIpathname\fP で指定されたパス名が相対パスの場合、このパス名はファイルディスクリプター \fIdirfd\fP
105 が参照するディレクトリに対する相対パスと解釈される (\fBreadlink\fP()
106 に相対パス名を渡した場合のように、呼び出したプロセスのカレントワーキングディレクトリに対する相対パスではない)。
108 \fIpathname\fP で指定されたパス名が相対パスで、 \fIdirfd\fP が特別な値 \fBAT_FDCWD\fP の場合、 (\fBreadlink\fP()
109 と同様に) \fIpathname\fP は呼び出したプロセスのカレントワーキングディレクトリに対する相対パスと解釈される。
111 \fIpathname\fP で指定されたパス名が絶対パスの場合、 \fIdirfd\fP は無視される。
113 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
114 Since Linux 2.6.39, \fIpathname\fP can be an empty string, in which case the
115 call operates on the file referred to by \fIdirfd\fP (which may have been
116 obtained using the \fBopen\fP(2) \fBO_PATH\fP flag). In this case, \fIdirfd\fP can
117 refer to any type of file, not just a directory.
119 \fBreadlinkat\fP() の必要性についての説明については \fBopenat\fP(2) を参照。
121 成功すると、これらのシステムコールは \fIbuf\fP に格納されたバイト数を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP
126 パスのディレクトリ部分に検索許可が与えられていない (\fBpath_resolution\fP(7) も参照すること)。
129 \fIbuf\fP がプロセスに割り当てられたアドレス空間の外を指している。
132 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
133 .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed,
134 .\" and this error can also occur if bufsiz < 0.
135 .\" See: http://thread.gmane.org/gmane.linux.man/380
136 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
140 指定したファイルがシンボリックリンクでない。
143 ファイルシステムの読み込み中に I/O エラーが起こった。
158 パスのディレクトリ部分がディレクトリでない。
160 \fBreadlinkat\fP() では以下のエラーも発生する。
163 \fIdirfd\fP が有効なファイルディスクリプタではない。
166 \fIpathname\fP が相対パスで、 \fIdirfd\fP がディレクトリ以外のファイルを参照しているファイルディスクリプタである。
168 \fBreadlinkat\fP() はカーネル 2.6.16 で Linux に追加された。 ライブラリによるサポートはバージョン 2.4 で glibc
171 4.4BSD (\fBreadlink\fP() は 4.2BSD で初めて登場した), POSIX.1\-2001, POSIX.1\-2008.
173 \fBreadlinkat\fP(): POSIX.1\-2008.
175 バージョン 2.4 以前の glibc (バージョン 2.4 を含む) では、 \fBreadlink\fP() の返り値の型は \fIint\fP
176 で宣言されていた。現在では、返り値の型は \fIssize_t\fP である (返り値 \fIssize_t\fP は POSIX.1\-2001 で (新たに)
179 Using a statically sized buffer might not provide enough room for the
180 symbolic link contents. The required size for the buffer can be obtained
181 from the \fIstat.st_size\fP value returned by a call to \fBlstat\fP(2) on the
182 link. However, the number of bytes written by \fBreadlink\fP() and
183 \fBreadlinkat\fP() should be checked to make sure that the size of the
184 symbolic link did not increase between the calls. Dynamically allocating
185 the buffer for \fBreadlink\fP() and \fBreadlinkat\fP() also addresses a common
186 portability problem when using \fIPATH_MAX\fP for the buffer size, as this
187 constant is not guaranteed to be defined per POSIX if the system does not
190 以下のプログラムは、 \fBreadlink\fP() が必要とするバッファを、
191 \fBlstat\fP() が提供する情報に基づいて動的に割り当てる。
192 また、両方の呼び出し間で競合条件がないことを保証している。
195 #include <sys/types.h>
196 #include <sys/stat.h>
202 main(int argc, char *argv[])
209 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
213 if (lstat(argv[1], &sb) == \-1) {
218 linkname = malloc(sb.st_size + 1);
219 if (linkname == NULL) {
220 fprintf(stderr, "insufficient memory\en");
224 r = readlink(argv[1], linkname, sb.st_size + 1);
231 if (r > sb.st_size) {
232 fprintf(stderr, "symlink increased in size "
233 "between lstat() and readlink()\en");
237 linkname[r] = \(aq\e0\(aq;
239 printf("\(aq%s\(aq points to \(aq%s\(aq\en", argv[1], linkname);
245 \fBreadlink\fP(1), \fBlstat\fP(2), \fBstat\fP(2), \fBsymlink\fP(2),
246 \fBpath_resolution\fP(7), \fBsymlink\fP(7)
248 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.64 の一部
249 である。プロジェクトの説明とバグ報告に関する情報は
250 http://www.kernel.org/doc/man\-pages/ に書かれている。