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 .\" %%%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
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.
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
35 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
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
43 .\"*******************************************************************
45 .\" This file was generated with po4a. Translate the source file.
47 .\"*******************************************************************
48 .TH READLINK 2 2011\-09\-20 Linux "Linux Programmer's Manual"
50 readlink \- シンボリックリンクの値を読む
52 \fB#include <unistd.h>\fP
54 \fBssize_t readlink(const char *\fP\fIpath\fP\fB, char *\fP\fIbuf\fP\fB, size_t
55 \fP\fIbufsiz\fP\fB);\fP
58 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
64 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
68 \fBreadlink\fP() は \fIpath\fP で与えられたシンボリックリンクの内容を \fIbuf\fP バッファーへ格納する、 \fIbuf\fP のサイズは
69 \fIbufsiz\fP である。 \fBreadlink\fP() は NULL バイトを \fIbuf\fP に追加しない。
70 その内容全てを格納するのにバッファーが小さ過ぎる場合は、 (\fIbufsiz\fP バイトの長さに) 内容を切り詰める。
72 成功すると、 \fBreadlink\fP() は \fIbuf\fP に格納されたバイト数を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP
77 パスのディレクトリ部分に検索許可が与えられていない (\fBpath_resolution\fP(7) も参照すること)。
80 \fIbuf\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?
91 指定したファイルがシンボリックリンクでない。
94 ファイルシステムの読み込み中に I/O エラーが起こった。
109 パスのディレクトリ部分がディレクトリでない。
111 4.4BSD (\fBreadlink\fP() は 4.2BSD で初めて登場した), POSIX.1\-2001.
113 バージョン 2.4 以前の glibc (バージョン 2.4 を含む) では、 \fBreadlink\fP() の返り値の型は \fIint\fP
114 で宣言されていた。現在では、返り値の型は \fIssize_t\fP である (返り値 \fIssize_t\fP は POSIX.1\-2001 で (新たに)
117 静的な大きさのバッファを使うと、シンボリックリンクの内容を
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 が定義されることが保証されていないからである。
130 以下のプログラムは、 \fBreadlink\fP() が必要とするバッファを、
131 \fBlstat\fP() が提供する情報に基づいて動的に割り当てる。
132 また、両方の呼び出し間で競合条件がないことを保証している。
135 #include <sys/types.h>
136 #include <sys/stat.h>
142 main(int argc, char *argv[])
149 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
153 if (lstat(argv[1], &sb) == \-1) {
158 linkname = malloc(sb.st_size + 1);
159 if (linkname == NULL) {
160 fprintf(stderr, "insufficient memory\en");
164 r = readlink(argv[1], linkname, sb.st_size + 1);
171 if (r > sb.st_size) {
172 fprintf(stderr, "symlink increased in size "
173 "between lstat() and readlink()\en");
177 linkname[sb.st_size] = \(aq\e0\(aq;
179 printf("\(aq%s\(aq points to \(aq%s\(aq\en", argv[1], linkname);
185 \fBreadlink\fP(1), \fBlstat\fP(2), \fBreadlinkat\fP(2), \fBstat\fP(2), \fBsymlink\fP(2),
186 \fBpath_resolution\fP(7), \fBsymlink\fP(7)
188 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
189 である。プロジェクトの説明とバグ報告に関する情報は
190 http://www.kernel.org/doc/man\-pages/ に書かれている。