首页 > 编程知识 正文

shell read -p,readlink简单实现

时间:2023-05-04 14:23:32 阅读:23855 作者:3899

示例:读取当前进程的绝对路径文件名

傲慢的天鹅/proc/self/exe是指向当前进程的符号链接。

/proc/self

[root@localhost self]$ ll exe

lrwxrwxrwx.1根根根0a ug 810:48 exe--/bin/bash

=====================================

名字

SYNOPSIS

#include unistd.h

size _ tread link (const char * pathname,char *buf,size_t bufsiz );

# include fcntl.h/* definition ofat _ * constants * /

#include unistd.h

size_treadlinkat(intdirFD,const char *pathname,

char *buf,size_t bufsiz;

featuretestmacrorequirementsforglibc (see feature _ test _ macros (7) ) :

_ x open _ source=500|_ POSIX _ c _ source=200112 l

|/* glibc版本=2.19: */_ BSD _ source

Since glibc 2.10:

_POSIX_C_SOURCE=200809L

Before glibc 2.10:

_ATFILE_SOURCE

描述顶部

buffer buf,which has size bufsiz. readlink () does not append a null

bytetobuf.itwill(silently ) truncatethecontents ) toalength

of bufsiz characters ),incasethebufferistoosmalltoholdallof

the contents。

readlinkat () )

thereadlinkat (系统语言操作系统xactlythesamewayas

readlink (,exceptforthedifferencesdescribedhere。

ifthepathnamegiveninpathnameisrelative,then it is interpreted

relativetothedirectoryreferredtobythefiledescriptordirfd

(ratherthanrelativetothecurrentworkingdirectoryofthecalling )

处理,as is done by readlink () for a relative pathname )。

ifpathnameisrelativeanddirfdisthespecialvalueat _ FD CWD,then

pathnameisinterpretedrelativetothecurrentworkingdirectoryof

技术阅读流程(like readlink ) )。

>

       If pathname is absolute, then dirfd is ignored.

       Since Linux 2.6.39, pathname can be an empty string, in which case
       the call operates on the symbolic link referred to by dirfd (which
       should have been obtained using open(2) with the O_PATH and
       O_NOFOLLOW flags).

       See openat(2) for an explanation of the need for readlinkat().
RETURN VALUE         top

       On success, these calls return the number of bytes placed in buf.
       (If the returned value equals bufsiz, then truncation may have
       occurred.)  On error, -1 is returned and errno is set to indicate the
       error.
ERRORS         top

              prefix.  (See also path_resolution(7).)

              pathname) is not a symbolic link.

              pathname.

              A pathname, or a component of a pathname, was too long.

              A component of the path prefix is not a directory.

       The following additional errors can occur for readlinkat():

              pathname is relative and dirfd is a file descriptor referring
              to a file other than a directory.
VERSIONS         top

       added to glibc in version 2.4.
CONFORMING TO         top

       POSIX.1-2001, POSIX.1-2008.

NOTES         top

       In versions of glibc up to and including glibc 2.4, the return type
       of readlink() was declared as int.  Nowadays, the return type is
       declared as ssize_t, as (newly) required in POSIX.1-2001.

       Using a statically sized buffer might not provide enough room for the
       symbolic link contents.  The required size for the buffer can be
       obtained from the stat.st_size value returned by a call to lstat(2)
       on the link.  However, the number of bytes written by readlink() and
       readlinkat() should be checked to make sure that the size of the
       symbolic link did not increase between the calls.  Dynamically
       allocating the buffer for readlink() and readlinkat() also addresses
       a common portability problem when using PATH_MAX for the buffer size,
       as this constant is not guaranteed to be defined per POSIX if the
       system does not have such limit.

   Glibc notes
       On older kernels where readlinkat() is unavailable, the glibc wrapper
       function falls back to the use of readlink().  When pathname is a
       relative pathname, glibc constructs a pathname based on the symbolic
       link in /proc/self/fd that corresponds to the dirfd argument.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。