unix环境高级编程-4.17-sylmink和readlink函数

本节将介绍symlink和readlink函数的一些基本原理。

symlink函数用来创建一个符号链接

#include<unistd.h>

int symlink(const char* a c t u a l p a t h,const char* sympath);

查看GNU C文档

int symlink (const char *oldname, const char *newname) [Function]
The symlink function makes a symbolic link to oldname named newname.
The normal return value from symlink is 0. A return value of -1 indicates an error.
In addition to the usual file name syntax errors (see Section 11.2.3 [File Name Errors],
page 224), the following errno error conditions are defined for this function:
EEXIST There is already an existing file named newname.
EROFS The file newname would exist on a read-only file system.
ENOSPC The directory or file system cannot be extended to make the new link.
EIO A hardware error occurred while reading or writing data on the disk.

该函数创建了一个指向a c t u a l p a t h的新目录项s y m p a t h,在创建此符号连接时,并不要求
a c t u a l p a t h已经存在(在上一节结束部分的例子中我们已经看到了这一点)。并且, a c t u a l p a t h
和s y m p a t h并不需要位于同一文件系统中。
因为o p e n函数跟随符号连接,所以需要有一种方法打开该连接本身,并读该连接中的名字。
r e a d l i n k函数提供了这种功能。

#include <unistd.h>
ssize_t readlink(const char* restrict pathname,char * restrict buf ,size_t bufsize);


GNUC 手册。

int readlink (const char *filename, char *buffer, size t size) [Function]
The readlink function gets the value of the symbolic link filename. The file name that
the link points to is copied into buffer. This file name string is not null-terminated;
readlink normally returns the number of characters copied. The size argument
specifies the maximum number of characters to copy, usually the allocation size of
buffer.
If the return value equals size, you cannot tell whether or not there was room to
return the entire name. So make a bigger buffer and call readlink again. Here is an
example:

char *
readlink_malloc (const char *filename)
{
int size = 100;
char *buffer = NULL;
while (1)
{
buffer = (char *) xrealloc (buffer, size);
int nchars = readlink (filename, buffer, size);
if (nchars < 0)
{
free (buffer);
return NULL;
}
if (nchars < size)
return buffer;
size *= 2;
}
}

A value of -1 is returned in case of error. In addition to the usual file name errors (see
Section 11.2.3 [File Name Errors], page 224), the following errno error conditions are
defined for this function:
EINVAL The named file is not a symbolic link.
EIO A hardware error occurred while reading or writing data on the disk.
In some situations it is desirable to resolve all the symbolic links to get the real name of
a file where no prefix names a symbolic link which is followed and no filename in the path
is . or ... This is for instance desirable if files have to be compare in which case different
names can refer to the same inode.

此函数组合了open,read ,close函数所有的操作。乳沟此函数成功执行,则返回读入的buf字节数。在buf忠返回符号连接的内容不以null字符终止。

更多内容欢迎访问:http://blog.csdn.net/wallwind

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值