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.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
35 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
36 .\" Modified Tue Jul 9 23:55:17 1996 by aeb
37 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
38 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
39 .\" Added text on dynamically allocating buffer + example program
41 .\"*******************************************************************
43 .\" This file was generated with po4a. Translate the source file.
45 .\"*******************************************************************
46 .TH READLINK 2 2011\-09\-20 Linux "Linux Programmer's Manual"
48 readlink \- シンボリックリンクの値を読む
50 \fB#include <unistd.h>\fP
52 \fBssize_t readlink(const char *\fP\fIpath\fP\fB, char *\fP\fIbuf\fP\fB, size_t
53 \fP\fIbufsiz\fP\fB);\fP
56 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
62 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
66 \fBreadlink\fP() は \fIpath\fP で与えられたシンボリックリンクの内容を \fIbuf\fP バッファーへ格納する、 \fIbuf\fP のサイズは
67 \fIbufsiz\fP である。 \fBreadlink\fP() は NULL バイトを \fIbuf\fP に追加しない。
68 その内容全てを格納するのにバッファーが小さ過ぎる場合は、 (\fIbufsiz\fP バイトの長さに) 内容を切り詰める。
70 成功すると、 \fBreadlink\fP() は \fIbuf\fP に格納されたバイト数を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP
75 パスのディレクトリ部分に検索許可が与えられていない (\fBpath_resolution\fP(7) も参照すること)。
78 \fIbuf\fP がプロセスに割り当てられたアドレス空間の外を指している。
81 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
82 .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed,
83 .\" and this error can also occur if bufsiz < 0.
84 .\" See: http://thread.gmane.org/gmane.linux.man/380
85 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
89 指定したファイルがシンボリックリンクでない。
92 ファイルシステムの読み込み中に I/O エラーが起こった。
107 パスのディレクトリ部分がディレクトリでない。
109 4.4BSD (\fBreadlink\fP() は 4.2BSD で初めて登場した), POSIX.1\-2001.
111 バージョン 2.4 以前の glibc (バージョン 2.4 を含む) では、 \fBreadlink\fP() の返り値の型は \fIint\fP
112 で宣言されていた。現在では、返り値の型は \fIssize_t\fP である (返り値 \fIssize_t\fP は POSIX.1\-2001 で (新たに)
115 静的な大きさのバッファを使うと、シンボリックリンクの内容を
117 バッファに必要なサイズは、そのシンボリックリンクに対して \fBlstat\fP(2)
118 の呼び出しで返される \fIstat.st_size\fP の値から取得できる。
119 ただし、 \fBreadlink\fP() が書き込んだバイト数をチェックして、
120 シンボリックリンクのサイズが \fBlstat\fP(2) と \fBreadlink\fP() の呼び出し
121 の間で増えていないことを確認すべきである。
122 \fBreadlink\fP() 用のバッファを動的に割り当てる方法でも、
123 バッファサイズとして \fIPATH_MAX\fP を使用する場合に共通する移植性の
124 問題を解決することができる。なぜなら、POSIX では、
125 システムがそのような上限値を定義していない場合には、
126 \fIPATH_MAX\fP が定義されることが保証されていないからである。
128 以下のプログラムは、 \fBreadlink\fP() が必要とするバッファを、
129 \fBlstat\fP() が提供する情報に基づいて動的に割り当てる。
130 また、両方の呼び出し間で競合条件がないことを保証している。
133 #include <sys/types.h>
134 #include <sys/stat.h>
140 main(int argc, char *argv[])
147 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
151 if (lstat(argv[1], &sb) == \-1) {
156 linkname = malloc(sb.st_size + 1);
157 if (linkname == NULL) {
158 fprintf(stderr, "insufficient memory\en");
162 r = readlink(argv[1], linkname, sb.st_size + 1);
169 if (r > sb.st_size) {
170 fprintf(stderr, "symlink increased in size "
171 "between lstat() and readlink()\en");
175 linkname[sb.st_size] = \(aq\e0\(aq;
177 printf("\(aq%s\(aq points to \(aq%s\(aq\en", argv[1], linkname);
183 \fBreadlink\fP(1), \fBlstat\fP(2), \fBreadlinkat\fP(2), \fBstat\fP(2), \fBsymlink\fP(2),
184 \fBpath_resolution\fP(7), \fBsymlink\fP(7)
186 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部
187 である。プロジェクトの説明とバグ報告に関する情報は
188 http://www.kernel.org/doc/man\-pages/ に書かれている。